Skip to content

Commit

Permalink
Extract useHistory hook
Browse files Browse the repository at this point in the history
  • Loading branch information
printfn committed Dec 6, 2024
1 parent 47d5ff1 commit ee85aac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
15 changes: 4 additions & 11 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type FormEvent, type KeyboardEvent, type ReactNode, useCallback, useEffect, useRef, useState } from 'react';
import { ThreeDotsScale } from 'react-svg-spinners';
import { fend } from './lib/fend';
import { useHistory } from './hooks/useHistory';

const examples = `
> 5'10" to cm
Expand Down Expand Up @@ -40,16 +41,10 @@ function NewTabLink({ children, href }: { children: ReactNode; href: string }) {
);
}

const initialHistory = JSON.parse(localStorage.getItem('fend_history') || '[]') as string[];

export default function App({ widget = false }: { widget?: boolean }) {
const [currentInput, setCurrentInput] = useState('');
const [output, setOutput] = useState<ReactNode>(widget ? <></> : exampleContent);
const [history, setHistory] = useState<string[]>(initialHistory);
useEffect(() => {
const history100 = history.slice(-100);
localStorage.setItem('fend_history', JSON.stringify(history100));
}, [history]);
const { history, addToHistory } = useHistory();
const [variables, setVariables] = useState('');
const [navigation, setNavigation] = useState(0);
const [hint, setHint] = useState('');
Expand Down Expand Up @@ -93,9 +88,7 @@ export default function App({ widget = false }: { widget?: boolean }) {
return;
}
const request = <p>{`> ${currentInput}`}</p>;
if (currentInput.trim().length > 0) {
setHistory(h => [...h, currentInput]);
}
addToHistory(currentInput);
setNavigation(0);
setPending(p => p + 1);
const fendResult = await fend(currentInput, 1000000000, variables);
Expand All @@ -119,7 +112,7 @@ export default function App({ widget = false }: { widget?: boolean }) {
inputHint.current?.scrollIntoView();
})();
},
[currentInput, variables],
[currentInput, addToHistory, variables],
);
const navigate = useCallback(
(event: KeyboardEvent<HTMLTextAreaElement>) => {
Expand Down
20 changes: 20 additions & 0 deletions web/src/hooks/useHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useCallback, useState } from 'react';

const initialHistory = JSON.parse(localStorage.getItem('fend_history') || '[]') as string[];

export function useHistory() {
const [history, setHistory] = useState<string[]>(initialHistory);

const addToHistory = useCallback((newEntry: string) => {
if (newEntry.startsWith(' ')) return;
if (newEntry.trim().length === 0) return;
setHistory(prevHistory => {
const updatedHistory = [...prevHistory, newEntry]
.filter((entry, idx, array) => idx === 0 || entry !== array[idx - 1]);
localStorage.setItem('fend_history', JSON.stringify(updatedHistory.slice(-100)));
return updatedHistory;
});
}, []);

return { history, addToHistory };
}

0 comments on commit ee85aac

Please sign in to comment.