mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +08:00
## Context Fixes a small bug whereby running any query cell within a notebook will mark the notebook tab with the unsaved changes status indicator `handleSqlCommit` gets called when we run the query, and it flips the notebook's status to "unsaved" hence why its happening. Hence opting to skip committing the changes in `handleSqlCommit` if there's no change to the SQL content <img width="224" height="69" alt="image" src="https://github.com/user-attachments/assets/7015b527-b249-4f2e-bcf1-948b5fe3f5a9" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Prevented unnecessary notebook updates when committed SQL is unchanged. - Continued saving SQL changes as expected. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
131 lines
4.6 KiB
TypeScript
131 lines
4.6 KiB
TypeScript
import { forwardRef, useState } from 'react'
|
|
import { type Snapshot } from 'valtio'
|
|
|
|
import { AddCellDropdown } from '../AddCellDropdown'
|
|
import { MoveCellDropdownContent } from '../MoveCellDropdownContent'
|
|
import { QueryEditor, type QueryEditorHandle } from '../QueryEditor'
|
|
import { type QueryDisplay, type QueryResult } from '../types'
|
|
import {
|
|
changeCellSource,
|
|
cloneChartConfig,
|
|
cloneQueryCell,
|
|
getCellDisplay,
|
|
setCellRowLimit,
|
|
setCellSql,
|
|
toQueryModel,
|
|
} from './QueryCell.utils'
|
|
import { SortableSection } from '@/components/ui/SortableSection'
|
|
import {
|
|
isQueryCell,
|
|
type QueryCell as QueryCellSchema,
|
|
} from '@/data/content/notebooks/notebook-schema'
|
|
import { type QuerySourceBinding } from '@/data/query-sources/query-source-registry'
|
|
import { useCurrentNotebook, useNotebooksStateSnapshot } from '@/state/notebooks/notebooks-state'
|
|
import { useLocalRoleImpersonationState } from '@/state/role-impersonation-state'
|
|
|
|
interface QueryCellProps {
|
|
cell: Snapshot<QueryCellSchema>
|
|
onEdit?: () => void
|
|
}
|
|
|
|
/** Notebook adapter around the shared QueryEditor. */
|
|
export const QueryCell = forwardRef<QueryEditorHandle, QueryCellProps>(function QueryCell(
|
|
{ cell, onEdit },
|
|
ref
|
|
) {
|
|
const snap = useNotebooksStateSnapshot()
|
|
const currentNotebook = useCurrentNotebook()
|
|
|
|
const [sql, setSql] = useState<string>(cell.unchecked_sql)
|
|
const [result, setResult] = useState<QueryResult>()
|
|
const roleImpersonationState = useLocalRoleImpersonationState()
|
|
|
|
const title = cell.title ?? 'Untitled query'
|
|
const showQuery =
|
|
snap.cellLocalState.get(cell._id)?.showQuery ?? currentNotebook?.status === 'new'
|
|
|
|
/**
|
|
* Applies an update to this cell. The updater runs against the cell as the store holds
|
|
* it rather than the snapshot this component rendered with, so a concurrent edit isn't
|
|
* clobbered; `isQueryCell` keeps the per-backend helpers off a markdown cell that
|
|
* somehow shares the id.
|
|
*/
|
|
const updateQueryCell = (updater: (candidate: Snapshot<QueryCellSchema>) => QueryCellSchema) => {
|
|
const notebookId = currentNotebook?.notebook.id
|
|
if (!notebookId) return
|
|
|
|
onEdit?.()
|
|
snap.updateCell({
|
|
id: notebookId,
|
|
cellId: cell._id,
|
|
updater: (candidate) => {
|
|
if (!isQueryCell(candidate)) return candidate
|
|
return updater(candidate)
|
|
},
|
|
})
|
|
}
|
|
|
|
const handleSourceChange = (source: QuerySourceBinding) => {
|
|
// The query text carries over (see `changeCellSource`), so the editor's buffer stays
|
|
// valid — but a result the old backend produced does not, since another engine
|
|
// returns unrelated columns.
|
|
const isBackendChange = (source._tag === 'logs') !== (cell._tag === 'log_cell')
|
|
if (isBackendChange) setResult(undefined)
|
|
|
|
updateQueryCell((candidate) => changeCellSource(candidate, source))
|
|
}
|
|
|
|
const handleTitleChange = (value: string) => {
|
|
const nextTitle = value.trim()
|
|
if (!nextTitle) return
|
|
updateQueryCell((candidate) => ({ ...cloneQueryCell(candidate), title: nextTitle }))
|
|
}
|
|
|
|
// Running a cell re-commits its current SQL (see QueryEditor's handleRunQuery) even when
|
|
// nothing changed — skip the store write so that doesn't spuriously mark the notebook
|
|
// unsaved.
|
|
const handleSqlCommit = (value: string) => {
|
|
if (value === cell.unchecked_sql) return
|
|
updateQueryCell((candidate) => setCellSql(candidate, value))
|
|
}
|
|
|
|
const handleDisplayChange = (display: QueryDisplay) =>
|
|
updateQueryCell((candidate) => ({
|
|
...cloneQueryCell(candidate),
|
|
view: display.view,
|
|
chart: cloneChartConfig(display.chart),
|
|
}))
|
|
|
|
const handleRowLimitChange = (rowLimit: number) =>
|
|
updateQueryCell((candidate) => setCellRowLimit(candidate, rowLimit))
|
|
|
|
return (
|
|
<SortableSection
|
|
id={cell._id}
|
|
actions={<AddCellDropdown cellId={cell._id} />}
|
|
gripDropdownContent={<MoveCellDropdownContent cellId={cell._id} />}
|
|
gripClassName="mt-2 opacity-0 group-hover:opacity-100 has-[[data-state=open]]:opacity-100 transition"
|
|
>
|
|
<QueryEditor
|
|
ref={ref}
|
|
id={cell._id}
|
|
variant="embedded"
|
|
title={title}
|
|
query={toQueryModel(cell, sql)}
|
|
result={result}
|
|
showQuery={showQuery}
|
|
onShowQueryChange={(showQuery) => snap.setQueryVisibility({ cellId: cell._id, showQuery })}
|
|
roleImpersonationState={roleImpersonationState}
|
|
display={getCellDisplay(cell)}
|
|
onTitleChange={handleTitleChange}
|
|
onSqlChange={setSql}
|
|
onSqlCommit={handleSqlCommit}
|
|
onSourceChange={handleSourceChange}
|
|
onResultChange={setResult}
|
|
onRowLimitChange={handleRowLimitChange}
|
|
onDisplayChange={handleDisplayChange}
|
|
/>
|
|
</SortableSection>
|
|
)
|
|
})
|