257 lines
8.7 KiB
TypeScript
257 lines
8.7 KiB
TypeScript
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<string, unknown>): 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 (
|
|
<div className="grid gap-6">
|
|
<div className="grid gap-1">
|
|
<h1 className="text-2xl">Dashboards</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
What a wall panel shows, built from the messages your flows carry.
|
|
</p>
|
|
</div>
|
|
|
|
{/* Its own root rather than a nested one: which screens exist is a
|
|
different question from which dashboards do. */}
|
|
<Dialog open={panelsOpen ?? false} onOpenChange={setPanelsOpen}>
|
|
<PanelsDialog focus={panelFocus} />
|
|
</Dialog>
|
|
|
|
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
|
|
<OverviewToolbar
|
|
search={search}
|
|
onSearch={setSearch}
|
|
searchLabel="Search dashboards"
|
|
searchTestId="search-dashboards"
|
|
createLabel="New dashboard"
|
|
createTestId="new-dashboard"
|
|
draftCount={drafts.length}
|
|
publishing={publishAll.isPending}
|
|
onPublishAll={() => publishAll.mutate(drafts)}
|
|
selectedCount={selection.selected.length}
|
|
onDeleteSelected={() => setDeleteOpen(true)}
|
|
duplicating={duplicate.isPending}
|
|
onDuplicateSelected={() => duplicate.mutate(selection.selected)}
|
|
>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-touch"
|
|
className="text-muted-foreground"
|
|
aria-label="Panels"
|
|
data-testid="open-panels"
|
|
onClick={() => setPanelsOpen(true)}
|
|
>
|
|
<MonitorSmartphone />
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Panels</TooltipContent>
|
|
</Tooltip>
|
|
</OverviewToolbar>
|
|
<DialogContent>
|
|
<form
|
|
className="grid gap-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault()
|
|
if (slug) create.mutate(slug)
|
|
}}
|
|
>
|
|
<DialogHeader>
|
|
<DialogTitle>New dashboard</DialogTitle>
|
|
<DialogDescription>
|
|
Name it after the panel it will hang on, or what it shows.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<Input
|
|
value={name}
|
|
placeholder="kitchen"
|
|
aria-label="New dashboard name"
|
|
autoComplete="off"
|
|
data-testid="new-dashboard-name"
|
|
onChange={(event) => setName(event.target.value)}
|
|
/>
|
|
<DialogFooter>
|
|
<Button
|
|
type="submit"
|
|
disabled={!slug || create.isPending}
|
|
data-testid="create-dashboard"
|
|
>
|
|
Create dashboard
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
|
|
<ConfirmDelete
|
|
open={deleteOpen}
|
|
onOpenChange={setDeleteOpen}
|
|
names={selection.selected}
|
|
noun="dashboard"
|
|
pending={remove.isPending}
|
|
onConfirm={() => {
|
|
setDeleteOpen(false)
|
|
remove.mutate(selection.selected)
|
|
}}
|
|
/>
|
|
|
|
{isPending ? (
|
|
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
|
{Array.from({ length: 3 }).map((_, index) => (
|
|
<Skeleton key={index} className="h-[5.5rem] rounded-lg" />
|
|
))}
|
|
</div>
|
|
) : dashboards.length === 0 ? (
|
|
<p className="text-sm text-muted-foreground">
|
|
{needle
|
|
? "No dashboard matches that."
|
|
: "No dashboards yet. Create one to start."}
|
|
</p>
|
|
) : (
|
|
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
|
{dashboards.map((dashboard) => (
|
|
<OverviewCard
|
|
key={dashboard.name}
|
|
link={{
|
|
to: "/dashboards/$name",
|
|
params: { name: dashboard.name },
|
|
}}
|
|
icon={LayoutDashboard}
|
|
title={dashboard.title || dashboard.name}
|
|
draft={dashboard.has_draft}
|
|
detail={`${dashboard.widget_count} widget${
|
|
dashboard.widget_count === 1 ? "" : "s"
|
|
}`}
|
|
selecting={selection.selecting}
|
|
selected={selection.selected.includes(dashboard.name)}
|
|
onToggle={() => selection.toggle(dashboard.name)}
|
|
testId="dashboard-card"
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|