fix(webui): preserve composer drafts across navigation and reloads (#5912)
* fix(webui): preserve composer drafts when switching sessions * fix(webui): restore ordinary composer drafts after reload * fix(webui): retain draft identity across composer remounts
This commit is contained in:
1 parent
6eccc2859a
commit
9756468a8c
7 files changed
+499
-7
No files matched your search
@@ -121,6 +121,8 @@ import {
|
||||
} from "@/lib/session-drag";
|
||||
import { formatQuotedUserMessage } from "@/lib/user-message-quote";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { composerMentionText } from "@/lib/composer-mention-text";
|
||||
import type { ComposerDraftStore } from "@/lib/composer-draft";
|
||||
|
||||
const VOICE_SHORTCUT_CODE = "KeyD";
|
||||
const VOICE_SHORTCUT_ARIA = "Control+Shift+D";
|
||||
@@ -230,6 +232,9 @@ interface ThreadComposerProps {
|
||||
onPickWorkspaceFolder?: () => Promise<string | null>;
|
||||
onWorkspaceScopeChange?: (scope: WorkspaceScopePayload) => void;
|
||||
pendingQueueKey?: string | null;
|
||||
draftKey?: string;
|
||||
draftStore?: ComposerDraftStore;
|
||||
persistDraft?: boolean;
|
||||
transcriptionProvider?: string | null;
|
||||
ingressLimits?: WebUIIngressLimits | null;
|
||||
quotedContext?: string | null;
|
||||
@@ -931,6 +936,9 @@ export function ThreadComposer({
|
||||
onPickWorkspaceFolder,
|
||||
onWorkspaceScopeChange,
|
||||
pendingQueueKey = null,
|
||||
draftKey,
|
||||
draftStore,
|
||||
persistDraft = false,
|
||||
transcriptionProvider = null,
|
||||
ingressLimits = null,
|
||||
quotedContext = null,
|
||||
@@ -938,10 +946,13 @@ export function ThreadComposer({
|
||||
onQuotedContextChange,
|
||||
}: ThreadComposerProps) {
|
||||
const { t } = useTranslation();
|
||||
const [value, setValue] = useState("");
|
||||
const [initialDraft] = useState(() => draftKey ? draftStore?.get(draftKey, persistDraft) : undefined);
|
||||
const [value, setValue] = useState(initialDraft?.text ?? "");
|
||||
const [composerFocused, setComposerFocused] = useState(false);
|
||||
const blurFrame = useRef<number | null>(null);
|
||||
const [selectedSessionMentions, setSelectedSessionMentions] = useState<SessionMention[]>([]);
|
||||
const [selectedSessionMentions, setSelectedSessionMentions] = useState<SessionMention[]>(
|
||||
initialDraft?.sessionMentions ?? [],
|
||||
);
|
||||
const [sessionDragPreview, setSessionDragPreview] = useState<{
|
||||
mention: SessionMention;
|
||||
start: number;
|
||||
@@ -1038,6 +1049,14 @@ export function ThreadComposer({
|
||||
const maxTextBytes = ingressLimits?.message.max_text_bytes ?? 64 * 1024;
|
||||
const { images, enqueue, remove, clear, restoreReadyImages, encoding, full } =
|
||||
useAttachedImages({ ingressLimits });
|
||||
const restoredDraftAttachments = useRef(false);
|
||||
const [draftAttachmentsReady, setDraftAttachmentsReady] = useState(!initialDraft?.files.length);
|
||||
useLayoutEffect(() => {
|
||||
if (restoredDraftAttachments.current) return;
|
||||
restoredDraftAttachments.current = true;
|
||||
if (initialDraft?.files.length) enqueue(initialDraft.files);
|
||||
setDraftAttachmentsReady(true);
|
||||
}, [enqueue, initialDraft]);
|
||||
|
||||
const formatRejection = useCallback(
|
||||
(reason: AttachmentError): string => {
|
||||
@@ -1330,6 +1349,20 @@ export function ThreadComposer({
|
||||
resetKey: pendingQueueKey,
|
||||
});
|
||||
const { rawSelection, replace: replaceMentionInput } = mentionInput;
|
||||
const draftText = composerMentionText(mentionInput.segments).raw;
|
||||
useLayoutEffect(() => {
|
||||
if (!draftKey || !draftStore || !draftAttachmentsReady) return;
|
||||
if (!draftText && images.length === 0 && !quotedContext) {
|
||||
draftStore.delete(draftKey);
|
||||
return;
|
||||
}
|
||||
draftStore.set(draftKey, {
|
||||
text: draftText,
|
||||
files: images.map((image) => image.file),
|
||||
sessionMentions: selectedSessionMentions,
|
||||
quotedContext,
|
||||
}, persistDraft);
|
||||
}, [draftAttachmentsReady, draftKey, draftStore, images, persistDraft, quotedContext, selectedSessionMentions, draftText]);
|
||||
const sessionDragInsertion = sessionDragPreview
|
||||
? mentionInsertion(
|
||||
value,
|
||||
@@ -2035,7 +2068,11 @@ export function ThreadComposer({
|
||||
const isSlashSideChannel = isSideChannelLifecycle(slashLifecycle);
|
||||
const finalizeActiveTurn =
|
||||
slashLifecycle === "finalize_active_turn";
|
||||
const submittedDraft = draftKey ? draftStore?.get(draftKey) : undefined;
|
||||
const finishSend = () => {
|
||||
// A pending send can finish after this composer unmounts and a newer draft is started.
|
||||
if (draftKey && draftStore && draftStore.get(draftKey) !== submittedDraft) return;
|
||||
if (draftKey) draftStore?.delete(draftKey);
|
||||
if (hasTouchPrimaryPointer) textareaRef.current?.blur();
|
||||
setQueuedPrompts([]);
|
||||
// Bubble owns the data URL copy; safe to revoke every staged blob
|
||||
@@ -2073,6 +2110,8 @@ export function ThreadComposer({
|
||||
activeMcpPresetMentions,
|
||||
activeSessionMentions,
|
||||
canSend,
|
||||
draftKey,
|
||||
draftStore,
|
||||
clear,
|
||||
clearComposerText,
|
||||
hasTouchPrimaryPointer,
|
||||
|
||||
@@ -16,6 +16,7 @@ import { PromptNavigator } from "@/components/thread/PromptNavigator";
|
||||
import { ModelFallbackNotice } from "@/components/thread/ModelFallbackNotice";
|
||||
import { RecoveryNotice } from "@/components/thread/RecoveryNotice";
|
||||
import { SessionInfoPopover } from "@/components/thread/SessionInfoPopover";
|
||||
import type { ComposerDraftStore } from "@/lib/composer-draft";
|
||||
import { ThreadComposer } from "@/components/thread/ThreadComposer";
|
||||
import type {
|
||||
ComposerContextUsage,
|
||||
@@ -411,6 +412,7 @@ interface ThreadShellProps {
|
||||
temporaryChatIds?: readonly string[];
|
||||
messageCache?: ThreadMessageCache;
|
||||
filePreviewStore?: FilePreviewStore;
|
||||
draftStore?: ComposerDraftStore;
|
||||
temporaryChatEnabled?: boolean;
|
||||
onTemporaryChatEnabledChange?: (enabled: boolean) => void;
|
||||
onToggleSidebar: () => void;
|
||||
@@ -633,6 +635,7 @@ export function ThreadShell({
|
||||
temporaryChatIds = [],
|
||||
messageCache,
|
||||
filePreviewStore,
|
||||
draftStore,
|
||||
temporaryChatEnabled = false,
|
||||
onTemporaryChatEnabledChange,
|
||||
onToggleSidebar,
|
||||
@@ -739,7 +742,15 @@ export function ThreadShell({
|
||||
const previewOpen = Boolean(activePreview);
|
||||
const [filePreviewMaxWidth, setFilePreviewMaxWidth] = useState(FILE_PREVIEW_MAX_WIDTH);
|
||||
const filePreviewWidth = clampFilePreviewWidth(previewState.width, filePreviewMaxWidth);
|
||||
const [quotedContext, setQuotedContext] = useState<string | null>(null);
|
||||
const draftKey = session?.key ?? (temporaryChatEnabled ? "new:temporary" : "new:chat");
|
||||
const persistDraft = session ? !temporary : !temporaryChatEnabled;
|
||||
const [quote, setQuote] = useState<{ key: string; text: string | null } | null>(null);
|
||||
const quotedContext = quote?.key === draftKey
|
||||
? quote.text
|
||||
: draftStore?.get(draftKey, persistDraft)?.quotedContext ?? null;
|
||||
const setQuotedContext = useCallback((text: string | null) => {
|
||||
setQuote({ key: draftKey, text });
|
||||
}, [draftKey]);
|
||||
const [composerFocusSignal, setComposerFocusSignal] = useState(0);
|
||||
const shellRef = useRef<HTMLElement | null>(null);
|
||||
const composerSurfaceRef = useRef<HTMLDivElement | null>(null);
|
||||
@@ -876,7 +887,6 @@ export function ThreadShell({
|
||||
filePreviewCloseTimerRef.current = null;
|
||||
}
|
||||
setClosingPreview(null);
|
||||
setQuotedContext(null);
|
||||
setSubmittedViewportTurnId(null);
|
||||
}, [previewSessionKey]);
|
||||
|
||||
@@ -896,7 +906,7 @@ export function ThreadShell({
|
||||
const handleQuoteSelection = useCallback((text: string) => {
|
||||
setQuotedContext(text);
|
||||
setComposerFocusSignal((value) => value + 1);
|
||||
}, []);
|
||||
}, [setQuotedContext]);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
@@ -1506,6 +1516,7 @@ export function ThreadShell({
|
||||
activeViewportTurnByChatIdRef.current.set(chatId, submitted.turnId);
|
||||
setSubmittedViewportTurnId(submitted.turnId);
|
||||
}
|
||||
return submitted !== null;
|
||||
},
|
||||
[chatId, send, withWorkspaceScope],
|
||||
);
|
||||
@@ -1715,6 +1726,10 @@ export function ThreadShell({
|
||||
) : null}
|
||||
{session ? (
|
||||
<ThreadComposer
|
||||
key={draftKey}
|
||||
draftKey={draftKey}
|
||||
draftStore={draftStore}
|
||||
persistDraft={persistDraft}
|
||||
onSend={handleThreadSend}
|
||||
disabled={!chatId}
|
||||
inputAriaLabel={composerInputAriaLabel}
|
||||
@@ -1765,6 +1780,10 @@ export function ThreadShell({
|
||||
/>
|
||||
) : (
|
||||
<ThreadComposer
|
||||
key={draftKey}
|
||||
draftKey={draftKey}
|
||||
draftStore={draftStore}
|
||||
persistDraft={persistDraft}
|
||||
onSend={handleWelcomeSend}
|
||||
disabled={booting}
|
||||
inputAriaLabel={composerInputAriaLabel}
|
||||
@@ -1808,6 +1827,8 @@ export function ThreadShell({
|
||||
onWorkspaceScopeChange={onWorkspaceScopeChange}
|
||||
transcriptionProvider={settingsSnapshot?.transcription?.provider}
|
||||
ingressLimits={ingressLimits}
|
||||
quotedContext={quotedContext}
|
||||
onQuotedContextChange={setQuotedContext}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
|
||||
Reference in new issue
Block a user