mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-07 03:10:12 +00:00
Guard the composer against draft resurrection now that removeDraft enqueues a durable DELETE, and fail closed on unreconstructable burn-on-read sends. - Add per-composer editor_guard module (keyed by serverUrl:channelId:rootId): tracks genuine unsaved edits + a generation bumped on Send/Delete. Shared by the save site and the invalidation sites; avoids prop-drilling. - post_input: route blur/background/unmount through guardedSave, which no-ops when the editor is clean and discards its write (via a captured-generation staleness check) if Send/Delete invalidated the editor mid-save. Genuine typing marks dirty; an external/remote value change resets the baseline clean; unmount clears the entry. - draft_handler.clearDraft and deleteDraftConfirmation invalidate the editor so a pending lifecycle save cannot recreate a just-sent/deleted draft. - handleDraftUpdate discards stale markdown-image metadata when the editor generation changed during the async parse. - handle_send_message: fail-closed burn-on-read guard on both send paths — an enabled BoR send with invalid/missing durations shows a localized error, keeps the draft, and never creates a downgraded plain post. Adds one en.json string. Checks: npm run tsc clean; eslint --quiet clean; 126 Jest tests pass (editor guard, handle_send_message incl. BoR fail-closed, draft actions, draft utils). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
157 lines
5.4 KiB
TypeScript
157 lines
5.4 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import React, {useCallback, useEffect, useRef} from 'react';
|
|
import {useIntl} from 'react-intl';
|
|
|
|
import {addFilesToDraft, removeDraft} from '@actions/local/draft';
|
|
import {useServerUrl} from '@context/server';
|
|
import useFileUploadError from '@hooks/file_upload_error';
|
|
import DraftEditPostUploadManager from '@managers/draft_upload_manager';
|
|
import {fileMaxWarning, fileSizeWarning, getUploadErrorMessage, uploadDisabledWarning} from '@utils/file';
|
|
|
|
import {buildEditorKey, invalidateEditor} from '../editor_guard';
|
|
import SendHandler from '../send_handler';
|
|
|
|
import type {ErrorHandlers} from '@typings/components/upload_error_handlers';
|
|
import type {AvailableScreens} from '@typings/screens/navigation';
|
|
|
|
type Props = {
|
|
testID?: string;
|
|
channelId: string;
|
|
cursorPosition: number;
|
|
rootId?: string;
|
|
canShowPostPriority?: boolean;
|
|
files?: FileInfo[];
|
|
maxFileCount: number;
|
|
maxFileSize: number;
|
|
canUploadFiles: boolean;
|
|
updateCursorPosition: React.Dispatch<React.SetStateAction<number>>;
|
|
updatePostInputTop: (top: number) => void;
|
|
updateValue: React.Dispatch<React.SetStateAction<string>>;
|
|
value: string;
|
|
setIsFocused: (isFocused: boolean) => void;
|
|
onPostCreated?: (postId: string) => void;
|
|
location?: AvailableScreens;
|
|
}
|
|
|
|
const emptyFileList: FileInfo[] = [];
|
|
|
|
export default function DraftHandler(props: Props) {
|
|
const {
|
|
testID,
|
|
channelId,
|
|
cursorPosition,
|
|
rootId = '',
|
|
canShowPostPriority,
|
|
files,
|
|
maxFileCount,
|
|
maxFileSize,
|
|
canUploadFiles,
|
|
updateCursorPosition,
|
|
updatePostInputTop,
|
|
updateValue,
|
|
value,
|
|
setIsFocused,
|
|
onPostCreated,
|
|
location,
|
|
} = props;
|
|
|
|
const serverUrl = useServerUrl();
|
|
const intl = useIntl();
|
|
|
|
const uploadErrorHandlers = useRef<ErrorHandlers>({});
|
|
const {uploadError, newUploadError} = useFileUploadError();
|
|
|
|
const handleUploadError = useCallback((errorMessage: string, errorName?: string) => {
|
|
newUploadError(getUploadErrorMessage(intl, errorMessage, errorName));
|
|
}, [intl, newUploadError]);
|
|
|
|
const clearDraft = useCallback(() => {
|
|
removeDraft(serverUrl, channelId, rootId);
|
|
|
|
// Send/Delete resurrection guard: invalidate the editor generation so a
|
|
// still-pending lifecycle save cannot recreate the just-cleared draft.
|
|
invalidateEditor(buildEditorKey(serverUrl, channelId, rootId));
|
|
updateValue('');
|
|
}, [serverUrl, channelId, rootId, updateValue]);
|
|
|
|
const addFiles = useCallback((newFiles: FileInfo[]) => {
|
|
if (!newFiles.length) {
|
|
return;
|
|
}
|
|
|
|
if (!canUploadFiles) {
|
|
newUploadError(uploadDisabledWarning(intl));
|
|
return;
|
|
}
|
|
|
|
const currentFileCount = files?.length || 0;
|
|
const availableCount = maxFileCount - currentFileCount;
|
|
if (newFiles.length > availableCount) {
|
|
newUploadError(fileMaxWarning(intl, maxFileCount));
|
|
return;
|
|
}
|
|
|
|
const largeFile = newFiles.find((file) => file.size > maxFileSize);
|
|
if (largeFile) {
|
|
newUploadError(fileSizeWarning(intl, maxFileSize));
|
|
return;
|
|
}
|
|
|
|
addFilesToDraft(serverUrl, channelId, rootId, newFiles);
|
|
|
|
for (const file of newFiles) {
|
|
DraftEditPostUploadManager.prepareUpload(serverUrl, file, channelId, rootId);
|
|
uploadErrorHandlers.current[file.clientId!] = DraftEditPostUploadManager.registerErrorHandler(file.clientId!, handleUploadError);
|
|
}
|
|
|
|
newUploadError(null);
|
|
}, [canUploadFiles, files?.length, maxFileCount, serverUrl, channelId, rootId, newUploadError, intl, maxFileSize, handleUploadError]);
|
|
|
|
// This effect mainly handles keeping clean the uploadErrorHandlers, and
|
|
// reinstantiate them on component mount and file retry.
|
|
useEffect(() => {
|
|
let loadingFiles: FileInfo[] = [];
|
|
if (files) {
|
|
loadingFiles = files.filter((v) => v.clientId && DraftEditPostUploadManager.isUploading(v.clientId));
|
|
}
|
|
|
|
for (const key of Object.keys(uploadErrorHandlers.current)) {
|
|
if (!loadingFiles.find((v) => v.clientId === key)) {
|
|
uploadErrorHandlers.current[key]?.();
|
|
delete (uploadErrorHandlers.current[key]);
|
|
}
|
|
}
|
|
|
|
for (const file of loadingFiles) {
|
|
if (!uploadErrorHandlers.current[file.clientId!]) {
|
|
uploadErrorHandlers.current[file.clientId!] = DraftEditPostUploadManager.registerErrorHandler(file.clientId!, handleUploadError);
|
|
}
|
|
}
|
|
}, [files, newUploadError, handleUploadError]);
|
|
|
|
return (
|
|
<SendHandler
|
|
testID={testID}
|
|
channelId={channelId}
|
|
rootId={rootId}
|
|
canShowPostPriority={canShowPostPriority}
|
|
|
|
// From draft handler
|
|
cursorPosition={cursorPosition}
|
|
value={value}
|
|
files={files || emptyFileList}
|
|
clearDraft={clearDraft}
|
|
addFiles={addFiles}
|
|
uploadFileError={uploadError}
|
|
updateCursorPosition={updateCursorPosition}
|
|
updatePostInputTop={updatePostInputTop}
|
|
updateValue={updateValue}
|
|
setIsFocused={setIsFocused}
|
|
onPostCreated={onPostCreated}
|
|
location={location}
|
|
/>
|
|
);
|
|
}
|