* Read the persisted history map, migrating the legacy global `string[]` format * (pre per-session) into the current session on first sight. Migration is * one-shot: once a sessioned map is written, the array branch never runs again.
(sessionId: string | undefined)
| 41 | * one-shot: once a sessioned map is written, the array branch never runs again. |
| 42 | */ |
| 43 | function loadMap(sessionId: string | undefined): Record<string, string[]> { |
| 44 | const raw = safeGetJson<unknown>(STORAGE_KEYS.inputHistory); |
| 45 | if (Array.isArray(raw)) { |
| 46 | const list = raw.filter((s): s is string => typeof s === 'string' && s.length > 0); |
| 47 | // No session yet (empty-session composer): leave the legacy value in place |
| 48 | // so a later docked mount — which has a session id — can migrate it. |
| 49 | if (!sessionId || list.length === 0) return {}; |
| 50 | const capped = list.length > MAX_HISTORY ? list.slice(-MAX_HISTORY) : list; |
| 51 | const map = { [sessionId]: capped }; |
| 52 | safeSetJson(STORAGE_KEYS.inputHistory, map); |
| 53 | return map; |
| 54 | } |
| 55 | if (raw && typeof raw === 'object') { |
| 56 | return raw as Record<string, string[]>; |
| 57 | } |
| 58 | return {}; |
| 59 | } |
| 60 | |
| 61 | export function useInputHistory(deps: InputHistoryDeps) { |
| 62 | const { text, textareaRef, autosize, sessionId } = deps; |
no test coverage detected