2021-09-24 20:02:30 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ChangeEventHandler } from 'react';
|
|
|
|
import React, { forwardRef, useState } from 'react';
|
2021-09-24 20:02:30 +00:00
|
|
|
|
2021-11-15 21:54:33 +00:00
|
|
|
import type {
|
|
|
|
InMemoryAttachmentDraftType,
|
|
|
|
AttachmentDraftType,
|
|
|
|
} from '../types/Attachment';
|
2021-09-24 20:02:30 +00:00
|
|
|
import { AttachmentToastType } from '../types/AttachmentToastType';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2021-09-24 20:02:30 +00:00
|
|
|
|
|
|
|
import { ToastCannotMixImageAndNonImageAttachments } from './ToastCannotMixImageAndNonImageAttachments';
|
|
|
|
import { ToastDangerousFileType } from './ToastDangerousFileType';
|
|
|
|
import { ToastFileSize } from './ToastFileSize';
|
|
|
|
import { ToastMaxAttachments } from './ToastMaxAttachments';
|
|
|
|
import { ToastOneNonImageAtATime } from './ToastOneNonImageAtATime';
|
|
|
|
import { ToastUnableToLoadAttachment } from './ToastUnableToLoadAttachment';
|
|
|
|
import type { HandleAttachmentsProcessingArgsType } from '../util/handleAttachmentsProcessing';
|
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
addAttachment: (
|
|
|
|
conversationId: string,
|
2021-11-15 21:54:33 +00:00
|
|
|
attachment: InMemoryAttachmentDraftType
|
2021-09-24 20:02:30 +00:00
|
|
|
) => unknown;
|
|
|
|
addPendingAttachment: (
|
|
|
|
conversationId: string,
|
2021-11-15 21:54:33 +00:00
|
|
|
pendingAttachment: AttachmentDraftType
|
2021-09-24 20:02:30 +00:00
|
|
|
) => unknown;
|
|
|
|
conversationId: string;
|
2021-11-15 21:54:33 +00:00
|
|
|
draftAttachments: ReadonlyArray<AttachmentDraftType>;
|
2021-09-24 20:02:30 +00:00
|
|
|
i18n: LocalizerType;
|
|
|
|
processAttachments: (options: HandleAttachmentsProcessingArgsType) => unknown;
|
|
|
|
removeAttachment: (conversationId: string, filePath: string) => unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const CompositionUpload = forwardRef<HTMLInputElement, PropsType>(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
addAttachment,
|
|
|
|
addPendingAttachment,
|
|
|
|
conversationId,
|
|
|
|
draftAttachments,
|
|
|
|
i18n,
|
|
|
|
processAttachments,
|
|
|
|
removeAttachment,
|
|
|
|
},
|
|
|
|
ref
|
|
|
|
) => {
|
|
|
|
const [toastType, setToastType] = useState<
|
|
|
|
AttachmentToastType | undefined
|
|
|
|
>();
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const onFileInputChange: ChangeEventHandler<HTMLInputElement> =
|
|
|
|
async event => {
|
|
|
|
const files = event.target.files || [];
|
2021-09-24 20:02:30 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
await processAttachments({
|
|
|
|
addAttachment,
|
|
|
|
addPendingAttachment,
|
|
|
|
conversationId,
|
|
|
|
files: Array.from(files),
|
|
|
|
draftAttachments,
|
|
|
|
onShowToast: setToastType,
|
|
|
|
removeAttachment,
|
|
|
|
});
|
|
|
|
};
|
2021-09-24 20:02:30 +00:00
|
|
|
|
|
|
|
function closeToast() {
|
|
|
|
setToastType(undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
let toast;
|
|
|
|
|
|
|
|
if (toastType === AttachmentToastType.ToastFileSize) {
|
|
|
|
toast = (
|
|
|
|
<ToastFileSize
|
|
|
|
i18n={i18n}
|
|
|
|
limit={100}
|
|
|
|
onClose={closeToast}
|
|
|
|
units="MB"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (toastType === AttachmentToastType.ToastDangerousFileType) {
|
|
|
|
toast = <ToastDangerousFileType i18n={i18n} onClose={closeToast} />;
|
|
|
|
} else if (toastType === AttachmentToastType.ToastMaxAttachments) {
|
|
|
|
toast = <ToastMaxAttachments i18n={i18n} onClose={closeToast} />;
|
|
|
|
} else if (toastType === AttachmentToastType.ToastOneNonImageAtATime) {
|
|
|
|
toast = <ToastOneNonImageAtATime i18n={i18n} onClose={closeToast} />;
|
|
|
|
} else if (
|
|
|
|
toastType ===
|
|
|
|
AttachmentToastType.ToastCannotMixImageAndNonImageAttachments
|
|
|
|
) {
|
|
|
|
toast = (
|
|
|
|
<ToastCannotMixImageAndNonImageAttachments
|
|
|
|
i18n={i18n}
|
|
|
|
onClose={closeToast}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (toastType === AttachmentToastType.ToastUnableToLoadAttachment) {
|
|
|
|
toast = <ToastUnableToLoadAttachment i18n={i18n} onClose={closeToast} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{toast}
|
|
|
|
<input
|
|
|
|
hidden
|
|
|
|
multiple
|
|
|
|
onChange={onFileInputChange}
|
|
|
|
ref={ref}
|
|
|
|
type="file"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|