import React, { memo, useEffect, useMemo, useState } from "react"; import { Card } from "../../tremor/Card"; import ChartCard from "../Chart/ChartCard"; import { Button } from "../../tremor/Button"; import { useDashboard } from "../../layouts/Dashboard/useDashboard"; import { useBackend } from "../../layouts/Wrapper"; import { toast } from "sonner"; import { Text, Title } from "../../tremor/Text"; import { Widget } from "@onvo-ai/js"; import { ArrowRightIcon, XMarkIcon } from "@heroicons/react/16/solid"; import { ChartPlaceholder } from "../ChartLoader"; import { Badge } from "../../tremor/Badge"; import { Empty } from "../Empty"; import { RectangleGroupIcon } from "@heroicons/react/24/outline"; const WidgetLibraryRaw: React.FC<{ onClose?: () => void; }> = ({ onClose }) => { const { backend, adminMode } = useBackend(); const { dashboard, refreshWidgets } = useDashboard(); const [library, setLibrary] = useState([]); const [loading, setLoading] = useState(true); const getLibrary = async () => { setLibrary([]); if (!dashboard) return; setLoading(true); let newSuggestions = await backend?.widgets.list({ dashboard: dashboard.id, use_in_library: true, }); if (newSuggestions && newSuggestions.length > 0) { setLibrary(newSuggestions); } setLoading(false); }; useEffect(() => { if (dashboard && dashboard.id) { getLibrary(); } }, [dashboard?.id]); const removeFromLibrary = async (id: string) => { toast.promise( async () => { return backend?.widgets.delete(id); }, { loading: "Deleting widget...", success: () => { getLibrary(); return "Widget deleted"; }, error: (error) => "Error deleting widget: " + error.message, } ); }; const addToDashboard = async (wid: Widget) => { let newObj = { ...wid, use_in_library: false }; // @ts-ignore delete newObj.id; if (!backend) return; toast.promise( async () => { let wid = await backend.widgets.create(newObj); let data = await backend.widget(wid.id).updateCache(); return true; }, { loading: "Adding widget to dashboard...", success: () => { refreshWidgets(backend); if (onClose) onClose(); return "Widget added to dashboard"; }, error: (error) => "Error adding widget to dashboard: " + error.message, } ); }; const AddToDashboardEnabled = useMemo(() => { if (adminMode) return true; if (dashboard?.settings?.can_create_widgets) return true; return false; }, [dashboard, adminMode]); if (!loading && library.length === 0) { if (adminMode) { } subtitle="You have not added any widgets to the library yet. You can do that using the copilot or from the widget settings." /> } return } subtitle="No widgets have been added to the library yet." /> ; } return (
{loading && (
{[1, 2, 3, 4].map((a) => ( ))}
)} {!loading && library.length > 0 && (
{library.map((a) => (
{adminMode && ( )}
{AddToDashboardEnabled && ( )}
} />
))}
)} {!loading && library.length === 0 && ( You have not added any widgets to the library yet.
You can add widgets to the library by asking a question and clicking on the Add to Library button
)} ); }; export const WidgetLibrary = memo(WidgetLibraryRaw);