Files
supabase/apps/studio/hooks/misc/useDashboardHistory.ts
Joshen LimandJordi Enric deda982074 Chore/refactor dashboard history logic (#38586)
* Shift dashboard history logic outside of app state, into its own hook

* Self remediate malformed history for sql editor

* Update apps/studio/hooks/misc/useDashboardHistory.ts

Co-authored-by: Jordi Enric <37541088+jordienr@users.noreply.github.com>

* Update apps/studio/hooks/misc/useDashboardHistory.ts

Co-authored-by: Jordi Enric <37541088+jordienr@users.noreply.github.com>

---------

Co-authored-by: Jordi Enric <37541088+jordienr@users.noreply.github.com>
2025-09-10 22:19:08 +08:00

31 lines
867 B
TypeScript

import { LOCAL_STORAGE_KEYS, useParams } from 'common'
import { useLocalStorageQuery } from './useLocalStorage'
type DashboardHistory = { editor?: string; sql?: string }
const DEFAULT_HISTORY = { editor: undefined, sql: undefined }
export const useDashboardHistory = () => {
// [Joshen] History should always refer to the project that the user is currently on
const { ref } = useParams()
const [history, setHistory, { isSuccess }] = useLocalStorageQuery<DashboardHistory>(
LOCAL_STORAGE_KEYS.DASHBOARD_HISTORY(ref ?? ''),
DEFAULT_HISTORY
)
const setLastVisitedTable = (id?: string) => {
setHistory({ ...history, editor: id })
}
const setLastVisitedSnippet = (id?: string) => {
setHistory({ ...history, sql: id })
}
return {
history,
setLastVisitedTable,
setLastVisitedSnippet,
isHistoryLoaded: isSuccess,
}
}