Files
supabase/apps/studio/components/interfaces/SQLEditor/useSnippetIdentity.ts
Charis c9a649cf73 refactor(sql-editor): extract snippet-identity/mount/prettify/title hooks (decompose 3b/6) (#47893)
## Summary

Continues the SQLEditor decomposition. Pulls four cohesive concerns out
of the `SQLEditorContent` composition root into co-located `use*` hooks.

## New hooks

- `useSnippetIdentity` — derives `id` / `generatedNewSnippetName` /
`isLoading` from the URL + snippet store (keeps the `[urlId]` memo dep
verbatim).
- `useEditorMount` — the editor `onMount` (scroll restore/track) + the
mount counter that lets a pre-mount diff request re-run.
- `usePrettifyQuery` — formats the editor SQL in place and writes it
back to the store.
- `useSnippetTitleGenerator` — the title-generation mutation +
`setAiTitle`.
2026-07-13 15:35:30 -04:00

36 lines
1.4 KiB
TypeScript

import { useParams } from 'common'
import { useMemo } from 'react'
import { generateSnippetTitle } from './SQLEditor.constants'
import { generateUuid } from '@/lib/api/snippets.browser'
import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor/sql-editor-state'
/**
* Derives the stable snippet id + display name for the editor from the URL.
*
* For `/sql/new` (or no id) a fresh name + uuid are generated; otherwise the URL
* id is used. `isLoading` is true while an existing snippet's content is still
* being fetched.
*/
export function useSnippetIdentity() {
const { id: urlId } = useParams()
const snapV2 = useSqlEditorV2StateSnapshot()
// generate a new snippet title and an id to be used for new snippets. The dependency on urlId is to avoid a bug which
// shows up when clicking on the SQL Editor while being in the SQL editor on a random snippet.
const [generatedNewSnippetName, generatedId] = useMemo(() => {
const name = generateSnippetTitle()
return [name, generateUuid([`${name}.sql`])]
}, [urlId])
// the id is stable across renders - it depends either on the url or on the memoized generated id
const id = !urlId || urlId === 'new' ? generatedId : urlId
const snippetIsLoading = !(
id in snapV2.snippets && snapV2.snippets[id].snippet.content !== undefined
)
const isLoading = urlId === 'new' ? false : snippetIsLoading
return { id, urlId, generatedNewSnippetName, isLoading }
}