fix: prevent file attachment dialog from opening twice

Add a ref-based guard to handleUploadClick that prevents the native file
picker from being triggered a second time when the browser re-dispatches
a click event after the dialog closes. This matches the existing pattern
used in document-upload-popup.tsx for the Upload Documents dialog.
This commit is contained in:
mainnebula 2026-02-16 18:12:10 -05:00
parent ce110faa5a
commit b3042a02be

View file

@ -253,6 +253,7 @@ const Composer: FC = () => {
const editorRef = useRef<InlineMentionEditorRef>(null); const editorRef = useRef<InlineMentionEditorRef>(null);
const editorContainerRef = useRef<HTMLDivElement>(null); const editorContainerRef = useRef<HTMLDivElement>(null);
const uploadInputRef = useRef<HTMLInputElement>(null); const uploadInputRef = useRef<HTMLInputElement>(null);
const isFileDialogOpenRef = useRef(false);
const documentPickerRef = useRef<DocumentMentionPickerRef>(null); const documentPickerRef = useRef<DocumentMentionPickerRef>(null);
const { search_space_id, chat_id } = useParams(); const { search_space_id, chat_id } = useParams();
const setMentionedDocumentIds = useSetAtom(mentionedDocumentIdsAtom); const setMentionedDocumentIds = useSetAtom(mentionedDocumentIdsAtom);
@ -516,11 +517,18 @@ const Composer: FC = () => {
); );
const handleUploadClick = useCallback(() => { const handleUploadClick = useCallback(() => {
if (isFileDialogOpenRef.current) return;
isFileDialogOpenRef.current = true;
uploadInputRef.current?.click(); uploadInputRef.current?.click();
// Reset after a delay to handle cancellation (which doesn't fire the change event).
setTimeout(() => {
isFileDialogOpenRef.current = false;
}, 1000);
}, []); }, []);
const handleUploadInputChange = useCallback( const handleUploadInputChange = useCallback(
async (event: React.ChangeEvent<HTMLInputElement>) => { async (event: React.ChangeEvent<HTMLInputElement>) => {
isFileDialogOpenRef.current = false;
const files = Array.from(event.target.files ?? []); const files = Array.from(event.target.files ?? []);
event.target.value = ""; event.target.value = "";
if (files.length === 0 || !search_space_id) return; if (files.length === 0 || !search_space_id) return;