mirror of
https://github.com/supabase/supabase.git
synced 2026-10-07 02:15:05 +03:00
## I have read the [CONTRIBUTING.md](<https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md>) file. YES ## What kind of change does this PR introduce? Bug fix for Studio notebook previews. ## What is the current behavior? Notebook diffs display the raw database_identifier value. This exposes opaque database IDs and does not communicate whether the cell targets the primary database or a read replica. Related issue: [https://linear.app/supabase/issue/FE-4246](<https://linear.app/supabase/issue/FE-4246>) ## What is the new behavior? Notebook database metadata is resolved independently from existing selector formatting: * an omitted database identifier is Primary * an identifier matching the project ref is Primary * other identifiers show a loading state while databases load * a loaded non-primary match is Replica * an unmatched identifier is Unknown * database lookup failures use a neutral unavailable state Labels are compact: Database: Primary, Database: Replica, and Database: Unknown. The databases query is enabled only when a preview contains an explicit non-primary identifier. ## Additional context Validation: * 39 focused notebook preview tests * Studio typecheck * targeted ESLint * Prettier check * git diff --check <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Notebook previews now identify database targets as primary, read replica, or unknown. * Database metadata is resolved automatically when needed. * Loading states display a clear “Loading database…” indicator. * Database metadata now handles unavailable, hidden, and error states more clearly. * Notebook entries reflect updated database information before and after replacement. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
144 lines
5.1 KiB
TypeScript
144 lines
5.1 KiB
TypeScript
import { useParams } from 'common'
|
|
import { NotebookText } from 'lucide-react'
|
|
import { useMemo, useState } from 'react'
|
|
import { Button, cn } from 'ui'
|
|
|
|
import {
|
|
formatNotebookDiffSummary,
|
|
getEntryKey,
|
|
notebookEntriesNeedDatabaseLookup,
|
|
summarizeNotebookDiff,
|
|
type NotebookDatabaseContext,
|
|
} from './AssistantNotebookPreview.utils'
|
|
import { AssistantNotebookPreviewCell } from './AssistantNotebookPreviewCell'
|
|
import {
|
|
ExplorerToolbar,
|
|
ExplorerToolbarActions,
|
|
ExplorerToolbarIcon,
|
|
ExplorerToolbarTitle,
|
|
} from '@/components/interfaces/Explorer/ExplorerToolbar'
|
|
import type { QueryResult } from '@/components/interfaces/Explorer/types'
|
|
import type { NotebookCellDiffEntry } from '@/data/content/notebooks/notebook-operations'
|
|
import { useReadReplicasQuery } from '@/data/read-replicas/replicas-query'
|
|
|
|
export interface AssistantNotebookPreviewProps {
|
|
entries: NotebookCellDiffEntry[]
|
|
mode: 'create' | 'update' | 'run'
|
|
/** The notebook's name, shown in the toolbar. Falls back to a generic label. */
|
|
title?: string
|
|
/** Query results keyed by persisted cell id. */
|
|
results?: Record<string, QueryResult>
|
|
className?: string
|
|
}
|
|
|
|
const VISIBLE_ENTRY_LIMIT = 5
|
|
|
|
const FALLBACK_TITLE = {
|
|
create: 'New notebook',
|
|
update: 'Notebook changes',
|
|
run: 'Notebook run',
|
|
} as const
|
|
|
|
/**
|
|
* Read-only minified notebook for assistant create/update proposals and notebook runs.
|
|
* Composes the same Explorer toolbar and query-result surfaces as notebook tabs; the
|
|
* surrounding `Confirm` card owns the frame. Pure presentational: no data fetching,
|
|
* approval, or notebook-editor state — callers adapt those concerns into entries/results.
|
|
*/
|
|
export const AssistantNotebookPreview = ({
|
|
entries,
|
|
mode,
|
|
title,
|
|
results,
|
|
className,
|
|
}: AssistantNotebookPreviewProps) => {
|
|
const { ref: projectRef } = useParams()
|
|
const [isShowingAllEntries, setIsShowingAllEntries] = useState(false)
|
|
const [expandedOverrides, setExpandedOverrides] = useState<Record<string, boolean>>({})
|
|
|
|
const needsDatabaseLookup = notebookEntriesNeedDatabaseLookup(entries, projectRef)
|
|
const {
|
|
data: databases,
|
|
isPending: isLoadingDatabases,
|
|
isError: isDatabaseError,
|
|
} = useReadReplicasQuery({ projectRef }, { enabled: needsDatabaseLookup })
|
|
const databasesByIdentifier = useMemo(
|
|
() => new Map((databases ?? []).map((database) => [database.identifier, database])),
|
|
[databases]
|
|
)
|
|
const databaseContext: NotebookDatabaseContext = !needsDatabaseLookup
|
|
? { status: 'success', projectRef, databasesByIdentifier }
|
|
: isLoadingDatabases
|
|
? { status: 'loading', projectRef }
|
|
: isDatabaseError
|
|
? { status: 'error', projectRef }
|
|
: { status: 'success', projectRef, databasesByIdentifier }
|
|
|
|
const summary = summarizeNotebookDiff(entries, mode)
|
|
const visibleEntries = isShowingAllEntries ? entries : entries.slice(0, VISIBLE_ENTRY_LIMIT)
|
|
const hiddenCount = entries.length - visibleEntries.length
|
|
const hasResults = results !== undefined
|
|
|
|
const isExpanded = (entry: NotebookCellDiffEntry) =>
|
|
expandedOverrides[getEntryKey(entry)] === true
|
|
|
|
return (
|
|
<div className={cn('flex w-full min-w-0 max-w-6xl mx-auto flex-col', className)}>
|
|
<ExplorerToolbar aria-label="Notebook toolbar">
|
|
<ExplorerToolbarIcon>
|
|
<NotebookText />
|
|
</ExplorerToolbarIcon>
|
|
<ExplorerToolbarTitle>{title ?? FALLBACK_TITLE[mode]}</ExplorerToolbarTitle>
|
|
<ExplorerToolbarActions>
|
|
<span className="shrink-0 text-sm text-muted-foreground">
|
|
{formatNotebookDiffSummary(summary)}
|
|
</span>
|
|
</ExplorerToolbarActions>
|
|
</ExplorerToolbar>
|
|
<div className="p-2">
|
|
<div
|
|
className={cn(
|
|
hasResults ? 'flex flex-col gap-2' : 'overflow-hidden rounded-md border bg-surface-100'
|
|
)}
|
|
>
|
|
<div className={cn(hasResults ? 'contents' : 'divide-y divide-border')}>
|
|
{visibleEntries.map((entry) => {
|
|
const key = getEntryKey(entry)
|
|
return (
|
|
<div
|
|
key={key}
|
|
className={cn(hasResults && 'overflow-hidden rounded-md border bg-surface-100')}
|
|
>
|
|
<AssistantNotebookPreviewCell
|
|
entry={entry}
|
|
mode={mode}
|
|
result={results?.[key]}
|
|
isExpanded={isExpanded(entry)}
|
|
databaseContext={databaseContext}
|
|
onExpandedChange={(open) =>
|
|
setExpandedOverrides((prev) => ({ ...prev, [key]: open }))
|
|
}
|
|
/>
|
|
</div>
|
|
)
|
|
})}
|
|
</div>
|
|
{hiddenCount > 0 && (
|
|
<Button
|
|
variant="text"
|
|
size="tiny"
|
|
className={cn(
|
|
'w-full text-foreground-light',
|
|
!hasResults && 'rounded-none border-0 border-t border-default'
|
|
)}
|
|
onClick={() => setIsShowingAllEntries(true)}
|
|
>
|
|
Show {hiddenCount} more cell{hiddenCount === 1 ? '' : 's'}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|