51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import { createStore, type SetStoreFunction, type Store } from "solid-js/store"
|
|
import type { Prompt } from "@/context/prompt"
|
|
import { Persist, persisted } from "@/utils/persist"
|
|
import {
|
|
prependHistoryEntry,
|
|
type PromptHistoryComment,
|
|
type PromptHistoryStoredEntry,
|
|
} from "./history"
|
|
|
|
export type PromptInputHistory = {
|
|
entries: (mode: "normal" | "shell") => PromptHistoryStoredEntry[]
|
|
add: (prompt: Prompt, mode: "normal" | "shell", comments: PromptHistoryComment[]) => void
|
|
}
|
|
|
|
type PromptHistoryState = { entries: PromptHistoryStoredEntry[] }
|
|
|
|
function createPromptInputHistoryStore(
|
|
normal: Store<PromptHistoryState>,
|
|
setNormal: SetStoreFunction<PromptHistoryState>,
|
|
shell: Store<PromptHistoryState>,
|
|
setShell: SetStoreFunction<PromptHistoryState>,
|
|
): PromptInputHistory {
|
|
return {
|
|
entries: (mode) => (mode === "shell" ? shell.entries : normal.entries),
|
|
add(prompt, mode, comments) {
|
|
const current = mode === "shell" ? shell : normal
|
|
const setCurrent = mode === "shell" ? setShell : setNormal
|
|
const next = prependHistoryEntry(current.entries, prompt, comments)
|
|
if (next === current.entries) return
|
|
setCurrent("entries", next)
|
|
},
|
|
}
|
|
}
|
|
|
|
export function createPromptInputHistory(): PromptInputHistory {
|
|
const [normal, setNormal] = createStore<PromptHistoryState>({ entries: [] })
|
|
const [shell, setShell] = createStore<PromptHistoryState>({ entries: [] })
|
|
return createPromptInputHistoryStore(normal, setNormal, shell, setShell)
|
|
}
|
|
|
|
export function createPersistedPromptInputHistory() {
|
|
const [normal, setNormal] = persisted(
|
|
Persist.global("prompt-history", ["prompt-history.v1"]),
|
|
createStore<PromptHistoryState>({ entries: [] }),
|
|
)
|
|
const [shell, setShell] = persisted(
|
|
Persist.global("prompt-history-shell", ["prompt-history-shell.v1"]),
|
|
createStore<PromptHistoryState>({ entries: [] }),
|
|
)
|
|
return createPromptInputHistoryStore(normal, setNormal, shell, setShell)
|
|
}
|