fix: keep clawd submitted text out of composer restore

This commit is contained in:
134 2026-06-29 18:07:08 +08:00
parent 02772bc343
commit 8a9fba7eab

View File

@ -376,6 +376,9 @@ const CLAWD_TABLE_MAX_BYTES = 10 * 1024 * 1024;
const CLAWD_PROCESSING_HINT = const CLAWD_PROCESSING_HINT =
"已收到,这个任务稍微繁重一些,处理时间可能会长一点,请耐心等候。"; "已收到,这个任务稍微繁重一些,处理时间可能会长一点,请耐心等候。";
const CLAWD_SUBMITTED_COMPOSER_RECOVERY_TTL_MS = 30 * 60 * 1000; const CLAWD_SUBMITTED_COMPOSER_RECOVERY_TTL_MS = 30 * 60 * 1000;
const CLAWD_SUBMITTED_COMPOSER_RESTORE_GUARD_DELAYS_MS = [
0, 80, 180, 360, 700, 1200, 2000, 3500, 6000, 10000,
];
const CLAWD_SUBMITTED_COMPOSER_STORAGE_KEY = const CLAWD_SUBMITTED_COMPOSER_STORAGE_KEY =
"ai-chatbot:clawd-submitted-composer"; "ai-chatbot:clawd-submitted-composer";
const CLAWD_SIMPLE_PROMPT_PATTERNS = [ const CLAWD_SIMPLE_PROMPT_PATTERNS = [
@ -2467,28 +2470,79 @@ export function CanvasChatPanel({
isClawdSubmissionForCurrentComposer, isClawdSubmissionForCurrentComposer,
]); ]);
const scheduleClearRestoredClawdComposerText = useCallback(() => {
if (typeof window === "undefined") return;
CLAWD_SUBMITTED_COMPOSER_RESTORE_GUARD_DELAYS_MS.forEach((delay) => {
window.setTimeout(clearRestoredClawdComposerText, delay);
});
}, [clearRestoredClawdComposerText]);
useEffect(() => { useEffect(() => {
if (typeof window === "undefined") return; if (typeof window === "undefined") return;
const clearAfterRestore = () => {
[0, 120, 500, 1500].forEach((delay) => {
window.setTimeout(clearRestoredClawdComposerText, delay);
});
};
const handleVisibilityChange = () => { const handleVisibilityChange = () => {
if (document.visibilityState === "visible") { if (document.visibilityState === "visible") {
clearAfterRestore(); scheduleClearRestoredClawdComposerText();
} }
}; };
document.addEventListener("visibilitychange", handleVisibilityChange); document.addEventListener("visibilitychange", handleVisibilityChange);
window.addEventListener("focus", clearAfterRestore); window.addEventListener("focus", scheduleClearRestoredClawdComposerText);
window.addEventListener("pageshow", clearAfterRestore); window.addEventListener("pageshow", scheduleClearRestoredClawdComposerText);
return () => { return () => {
document.removeEventListener("visibilitychange", handleVisibilityChange); document.removeEventListener("visibilitychange", handleVisibilityChange);
window.removeEventListener("focus", clearAfterRestore); window.removeEventListener("focus", scheduleClearRestoredClawdComposerText);
window.removeEventListener("pageshow", clearAfterRestore); window.removeEventListener("pageshow", scheduleClearRestoredClawdComposerText);
};
}, [scheduleClearRestoredClawdComposerText]);
useEffect(() => {
if (typeof window === "undefined" || typeof MutationObserver === "undefined") {
return;
}
let observedEditor: HTMLDivElement | null = null;
let observer: MutationObserver | null = null;
let scheduled = false;
let scheduledTimer: number | null = null;
const scheduleMutationCheck = () => {
if (scheduled) return;
scheduled = true;
scheduledTimer = window.setTimeout(() => {
scheduled = false;
scheduledTimer = null;
clearRestoredClawdComposerText();
}, 0);
};
const observeCurrentEditor = () => {
const editableDiv = textareaRef.current as HTMLDivElement | null;
if (editableDiv === observedEditor) return;
observer?.disconnect();
observer = null;
observedEditor = editableDiv;
if (!editableDiv) return;
observer = new MutationObserver(scheduleMutationCheck);
observer.observe(editableDiv, {
childList: true,
characterData: true,
subtree: true,
});
};
observeCurrentEditor();
const observerRefreshTimer = window.setInterval(observeCurrentEditor, 1000);
return () => {
observer?.disconnect();
window.clearInterval(observerRefreshTimer);
if (scheduledTimer !== null) {
window.clearTimeout(scheduledTimer);
}
}; };
}, [clearRestoredClawdComposerText]); }, [clearRestoredClawdComposerText]);
@ -10050,9 +10104,7 @@ export function CanvasChatPanel({
]) ])
); );
clearComposerEditor(); clearComposerEditor();
[0, 120, 500, 1500].forEach((delay) => { scheduleClearRestoredClawdComposerText();
window.setTimeout(clearRestoredClawdComposerText, delay);
});
setAttachments([]); setAttachments([]);
setClawdTableFiles([]); setClawdTableFiles([]);
setSelectedSkillTagId(null); setSelectedSkillTagId(null);
@ -10513,7 +10565,7 @@ export function CanvasChatPanel({
setSendGuard, setSendGuard,
setMessages, setMessages,
clearComposerEditor, clearComposerEditor,
clearRestoredClawdComposerText, scheduleClearRestoredClawdComposerText,
shouldBlockDuplicateSend, shouldBlockDuplicateSend,
status, status,
mutate, mutate,
@ -14232,6 +14284,9 @@ export function CanvasChatPanel({
activeClawdSubmission, activeClawdSubmission,
textContent textContent
); );
if (isRestoredSubmittedClawdText) {
scheduleClearRestoredClawdComposerText();
}
if (!isRestoredSubmittedClawdText) { if (!isRestoredSubmittedClawdText) {
composerUserEditedAtRef.current = Date.now(); composerUserEditedAtRef.current = Date.now();
} }