Signal-Desktop/sticker-creator/app/stages/UploadStage.tsx

78 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-01-22 17:32:53 +00:00
// Copyright 2019-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2019-12-17 20:25:57 +00:00
import * as React from 'react';
import { noop } from 'lodash';
import { AppStage } from './AppStage';
import * as styles from './UploadStage.scss';
import { history } from '../../util/history';
import { ProgressBar } from '../../elements/ProgressBar';
import { H2, Text } from '../../elements/Typography';
import { Button } from '../../elements/Button';
import { stickersDuck } from '../../store';
import { encryptAndUpload } from '../../util/preload';
import { useI18n } from '../../util/i18n';
const handleCancel = () => {
history.push('/add-meta');
};
2019-12-17 20:25:57 +00:00
export const UploadStage: React.ComponentType = () => {
2019-12-17 20:25:57 +00:00
const i18n = useI18n();
const actions = stickersDuck.useStickerActions();
const cover = stickersDuck.useCover();
const title = stickersDuck.useTitle();
const author = stickersDuck.useAuthor();
const orderedData = stickersDuck.useSelectOrderedData();
const total = orderedData.length;
const [complete, setComplete] = React.useState(0);
2020-01-08 17:44:54 +00:00
React.useEffect(() => {
(async () => {
const onProgress = () => {
setComplete(i => i + 1);
};
2020-01-08 17:44:54 +00:00
try {
const packMeta = await encryptAndUpload(
{ title, author },
orderedData,
cover,
onProgress
);
actions.setPackMeta(packMeta);
history.push('/share');
} catch (e) {
window.SignalContext.log.error('Error uploading image:', e);
actions.addToast({
key: 'StickerCreator--Toasts--errorUploading',
subs: [e.message],
});
2020-01-08 17:44:54 +00:00
history.push('/add-meta');
}
})();
2019-12-17 20:25:57 +00:00
2020-01-08 17:44:54 +00:00
return noop;
}, [actions, title, author, cover, orderedData]);
2019-12-17 20:25:57 +00:00
return (
<AppStage empty>
2019-12-17 20:25:57 +00:00
<div className={styles.base}>
<H2>{i18n('StickerCreator--UploadStage--title')}</H2>
<Text>
{i18n('StickerCreator--UploadStage-uploaded', {
2021-01-22 17:32:53 +00:00
// We convert these to string so that 0 isn't falsy, which i18n checks for.
count: String(complete),
total: String(total),
})}
2019-12-17 20:25:57 +00:00
</Text>
<ProgressBar
count={complete}
total={total}
className={styles.progress}
/>
<Button onClick={handleCancel}>{i18n('cancel')}</Button>
</div>
</AppStage>
);
};