ConversationView: Move attachments processing into redux
This commit is contained in:
parent
ff6750e4fd
commit
452e0b7b31
25 changed files with 544 additions and 763 deletions
|
@ -2,116 +2,43 @@
|
|||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { ChangeEventHandler } from 'react';
|
||||
import React, { forwardRef, useState } from 'react';
|
||||
import React, { forwardRef } from 'react';
|
||||
|
||||
import type {
|
||||
InMemoryAttachmentDraftType,
|
||||
AttachmentDraftType,
|
||||
} from '../types/Attachment';
|
||||
import type { AttachmentDraftType } from '../types/Attachment';
|
||||
import { isVideoAttachment, isImageAttachment } from '../types/Attachment';
|
||||
import { AttachmentToastType } from '../types/AttachmentToastType';
|
||||
import type { LocalizerType } from '../types/Util';
|
||||
|
||||
import { ToastCannotMixMultiAndNonMultiAttachments } from './ToastCannotMixMultiAndNonMultiAttachments';
|
||||
import { ToastDangerousFileType } from './ToastDangerousFileType';
|
||||
import { ToastFileSize } from './ToastFileSize';
|
||||
import { ToastMaxAttachments } from './ToastMaxAttachments';
|
||||
import { ToastUnsupportedMultiAttachment } from './ToastUnsupportedMultiAttachment';
|
||||
import { ToastUnableToLoadAttachment } from './ToastUnableToLoadAttachment';
|
||||
import type { HandleAttachmentsProcessingArgsType } from '../util/handleAttachmentsProcessing';
|
||||
import {
|
||||
getSupportedImageTypes,
|
||||
getSupportedVideoTypes,
|
||||
} from '../util/GoogleChrome';
|
||||
|
||||
export type PropsType = {
|
||||
addAttachment: (
|
||||
conversationId: string,
|
||||
attachment: InMemoryAttachmentDraftType
|
||||
) => unknown;
|
||||
addPendingAttachment: (
|
||||
conversationId: string,
|
||||
pendingAttachment: AttachmentDraftType
|
||||
) => unknown;
|
||||
conversationId: string;
|
||||
draftAttachments: ReadonlyArray<AttachmentDraftType>;
|
||||
i18n: LocalizerType;
|
||||
processAttachments: (options: HandleAttachmentsProcessingArgsType) => unknown;
|
||||
removeAttachment: (conversationId: string, filePath: string) => unknown;
|
||||
processAttachments: (options: {
|
||||
conversationId: string;
|
||||
files: ReadonlyArray<File>;
|
||||
}) => unknown;
|
||||
};
|
||||
|
||||
export const CompositionUpload = forwardRef<HTMLInputElement, PropsType>(
|
||||
function CompositionUploadInner(
|
||||
{
|
||||
addAttachment,
|
||||
addPendingAttachment,
|
||||
conversationId,
|
||||
draftAttachments,
|
||||
i18n,
|
||||
processAttachments,
|
||||
removeAttachment,
|
||||
},
|
||||
{ conversationId, draftAttachments, processAttachments },
|
||||
ref
|
||||
) {
|
||||
const [toastType, setToastType] = useState<
|
||||
AttachmentToastType | undefined
|
||||
>();
|
||||
|
||||
const onFileInputChange: ChangeEventHandler<
|
||||
HTMLInputElement
|
||||
> = async event => {
|
||||
const files = event.target.files || [];
|
||||
|
||||
await processAttachments({
|
||||
addAttachment,
|
||||
addPendingAttachment,
|
||||
conversationId,
|
||||
files: Array.from(files),
|
||||
draftAttachments,
|
||||
onShowToast: setToastType,
|
||||
removeAttachment,
|
||||
});
|
||||
};
|
||||
|
||||
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.ToastUnsupportedMultiAttachment
|
||||
) {
|
||||
toast = (
|
||||
<ToastUnsupportedMultiAttachment i18n={i18n} onClose={closeToast} />
|
||||
);
|
||||
} else if (
|
||||
toastType ===
|
||||
AttachmentToastType.ToastCannotMixMultiAndNonMultiAttachments
|
||||
) {
|
||||
toast = (
|
||||
<ToastCannotMixMultiAndNonMultiAttachments
|
||||
i18n={i18n}
|
||||
onClose={closeToast}
|
||||
/>
|
||||
);
|
||||
} else if (toastType === AttachmentToastType.ToastUnableToLoadAttachment) {
|
||||
toast = <ToastUnableToLoadAttachment i18n={i18n} onClose={closeToast} />;
|
||||
}
|
||||
|
||||
const anyVideoOrImageAttachments = draftAttachments.some(attachment => {
|
||||
return isImageAttachment(attachment) || isVideoAttachment(attachment);
|
||||
});
|
||||
|
@ -121,17 +48,14 @@ export const CompositionUpload = forwardRef<HTMLInputElement, PropsType>(
|
|||
: null;
|
||||
|
||||
return (
|
||||
<>
|
||||
{toast}
|
||||
<input
|
||||
hidden
|
||||
multiple
|
||||
onChange={onFileInputChange}
|
||||
ref={ref}
|
||||
type="file"
|
||||
accept={acceptContentTypes?.join(',')}
|
||||
/>
|
||||
</>
|
||||
<input
|
||||
hidden
|
||||
multiple
|
||||
onChange={onFileInputChange}
|
||||
ref={ref}
|
||||
type="file"
|
||||
accept={acceptContentTypes?.join(',')}
|
||||
/>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue