Skip to content

Commit 111169d

Browse files
Copilothotlong
andcommitted
Add data loading to ReportView component
- Import DataSource type from @object-ui/types - Add state for reportRuntimeData and dataLoading - Add effect to load data based on report.data (inline), report.dataSource, or report.objectName - Pass data and loading to viewerSchema for ReportViewer component - Supports three data loading patterns: inline data, dataSource config, and objectName-based queries Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent eacc778 commit 111169d

1 file changed

Lines changed: 82 additions & 2 deletions

File tree

apps/console/src/components/ReportView.tsx

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Empty, EmptyTitle, EmptyDescription, Button } from '@object-ui/componen
55
import { PenLine, ChevronLeft, BarChart3, Loader2 } from 'lucide-react';
66
import { MetadataToggle, MetadataPanel, useMetadataInspector } from './MetadataInspector';
77
import { useMetadata } from '../context/MetadataProvider';
8+
import type { DataSource } from '@object-ui/types';
89

910
// Mock fields for the builder since we don't have a dynamic schema provider here yet
1011
const MOCK_FIELDS = [
@@ -18,7 +19,7 @@ const MOCK_FIELDS = [
1819
{ name: 'amount', label: 'Amount', type: 'currency' },
1920
];
2021

21-
export function ReportView({ dataSource: _dataSource }: { dataSource?: any }) {
22+
export function ReportView({ dataSource }: { dataSource?: DataSource }) {
2223
const { reportName } = useParams<{ reportName: string }>();
2324
const { showDebug, toggleDebug } = useMetadataInspector();
2425
const [isEditing, setIsEditing] = useState(false);
@@ -28,11 +29,88 @@ export function ReportView({ dataSource: _dataSource }: { dataSource?: any }) {
2829
const initialReport = reports?.find((r: any) => r.name === reportName);
2930
const [reportData, setReportData] = useState(initialReport);
3031

32+
// State for report runtime data
33+
const [reportRuntimeData, setReportRuntimeData] = useState<any[]>([]);
34+
const [dataLoading, setDataLoading] = useState(false);
35+
3136
// Sync reportData when metadata finishes loading or reportName changes
3237
useEffect(() => {
3338
setReportData(initialReport);
3439
}, [initialReport]);
3540

41+
// Load report runtime data when report definition changes
42+
useEffect(() => {
43+
if (!reportData || !dataSource) {
44+
setReportRuntimeData([]);
45+
return;
46+
}
47+
48+
// If report has inline data, use it directly
49+
if (reportData.data && Array.isArray(reportData.data)) {
50+
setReportRuntimeData(reportData.data);
51+
return;
52+
}
53+
54+
// If report has a dataSource config, fetch data using it
55+
if (reportData.dataSource) {
56+
const fetchDataFromSource = async () => {
57+
setDataLoading(true);
58+
try {
59+
// Use the dataSource configuration to fetch data
60+
const resource = reportData.dataSource.object || reportData.dataSource.resource;
61+
if (!resource) {
62+
console.warn('ReportView: dataSource missing object/resource property');
63+
setReportRuntimeData([]);
64+
return;
65+
}
66+
67+
const result = await dataSource.find(resource, {
68+
$filter: reportData.dataSource.filter,
69+
$orderby: reportData.dataSource.sort,
70+
$top: reportData.dataSource.limit,
71+
});
72+
73+
setReportRuntimeData(result.data || []);
74+
} catch (error) {
75+
console.error('ReportView: Failed to load data from dataSource', error);
76+
setReportRuntimeData([]);
77+
} finally {
78+
setDataLoading(false);
79+
}
80+
};
81+
82+
fetchDataFromSource();
83+
return;
84+
}
85+
86+
// If report has an objectName, fetch data from that object
87+
if (reportData.objectName) {
88+
const fetchDataFromObject = async () => {
89+
setDataLoading(true);
90+
try {
91+
const result = await dataSource.find(reportData.objectName, {
92+
$filter: reportData.filters,
93+
$orderby: reportData.sort,
94+
$top: reportData.limit || 100, // Default limit to avoid fetching too much data
95+
});
96+
97+
setReportRuntimeData(result.data || []);
98+
} catch (error) {
99+
console.error('ReportView: Failed to load data from objectName', error);
100+
setReportRuntimeData([]);
101+
} finally {
102+
setDataLoading(false);
103+
}
104+
};
105+
106+
fetchDataFromObject();
107+
return;
108+
}
109+
110+
// No data source configured
111+
setReportRuntimeData([]);
112+
}, [reportData, dataSource]);
113+
36114
if (loading) {
37115
return (
38116
<div className="h-full flex items-center justify-center p-8">
@@ -96,8 +174,10 @@ export function ReportView({ dataSource: _dataSource }: { dataSource?: any }) {
96174
const viewerSchema = {
97175
type: 'report-viewer',
98176
report: reportData, // The report definition
177+
data: reportRuntimeData, // Runtime data fetched from the data source
99178
showToolbar: true,
100-
allowExport: true
179+
allowExport: true,
180+
loading: dataLoading, // Loading state for data fetching
101181
};
102182

103183
return (

0 commit comments

Comments
 (0)