Implementation:TobikoData Sqlmesh Editor Context
| Knowledge Sources | |
|---|---|
| Domains | Web_UI, Code_Editor, State_Management |
| Last Updated | 2026-02-07 20:00 GMT |
Overview
State management store for the SQLMesh code editor, managing open tabs, SQL dialects, query previews, and editor layouts.
Description
The Editor_Context module implements a Zustand store for the SQL editor functionality in the SQLMesh web UI. It manages editor tabs (open files), SQL dialects (engine-specific syntax), query previews (rendered SQL and results), diff views (comparing changes), and editor layouts (vertical/horizontal splits). The store integrates with Web Workers for SQL validation and provides tab persistence across browser sessions using localStorage.
Usage
Use this context to manage the state of the SQL editor component, including opening/closing files, switching between tabs, previewing query results, and managing dialect-specific SQL syntax highlighting.
Code Reference
Source Location
- Repository: TobikoData_Sqlmesh
- File: web/client/src/context/editor.ts
Signature
interface EditorStore {
storedTabId?: ID
storedTabs: StoredTab[]
tabs: Map<ModelFile, EditorTab>
tab?: EditorTab
engine: Worker
dialects: Dialect[]
previewQuery?: string
previewTable?: [TableColumn[], TableRow[]]
previewDiff?: any
direction: 'vertical' | 'horizontal'
setDirection: (direction: 'vertical' | 'horizontal') => void
selectTab: (tab?: EditorTab) => void
replaceTab: (from: EditorTab, to: EditorTab) => void
updateStoredTabsIds: () => void
inTabs: (file: ModelFile) => boolean
addTab: (tab: EditorTab) => void
addTabs: (tabs: EditorTab[]) => void
closeTab: (file: ModelFile) => void
createTab: (file?: ModelFile) => EditorTab
setDialects: (dialects: Dialect[]) => void
refreshTab: (tab?: EditorTab) => void
setPreviewQuery: (previewQuery?: string) => void
setPreviewTable: (previewTable?: [TableColumn[], TableRow[]]) => void
setPreviewDiff: (previewDiff?: any) => void
}
export interface EditorTab {
id: string
file: ModelFile
isValid: boolean
dialect?: string
dialectOptions?: {
keywords: string
types: string
}
preview?: EditorPreview
el?: HTMLElement
}
export const useStoreEditor = create<EditorStore>()
Import
import { useStoreEditor } from '~/context/editor'
I/O Contract
Key State Properties
| Name | Type | Description |
|---|---|---|
| tabs | Map<ModelFile, EditorTab> | All open editor tabs indexed by file |
| tab | EditorTab | Currently active/selected tab |
| dialects | Dialect[] | Available SQL dialects (engines) |
| previewQuery | string | Rendered SQL query for preview |
| previewTable | [TableColumn[], TableRow[]] | Query execution results |
| previewDiff | any | Model changes comparison view |
| direction | 'vertical' or 'horizontal' | Editor split pane orientation |
| engine | Worker | Web Worker for SQL validation (sqlglot) |
Key Methods
| Name | Parameters | Description |
|---|---|---|
| addTab | EditorTab | Open a new file in a tab (inserts after current tab) |
| closeTab | ModelFile | Close tab and clean up file changes |
| selectTab | EditorTab | Switch to a different tab |
| replaceTab | from, to | Replace tab (e.g., when saving untitled file) |
| setDialects | Dialect[] | Update available SQL dialects |
| setPreviewQuery | string | Set rendered SQL for preview |
| setPreviewTable | [columns, rows] | Set query execution results |
| setDirection | 'vertical' or 'horizontal' | Change editor layout orientation |
Implementation Details
Tab Management
- Tab ordering: New tabs inserted after current tab position
- Tab closure: Automatically selects previous tab when closing
- Single tab behavior: Unselects tab when last tab is closed
- File association: Each tab is backed by a ModelFile instance
Tab Persistence
Tabs are persisted to localStorage with the following rules:
- Local unchanged files: Not persisted (default scratch pad)
- Remote files: Always persisted
- Changed files: Persisted with content for recovery
- Active tab: Tab ID stored to restore on reload
SQL Dialect Support
- Dialect metadata: Keywords and types for syntax highlighting
- Validation: Real-time SQL validation via Web Worker
- Engine integration: Supports 16+ SQL engines through sqlglot
Preview System
Three types of previews:
- Query preview: Rendered SQL with substituted variables
- Table preview: Query execution results as data grid
- Diff preview: Model changes comparison
Usage Examples
import { useStoreEditor, createLocalFile } from '~/context/editor'
function Editor() {
const {
tabs,
tab,
addTab,
closeTab,
selectTab,
setPreviewQuery,
direction,
setDirection
} = useStoreEditor()
// Create and open new tab
const newFile = createLocalFile()
const newTab = createTab(newFile)
addTab(newTab)
// Switch to tab
selectTab(newTab)
// Close tab
closeTab(newFile)
// Set query preview
setPreviewQuery('SELECT * FROM my_model')
// Toggle layout
setDirection(direction === 'vertical' ? 'horizontal' : 'vertical')
return <div>Active: {tab?.file.name}</div>
}