signal-desktop/ts/components/StoriesAddStoryButton.tsx

94 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-08-19 18:36:47 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactNode } from 'react';
import React from 'react';
import type { LocalizerType } from '../types/Util';
import type { ShowToastActionCreatorType } from '../state/ducks/toast';
import { ContextMenu } from './ContextMenu';
import { Theme } from '../util/theme';
import { ToastType } from '../types/Toast';
2022-08-19 18:36:47 +00:00
import {
isVideoGoodForStories,
ReasonVideoNotGood,
} from '../util/isVideoGoodForStories';
export type PropsType = {
children?: ReactNode;
i18n: LocalizerType;
moduleClassName?: string;
onAddStory: (file?: File) => unknown;
onContextMenuShowingChanged?: (value: boolean) => void;
2022-08-19 18:36:47 +00:00
showToast: ShowToastActionCreatorType;
};
2022-11-18 00:45:19 +00:00
export function StoriesAddStoryButton({
2022-08-19 18:36:47 +00:00
children,
i18n,
moduleClassName,
onAddStory,
showToast,
onContextMenuShowingChanged,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element {
2022-08-19 18:36:47 +00:00
return (
<ContextMenu
ariaLabel={i18n('Stories__add')}
i18n={i18n}
onMenuShowingChanged={onContextMenuShowingChanged}
2022-08-19 18:36:47 +00:00
menuOptions={[
{
label: i18n('Stories__add-story--media'),
onClick: () => {
const input = document.createElement('input');
input.accept = 'image/*,video/mp4';
input.type = 'file';
input.onchange = async () => {
const file = input.files ? input.files[0] : undefined;
if (!file) {
return;
}
const result = await isVideoGoodForStories(file);
if (
result === ReasonVideoNotGood.UnsupportedCodec ||
result === ReasonVideoNotGood.UnsupportedContainer
) {
showToast(ToastType.StoryVideoUnsupported);
return;
}
if (result === ReasonVideoNotGood.TooLong) {
showToast(ToastType.StoryVideoTooLong);
return;
}
if (result !== ReasonVideoNotGood.AllGoodNevermind) {
showToast(ToastType.StoryVideoError);
return;
}
onAddStory(file);
};
input.click();
},
},
{
label: i18n('Stories__add-story--text'),
onClick: () => onAddStory(),
},
]}
moduleClassName={moduleClassName}
popperOptions={{
placement: 'bottom',
strategy: 'absolute',
}}
theme={Theme.Dark}
>
{children}
</ContextMenu>
);
2022-11-18 00:45:19 +00:00
}