From b00d0fa76f4fad187b4c93e7802f8bec717a96b4 Mon Sep 17 00:00:00 2001 From: Manohar Gupta Date: Sat, 23 May 2026 17:53:17 +0530 Subject: [PATCH] Add file preview to display CSV contents when clicking on profiles --- packages/web/components/ProfileViewer.tsx | 115 +++++++++++++++++++--- 1 file changed, 99 insertions(+), 16 deletions(-) diff --git a/packages/web/components/ProfileViewer.tsx b/packages/web/components/ProfileViewer.tsx index 05411f9..64b7ed7 100644 --- a/packages/web/components/ProfileViewer.tsx +++ b/packages/web/components/ProfileViewer.tsx @@ -87,6 +87,33 @@ export function ProfileViewer() { }>({}); const [isUploading, setIsUploading] = useState(false); const [uploadError, setUploadError] = useState(null); + const [selectedFile, setSelectedFile] = useState<{ + name: string; + path: string; + data: string[][]; + headers: string[]; + } | null>(null); + const [isLoadingFile, setIsLoadingFile] = useState(false); + + const API_BASE = typeof window !== 'undefined' + ? ((window as any).NEXT_PUBLIC_API_URL || 'http://localhost:8000') + : 'http://localhost:8000'; + + async function fetchAndDisplayFile(path: string, name: string) { + setIsLoadingFile(true); + try { + const res = await fetch(`${API_BASE}${path}`); + const text = await res.text(); + const lines = text.trim().split('\n'); + const headers = lines[0].split(','); + const data = lines.slice(1).map(line => line.split(',')); + setSelectedFile({ name, path, data, headers }); + } catch (err) { + console.error('Failed to fetch file:', err); + } finally { + setIsLoadingFile(false); + } + } // Calculate stats for a profile const calculateStats = (data: number[]) => { @@ -343,20 +370,19 @@ export function ProfileViewer() { {/* Bundled profiles section */}

Bundled Reference Files

-
+
{[ - { name: "Solar - Gujarat", path: "/api/profiles/solar/GJ", type: "solar" }, - { name: "Solar - Karnataka", path: "/api/profiles/solar/KA", type: "solar" }, - { name: "Solar - Rajasthan", path: "/api/profiles/solar/RJ", type: "solar" }, - { name: "Wind - Gujarat", path: "/api/profiles/wind/GJ", type: "wind" }, - { name: "Wind - Karnataka", path: "/api/profiles/wind/KA", type: "wind" }, - { name: "Wind - Rajasthan", path: "/api/profiles/wind/RJ", type: "wind" }, + { name: "Solar - Gujarat", path: "/api/profiles/solar/GJ", type: "solar", id: "solar_gj" }, + { name: "Solar - Karnataka", path: "/api/profiles/solar/KA", type: "solar", id: "solar_ka" }, + { name: "Solar - Rajasthan", path: "/api/profiles/solar/RJ", type: "solar", id: "solar_rj" }, + { name: "Wind - Gujarat", path: "/api/profiles/wind/GJ", type: "wind", id: "wind_gj" }, + { name: "Wind - Karnataka", path: "/api/profiles/wind/KA", type: "wind", id: "wind_ka" }, + { name: "Wind - Rajasthan", path: "/api/profiles/wind/RJ", type: "wind", id: "wind_rj" }, ].map((profile) => ( - fetchAndDisplayFile(profile.path, profile.name)} + className="flex items-center gap-2 p-3 border rounded-lg hover:bg-accent transition-colors text-left" > {profile.type === "solar" ? "☀️" : "💨"}
@@ -364,15 +390,72 @@ export function ProfileViewer() {
8760 hourly values
- - - + + -
+ ))}
+ {/* File preview section */} + {selectedFile && ( +
+
+
+ {selectedFile.name} + ({selectedFile.data.length} rows) +
+
+ + Download CSV + + +
+
+
+ + + + + + + + + + + + + {selectedFile.data.slice(0, 100).map((row: string[], i: number) => ( + + + + + + + + + ))} + +
HourDateMonthDayHour of Day% Generation
{row[0]}{row[1]}{row[2]}{row[3]}{row[4]}{row[5]}
+ {selectedFile.data.length > 100 && ( +
+ Showing first 100 of {selectedFile.data.length} rows. Download CSV for full data. +
+ )} +
+
+ )} + {activeTab === "solar" && (