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

163 lines
4.9 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 { ConfirmationDialog } from '../ConfirmationDialog';
import type { LocalizerType } from '../../types/Util';
import type { StickerPackType } from '../../state/ducks/stickers';
2022-12-14 00:06:15 +00:00
import { Button, ButtonVariant } from '../Button';
2023-04-20 17:03:43 +00:00
import { UserText } from '../UserText';
export type OwnProps = {
readonly i18n: LocalizerType;
readonly pack: StickerPackType;
readonly onClickPreview?: (sticker: StickerPackType) => unknown;
readonly installStickerPack?: (packId: string, packKey: string) => unknown;
readonly uninstallStickerPack?: (packId: string, packKey: string) => unknown;
};
export type Props = OwnProps;
export const StickerManagerPackRow = React.memo(
2022-11-18 00:45:19 +00:00
function StickerManagerPackRowInner({
installStickerPack,
uninstallStickerPack,
onClickPreview,
pack,
i18n,
2022-11-18 00:45:19 +00:00
}: Props) {
const { id, key, isBlessed } = pack;
const [uninstalling, setUninstalling] = React.useState(false);
2020-01-08 17:44:54 +00:00
const clearUninstalling = React.useCallback(() => {
setUninstalling(false);
}, [setUninstalling]);
const handleInstall = React.useCallback(
(e: React.MouseEvent) => {
e.stopPropagation();
if (installStickerPack) {
installStickerPack(id, key);
}
},
2020-09-14 22:14:03 +00:00
[id, installStickerPack, key]
);
const handleUninstall = React.useCallback(
(e: React.MouseEvent) => {
e.stopPropagation();
if (isBlessed && uninstallStickerPack) {
uninstallStickerPack(id, key);
} else {
setUninstalling(true);
}
},
2020-09-14 22:14:03 +00:00
[id, isBlessed, key, setUninstalling, uninstallStickerPack]
);
2020-01-08 17:44:54 +00:00
const handleConfirmUninstall = React.useCallback(() => {
clearUninstalling();
if (uninstallStickerPack) {
uninstallStickerPack(id, key);
}
2020-09-14 22:14:03 +00:00
}, [id, key, clearUninstalling, uninstallStickerPack]);
2019-11-07 21:36:16 +00:00
const handleKeyDown = React.useCallback(
(event: React.KeyboardEvent) => {
if (
onClickPreview &&
(event.key === 'Enter' || event.key === 'Space')
) {
event.stopPropagation();
event.preventDefault();
onClickPreview(pack);
}
},
[onClickPreview, pack]
);
const handleClickPreview = React.useCallback(
2019-11-07 21:36:16 +00:00
(event: React.MouseEvent) => {
if (onClickPreview) {
2019-11-07 21:36:16 +00:00
event.stopPropagation();
event.preventDefault();
onClickPreview(pack);
}
},
[onClickPreview, pack]
);
return (
<>
{uninstalling ? (
<ConfirmationDialog
2022-09-27 20:24:21 +00:00
dialogName="StickerManagerPackRow.confirmUninstall"
i18n={i18n}
onClose={clearUninstalling}
2020-05-27 21:37:06 +00:00
actions={[
{
style: 'negative',
2023-03-30 00:03:25 +00:00
text: i18n('icu:stickers--StickerManager--Uninstall'),
2020-05-27 21:37:06 +00:00
action: handleConfirmUninstall,
},
]}
>
2023-03-30 00:03:25 +00:00
{i18n('icu:stickers--StickerManager--UninstallWarning')}
</ConfirmationDialog>
) : null}
<div
2019-11-07 21:36:16 +00:00
tabIndex={0}
// This can't be a button because we have buttons as descendants
role="button"
2019-11-07 21:36:16 +00:00
onKeyDown={handleKeyDown}
onClick={handleClickPreview}
className="module-sticker-manager__pack-row"
2023-01-13 00:24:59 +00:00
data-testid={id}
>
{pack.cover ? (
<img
src={pack.cover.url}
alt={pack.title}
className="module-sticker-manager__pack-row__cover"
/>
) : (
<div className="module-sticker-manager__pack-row__cover-placeholder" />
)}
<div className="module-sticker-manager__pack-row__meta">
<div className="module-sticker-manager__pack-row__meta__title">
2023-04-20 17:03:43 +00:00
<UserText text={pack.title} />
{pack.isBlessed ? (
<span className="module-sticker-manager__pack-row__meta__blessed-icon" />
) : null}
</div>
<div className="module-sticker-manager__pack-row__meta__author">
{pack.author}
</div>
</div>
<div className="module-sticker-manager__pack-row__controls">
{pack.status === 'installed' ? (
2022-12-14 00:06:15 +00:00
<Button
2023-03-30 00:03:25 +00:00
aria-label={i18n('icu:stickers--StickerManager--Uninstall')}
2022-12-14 00:06:15 +00:00
variant={ButtonVariant.Secondary}
onClick={handleUninstall}
2022-12-14 00:06:15 +00:00
>
2023-03-30 00:03:25 +00:00
{i18n('icu:stickers--StickerManager--Uninstall')}
2022-12-14 00:06:15 +00:00
</Button>
) : (
2022-12-14 00:06:15 +00:00
<Button
2023-03-30 00:03:25 +00:00
aria-label={i18n('icu:stickers--StickerManager--Install')}
2022-12-14 00:06:15 +00:00
variant={ButtonVariant.Secondary}
onClick={handleInstall}
2022-12-14 00:06:15 +00:00
>
2023-03-30 00:03:25 +00:00
{i18n('icu:stickers--StickerManager--Install')}
2022-12-14 00:06:15 +00:00
</Button>
)}
</div>
</div>
</>
);
}
);