Files
supabase/apps/studio/components/interfaces/SQLEditor/useSnippetTitleGenerator.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

33 lines
1.2 KiB
TypeScript

import { useCallback } from 'react'
import { useSqlTitleGenerateMutation } from '@/data/ai/sql-title-mutation'
import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor/sql-editor-state'
import { createTabId, useTabsStateSnapshot } from '@/state/tabs'
/**
* Exposes the SQL-title generation mutation plus a fire-and-forget helper that
* names an untitled snippet from its SQL (updating the store + open tab label).
*/
export function useSnippetTitleGenerator() {
const { mutateAsync: generateSqlTitle } = useSqlTitleGenerateMutation()
const snapV2 = useSqlEditorV2StateSnapshot()
const tabs = useTabsStateSnapshot()
const setAiTitle = useCallback(
async (id: string, sql: string) => {
try {
const { title: name } = await generateSqlTitle({ sql })
snapV2.updateSnippet({ id, snippet: { name } })
snapV2.addNeedsSaving(id)
const tabId = createTabId('sql', { id })
tabs.updateTab(tabId, { label: name })
} catch (error) {
// [Joshen] No error handler required as this happens in the background and not necessary to ping the user
}
},
[generateSqlTitle, snapV2]
)
return { generateSqlTitle, setAiTitle }
}