fix(frontend): wait for async chat submit before clearing (#2940)

* fix(frontend): wait for async chat submit before clearing

* test(frontend): cover pending attachment uploads

* fix(frontend): preserve sync submit semantics
This commit is contained in:
Admire
2026-05-15 22:27:10 +08:00
committed by GitHub
parent 7a2670eaea
commit 7c42ab3e16
5 changed files with 120 additions and 17 deletions
@@ -499,6 +499,10 @@ export const PromptInput = ({
// Keep a ref to files for cleanup on unmount (avoids stale closure)
const filesRef = useRef(files);
filesRef.current = files;
const providerTextRef = useRef("");
if (usingProvider) {
providerTextRef.current = controller.textInput.value;
}
const openFileDialogLocal = useCallback(() => {
inputRef.current?.click();
@@ -768,6 +772,24 @@ export const PromptInput = ({
}
// Convert blob URLs to data URLs asynchronously
const submittedFileIds = files.map((file) => file.id);
const clearSubmittedState = () => {
const currentFileIds = new Set(filesRef.current.map((file) => file.id));
const submittedFileIdsStillPresent = submittedFileIds.filter((id) =>
currentFileIds.has(id),
);
if (submittedFileIdsStillPresent.length === filesRef.current.length) {
clear();
} else {
for (const id of submittedFileIdsStillPresent) {
remove(id);
}
}
if (usingProvider && providerTextRef.current === text) {
controller.textInput.clear();
}
};
Promise.all(
files.map(async ({ id, ...item }) => {
if (item.file instanceof File) {
@@ -793,20 +815,14 @@ export const PromptInput = ({
if (result instanceof Promise) {
result
.then(() => {
clear();
if (usingProvider) {
controller.textInput.clear();
}
clearSubmittedState();
})
.catch(() => {
// Don't clear on error - user may want to retry
});
} else {
// Sync function completed without throwing, clear attachments
clear();
if (usingProvider) {
controller.textInput.clear();
}
clearSubmittedState();
}
} catch {
// Don't clear on error - user may want to retry