mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 19:05:06 +03:00
## 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`.
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import { useCallback } from 'react'
|
|
|
|
import { getEditorSql } from './SQLEditor.utils'
|
|
import { useSQLEditorContext } from './SQLEditorContext'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { formatSql } from '@/lib/formatSql'
|
|
import {
|
|
getSqlEditorV2StateSnapshot,
|
|
useSqlEditorV2StateSnapshot,
|
|
} from '@/state/sql-editor/sql-editor-state'
|
|
|
|
/**
|
|
* Formats the current editor SQL in place (respecting a selection) and writes
|
|
* the formatted SQL back to the snippet store. No-op while a diff is open.
|
|
*/
|
|
export function usePrettifyQuery({ id, isDiffOpen }: { id: string; isDiffOpen: boolean }) {
|
|
const { editorRef } = useSQLEditorContext()
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const snapV2 = useSqlEditorV2StateSnapshot()
|
|
|
|
return useCallback(async () => {
|
|
if (isDiffOpen) return
|
|
|
|
// use the latest state
|
|
const state = getSqlEditorV2StateSnapshot()
|
|
const snippet = state.snippets[id]
|
|
|
|
if (editorRef.current && project) {
|
|
const editor = editorRef.current
|
|
const sql = getEditorSql(editor, snippet?.snippet.content?.unchecked_sql)
|
|
const formattedSql = formatSql(sql)
|
|
|
|
const editorModel = editorRef?.current?.getModel()
|
|
if (editorRef.current && editorModel) {
|
|
editorRef.current.executeEdits('apply-prettify-edit', [
|
|
{
|
|
text: formattedSql,
|
|
range: editorModel.getFullModelRange(),
|
|
},
|
|
])
|
|
snapV2.setSql({ id, sql: formattedSql })
|
|
}
|
|
}
|
|
}, [editorRef, id, isDiffOpen, project, snapV2])
|
|
}
|