MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / useInputHistory

Function useInputHistory

apps/cli/src/ui/hooks/useInputHistory.ts:22–127  ·  view source on GitHub ↗
(options: UseInputHistoryOptions = {})

Source from the content-addressed store, hash-verified

20}
21
22export function useInputHistory(options: UseInputHistoryOptions = {}): UseInputHistoryReturn {
23 const { isActive = true, getCurrentInput } = options
24
25 // All history entries (oldest first, newest at end)
26 const [history, setHistory] = useState<string[]>([])
27
28 // Current position in history (-1 = not browsing, 0 = oldest, history.length-1 = newest)
29 const [historyIndex, setHistoryIndex] = useState(-1)
30
31 // The user's typed text before they started navigating history
32 const [draft, setDraft] = useState("")
33
34 // Flag to track if history has been loaded
35 const historyLoaded = useRef(false)
36
37 // Load history on mount
38 useEffect(() => {
39 if (!historyLoaded.current) {
40 historyLoaded.current = true
41 loadHistory()
42 .then(setHistory)
43 .catch(() => {
44 // Ignore load errors - history is not critical
45 })
46 }
47 }, [])
48
49 // Navigate to older history entry
50 const navigateUp = useCallback(() => {
51 if (!isActive) return
52 if (history.length === 0) return
53
54 if (historyIndex === -1) {
55 // Starting to browse - save current input as draft
56 if (getCurrentInput) {
57 setDraft(getCurrentInput())
58 }
59 // Go to newest entry
60 setHistoryIndex(history.length - 1)
61 } else if (historyIndex > 0) {
62 // Go to older entry
63 setHistoryIndex(historyIndex - 1)
64 }
65 // At oldest entry - stay there
66 }, [isActive, history, historyIndex, getCurrentInput])
67
68 // Navigate to newer history entry
69 const navigateDown = useCallback(() => {
70 if (!isActive) return
71 if (historyIndex === -1) return // Not browsing
72
73 if (historyIndex < history.length - 1) {
74 // Go to newer entry
75 setHistoryIndex(historyIndex + 1)
76 } else {
77 // At newest entry - return to draft
78 setHistoryIndex(-1)
79 }

Callers 1

AutocompleteInputInnerFunction · 0.85

Calls 2

loadHistoryFunction · 0.85
addToHistoryFunction · 0.85

Tested by

no test coverage detected