MCPcopy Create free account
hub / github.com/anus-dev/ANUS / useInputHistory

Function useInputHistory

packages/cli/src/ui/hooks/useInputHistory.ts:24–112  ·  view source on GitHub ↗
({
  userMessages,
  onSubmit,
  isActive,
  currentQuery,
  onChange,
}: UseInputHistoryProps)

Source from the content-addressed store, hash-verified

22}
23
24export function useInputHistory({
25 userMessages,
26 onSubmit,
27 isActive,
28 currentQuery,
29 onChange,
30}: UseInputHistoryProps): UseInputHistoryReturn {
31 const [historyIndex, setHistoryIndex] = useState<number>(-1);
32 const [originalQueryBeforeNav, setOriginalQueryBeforeNav] =
33 useState<string>('');
34
35 const resetHistoryNav = useCallback(() => {
36 setHistoryIndex(-1);
37 setOriginalQueryBeforeNav('');
38 }, []);
39
40 const handleSubmit = useCallback(
41 (value: string) => {
42 const trimmedValue = value.trim();
43 if (trimmedValue) {
44 onSubmit(trimmedValue); // Parent handles clearing the query
45 }
46 resetHistoryNav();
47 },
48 [onSubmit, resetHistoryNav],
49 );
50
51 const navigateUp = useCallback(() => {
52 if (!isActive) return false;
53 if (userMessages.length === 0) return false;
54
55 let nextIndex = historyIndex;
56 if (historyIndex === -1) {
57 // Store the current query from the parent before navigating
58 setOriginalQueryBeforeNav(currentQuery);
59 nextIndex = 0;
60 } else if (historyIndex < userMessages.length - 1) {
61 nextIndex = historyIndex + 1;
62 } else {
63 return false; // Already at the oldest message
64 }
65
66 if (nextIndex !== historyIndex) {
67 setHistoryIndex(nextIndex);
68 const newValue = userMessages[userMessages.length - 1 - nextIndex];
69 onChange(newValue);
70 return true;
71 }
72 return false;
73 }, [
74 historyIndex,
75 setHistoryIndex,
76 onChange,
77 userMessages,
78 isActive,
79 currentQuery, // Use currentQuery from props
80 setOriginalQueryBeforeNav,
81 ]);

Callers 2

InputPromptFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected