Files
Joshen Lim 7f23394818 Persist connect sheet user preference (#46213)
## Context

As per PR title - persists the opened state of the connect sheet into
local storage so re-opening it again will have the same parameters
chosen

## To test

- [ ] Verify that the persisting of the opened state of the connect
sheet works

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Connect Sheet now saves user preferences so selections persist across
sessions.

* **Improvements**
* Better synchronization between the UI and the URL for more consistent
state when opening or sharing links.
  * Clearing selections reliably removes all relevant filters.

* **Refactor**
* Internal state handling simplified for more predictable mode and field
changes.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/46213?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-25 11:20:43 +07:00

59 lines
1.8 KiB
TypeScript

import { LOCAL_STORAGE_KEYS } from 'common'
import { parseAsString, useQueryStates } from 'nuqs'
import { useCallback } from 'react'
import { useLocalStorage } from '@/hooks/misc/useLocalStorage'
export type ConnectSheetPrefs = {
connectTab?: string
framework?: string
using?: string
method?: string
type?: string
mcpClient?: string
}
const DEFAULT_CONNECT_SHEET_PREFS: ConnectSheetPrefs = {}
type ConnectParamKey = keyof ConnectSheetPrefs
type ConnectParamUpdates = Partial<Record<ConnectParamKey, string | null>>
/**
* Owns the URL params + localStorage that drive the Connect sheet's initial
* state. `setConnectParams` mirrors writes to both. `setQueryParams` writes
* the URL only — used to clear URL on close and to hydrate URL from storage
* on open (where storage is already the source of truth).
*/
export function useConnectSheetParams() {
const [params, setQueryParams] = useQueryStates({
connectTab: parseAsString,
framework: parseAsString,
using: parseAsString,
method: parseAsString,
type: parseAsString,
mcpClient: parseAsString,
})
const [storedPrefs, setStoredPrefs] = useLocalStorage<ConnectSheetPrefs>(
LOCAL_STORAGE_KEYS.CONNECT_SHEET_PREFS,
DEFAULT_CONNECT_SHEET_PREFS
)
const setConnectParams = useCallback(
(updates: ConnectParamUpdates) => {
setQueryParams(updates)
setStoredPrefs((prev) => {
const next: ConnectSheetPrefs = { ...prev }
for (const key of Object.keys(updates) as ConnectParamKey[]) {
// null in URL maps to undefined in storage so the preference is truly forgotten
next[key] = updates[key] ?? undefined
}
return next
})
},
[setQueryParams, setStoredPrefs]
)
return { params, storedPrefs, setConnectParams, setQueryParams }
}