mirror of
https://github.com/supabase/supabase.git
synced 2026-10-16 06:39:09 +03:00
6.4 KiB
6.4 KiB
Table View State Management (Filters, Sorts, etc.)
This document outlines how the state for the table view (specifically filters, sorts, column visibility, and column order) is managed across URL parameters, browser local storage, React hooks, and the Valtio state store.
Components of State Management
-
URL Parameters (
filter,sort,hidden_cols,col_order)- Role: Represents the active state for the current browser session/page load. This makes the specific view (filters, sorts applied) shareable via the URL and bookmarkable. It's the primary source of truth for the current session.
- Interaction:
- Read by the
useTableEditorFiltersSorthook on component render/navigation usingnext/routerandURLSearchParams. - Written to by
useTableEditorFiltersSort(via itssetParamsfunction) when state changes are intentionally applied (e.g., user clicks "Apply Filters").
- Read by the
-
Browser Local Storage (
supabase_grid_state_...)- Role: Persistent storage across browser sessions. Acts as a fallback or memory to restore the last used state when the user revisits the table view without specific parameters in the URL.
- Interaction:
- Written to by the
saveTableEditorStateToLocalStorageutility function, which is invoked by theuseSaveTableEditorStatehook. - Read by
loadTableEditorStateFromLocalStorageutility function. This is used by theuseLoadTableEditorStateFromLocalStorageIntoUrlhook to potentially populate URL parameters if they are missing on initial load.
- Written to by the
-
React Hooks (Composition)
- Role: Orchestrate the flow of state between the URL, UI interactions, local storage, and the Valtio store. They encapsulate and centralize the state management logic.
- Key Hooks:
useTableEditorFiltersSort: Reads/writes raw state strings to/from URL parameters. Provides thesetParamsfunction.useTableFilter,useTableSort,useTableColumnVisibility,useTableColumnOrder:- Consume
useTableEditorFiltersSortto get URL state and thesetParamsfunction. - Use utility functions (e.g.,
formatFilterURLParams) to convert URL strings into structured application state. - Provide callbacks (e.g.,
onApplyFilters,hideColumn) to the UI. - When callbacks are triggered by user interaction, they call
setParamsto update the URL and call functions fromuseSaveTableEditorStateto persist the state.
- Consume
useSaveTableEditorState:- Provides functions (e.g.,
saveFiltersAndTriggerSideEffects) called by the hooks above. - Reads necessary context from the Valtio snapshot (
snap.table.name,snap.gridColumns). - Calls
saveTableEditorStateToLocalStorageto write to local storage. - Modifies the Valtio store (e.g.,
snap.setPage(1)) to trigger side effects like pagination resets.
- Provides functions (e.g.,
useLoadTableEditorStateFromLocalStorageIntoUrl: Handles the initial load logic, reading from local storage and potentially updating URL parameters if they are empty.
-
Valtio Store (
store) / Snapshots (snap)- Role: Manages other UI state related to the grid, complementary to the filter/sort/column definitions stored via URL/localStorage.
- Managed State Examples: Current page number (
snap.page), rows per page (tableEditorSnap.rowsPerPage), table metadata (snap.table.name), data loading states, cell selection/editing state. - Interaction:
- Read by hooks (e.g.,
useSaveTableEditorStateneedssnap.table.namefor the local storage key). - Read by components for displaying UI elements (e.g., pagination controls use
snap.page). - Read by data fetching hooks (
useTableRowsQuery) to combine filter/sort state (from URL hooks) with pagination state (from Valtio). - Written to by hooks (
useSaveTableEditorState) to trigger side effects (e.g.,snap.setPage(1)when filters change).
- Read by hooks (e.g.,
Interaction Flows
Flow: Applying a Filter
- User interacts with
FilterPopover.tsxUI and clicks "Apply". FilterPopover.tsxcalls theonApplyFilterscallback obtained fromuseTableFilter().- Inside
onApplyFilters(defined inuseTableFilter): a. The applied filter state is formatted into a URL string array (filtersToUrlParams). b.setParams(fromuseTableEditorFiltersSort) is called to update thefilterquery parameter in the URL. c.saveFiltersAndTriggerSideEffects(fromuseSaveTableEditorState) is called with the same URL string array. - Inside
saveFiltersAndTriggerSideEffects(defined inuseSaveTableEditorState): a. It reads the table name/schema from the Valtio snapshot (snap). b. It callssaveTableEditorStateToLocalStorageto persist the filter strings under the correct key. c. It modifies the Valtio store (snap.setPage(1)) to reset pagination. - The URL change and Valtio state change trigger re-renders.
SupabaseGrid.tsxre-renders: a.useTableFilterre-runs, reads the new URL param viauseTableEditorFiltersSort, formats it, and returns the updatedfiltersstate. b.useTableRowsQueryreceives the newfiltersstate (and the reset page number fromsnap.page) and re-fetches data. c. UI components (Header,Grid) receive the newfiltersstate and updatedrowsand re-render to reflect the changes.
Flow: Initial Page Load (No URL Params)
- User navigates to the table page URL without specific
filterorsortparams. SupabaseGrid.tsxmounts.- The
useLoadTableEditorStateFromLocalStorageIntoUrlhook runs. a. It checkswindow.location.searchfor existingfilter/sortparams. None are found. b. It callsloadTableEditorStateFromLocalStorageto read any previously saved state (filters, sorts) for this table. c. If saved state is found, it callssetParams(fromuseUrlState) to update the URL query parameters with the loaded filters/sorts. - The URL update triggers a re-render.
- On the re-render,
useTableFilter/useTableSort(viauseTableEditorFiltersSort) read the newly added parameters from the URL. - The rest of the flow proceeds like step 6 in "Applying a Filter", using the state restored from local storage via the URL.
This separation ensures that the URL accurately reflects the shareable state, local storage provides persistence, Valtio handles other UI state, and hooks manage the coordination between them all.