mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03:00
Audited all proxy()/useSnapshot() usage against the v1→v2 migration guide; no breaking changes apply (no reused proxy() inputs, no promise-valued state, all consumers already client components). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Updated the Valtio dependency to a newer version for improved compatibility. * **Bug Fixes** * Improved AI assistant persistence in IndexedDB so chat sessions reliably save (while keeping only the most recent 20 messages per chat). * Hardened tabs restoration from storage to fall back to fresh defaults when data is missing, invalid, or fails validation. * **Refactor** * Switched multiple studio panels to use fresh initial-state factories for initialization and reset reliability. * Updated advisor state so the derived notification filter count is no longer exposed. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
import { safeSql, type DisplayableSqlFragment } from '@supabase/pg-meta'
|
|
import { proxy, snapshot, useSnapshot } from 'valtio'
|
|
|
|
type Template = {
|
|
name: string
|
|
description: string
|
|
content: string
|
|
}
|
|
|
|
export type SqlError = {
|
|
error?: string
|
|
formattedError?: string
|
|
message?: string
|
|
}
|
|
|
|
type EditorPanelState = {
|
|
value: DisplayableSqlFragment
|
|
templates: Template[]
|
|
results: Record<string, unknown>[] | undefined
|
|
error: SqlError | undefined
|
|
initialPrompt: string
|
|
onChange: ((value: DisplayableSqlFragment) => void) | undefined
|
|
activeSnippetId: string | null
|
|
pendingReset: boolean
|
|
}
|
|
|
|
const createInitialState = (): EditorPanelState => ({
|
|
value: safeSql``,
|
|
templates: [],
|
|
results: undefined,
|
|
error: undefined,
|
|
initialPrompt: '',
|
|
onChange: undefined,
|
|
activeSnippetId: null,
|
|
pendingReset: false,
|
|
})
|
|
|
|
export const editorPanelState = proxy({
|
|
...createInitialState(),
|
|
setValue(value: DisplayableSqlFragment) {
|
|
editorPanelState.value = value
|
|
editorPanelState.onChange?.(value)
|
|
editorPanelState.setResults(undefined)
|
|
editorPanelState.setError(undefined)
|
|
},
|
|
setTemplates(templates: Template[]) {
|
|
editorPanelState.templates = templates
|
|
},
|
|
setResults(results: Record<string, unknown>[] | undefined) {
|
|
editorPanelState.results = results
|
|
},
|
|
setError(error: SqlError | undefined) {
|
|
editorPanelState.error = error
|
|
},
|
|
setInitialPrompt(initialPrompt: string) {
|
|
editorPanelState.initialPrompt = initialPrompt
|
|
},
|
|
setActiveSnippetId(id: string | null) {
|
|
editorPanelState.activeSnippetId = id
|
|
},
|
|
openAsNew() {
|
|
editorPanelState.value = safeSql``
|
|
editorPanelState.results = undefined
|
|
editorPanelState.error = undefined
|
|
editorPanelState.pendingReset = true
|
|
},
|
|
reset() {
|
|
Object.assign(editorPanelState, createInitialState())
|
|
},
|
|
})
|
|
|
|
export const getEditorPanelStateSnapshot = () => snapshot(editorPanelState)
|
|
|
|
export const useEditorPanelStateSnapshot = (options?: Parameters<typeof useSnapshot>[1]) =>
|
|
useSnapshot(editorPanelState, options)
|