signal-desktop/ts/components/stickers/StickerManager.tsx

179 lines
6 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2019 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import * as React from 'react';
import { StickerManagerPackRow } from './StickerManagerPackRow';
import { StickerPreviewModal } from './StickerPreviewModal';
import type { LocalizerType } from '../../types/Util';
import type { StickerPackType } from '../../state/ducks/stickers';
2022-12-14 00:06:15 +00:00
import { Tabs } from '../Tabs';
export type OwnProps = {
readonly blessedPacks: ReadonlyArray<StickerPackType>;
readonly closeStickerPackPreview: () => unknown;
readonly downloadStickerPack: (packId: string, packKey: string) => unknown;
2022-12-14 00:06:15 +00:00
readonly i18n: LocalizerType;
readonly installStickerPack: (packId: string, packKey: string) => unknown;
2022-12-14 00:06:15 +00:00
readonly installedPacks: ReadonlyArray<StickerPackType>;
readonly knownPacks?: ReadonlyArray<StickerPackType>;
readonly receivedPacks: ReadonlyArray<StickerPackType>;
readonly uninstallStickerPack: (packId: string, packKey: string) => unknown;
};
export type Props = OwnProps;
2022-12-14 00:06:15 +00:00
enum TabViews {
Available = 'Available',
Installed = 'Installed',
}
2022-11-18 00:45:19 +00:00
export const StickerManager = React.memo(function StickerManagerInner({
blessedPacks,
2022-12-14 00:06:15 +00:00
closeStickerPackPreview,
2022-11-18 00:45:19 +00:00
downloadStickerPack,
2022-12-14 00:06:15 +00:00
i18n,
2022-11-18 00:45:19 +00:00
installStickerPack,
2022-12-14 00:06:15 +00:00
installedPacks,
knownPacks,
receivedPacks,
2022-11-18 00:45:19 +00:00
uninstallStickerPack,
}: Props) {
const focusRef = React.createRef<HTMLDivElement>();
const [packToPreview, setPackToPreview] =
React.useState<StickerPackType | null>(null);
2022-11-18 00:45:19 +00:00
React.useEffect(() => {
if (!knownPacks) {
return;
}
knownPacks.forEach(pack => {
downloadStickerPack(pack.id, pack.key);
});
2022-11-18 00:45:19 +00:00
// When this component is created, it's initially not part of the DOM, and then it's
// added off-screen and animated in. This ensures that the focus takes.
setTimeout(() => {
if (focusRef.current) {
focusRef.current.focus();
}
});
// We only want to attempt downloads on initial load
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
2022-11-18 00:45:19 +00:00
const clearPackToPreview = React.useCallback(() => {
setPackToPreview(null);
}, [setPackToPreview]);
2022-11-18 00:45:19 +00:00
const previewPack = React.useCallback((pack: StickerPackType) => {
setPackToPreview(pack);
}, []);
2022-11-18 00:45:19 +00:00
return (
<>
{packToPreview ? (
<StickerPreviewModal
2022-12-14 00:06:15 +00:00
closeStickerPackPreview={closeStickerPackPreview}
2022-11-18 00:45:19 +00:00
downloadStickerPack={downloadStickerPack}
2022-12-14 00:06:15 +00:00
i18n={i18n}
2022-11-18 00:45:19 +00:00
installStickerPack={installStickerPack}
2022-12-14 00:06:15 +00:00
onClose={clearPackToPreview}
pack={packToPreview}
2022-11-18 00:45:19 +00:00
uninstallStickerPack={uninstallStickerPack}
/>
) : null}
2023-01-13 00:24:59 +00:00
<div
className="module-sticker-manager"
data-testid="StickerManager"
tabIndex={-1}
ref={focusRef}
>
2022-12-14 00:06:15 +00:00
<Tabs
initialSelectedTab={TabViews.Available}
tabs={[
{
id: TabViews.Available,
2023-03-30 00:03:25 +00:00
label: i18n('icu:stickers--StickerManager--Available'),
2022-12-14 00:06:15 +00:00
},
{
id: TabViews.Installed,
2023-03-30 00:03:25 +00:00
label: i18n('icu:stickers--StickerManager--InstalledPacks'),
2022-12-14 00:06:15 +00:00
},
]}
>
{({ selectedTab }) => (
<>
{selectedTab === TabViews.Available && (
<>
<h2 className="module-sticker-manager__text module-sticker-manager__text--heading">
2023-03-30 00:03:25 +00:00
{i18n('icu:stickers--StickerManager--BlessedPacks')}
2022-12-14 00:06:15 +00:00
</h2>
{blessedPacks.length > 0 ? (
blessedPacks.map(pack => (
<StickerManagerPackRow
key={pack.id}
pack={pack}
i18n={i18n}
onClickPreview={previewPack}
installStickerPack={installStickerPack}
uninstallStickerPack={uninstallStickerPack}
/>
))
) : (
<div className="module-sticker-manager__empty">
2023-03-30 00:03:25 +00:00
{i18n(
'icu:stickers--StickerManager--BlessedPacks--Empty'
)}
2022-12-14 00:06:15 +00:00
</div>
)}
2022-12-14 00:06:15 +00:00
<h2 className="module-sticker-manager__text module-sticker-manager__text--heading">
2023-03-30 00:03:25 +00:00
{i18n('icu:stickers--StickerManager--ReceivedPacks')}
2022-12-14 00:06:15 +00:00
</h2>
{receivedPacks.length > 0 ? (
receivedPacks.map(pack => (
<StickerManagerPackRow
key={pack.id}
pack={pack}
i18n={i18n}
onClickPreview={previewPack}
installStickerPack={installStickerPack}
uninstallStickerPack={uninstallStickerPack}
/>
))
) : (
<div className="module-sticker-manager__empty">
2023-03-30 00:03:25 +00:00
{i18n(
'icu:stickers--StickerManager--ReceivedPacks--Empty'
)}
2022-12-14 00:06:15 +00:00
</div>
)}
</>
2022-11-18 00:45:19 +00:00
)}
2022-12-14 00:06:15 +00:00
{selectedTab === TabViews.Installed &&
(installedPacks.length > 0 ? (
installedPacks.map(pack => (
<StickerManagerPackRow
key={pack.id}
pack={pack}
i18n={i18n}
onClickPreview={previewPack}
installStickerPack={installStickerPack}
uninstallStickerPack={uninstallStickerPack}
/>
))
) : (
<div className="module-sticker-manager__empty">
2023-03-30 00:03:25 +00:00
{i18n(
'icu:stickers--StickerManager--InstalledPacks--Empty'
)}
2022-12-14 00:06:15 +00:00
</div>
))}
</>
)}
</Tabs>
2022-11-18 00:45:19 +00:00
</div>
</>
);
});