mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 4 of a 6-PR stack migrating all `next/router` and `next/compat/router` imports in `apps/studio` onto TanStack Router hooks and removing the router shims. | Part | PR | Scope | | --- | --- | --- | | 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`, `data/prefetchers/**` | | 2 | #54054 | `components/ui/**` | | 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` | | **4** | **#54056 ← this PR** | **Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor** | | 5 | #54057 | Remaining `components/interfaces/**` and `QueryInsightsTable` router events | | 6 | #54058 | Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks | Migrates the database, branching, compute, functions, explorer, SQL editor, storage, and table editor interfaces off `next/router` and `next/compat/router`. **Changed:** - Migrated the sole `next/compat/router` consumer (`components/interfaces/Database/Replication/Destinations.tsx`) to `useNavigate` - Replaced `useRouter` across `BranchManagement`, `Branching`, `Compute`, `Database` (`Backups`, `Functions`, `Replication`, `Schemas`, `Tables`, `Triggers`), `EdgeFunctions`, `Explorer`, `Functions`, `SQLEditor`, `Storage` (`AnalyticsBuckets`, `FilesBuckets`, `VectorBuckets`, `StorageMenuV2`, `Storage.utils`), and `TableGridEditor` with TanStack Router hooks (`useNavigate`, `useLocation`, `useRoutePattern`) and `useParams` from `common` - Updated unit and component tests across `Database/Replication`, `Explorer`, `Functions`, and `Storage/FilesBuckets/DeleteBucketModal.test.tsx` to use TanStack Router **Simplified:** - `useSnippetEditor` and `useRedirectLegacyReadReplicaDestination` read search values from `useParams` instead of casting `location.search` - `resolveSourceSwitch` returns `replace: boolean` instead of a `push`/`replace` method name - `ExplorerQueryTab` and `EdgeFunctionDetails` guard on a defined ref instead of navigating with `ref ?? ''` - `useFunctionsDetailShortcuts` and `StorageMenuV2` use a local `goTo` helper for their navigation shortcuts - Tests use the shared `createTestRouter` and `customRender` router, use `toMatchObject` for search assertions, and drop duplicate assertions Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] SQL Editor: creating, renaming, moving, opening templates/examples, and keyboard shortcuts navigate between snippets cleanly - [ ] Table Editor & Database: opening tables, schema table nodes, triggers, functions, backups/PITR, and replication destination flows navigate as expected - [ ] Storage: switching between Files, Analytics, and Vector buckets and deleting a bucket navigates cleanly - [ ] Edge Functions & Explorer: opening function details/logs, explorer chat/query tabs, and branch management actions navigate properly --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
82 lines
3.6 KiB
TypeScript
82 lines
3.6 KiB
TypeScript
import { useNavigate } from '@tanstack/react-router'
|
|
import { useParams } from 'common'
|
|
import { useEffect, useEffectEvent } from 'react'
|
|
|
|
import { parseSqlSnippetSource } from './querySource'
|
|
import { createSqlSnippetSkeletonV2 } from './SQLEditor.utils'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { navigateToUrl } from '@/lib/navigation'
|
|
import { useProfile } from '@/lib/profile'
|
|
import { wasNeverPersisted } from '@/state/sql-editor/sql-editor-lifecycle'
|
|
import { canEditSnippet } from '@/state/sql-editor/sql-editor-rules'
|
|
import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor/sql-editor-state'
|
|
import { useTabsStateSnapshot } from '@/state/tabs'
|
|
|
|
/**
|
|
* Owns the editing lifecycle of a single SQL snippet: creating the snippet in
|
|
* the store on first edit (and routing to its URL), writing changes back to the
|
|
* store via `setSql`, and seeding the editor from a `?content=` deep link.
|
|
*
|
|
* Edits are written to the store synchronously on every change; debouncing the
|
|
* actual persistence is the save mechanism's job (see `createSaveMechanism`), so
|
|
* the store — and therefore the snippet's dirty status — always reflects the
|
|
* latest edit immediately.
|
|
*
|
|
* Extracted from `MonacoEditor` so that component stays a thin editor shell.
|
|
*/
|
|
export function useSnippetEditor({ id, snippetName }: { id: string; snippetName: string }) {
|
|
const navigate = useNavigate()
|
|
const { profile } = useProfile()
|
|
const { ref, content, source } = useParams()
|
|
const { data: project } = useSelectedProjectQuery()
|
|
|
|
const snapV2 = useSqlEditorV2StateSnapshot()
|
|
const tabsSnap = useTabsStateSnapshot()
|
|
|
|
const snippet = snapV2.snippets[id]
|
|
const disableEdit = !!snippet && !canEditSnippet(snippet.snippet, profile?.id)
|
|
|
|
function handleEditorChange(value: string | undefined) {
|
|
tabsSnap.makeActiveTabPermanent()
|
|
if (!id || !value) return
|
|
|
|
if (!snippet && ref && profile !== undefined && project !== undefined) {
|
|
const newSnippet = createSqlSnippetSkeletonV2({
|
|
idOverride: id,
|
|
name: snippetName,
|
|
sql: value,
|
|
owner_id: profile?.id,
|
|
project_id: project?.id,
|
|
// A creation entry (e.g. "New logs query") threads the target backend
|
|
// through `/sql/new?source=`; default to a database snippet otherwise.
|
|
source: parseSqlSnippetSource(source),
|
|
})
|
|
snapV2.addSnippet({ projectRef: ref, snippet: newSnippet })
|
|
// When the editor was seeded from a `content` deep-link, replace rather
|
|
// than push. The caller navigated to `/sql/new?content=...` (a long,
|
|
// one-shot URL); replacing collapses it out of history so Back returns to
|
|
// the originating page instead of a wasted step that re-seeds the snippet.
|
|
void navigateToUrl(navigate, `/project/${ref}/sql/${newSnippet.id}`, {
|
|
replace: content !== undefined,
|
|
})
|
|
}
|
|
|
|
// A snippet that has never been persisted needs the snippet/folder lists
|
|
// invalidated so it shows up in the sidebar. If `snippet` was undefined at
|
|
// render we just created one above (status 'new'), which always qualifies.
|
|
const shouldInvalidate = snippet ? wasNeverPersisted(snippet.snippet.status) : true
|
|
snapV2.setSql({ id, sql: value, shouldInvalidate })
|
|
}
|
|
|
|
// if an SQL query is passed by the content parameter, set the editor value to its content. This
|
|
// is usually used for sending the user to SQL editor from other pages with SQL.
|
|
const seedFromContentParam = useEffectEvent(() => {
|
|
if (content && content.length > 0) handleEditorChange(content)
|
|
})
|
|
useEffect(() => {
|
|
seedFromContentParam()
|
|
}, [])
|
|
|
|
return { snippet, disableEdit, handleEditorChange }
|
|
}
|