"use client"; import { useState, useEffect, useMemo } from "react"; import { Search, ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight, Table2, RefreshCw } from "lucide-react"; interface PaginatedRecordInspectorProps { initialRows: Record[]; totalRecordsCount: number; } export default function PaginatedRecordInspector({ initialRows, }: PaginatedRecordInspectorProps) { const [page, setPage] = useState(1); const [pageSize] = useState(25); const [diseaseFilter, setDiseaseFilter] = useState("all"); const [searchTerm, setSearchTerm] = useState(""); const [loading, setLoading] = useState(false); const [isBackendConnected, setIsBackendConnected] = useState(false); // Client-side fallback computations const clientFiltered = useMemo(() => { let filtered = [...initialRows]; if (diseaseFilter !== "all") { filtered = filtered.filter( (r) => String(r["Predicted Disease"] || r["Diagnosis"] || "") .toLowerCase() === diseaseFilter.toLowerCase() ); } if (searchTerm.trim()) { filtered = filtered.filter((r) => String(r["Patient Number"] || r["Patient_ID"] || "") .toLowerCase() .includes(searchTerm.toLowerCase()) ); } return filtered; }, [initialRows, diseaseFilter, searchTerm]); const clientPageRows = useMemo(() => { const startIdx = (page - 1) * pageSize; return clientFiltered.slice(startIdx, startIdx + pageSize); }, [clientFiltered, page, pageSize]); const [backendData, setBackendData] = useState<{ rows: Record[]; total: number; } | null>(null); useEffect(() => { const apiUrl = process.env.NEXT_PUBLIC_API_URL; if (!apiUrl) return; let isSubscribed = true; const controller = new AbortController(); const loadData = async () => { try { const params = new URLSearchParams({ page: String(page), limit: String(pageSize), ...(diseaseFilter !== "all" && { disease: diseaseFilter }), ...(searchTerm.trim() && { search: searchTerm.trim() }), }); const res = await fetch(`${apiUrl}/dataset?${params.toString()}`, { signal: controller.signal, }); if (res.ok) { const data = await res.json(); if (isSubscribed && data && Array.isArray(data.rows)) { setBackendData({ rows: data.rows, total: data.total || 60000, }); setIsBackendConnected(true); setLoading(false); } } } catch { // Fall through to client computation } }; loadData(); return () => { isSubscribed = false; controller.abort(); }; }, [page, diseaseFilter, searchTerm, pageSize]); const rows = backendData ? backendData.rows : clientPageRows; const total = backendData ? backendData.total : clientFiltered.length; const totalPages = Math.max(1, Math.ceil(total / pageSize)); const columns = rows.length > 0 ? Object.keys(rows[0]) : [ "Patient Number", "Heart Rate (bpm)", "SpO2 Level (%)", "Systolic Blood Pressure (mmHg)", "Diastolic Blood Pressure (mmHg)", "Body Temperature (°C)", "Predicted Disease", "Heart Rate Alert", "Blood Pressure Alert", "Data Accuracy (%)", ]; return (
{/* Header & Filter Controls */}
HIGH-SPEED TELEMETRY OBSERVATION INSPECTOR

Paginated Record Explorer ({total.toLocaleString()} Matching Rows)

{/* Filters */}
{/* Search Box */}
{ setSearchTerm(e.target.value); setPage(1); }} className="min-h-11 pl-9 pr-3 py-2 bg-white border-2 border-ink text-[12px] font-mono text-ink placeholder:text-slate/60 outline-none w-full sm:w-48 focus:ring-1 focus:ring-hazard" />
{/* Disease Filter */}
{/* Mobile Swipe Hint */}
← SWIPE TABLE TO VIEW ALL ATTRIBUTES → PINNED PATIENT #
{/* Table Window */}
{loading && (
QUERYING 60,000 TELEMETRY RECORDS...
)} {columns.map((col, cIdx) => ( ))} {rows.length === 0 ? ( ) : ( rows.map((row, i) => { const disease = String( row["Predicted Disease"] || row["Diagnosis"] || "" ); const isHeartDisease = disease.toLowerCase().includes("heart"); return ( {columns.map((col, cIdx) => { const val = row[col]; const colLower = col.toLowerCase(); const isAlertCol = colLower.includes("alert"); const isAlertAbnormal = String(val).toUpperCase() === "ABNORMAL" || String(val).toUpperCase() === "YES"; const isFirst = cIdx === 0; return ( ); })} ); }) )}
{col.replace("_", " ")}
No matching telemetry records found for query.
{isAlertCol ? ( {String(val ?? "NORMAL")} ) : colLower.includes("disease") || colLower.includes("diagnosis") ? ( {String(val ?? "---")} ) : typeof val === "number" && !Number.isInteger(val) ? ( val.toFixed(2) ) : ( String(val ?? "---") )}
{/* Pagination Footer */}
Page {page.toLocaleString()} of {totalPages.toLocaleString()} · Showing{" "} {Math.min(pageSize, rows.length)} of {total.toLocaleString()} Records {isBackendConnected ? ( ● FAST SERVER STREAM ) : ( ● EDGE HIGH-SPEED CALIBRATION )}
{/* Controls */}
{page} / {totalPages}
); }