| 2 | import { History } from './interface'; |
| 3 | |
| 4 | export const useHistory = (defaultValue: Array<History>) => { |
| 5 | const [history, setHistory] = React.useState<Array<History>>(defaultValue); |
| 6 | const [command, setCommand] = React.useState<string>(''); |
| 7 | const [lastCommandIndex, setLastCommandIndex] = React.useState<number>(0); |
| 8 | |
| 9 | return { |
| 10 | history, |
| 11 | command, |
| 12 | lastCommandIndex, |
| 13 | setHistory: (value: string) => |
| 14 | setHistory([ |
| 15 | ...history, |
| 16 | { |
| 17 | id: history.length, |
| 18 | date: new Date(), |
| 19 | command, |
| 20 | output: value, |
| 21 | }, |
| 22 | ]), |
| 23 | setCommand, |
| 24 | setLastCommandIndex, |
| 25 | clearHistory: () => setHistory([]), |
| 26 | }; |
| 27 | }; |