useDashboardHistory.ts31 lines · main
| 1 | import { LOCAL_STORAGE_KEYS, useParams } from 'common' |
| 2 | |
| 3 | import { useLocalStorageQuery } from './useLocalStorage' |
| 4 | |
| 5 | type DashboardHistory = { editor?: string; sql?: string } |
| 6 | const DEFAULT_HISTORY = { editor: undefined, sql: undefined } |
| 7 | |
| 8 | export const useDashboardHistory = () => { |
| 9 | // [Joshen] History should always refer to the project that the user is currently on |
| 10 | const { ref } = useParams() |
| 11 | |
| 12 | const [history, setHistory, { isSuccess }] = useLocalStorageQuery<DashboardHistory>( |
| 13 | LOCAL_STORAGE_KEYS.DASHBOARD_HISTORY(ref ?? ''), |
| 14 | DEFAULT_HISTORY |
| 15 | ) |
| 16 | |
| 17 | const setLastVisitedTable = (id?: string) => { |
| 18 | setHistory({ ...history, editor: id }) |
| 19 | } |
| 20 | |
| 21 | const setLastVisitedSnippet = (id?: string) => { |
| 22 | setHistory({ ...history, sql: id }) |
| 23 | } |
| 24 | |
| 25 | return { |
| 26 | history, |
| 27 | setLastVisitedTable, |
| 28 | setLastVisitedSnippet, |
| 29 | isHistoryLoaded: isSuccess, |
| 30 | } |
| 31 | } |