feat: wire frontend state and data flow for multi-chart support
- Add activeChartId/charts state to page.tsx with chart fetching on mount - ChartSelector integrated in sidebar between header and file upload - CandleChart and SvgOverlay fetch data scoped by activeChartId - FileUpload returns chart info, auto-selects new chart after upload - Annotation create/delete flows include chart_id - Export scoped by activeChartId - Toolbox receives activeChartId prop
This commit is contained in:
parent
a3c7137b01
commit
b9771fe89f
6 changed files with 126 additions and 37 deletions
|
|
@ -34,13 +34,13 @@
|
||||||
|
|
||||||
## 6. Frontend State & Data Flow
|
## 6. Frontend State & Data Flow
|
||||||
|
|
||||||
- [ ] 6.1 Add `activeChartId` and `charts` state to `page.tsx`
|
- [x] 6.1 Add `activeChartId` and `charts` state to `page.tsx`
|
||||||
- [ ] 6.2 Fetch charts list on mount via `GET /api/charts`, auto-select the most recent chart
|
- [x] 6.2 Fetch charts list on mount via `GET /api/charts`, auto-select the most recent chart
|
||||||
- [ ] 6.3 Pass `activeChartId` to `CandleChart` component — update it to fetch candles/annotations with `?chartId=` param
|
- [x] 6.3 Pass `activeChartId` to `CandleChart` component — update it to fetch candles/annotations with `?chartId=` param
|
||||||
- [ ] 6.4 Update `handleUploadSuccess` to receive the new chart from the upload response, add it to `charts` state, and set it as `activeChartId`
|
- [x] 6.4 Update `handleUploadSuccess` to receive the new chart from the upload response, add it to `charts` state, and set it as `activeChartId`
|
||||||
- [ ] 6.5 Update annotation create/delete flows to include `chart_id` in requests
|
- [x] 6.5 Update annotation create/delete flows to include `chart_id` in requests
|
||||||
- [ ] 6.6 When `activeChartId` changes, refetch candles and annotations for the new chart
|
- [x] 6.6 When `activeChartId` changes, refetch candles and annotations for the new chart
|
||||||
- [ ] 6.7 Update export handler to include `?chartId=` in the export URL
|
- [x] 6.7 Update export handler to include `?chartId=` in the export URL
|
||||||
|
|
||||||
## 7. UI Polish
|
## 7. UI Polish
|
||||||
|
|
||||||
|
|
|
||||||
118
src/app/page.tsx
118
src/app/page.tsx
|
|
@ -1,12 +1,20 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState, useRef, useEffect } from 'react';
|
import { useState, useRef, useEffect, useCallback } from 'react';
|
||||||
import Toolbox, { Tool } from '@/components/Toolbox';
|
import Toolbox, { Tool } from '@/components/Toolbox';
|
||||||
import FileUpload from '@/components/FileUpload';
|
import FileUpload from '@/components/FileUpload';
|
||||||
import CandleChart, { CandleChartHandle } from '@/components/CandleChart';
|
import CandleChart, { CandleChartHandle } from '@/components/CandleChart';
|
||||||
|
import ChartSelector from '@/components/ChartSelector';
|
||||||
|
|
||||||
|
interface Chart {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
created_at: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface Annotation {
|
interface Annotation {
|
||||||
id: number;
|
id: number;
|
||||||
|
chart_id: number;
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
label_type: string;
|
label_type: string;
|
||||||
geometry: any;
|
geometry: any;
|
||||||
|
|
@ -18,28 +26,83 @@ export default function Home() {
|
||||||
const [selectedColor, setSelectedColor] = useState('#3b82f6');
|
const [selectedColor, setSelectedColor] = useState('#3b82f6');
|
||||||
const [selectedLabelId, setSelectedLabelId] = useState<number | null>(null);
|
const [selectedLabelId, setSelectedLabelId] = useState<number | null>(null);
|
||||||
const [annotations, setAnnotations] = useState<Annotation[]>([]);
|
const [annotations, setAnnotations] = useState<Annotation[]>([]);
|
||||||
|
const [charts, setCharts] = useState<Chart[]>([]);
|
||||||
|
const [activeChartId, setActiveChartId] = useState<number | null>(null);
|
||||||
const chartRef = useRef<CandleChartHandle>(null);
|
const chartRef = useRef<CandleChartHandle>(null);
|
||||||
|
|
||||||
|
// Fetch charts list
|
||||||
|
const fetchCharts = useCallback(async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/charts');
|
||||||
|
const data = await response.json();
|
||||||
|
setCharts(data);
|
||||||
|
return data as Chart[];
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch charts:', error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Fetch annotations for active chart
|
||||||
|
const fetchAnnotations = useCallback(async (chartId: number | null) => {
|
||||||
|
if (!chartId) {
|
||||||
|
setAnnotations([]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/annotations?chartId=${chartId}`);
|
||||||
|
const data = await response.json();
|
||||||
|
setAnnotations(data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch annotations:', error);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Fetch charts on mount, auto-select the most recent
|
||||||
|
useEffect(() => {
|
||||||
|
const init = async () => {
|
||||||
|
const chartList = await fetchCharts();
|
||||||
|
if (chartList.length > 0) {
|
||||||
|
setActiveChartId(chartList[0].id); // sorted by created_at desc
|
||||||
|
}
|
||||||
|
};
|
||||||
|
init();
|
||||||
|
}, [fetchCharts]);
|
||||||
|
|
||||||
|
// When activeChartId changes, refetch data
|
||||||
|
useEffect(() => {
|
||||||
|
if (activeChartId !== null) {
|
||||||
|
chartRef.current?.refreshData();
|
||||||
|
fetchAnnotations(activeChartId);
|
||||||
|
setSelectedLabelId(null);
|
||||||
|
}
|
||||||
|
}, [activeChartId, fetchAnnotations]);
|
||||||
|
|
||||||
const handleExport = () => {
|
const handleExport = () => {
|
||||||
|
if (activeChartId) {
|
||||||
|
window.location.href = `/api/export?chartId=${activeChartId}`;
|
||||||
|
} else {
|
||||||
window.location.href = '/api/export';
|
window.location.href = '/api/export';
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUploadSuccess = () => {
|
const handleUploadSuccess = (chart: { id: number; name: string }) => {
|
||||||
// Refresh chart data after successful upload
|
// Add new chart to list and select it
|
||||||
chartRef.current?.refreshData();
|
const newChart: Chart = {
|
||||||
|
id: chart.id,
|
||||||
|
name: chart.name,
|
||||||
|
created_at: Math.floor(Date.now() / 1000),
|
||||||
|
};
|
||||||
|
setCharts((prev) => [newChart, ...prev]);
|
||||||
|
setActiveChartId(chart.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAnnotationChange = async () => {
|
const handleAnnotationChange = async () => {
|
||||||
// Refresh chart when annotations change
|
|
||||||
await chartRef.current?.refreshData();
|
await chartRef.current?.refreshData();
|
||||||
// Fetch annotations for sidebar
|
await fetchAnnotations(activeChartId);
|
||||||
const response = await fetch('/api/annotations');
|
|
||||||
const data = await response.json();
|
|
||||||
setAnnotations(data);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLabelDelete = async (id: number) => {
|
const handleLabelDelete = async (id: number) => {
|
||||||
// Remove from local state
|
|
||||||
setAnnotations(annotations.filter((a) => a.id !== id));
|
setAnnotations(annotations.filter((a) => a.id !== id));
|
||||||
if (selectedLabelId === id) {
|
if (selectedLabelId === id) {
|
||||||
setSelectedLabelId(null);
|
setSelectedLabelId(null);
|
||||||
|
|
@ -50,19 +113,24 @@ export default function Home() {
|
||||||
setSelectedLabelId(id === -1 ? null : id);
|
setSelectedLabelId(id === -1 ? null : id);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fetch annotations on mount
|
const handleSelectChart = (chartId: number) => {
|
||||||
useEffect(() => {
|
setActiveChartId(chartId);
|
||||||
const fetchAnnotations = async () => {
|
};
|
||||||
|
|
||||||
|
const handleDeleteChart = async (chartId: number) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/annotations');
|
const response = await fetch(`/api/charts/${chartId}`, { method: 'DELETE' });
|
||||||
const data = await response.json();
|
if (response.ok) {
|
||||||
setAnnotations(data);
|
const remaining = charts.filter((c) => c.id !== chartId);
|
||||||
|
setCharts(remaining);
|
||||||
|
if (activeChartId === chartId) {
|
||||||
|
setActiveChartId(remaining.length > 0 ? remaining[0].id : null);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch annotations:', error);
|
console.error('Failed to delete chart:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
fetchAnnotations();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
// Keyboard handler for Delete/Backspace key
|
// Keyboard handler for Delete/Backspace key
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -100,7 +168,15 @@ export default function Home() {
|
||||||
Manage Annotation Types
|
Manage Annotation Types
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<div className="p-6">
|
<div className="p-6 pb-3">
|
||||||
|
<ChartSelector
|
||||||
|
charts={charts}
|
||||||
|
activeChartId={activeChartId}
|
||||||
|
onSelectChart={handleSelectChart}
|
||||||
|
onDeleteChart={handleDeleteChart}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="px-6 pb-3">
|
||||||
<FileUpload onUploadSuccess={handleUploadSuccess} />
|
<FileUpload onUploadSuccess={handleUploadSuccess} />
|
||||||
</div>
|
</div>
|
||||||
<div className="px-6 pb-6">
|
<div className="px-6 pb-6">
|
||||||
|
|
@ -114,6 +190,7 @@ export default function Home() {
|
||||||
selectedLabelId={selectedLabelId}
|
selectedLabelId={selectedLabelId}
|
||||||
onLabelSelect={handleLabelSelect}
|
onLabelSelect={handleLabelSelect}
|
||||||
onLabelDelete={handleLabelDelete}
|
onLabelDelete={handleLabelDelete}
|
||||||
|
activeChartId={activeChartId}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
@ -127,6 +204,7 @@ export default function Home() {
|
||||||
selectedColor={selectedColor}
|
selectedColor={selectedColor}
|
||||||
selectedLabelId={selectedLabelId}
|
selectedLabelId={selectedLabelId}
|
||||||
onLabelSelect={handleLabelSelect}
|
onLabelSelect={handleLabelSelect}
|
||||||
|
activeChartId={activeChartId}
|
||||||
/>
|
/>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,7 @@ interface CandleChartProps {
|
||||||
selectedColor: string;
|
selectedColor: string;
|
||||||
selectedLabelId?: number | null;
|
selectedLabelId?: number | null;
|
||||||
onLabelSelect?: (id: number) => void;
|
onLabelSelect?: (id: number) => void;
|
||||||
|
activeChartId?: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CandleChartHandle {
|
export interface CandleChartHandle {
|
||||||
|
|
@ -49,7 +50,7 @@ export interface CandleChartHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
const CandleChart = forwardRef<CandleChartHandle, CandleChartProps>(
|
const CandleChart = forwardRef<CandleChartHandle, CandleChartProps>(
|
||||||
({ activeTool, onAnnotationChange, selectedColor, selectedLabelId, onLabelSelect }, ref) => {
|
({ activeTool, onAnnotationChange, selectedColor, selectedLabelId, onLabelSelect, activeChartId }, ref) => {
|
||||||
const chartContainerRef = useRef<HTMLDivElement>(null);
|
const chartContainerRef = useRef<HTMLDivElement>(null);
|
||||||
const chartRef = useRef<IChartApi | null>(null);
|
const chartRef = useRef<IChartApi | null>(null);
|
||||||
const seriesRef = useRef<ISeriesApi<'Candlestick'> | null>(null);
|
const seriesRef = useRef<ISeriesApi<'Candlestick'> | null>(null);
|
||||||
|
|
@ -68,7 +69,8 @@ const CandleChart = forwardRef<CandleChartHandle, CandleChartProps>(
|
||||||
// Fetch candles from API
|
// Fetch candles from API
|
||||||
const fetchCandles = async () => {
|
const fetchCandles = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/candles');
|
const url = activeChartId ? `/api/candles?chartId=${activeChartId}` : '/api/candles';
|
||||||
|
const response = await fetch(url);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setCandles(data);
|
setCandles(data);
|
||||||
setIsEmpty(data.length === 0);
|
setIsEmpty(data.length === 0);
|
||||||
|
|
@ -82,7 +84,8 @@ const CandleChart = forwardRef<CandleChartHandle, CandleChartProps>(
|
||||||
// Fetch annotations from API
|
// Fetch annotations from API
|
||||||
const fetchAnnotations = async () => {
|
const fetchAnnotations = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/annotations');
|
const url = activeChartId ? `/api/annotations?chartId=${activeChartId}` : '/api/annotations';
|
||||||
|
const response = await fetch(url);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setAnnotations(data);
|
setAnnotations(data);
|
||||||
return data;
|
return data;
|
||||||
|
|
@ -308,6 +311,7 @@ const CandleChart = forwardRef<CandleChartHandle, CandleChartProps>(
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
timestamp: nearestCandle.time,
|
timestamp: nearestCandle.time,
|
||||||
label_type: activeTool,
|
label_type: activeTool,
|
||||||
|
chart_id: activeChartId,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -410,6 +414,7 @@ const CandleChart = forwardRef<CandleChartHandle, CandleChartProps>(
|
||||||
activeTool={activeTool}
|
activeTool={activeTool}
|
||||||
onAnnotationChange={onAnnotationChange}
|
onAnnotationChange={onAnnotationChange}
|
||||||
selectedColor={selectedColor}
|
selectedColor={selectedColor}
|
||||||
|
activeChartId={activeChartId}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import { Button } from '@/components/ui/button';
|
||||||
import { Upload } from 'lucide-react';
|
import { Upload } from 'lucide-react';
|
||||||
|
|
||||||
interface FileUploadProps {
|
interface FileUploadProps {
|
||||||
onUploadSuccess: () => void;
|
onUploadSuccess: (chart: { id: number; name: string }) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function FileUpload({ onUploadSuccess }: FileUploadProps) {
|
export default function FileUpload({ onUploadSuccess }: FileUploadProps) {
|
||||||
|
|
@ -36,7 +36,7 @@ export default function FileUpload({ onUploadSuccess }: FileUploadProps) {
|
||||||
type: 'success',
|
type: 'success',
|
||||||
text: `Successfully uploaded ${data.count} candle records`,
|
text: `Successfully uploaded ${data.count} candle records`,
|
||||||
});
|
});
|
||||||
onUploadSuccess();
|
onUploadSuccess(data.chart);
|
||||||
} else {
|
} else {
|
||||||
setMessage({
|
setMessage({
|
||||||
type: 'error',
|
type: 'error',
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ interface SvgOverlayProps {
|
||||||
activeTool: string | null;
|
activeTool: string | null;
|
||||||
onAnnotationChange?: () => void;
|
onAnnotationChange?: () => void;
|
||||||
selectedColor: string;
|
selectedColor: string;
|
||||||
|
activeChartId?: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Point {
|
interface Point {
|
||||||
|
|
@ -42,6 +43,7 @@ export default function SvgOverlay({
|
||||||
activeTool,
|
activeTool,
|
||||||
onAnnotationChange,
|
onAnnotationChange,
|
||||||
selectedColor,
|
selectedColor,
|
||||||
|
activeChartId,
|
||||||
}: SvgOverlayProps) {
|
}: SvgOverlayProps) {
|
||||||
const svgRef = useRef<SVGSVGElement>(null);
|
const svgRef = useRef<SVGSVGElement>(null);
|
||||||
const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
|
const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
|
||||||
|
|
@ -54,7 +56,8 @@ export default function SvgOverlay({
|
||||||
// Fetch annotations
|
// Fetch annotations
|
||||||
const fetchAnnotations = async () => {
|
const fetchAnnotations = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/annotations');
|
const url = activeChartId ? `/api/annotations?chartId=${activeChartId}` : '/api/annotations';
|
||||||
|
const response = await fetch(url);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setAnnotations(data);
|
setAnnotations(data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -232,6 +235,7 @@ export default function SvgOverlay({
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
timestamp: drawingLine.start.time,
|
timestamp: drawingLine.start.time,
|
||||||
label_type: 'line',
|
label_type: 'line',
|
||||||
|
chart_id: activeChartId,
|
||||||
color: selectedColor,
|
color: selectedColor,
|
||||||
geometry: {
|
geometry: {
|
||||||
startTime: drawingLine.start.time,
|
startTime: drawingLine.start.time,
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ interface ToolboxProps {
|
||||||
selectedLabelId?: number | null;
|
selectedLabelId?: number | null;
|
||||||
onLabelSelect?: (id: number) => void;
|
onLabelSelect?: (id: number) => void;
|
||||||
onLabelDelete?: (id: number) => void;
|
onLabelDelete?: (id: number) => void;
|
||||||
|
activeChartId?: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Toolbox({
|
export default function Toolbox({
|
||||||
|
|
@ -48,6 +49,7 @@ export default function Toolbox({
|
||||||
selectedLabelId = null,
|
selectedLabelId = null,
|
||||||
onLabelSelect,
|
onLabelSelect,
|
||||||
onLabelDelete,
|
onLabelDelete,
|
||||||
|
activeChartId,
|
||||||
}: ToolboxProps) {
|
}: ToolboxProps) {
|
||||||
const [labelsExpanded, setLabelsExpanded] = useState(true);
|
const [labelsExpanded, setLabelsExpanded] = useState(true);
|
||||||
const [searchText, setSearchText] = useState('');
|
const [searchText, setSearchText] = useState('');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue