@@ -55,6 +55,38 @@ function composeMessage(text: string, quote: string | null | undefined): string return parts.join(' ') } +type DraftPayload = { + text: string + quote: string | null +} + +function readDraft(raw: string | null): DraftPayload | null { + if (raw === null || raw === '') { + return null + } + try { + const parsed = JSON.parse(raw) as unknown + if (parsed && typeof parsed === 'object' && 'text' in parsed) { + const draft = parsed as { text?: unknown; quote?: unknown } + return { + text: typeof draft.text === 'string' ? draft.text : '', + quote: typeof draft.quote === 'string' && draft.quote !== '' ? draft.quote : null, + } + } + } catch { + // legacy: plain text string + } + return { text: raw, quote: null } +} + +function writeDraft(key: string, text: string, quote: string | null) { + if (text === '' && quote == null) { + window.localStorage.removeItem(key) + return + } + window.localStorage.setItem(key, JSON.stringify({ text, quote } satisfies DraftPayload)) +} + interface Input { loading: boolean wonderMe?: () => void @@ -120,6 +152,8 @@ export const ModelInput: FC = ({ const draftHydratedRef = useRef(false) const setValueRef = useRef(setValue) setValueRef.current = setValue + const onQuoteChangeRef = useRef(onQuoteChange) + onQuoteChangeRef.current = onQuoteChange const clearDraft = useCallback(() => { try { @@ -224,16 +258,21 @@ export const ModelInput: FC = ({ if (resendValue) { return } - const saved = window.localStorage.getItem(draftStorageKey) - if (saved !== null && saved !== '' && value === '') { - setValueRef.current(saved) + const draft = readDraft(window.localStorage.getItem(draftStorageKey)) + if (draft) { + if (draft.text !== '' && value === '') { + setValueRef.current(draft.text) + } + if (draft.quote) { + onQuoteChangeRef.current?.(draft.quote) + } return } } - window.localStorage.setItem(draftStorageKey, value) + writeDraft(draftStorageKey, value, quoteValue ?? null) } catch { } - }, [draftStorageKey, resendValue, value]) + }, [draftStorageKey, resendValue, value, quoteValue]) useEffect(() => { const handleTourSuggestedQuery = (e: CustomEvent<{ query: string }>) => {