50 lines
1.9 KiB
TypeScript
50 lines
1.9 KiB
TypeScript
import { useQuery } from "@tanstack/react-query"
|
|
import { createFileRoute } from "@tanstack/react-router"
|
|
import { useCallback, useState } from "react"
|
|
|
|
import { DashboardEditor } from "@/components/Dashboard/DashboardEditor"
|
|
import type { Dashboard } from "@/components/Dashboard/DashboardView"
|
|
import { dashboardQueryOptions } from "@/components/Dashboard/queries"
|
|
import { useFlowSocket } from "@/components/Flow/useFlowSocket"
|
|
|
|
type Search = { edit?: boolean }
|
|
|
|
export const Route = createFileRoute("/_canvas/dashboards/$id")({
|
|
component: DashboardRoute,
|
|
// Edit mode is a search param so viewing stays the default a panel opens
|
|
// into, and an editing session is a link someone can send.
|
|
validateSearch: (search: Record<string, unknown>): Search => ({
|
|
edit: search.edit === true || search.edit === "true" || undefined,
|
|
}),
|
|
})
|
|
|
|
function DashboardRoute() {
|
|
const { id } = Route.useParams()
|
|
const { edit } = Route.useSearch()
|
|
// Widgets read live values; the canvas shell has no socket of its own.
|
|
useFlowSocket()
|
|
// The working copy: an edit is the editor's until it is published, so what
|
|
// a wall panel is showing right now stays untouched by this session.
|
|
const { data: dashboard } = useQuery(dashboardQueryOptions(id, true))
|
|
// Bumped when the editor has to take the server's document over its own,
|
|
// as after a rename: its draft is state seeded on mount.
|
|
const [epoch, setEpoch] = useState(0)
|
|
const reload = useCallback(() => setEpoch((n) => n + 1), [])
|
|
|
|
if (!dashboard) return null
|
|
|
|
return (
|
|
<>
|
|
{/* From the document, since the id in the address can be opaque. */}
|
|
<title>{`${dashboard.title || dashboard.name} - Fluksio`}</title>
|
|
<DashboardEditor
|
|
// Leaving edit mode drops the draft, so the session starts clean.
|
|
key={`${id}:${edit ? "edit" : "view"}:${epoch}`}
|
|
dashboard={dashboard as Dashboard}
|
|
edit={Boolean(edit)}
|
|
onReload={reload}
|
|
/>
|
|
</>
|
|
)
|
|
}
|