2019-05-16 22:32:11 +00:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { StickerManagerPackRow } from './StickerManagerPackRow';
|
|
|
|
import { StickerPreviewModal } from './StickerPreviewModal';
|
|
|
|
import { LocalizerType } from '../../types/Util';
|
|
|
|
import { StickerPackType } from '../../state/ducks/stickers';
|
|
|
|
|
|
|
|
export type OwnProps = {
|
|
|
|
readonly installedPacks: ReadonlyArray<StickerPackType>;
|
|
|
|
readonly receivedPacks: ReadonlyArray<StickerPackType>;
|
|
|
|
readonly blessedPacks: ReadonlyArray<StickerPackType>;
|
2019-05-24 01:27:42 +00:00
|
|
|
readonly knownPacks?: ReadonlyArray<StickerPackType>;
|
|
|
|
readonly downloadStickerPack: (packId: string, packKey: string) => unknown;
|
2019-05-16 22:32:11 +00:00
|
|
|
readonly installStickerPack: (packId: string, packKey: string) => unknown;
|
|
|
|
readonly uninstallStickerPack: (packId: string, packKey: string) => unknown;
|
|
|
|
readonly i18n: LocalizerType;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Props = OwnProps;
|
|
|
|
|
|
|
|
export const StickerManager = React.memo(
|
2019-11-07 21:36:16 +00:00
|
|
|
// tslint:disable-next-line max-func-body-length
|
2019-05-16 22:32:11 +00:00
|
|
|
({
|
|
|
|
installedPacks,
|
|
|
|
receivedPacks,
|
2019-05-24 01:27:42 +00:00
|
|
|
knownPacks,
|
2019-05-16 22:32:11 +00:00
|
|
|
blessedPacks,
|
2019-05-24 01:27:42 +00:00
|
|
|
downloadStickerPack,
|
2019-05-16 22:32:11 +00:00
|
|
|
installStickerPack,
|
|
|
|
uninstallStickerPack,
|
|
|
|
i18n,
|
|
|
|
}: Props) => {
|
2019-11-21 19:16:06 +00:00
|
|
|
const focusRef = React.createRef<HTMLDivElement>();
|
2019-05-16 22:32:11 +00:00
|
|
|
const [
|
|
|
|
packToPreview,
|
|
|
|
setPackToPreview,
|
|
|
|
] = React.useState<StickerPackType | null>(null);
|
|
|
|
|
2019-05-24 01:27:42 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!knownPacks) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
knownPacks.forEach(pack => {
|
|
|
|
downloadStickerPack(pack.id, pack.key);
|
|
|
|
});
|
2019-11-21 19:16:06 +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();
|
|
|
|
}
|
|
|
|
});
|
2019-05-24 01:27:42 +00:00
|
|
|
}, []);
|
|
|
|
|
2020-01-08 17:44:54 +00:00
|
|
|
const clearPackToPreview = React.useCallback(() => {
|
|
|
|
setPackToPreview(null);
|
|
|
|
}, [setPackToPreview]);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
const previewPack = React.useCallback(
|
|
|
|
(pack: StickerPackType) => {
|
|
|
|
setPackToPreview(pack);
|
|
|
|
},
|
|
|
|
[clearPackToPreview]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{packToPreview ? (
|
|
|
|
<StickerPreviewModal
|
|
|
|
i18n={i18n}
|
|
|
|
pack={packToPreview}
|
|
|
|
onClose={clearPackToPreview}
|
2019-05-24 01:27:42 +00:00
|
|
|
downloadStickerPack={downloadStickerPack}
|
2019-05-16 22:32:11 +00:00
|
|
|
installStickerPack={installStickerPack}
|
|
|
|
uninstallStickerPack={uninstallStickerPack}
|
|
|
|
/>
|
|
|
|
) : null}
|
2019-11-21 19:16:06 +00:00
|
|
|
<div className="module-sticker-manager" tabIndex={-1} ref={focusRef}>
|
2019-05-16 22:32:11 +00:00
|
|
|
{[
|
|
|
|
{
|
|
|
|
i18nKey: 'stickers--StickerManager--InstalledPacks',
|
|
|
|
i18nEmptyKey: 'stickers--StickerManager--InstalledPacks--Empty',
|
|
|
|
packs: installedPacks,
|
2019-05-29 21:36:34 +00:00
|
|
|
hideIfEmpty: false,
|
2019-05-16 22:32:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
i18nKey: 'stickers--StickerManager--BlessedPacks',
|
|
|
|
i18nEmptyKey: 'stickers--StickerManager--BlessedPacks--Empty',
|
|
|
|
packs: blessedPacks,
|
2019-05-29 21:36:34 +00:00
|
|
|
hideIfEmpty: true,
|
2019-05-16 22:32:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
i18nKey: 'stickers--StickerManager--ReceivedPacks',
|
|
|
|
i18nEmptyKey: 'stickers--StickerManager--ReceivedPacks--Empty',
|
|
|
|
packs: receivedPacks,
|
2019-05-29 21:36:34 +00:00
|
|
|
hideIfEmpty: false,
|
2019-05-16 22:32:11 +00:00
|
|
|
},
|
2019-05-29 21:36:34 +00:00
|
|
|
].map(section => {
|
|
|
|
if (section.hideIfEmpty && section.packs.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment key={section.i18nKey}>
|
|
|
|
<h2
|
|
|
|
className={classNames(
|
|
|
|
'module-sticker-manager__text',
|
|
|
|
'module-sticker-manager__text--heading'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{i18n(section.i18nKey)}
|
|
|
|
</h2>
|
|
|
|
{section.packs.length > 0 ? (
|
|
|
|
section.packs.map(pack => (
|
|
|
|
<StickerManagerPackRow
|
|
|
|
key={pack.id}
|
|
|
|
pack={pack}
|
|
|
|
i18n={i18n}
|
|
|
|
onClickPreview={previewPack}
|
|
|
|
installStickerPack={installStickerPack}
|
|
|
|
uninstallStickerPack={uninstallStickerPack}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<div className="module-sticker-manager__empty">
|
|
|
|
{i18n(section.i18nEmptyKey)}
|
|
|
|
</div>
|
2019-05-16 22:32:11 +00:00
|
|
|
)}
|
2019-05-29 21:36:34 +00:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
})}
|
2019-05-16 22:32:11 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|