Files
DataClaw/frontend/src/store/dashboardStore.ts
T

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2026-03-14 15:52:27 +08:00
import { create } from 'zustand';
2026-03-15 17:57:09 +08:00
import type { ChartSpec } from './visualizationStore';
2026-03-14 15:52:27 +08:00
type ChartRow = Record<string, unknown>;
type GridLayout = { i: string; x: number; y: number; w: number; h: number };
export interface ChartConfig {
id: string;
title: string;
type: 'bar' | 'line';
data: ChartRow[];
sql: string;
2026-03-15 17:57:09 +08:00
chartSpec?: ChartSpec | null;
2026-03-14 15:52:27 +08:00
layout: GridLayout;
}
interface DashboardState {
charts: ChartConfig[];
addChart: (chart: Omit<ChartConfig, 'layout'>) => void;
removeChart: (id: string) => void;
updateLayout: (layouts: GridLayout[]) => void;
}
export const useDashboardStore = create<DashboardState>((set) => ({
charts: [],
addChart: (chart) => set((state) => {
const newLayout: GridLayout = {
i: chart.id,
2026-03-15 18:04:23 +08:00
x: (state.charts.length * 4) % 12,
2026-03-14 15:52:27 +08:00
y: Infinity,
2026-03-15 18:04:23 +08:00
w: 4,
h: 4,
2026-03-14 15:52:27 +08:00
};
return { charts: [...state.charts, { ...chart, layout: newLayout }] };
}),
removeChart: (id) => set((state) => ({
charts: state.charts.filter((c) => c.id !== id),
})),
updateLayout: (layouts) => set((state) => ({
charts: state.charts.map((chart) => {
const layout = layouts.find((l) => l.i === chart.id);
return layout ? { ...chart, layout } : chart;
}),
})),
}));