mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 20:35:07 +03:00
## Context Improves the tab behaviour for explorer to follow the SQL Editor - Tabs now start as preview tabs and become permanent once you start interacting with them - Query Tabs become permanent as soon as you start typing in the editor - Chat tabs become permanent as soon as you start typing in the chat input - Notebook tabs become permanenet as soon as you make any changes to the notebook - Notebooks with unsaved changes will show the orange dot indicator <img width="197" height="67" alt="image" src="https://github.com/user-attachments/assets/3005c379-a49a-4cd8-8d90-65406b186141" /> - Closing a notebook tab with unsaved changes will show a confirmation dialog - Except if the new notebook has no content (no changes) <img width="375" height="218" alt="image" src="https://github.com/user-attachments/assets/6ad10779-6415-4c55-bb4f-61d938e744c9" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Explorer chat, query, and notebook tabs now begin as previews and become permanent when edited or saved. * Added unsaved-change indicators and close confirmation for edited notebook tabs. * Confirmed closure of edited notebooks now discards unsaved changes. * **Bug Fixes** * Improved restoration and persistence of Explorer drafts. * Notebook saves now reflect the latest edits and tab state. * Prevented stale save responses from incorrectly marking newer edits as saved. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
124 lines
4.0 KiB
TypeScript
124 lines
4.0 KiB
TypeScript
import { useParams } from 'common'
|
|
import { Loader2, SquareCode } from 'lucide-react'
|
|
import { useRouter } from 'next/router'
|
|
import { useCallback, useContext, useEffect, useState } from 'react'
|
|
import { Button } from 'ui'
|
|
|
|
import { QueryEditor, type ExplorerQueryModel } from './QueryEditor'
|
|
import { type QueryResult } from './types'
|
|
import { toQuerySourceBinding } from '@/data/query-sources/query-source-registry'
|
|
import { explorerQueryState, useExplorerQueryStateSnapshot } from '@/state/explorer-query'
|
|
import { useControlledRoleImpersonationState } from '@/state/role-impersonation-state'
|
|
import { createTabId, TabsStateContext } from '@/state/tabs'
|
|
|
|
/** Query-tab lifecycle adapter around the shared QueryEditor. */
|
|
export const ExplorerQueryTab = () => {
|
|
const { id, ref } = useParams()
|
|
const router = useRouter()
|
|
const tabs = useContext(TabsStateContext)
|
|
const querySnap = useExplorerQueryStateSnapshot()
|
|
|
|
const [restoredQueryKey, setRestoredQueryKey] = useState<string>()
|
|
const [showQuery, setShowQuery] = useState(true)
|
|
|
|
const stateDraft = id ? querySnap.drafts[id] : undefined
|
|
const draft = stateDraft?.projectRef === ref ? stateDraft : undefined
|
|
const result = draft && id ? querySnap.results[id] : undefined
|
|
const queryKey = id && ref ? `${ref}:${id}` : undefined
|
|
|
|
const roleImpersonationState = useControlledRoleImpersonationState(
|
|
draft?._tag === 'database' ? draft.role : undefined,
|
|
useCallback(
|
|
(role) => {
|
|
if (id) explorerQueryState.setRole({ id, role })
|
|
},
|
|
[id]
|
|
)
|
|
)
|
|
|
|
useEffect(() => {
|
|
if (!id || !ref) return
|
|
|
|
setShowQuery(true)
|
|
explorerQueryState.restoreDraft({ id, projectRef: ref })
|
|
setRestoredQueryKey(`${ref}:${id}`)
|
|
}, [id, ref])
|
|
|
|
if (!queryKey || restoredQueryKey !== queryKey) {
|
|
return (
|
|
<div
|
|
role="status"
|
|
aria-label="Loading query"
|
|
className="flex h-full items-center justify-center bg-surface-100"
|
|
>
|
|
<Loader2 className="animate-spin text-foreground-muted" size={18} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (!id || !draft) {
|
|
return (
|
|
<div className="flex h-full flex-col items-center justify-center gap-3 bg-surface-100">
|
|
<SquareCode className="text-foreground-muted" size={24} />
|
|
<div className="text-center">
|
|
<p className="text-sm font-medium">Query draft not found</p>
|
|
<p className="text-sm text-foreground-lighter">
|
|
This local draft may have been closed or cleared from this browser.
|
|
</p>
|
|
</div>
|
|
<Button onClick={() => router.push(`/project/${ref}/explorer`)}>Back to Explorer</Button>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const handleResultChange = (nextResult: QueryResult) => {
|
|
explorerQueryState.setResult({
|
|
id,
|
|
result: { ...nextResult, executedAt: Date.now() },
|
|
})
|
|
}
|
|
|
|
const persistTab = () => tabs.makeTabPermanent(createTabId('query', { id }))
|
|
|
|
const query: ExplorerQueryModel =
|
|
draft._tag === 'logs'
|
|
? { ...toQuerySourceBinding(draft), uncheckedSql: draft.uncheckedSql }
|
|
: {
|
|
...toQuerySourceBinding(draft),
|
|
uncheckedSql: draft.uncheckedSql,
|
|
rowLimit: draft.rowLimit,
|
|
}
|
|
|
|
return (
|
|
<QueryEditor
|
|
id={id}
|
|
variant="viewport"
|
|
title={draft.name}
|
|
query={query}
|
|
result={result}
|
|
showQuery={showQuery}
|
|
onShowQueryChange={setShowQuery}
|
|
roleImpersonationState={roleImpersonationState}
|
|
onTitleChange={(value) => {
|
|
persistTab()
|
|
const name = value.trim() || 'Untitled query'
|
|
explorerQueryState.updateDraft({ id, name })
|
|
tabs.updateTab(createTabId('query', { id }), { label: name })
|
|
}}
|
|
onSqlChange={(sql) => {
|
|
persistTab()
|
|
explorerQueryState.updateDraft({ id, sql })
|
|
}}
|
|
onSourceChange={(source) => {
|
|
persistTab()
|
|
explorerQueryState.updateDraft({ id, source })
|
|
}}
|
|
onRowLimitChange={(rowLimit) => {
|
|
persistTab()
|
|
explorerQueryState.updateDraft({ id, rowLimit })
|
|
}}
|
|
onResultChange={handleResultChange}
|
|
/>
|
|
)
|
|
}
|