import React from 'react'; import { Box, IconButton, Tooltip, Typography } from '@mui/material'; import { CloseFullscreen, OpenInFull, VerticalSplit, } from '@mui/icons-material'; import SplitPane, { Pane } from 'split-pane-react'; import 'split-pane-react/esm/themes/default.css'; import { Editor } from '../editor'; import { ChartsPreviewPane, ChartsSetupState, useChartsMarkers, } from '../dbtCharts'; import { useTabs } from '../../context/TabsProvider'; import { useTaskManager } from '../../context/TaskManagerProvider'; import { useRefreshProjectFiles } from '../../hooks/useRefreshProjectFiles'; import { useGetSettings } from '../../controllers'; import { chartsErrorMessage, useChartsInstallLine, useChartsServer, useChartsValidation, useInstallDbtCharts, useProjectChartsState, useRunChartsDbtParse, } from '../../controllers/dbtCharts.controller'; import { subscribeToToolResult } from '../../services/agentEvents.service'; import { DBT_CHARTS_INSTALL_TASK, DBT_CHARTS_REFRESH_MUTATION, boardUrlPath, chartsRelativePath, folderUrlPath, isChartsBoardPath, isChartsPartial, looksLikeChartsPath, } from '../../../shared/dbtCharts'; import { codeFileView } from './CodeFileView'; import type { FileViewDefinition, FileViewProps } from './types'; type Layout = 'split' | 'editor' | 'preview'; const LAYOUT_KEY = 'dbt-studio:charts-layout'; const VALIDATE_DEBOUNCE_MS = 400; const readLayout = (): Layout => { try { const stored = window.localStorage.getItem(LAYOUT_KEY); return stored === 'editor' || stored === 'preview' ? stored : 'split'; } catch { return 'split'; } }; const writeLayout = (layout: Layout) => { try { window.localStorage.setItem(LAYOUT_KEY, layout); } catch { // Storage unavailable: the layout just isn't remembered. } }; const Sash = (_: number, active: boolean) => ( ); // The refresh tool result: { ok: true, mutation, relativePath }. const refreshedPath = (toolName: string, result: unknown): string | null => { if (toolName !== 'studio_charts_refresh_preview') return null; if (!result || typeof result !== 'object') return null; const value = result as Record; if (value.ok !== true || value.mutation !== DBT_CHARTS_REFRESH_MUTATION) { return null; } return typeof value.relativePath === 'string' ? value.relativePath : null; }; const ValidationStatus: React.FC<{ checking: boolean; errorCount: number; warningCount: number; failed?: boolean; visible: boolean; }> = ({ checking, errorCount, warningCount, failed, visible }) => { if (!visible) return null; let color = 'success.main'; let text = 'Valid'; if (checking) { color = 'warning.main'; text = 'Checking…'; } else if (failed) { color = 'error.main'; text = 'Validation failed'; } else if (errorCount > 0) { color = 'error.main'; text = `${errorCount} ${errorCount === 1 ? 'error' : 'errors'}`; } else if (warningCount > 0) { color = 'warning.main'; text = `${warningCount} ${warningCount === 1 ? 'warning' : 'warnings'}`; } return ( {text} ); }; // YAML editor + live dct board for one board file under /charts/. const ChartsBoardView: React.FC = ({ tab, project }) => { const { updateTabContent, markTabSaved, setTabError, openFile } = useTabs(); const refreshProjectFiles = useRefreshProjectFiles(); const { data: settings } = useGetSettings(); const { tasks, cancel: cancelTask } = useTaskManager(); const installLine = useChartsInstallLine(); const installMutation = useInstallDbtCharts(); const parseMutation = useRunChartsDbtParse(); const stateQuery = useProjectChartsState(project.path); const state = stateQuery.data; const relativePath = chartsRelativePath(project.path, tab.path); const isPartial = isChartsPartial(tab.path); const installTask = tasks.find( (task) => task.type === DBT_CHARTS_INSTALL_TASK && (task.status === 'pending' || task.status === 'running'), ); const ready = !!state && state.support.supported && state.extraInstalled && !installTask; // An install started from any board tab: re-read the state when it ends. const installing = !!installTask; const { refetch } = stateQuery; React.useEffect(() => { if (!installing) refetch(); }, [installing, refetch]); const { status: server, startError, retry, } = useChartsServer(project.path, ready); const { result, checking, validate } = useChartsValidation( project.path, tab.path, ); useChartsMarkers(project.id, tab.path, relativePath, result?.diagnostics); // Validate on open and after every save or agent write (both change // savedContent). dct's own live reload refreshes the board itself. React.useEffect(() => { if (!state?.extraInstalled || installTask) return undefined; const timer = window.setTimeout(() => { validate().catch(() => undefined); }, VALIDATE_DEBOUNCE_MS); return () => window.clearTimeout(timer); }, [ state?.extraInstalled, installTask, tab.path, tab.savedContent, validate, ]); // The agent's studio_charts_refresh_preview reloads this board explicitly. const [reloadKey, setReloadKey] = React.useState(0); const reload = React.useCallback(() => setReloadKey((key) => key + 1), []); React.useEffect( () => subscribeToToolResult((payload) => { const refreshed = refreshedPath(payload.toolName, payload.result); if (!refreshed || refreshed !== relativePath) return; validate().catch(() => undefined); reload(); }), [relativePath, validate, reload], ); const [layout, setLayout] = React.useState(readLayout); const changeLayout = (next: Layout) => { setLayout(next); writeLayout(next); }; const [sizes, setSizes] = React.useState<(string | number)[]>(['50%', '50%']); const [installError, setInstallError] = React.useState(null); const install = () => { setInstallError(null); installMutation .mutateAsync(project.path) .catch((error) => setInstallError(chartsErrorMessage(error))); }; const runParse = () => { parseMutation .mutateAsync(project.path) .then((parsed) => (parsed.ok ? retry() : undefined)) .catch(() => undefined); }; const dbtV2 = !!settings?.dbtVersion?.startsWith('2.'); const renderSetup = (): React.ReactNode | null => { if (stateQuery.isLoading || !state) { return ; } if (!state.support.supported) { return ( ); } if (installTask) { return ( cancelTask(installTask.id) : undefined } /> ); } if (!state.extraInstalled) { return ( ); } if (parseMutation.isLoading || server.state === 'parsing') { return ; } if (server.state === 'parse-failed') { return ( ); } if (startError || server.state === 'crashed') { return ( { retry().catch(() => undefined); }} /> ); } if (server.state === 'stopped' && server.userStopped) { return ( { retry().catch(() => undefined); }} /> ); } if (server.state !== 'running') { return ; } return null; }; const editor = ( { markTabSaved(tabId); refreshProjectFiles(); }} onTabError={setTabError} onGitStatusRefresh={refreshProjectFiles} onOpenFile={openFile} extraActions={ <> changeLayout(layout === 'editor' ? 'split' : 'editor') } > changeLayout('preview')}> } /> ); const preview = ( {layout === 'preview' && ( changeLayout('split')} sx={{ position: 'absolute', left: 4, bottom: 4, zIndex: 1, bgcolor: 'background.paper', border: 1, borderColor: 'divider', }} > )} ); return ( {layout === 'editor' && editor} {layout === 'preview' && preview} {layout === 'split' && ( {editor} {preview} )} ); }; const ChartsFileView: React.FC = ({ tab, project }) => { if (!isChartsBoardPath(project.path, tab.path)) { // A `charts` folder that is not the project-root one (models/charts/…). const CodeView = codeFileView.Component; return ; } return ; }; export const chartsFileView: FileViewDefinition = { id: 'dbt-charts', matches: (tab) => looksLikeChartsPath(tab.path), Component: ChartsFileView, };