Skip to content

Commit 1b129b0

Browse files
Copilothotlong
andcommitted
fix: sync ReportView reportData state with async metadata and add loading state
The ReportView component had a stale state bug where reportData was initialized via useState(initialReport) on first render when metadata was still loading (reports=[]), causing it to remain undefined even after metadata loaded. Added useEffect to sync state and loading check. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 287edf7 commit 1b129b0

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

apps/console/src/components/ReportView.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { useState } from 'react';
1+
import { useState, useEffect } from 'react';
22
import { useParams } from 'react-router-dom';
33
import { ReportViewer, ReportBuilder } from '@object-ui/plugin-report';
44
import { Empty, EmptyTitle, EmptyDescription, Button } from '@object-ui/components';
5-
import { PenLine, ChevronLeft, BarChart3 } from 'lucide-react';
5+
import { PenLine, ChevronLeft, BarChart3, Loader2 } from 'lucide-react';
66
import { MetadataToggle, MetadataPanel, useMetadataInspector } from './MetadataInspector';
77
import { useMetadata } from '../context/MetadataProvider';
88

@@ -24,10 +24,25 @@ export function ReportView({ dataSource: _dataSource }: { dataSource?: any }) {
2424
const [isEditing, setIsEditing] = useState(false);
2525

2626
// Find report definition from API-driven metadata
27-
const { reports } = useMetadata();
27+
const { reports, loading } = useMetadata();
2828
const initialReport = reports?.find((r: any) => r.name === reportName);
2929
const [reportData, setReportData] = useState(initialReport);
3030

31+
// Sync reportData when metadata finishes loading or reportName changes
32+
useEffect(() => {
33+
if (initialReport) {
34+
setReportData(initialReport);
35+
}
36+
}, [initialReport]);
37+
38+
if (loading) {
39+
return (
40+
<div className="h-full flex items-center justify-center p-8">
41+
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
42+
</div>
43+
);
44+
}
45+
3146
if (!initialReport) {
3247
return (
3348
<div className="h-full flex items-center justify-center p-8">

0 commit comments

Comments
 (0)