useDashboardHistory.ts 868 B

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