import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { createFileRoute, useNavigate } from "@tanstack/react-router" import { LayoutDashboard, MonitorSmartphone } from "lucide-react" import { useState } from "react" import { DashboardsService } from "@/client" import { OverviewCard, useSelection } from "@/components/Common/OverviewCard" import { ConfirmDelete, OverviewToolbar, useDeleteSelected, useDuplicateSelected, usePublishAll, } from "@/components/Common/OverviewToolbar" import { PanelsDialog } from "@/components/Dashboard/PanelsDialog" import { dashboardKeys, dashboardsQueryOptions, } from "@/components/Dashboard/queries" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Skeleton } from "@/components/ui/skeleton" import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip" import useCustomToast from "@/hooks/useCustomToast" import { handleError } from "@/utils" type Search = { panels?: boolean; panel?: string } export const Route = createFileRoute("/_layout/dashboards/")({ component: Dashboards, // The panels dialog has no route of its own, so the address is how anything // else — the global search among them — arrives at it. validateSearch: (search: Record): Search => ({ panels: search.panels === true || search.panels === "true" || undefined, // Which panel to scroll to, for a link that is about one of them. panel: typeof search.panel === "string" ? search.panel : undefined, }), }) function Dashboards() { const { data, isPending } = useQuery(dashboardsQueryOptions()) const queryClient = useQueryClient() const navigate = useNavigate() const { showErrorToast } = useCustomToast() const [name, setName] = useState("") const [search, setSearch] = useState("") const [dialogOpen, setDialogOpen] = useState(false) const [deleteOpen, setDeleteOpen] = useState(false) // Which screens exist is a question with an address, so anything can link to // it — the global search lands a panel here. const { panels: panelsOpen, panel: panelFocus } = Route.useSearch() const setPanelsOpen = (open: boolean) => navigate({ to: "/dashboards", search: open ? { panels: true } : {} }) const create = useMutation({ mutationFn: (dashboard: string) => DashboardsService.createDashboard({ name: dashboard }), onSuccess: (created) => { queryClient.invalidateQueries({ queryKey: dashboardKeys.all }) setDialogOpen(false) navigate({ to: "/dashboards/$name", params: { name: created.name }, search: { edit: true }, }) }, onError: handleError.bind(showErrorToast), }) // The summary carries the working copy's version, which is the one publishing // is based on — so the list already holds everything this needs. const publishAll = usePublishAll( (dashboard) => DashboardsService.publishDashboard({ name: dashboard, requestBody: { version: data?.data.find((d) => d.name === dashboard)?.version ?? 1, }, }), "dashboard", () => queryClient.invalidateQueries({ queryKey: dashboardKeys.all }), ) // The store only accepts this shape, so say so before the request does. const slug = name .trim() .toLowerCase() .replace(/[^a-z0-9]+/g, "_") const needle = search.trim().toLowerCase() const dashboards = (data?.data ?? []).filter((dashboard) => `${dashboard.name} ${dashboard.title ?? ""}`.toLowerCase().includes(needle), ) const drafts = (data?.data ?? []) .filter((dashboard) => dashboard.has_draft) .map((dashboard) => dashboard.name) const selection = useSelection(dashboards.map((dashboard) => dashboard.name)) const remove = useDeleteSelected( (dashboard) => DashboardsService.deleteDashboard({ name: dashboard }), "dashboard", () => queryClient.invalidateQueries({ queryKey: dashboardKeys.all }), ) const duplicate = useDuplicateSelected( (dashboard) => DashboardsService.duplicateDashboard({ name: dashboard }), "dashboard", () => { selection.clear() queryClient.invalidateQueries({ queryKey: dashboardKeys.all }) }, ) return (

Dashboards

What a wall panel shows, built from the messages your flows carry.

{/* Its own root rather than a nested one: which screens exist is a different question from which dashboards do. */} publishAll.mutate(drafts)} selectedCount={selection.selected.length} onDeleteSelected={() => setDeleteOpen(true)} duplicating={duplicate.isPending} onDuplicateSelected={() => duplicate.mutate(selection.selected)} > Panels
{ event.preventDefault() if (slug) create.mutate(slug) }} > New dashboard Name it after the panel it will hang on, or what it shows. setName(event.target.value)} />
{ setDeleteOpen(false) remove.mutate(selection.selected) }} /> {isPending ? (
{Array.from({ length: 3 }).map((_, index) => ( ))}
) : dashboards.length === 0 ? (

{needle ? "No dashboard matches that." : "No dashboards yet. Create one to start."}

) : (
{dashboards.map((dashboard) => ( selection.toggle(dashboard.name)} testId="dashboard-card" /> ))}
)}
) }