import React, { useState, useEffect, useMemo, useRef } from "react"; import { Card } from "../../tremor/Card"; import { Title, Text } from "../../tremor/Text"; import { Input } from "../../tremor/Input"; import { Logo } from "../../components/Logo/Logo"; import { useBackend } from "../Wrapper"; import { useTheme } from "../Dashboard/useTheme"; import { Dashboard } from "../Dashboard/Dashboard"; import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import { ClockIcon, PencilIcon, TableCellsIcon, LinkIcon } from "@heroicons/react/24/outline"; import { PresentationChartBarIcon } from "@heroicons/react/24/solid"; dayjs.extend(relativeTime); const DashboardCard: React.FC<{ dashboard: any; onClick?: (dashboard: any) => void; variant?: "list" | "grid"; }> = ({ dashboard, onClick, variant = "grid" }) => { return ( onClick && onClick(dashboard)} className={ "onvo-dashboard-card !onvo-p-4 onvo-cursor-pointer onvo-gap-4 onvo-flex " + (variant === "grid" ? "onvo-flex-col" : "onvo-flex-row onvo-items-center") } >
{dashboard.thumbnail && dashboard.thumbnail.trim() !== "" ? (
{dashboard.title}
) : (
)}
{dashboard.title} {dashboard.description}
{dashboard._meta.widgets}{" "} widgets {dashboard._meta.datasources}{" "} datasources
Created {dayjs(dashboard.created_at).fromNow()} Updated {dayjs(dashboard.last_updated_at).fromNow()}
); }; export const DashboardList: React.FC<{ onClickItem?: (dashboard: any) => void; variant?: "list" | "grid"; title?: string; theme?: "light" | "dark" | "system"; layout?: "default" | "sidebar" | "tabs"; }> = ({ onClickItem, variant = "grid", title = "Dashboards", theme: themeProp = "system", layout = "default", }) => { const [dashboards, setDashboards] = useState([]); const [loading, setLoading] = useState(true); const { backend, team } = useBackend(); const theme = useTheme(themeProp); const [search, setSearch] = useState(""); const [selectedDashboard, setSelectedDashboard] = useState(null); const containerRef = useRef(null); const [containerHeight, setContainerHeight] = useState(0); console.log("THEME: ", theme); useEffect(() => { if (backend) { setLoading(true); backend.dashboards .list() .then((a) => { // @ts-ignore setDashboards(a); if (a.length > 0) { setSelectedDashboard(a[0]); } setLoading(false); }) .catch((e) => { setLoading(false); }); } }, [backend]); useEffect(() => { const updateHeight = () => { if (containerRef.current && containerRef.current.parentElement) { const parentHeight = containerRef.current.parentElement.offsetHeight; setContainerHeight(parentHeight); } }; // Use setTimeout to ensure DOM is fully rendered const timer = setTimeout(updateHeight, 100); window.addEventListener('resize', updateHeight); return () => { clearTimeout(timer); window.removeEventListener('resize', updateHeight); }; }, []); const filtered = useMemo(() => { return ( search === "" ? dashboards : dashboards.filter((a) => { return (a.id + " " + a.title + " " + a.description) .toLowerCase() .includes(search.toLowerCase()); }) ) }, [dashboards, search]); return (
{layout === "default" ? (
{team?.logo ? (
{team?.name
) : (
)} {title}
setSearch(e.target.value)} />
{filtered.map((a) => ( ))}
) : layout === "sidebar" ? (
{team?.logo ? (
{team?.name
) : (
)} {title}
setSearch(e.target.value)} />
{selectedDashboard ? ( ) : (
Select a dashboard to view
)}
) : (
{team?.logo ? (
{team?.name
) : (
)} {title}
setSearch(e.target.value)} />
{filtered.map((dashboard) => ( ))}
{selectedDashboard ? ( ) : (
Select a dashboard to view
)}
)}
); };