mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 09:25:06 +03:00
## What PR 7 of the SQL-editor query-source (Database vs Logs) stack. Surfaces `log_sql` snippets as a distinct query source across the SQL editor sidebar. Stacked on **`charislam/toolbar-ui-creation-flow`** (PR 6 — toolbar UI + creation flow); review/merge that first. Nothing is user-visible until the flags roll out — every entry point requires **both** `sqlEditorLogsSource` **and** `otelLegacyLogs`. ## Changes - **Nav** — a flag-gated **Logs** section (`LogsSnippetsSection`) backed by its own single-type `log_sql` query. The active snippet is injected only into the section it belongs to, via a shared `withActiveSnippet(snippets, active, belongsPredicate)` helper (also DRYs the private/favorites/shared injections). - **Search** (`SearchList`) — a **Logs** result group with a shared, extracted `SqlSnippetTree`; the "N results found" count now sums database + logs, with loading/empty states covering both queries. - **Tabs** — an immutable `sqlSource` field on tab/recent-item metadata (set at tab creation, lazily backfilled once the snippet loads via `useEffectEvent`), and a distinct `ScrollText` icon via a shared `LogsSnippetIcon`. Tab cleanup treats `log_sql` tabs as live and only prunes them when logs data is authoritative (`canPruneLogsTabs`), so a disabled/erroring logs query never wrongly deletes logs tabs or blocks database-tab cleanup. - **Data layer** — `useSqlSnippetsQuery` gains an optional `type` param so logs reuse the same `SnippetWithContent` shape as the other sections (no casts). ## Tests - `state/tabs.test.ts` — `sqlSource` backfill + creation-time carry-through. - `components/layouts/Tabs/Tabs.utils.test.tsx` — cleanup prunes stale database/logs snippets, keeps live ones, and preserves logs tabs when logs data isn't authoritative. ## Verification - `pnpm --filter studio typecheck` ✓ - `pnpm --filter studio run lint:ratchet` ✓ - `pnpm test:studio` (affected suites) ✓ <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a collapsible Logs section to the SQL editor sidebar for browsing, sorting, selecting, renaming, and deleting log queries. * Expanded SQL search with separate, paginated results for database and log queries. * Added dedicated log-query icons across navigation, tabs, previews, and recent items. * **Bug Fixes** * Improved tab and recent-item cleanup while preserving active log queries and accurate source metadata. * **Tests** * Added coverage for log tab cleanup and SQL source metadata synchronization. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
98 lines
2.7 KiB
TypeScript
98 lines
2.7 KiB
TypeScript
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
|
|
|
|
import { Content } from './content-query'
|
|
import { remapSqlContentFields } from './content-remap'
|
|
import { contentKeys } from './keys'
|
|
import { SNIPPET_PAGE_LIMIT, withSavedStatus, type SnippetWithContent } from './sql-folders-query'
|
|
import { get } from '@/data/fetchers'
|
|
import type { UseCustomInfiniteQueryOptions } from '@/types'
|
|
|
|
export type SqlSnippet = Extract<Content, { type: 'sql' }>
|
|
|
|
/** The snippet content types this query can list. Defaults to `'sql'`; the nav's
|
|
* Logs section passes `'log_sql'` to list logs snippets through the same shape. */
|
|
type SqlSnippetType = 'sql' | 'log_sql'
|
|
|
|
interface GetSqlSnippetsVariables {
|
|
projectRef?: string
|
|
cursor?: string
|
|
type?: SqlSnippetType
|
|
visibility?: SqlSnippet['visibility']
|
|
favorite?: boolean
|
|
name?: string
|
|
sort?: 'name' | 'inserted_at'
|
|
}
|
|
|
|
export async function getSqlSnippets(
|
|
{ projectRef, cursor, type = 'sql', visibility, favorite, name, sort }: GetSqlSnippetsVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (typeof projectRef === 'undefined') {
|
|
throw new Error('projectRef is required for getSqlSnippets')
|
|
}
|
|
|
|
const sortOrder = sort === 'name' ? 'asc' : 'desc'
|
|
|
|
const { data, error } = await get('/platform/projects/{ref}/content', {
|
|
params: {
|
|
path: { ref: projectRef },
|
|
query: {
|
|
type,
|
|
cursor,
|
|
visibility,
|
|
favorite,
|
|
name,
|
|
limit: SNIPPET_PAGE_LIMIT.toString(),
|
|
sort_by: sort,
|
|
sort_order: sortOrder,
|
|
},
|
|
},
|
|
signal,
|
|
})
|
|
|
|
if (error) {
|
|
throw error
|
|
}
|
|
|
|
const snippets = remapSqlContentFields(data.data as unknown as SnippetWithContent[])
|
|
return {
|
|
cursor: data.cursor,
|
|
contents: snippets.map(withSavedStatus),
|
|
}
|
|
}
|
|
|
|
export type SqlSnippetsData = Awaited<ReturnType<typeof getSqlSnippets>>
|
|
export type SqlSnippetsError = unknown
|
|
|
|
export const useSqlSnippetsQuery = <TData = SqlSnippetsData>(
|
|
{
|
|
projectRef,
|
|
type = 'sql',
|
|
sort,
|
|
name,
|
|
visibility,
|
|
favorite,
|
|
}: Omit<GetSqlSnippetsVariables, 'cursor'>,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomInfiniteQueryOptions<
|
|
SqlSnippetsData,
|
|
SqlSnippetsError,
|
|
InfiniteData<TData>,
|
|
readonly unknown[],
|
|
string | undefined
|
|
> = {}
|
|
) =>
|
|
useInfiniteQuery({
|
|
queryKey: contentKeys.sqlSnippets(projectRef, { type, sort, name, visibility, favorite }),
|
|
queryFn: ({ signal, pageParam: cursor }) =>
|
|
getSqlSnippets({ projectRef, cursor, type, sort, name, visibility, favorite }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
initialPageParam: undefined,
|
|
getNextPageParam(lastPage) {
|
|
return lastPage.cursor
|
|
},
|
|
...options,
|
|
})
|