Rename UI: superuser Key row and rename dialog for flows and dashboards (live plan with rewritten documents, counts, refusals and warnings, typed confirmation, what the drain waits for and calling it off); applyRename and the socket's renamed remap the live store and refetch at unchanged URLs, editors whose document moved reload so neither undo nor a queued save restores old bindings, a reconnect checks again and its snapshot replaces values; a retry toast for the rename's 503s, autosaves send themselves again; dashboard saves are based on their own versions only; Playwright rename spec, liveStore check, editor and dashboard docs

This commit is contained in:
2026-11-02 22:58:41 +01:00
parent ba4aae390d
commit 3ae5255617
20 changed files with 1313 additions and 38 deletions
@@ -33,6 +33,8 @@ import {
} from "@/components/Common/Dock"
import { devicesQueryOptions } from "@/components/Devices/queries"
import { CanvasTitle } from "@/components/Flow/CanvasTitle"
import { RETRY_LATER } from "@/components/Flow/queries"
import { listenRenames } from "@/components/Flow/useFlowSocket"
import { Button } from "@/components/ui/button"
import {
Tooltip,
@@ -42,10 +44,11 @@ import {
import useCustomToast from "@/hooks/useCustomToast"
import { useIsMobile } from "@/hooks/useMobile"
import { slideUp, transitions } from "@/lib/motion"
import { notify } from "@/lib/notificationStore"
import { appPath } from "@/lib/portal"
import { useShortcuts } from "@/lib/shortcuts"
import { cn } from "@/lib/utils"
import { handleError } from "@/utils"
import { handleError, retryLater } from "@/utils"
import { copyWidget, useCopiedWidget } from "./clipboard"
import { DashboardPalette } from "./DashboardPalette"
import {
@@ -380,9 +383,12 @@ const same = (a: Layout, b: Layout) =>
export function DashboardEditor({
dashboard,
edit,
onReload,
}: {
dashboard: Dashboard
edit: boolean
/** Take the server's document over this one, by remounting. */
onReload: () => void
}) {
const [draft, setDraft] = useState<Dashboard>(dashboard)
// What the API, the routes and the devices know it by; never edited.
@@ -416,13 +422,41 @@ export function DashboardEditor({
// going out with the version it is about to replace.
const inflight = useRef<Promise<void> | null>(null)
// The saved version is what the next save is based on; without following it
// the second save of a session is always a conflict.
// the second save of a session is always a conflict. Only this editor's own
// saves move it: taking a refetched version would let the next save of an
// older draft write over whatever moved the document, a rename included.
const version = useRef(dashboard.version)
const history = useRef<History>({ past: [], future: [], at: 0 })
// The newest local document and whether the server has it yet.
const latest = useRef(dashboard)
const unsaved = useRef(false)
useEffect(() => {
version.current = dashboard.version
}, [dashboard.version])
// As in the flow editor: a rename, or a socket back after one may have gone
// by, and a server copy newer than the one this draft is based on. That
// copy replaces this one, undo history and queued save included.
useEffect(
() =>
listenRenames((event) => {
if (event.type === "rename_waiting") return
// Read directly: a refetch through the cache is cancelled by the
// invalidations the same rename sets off.
DashboardsService.readDashboard({ dashboardId: id, draft: true })
.then((fresh) => {
if ((fresh.version ?? 0) <= (version.current ?? 0)) return
if (event.type !== "renamed" && unsaved.current) return
if (event.type === "renamed" && !event.local)
notify(
`${event.old} is now ${event.new}. The dashboard reloaded with it.${unsaved.current ? " Edits not yet saved were dropped." : ""}`,
"info",
{ id: `rename:${event.op}` },
)
queryClient.setQueryData(dashboardKeys.detail(id, true), fresh)
onReload()
})
.catch(() => {})
}),
[id, queryClient, onReload],
)
useEffect(() => {
if (selected) setFace(selected)
@@ -433,14 +467,28 @@ export function DashboardEditor({
.mutateAsync({ ...next, version: version.current })
.then((saved) => {
version.current = saved.version
if (latest.current === next) unsaved.current = false
})
.catch((error) => {
// Turned away while a rename runs: the newest draft goes again
// shortly, unless an edit has queued it already.
if (!retryLater(error, true)) {
handleError.call(showErrorToast, error as ApiError)
} else if (!timer.current) {
timer.current = setTimeout(() => {
timer.current = null
void store(latest.current)
}, RETRY_LATER)
}
})
.catch((error) => handleError.call(showErrorToast, error as ApiError))
inflight.current = request
return request
}
/** Put a dashboard on the canvas and on its way to the server. */
const apply = (next: Dashboard) => {
latest.current = next
unsaved.current = true
setDraft(next)
if (timer.current) clearTimeout(timer.current)
timer.current = setTimeout(() => {
@@ -1044,6 +1092,7 @@ export function DashboardEditor({
onChange={(changes) => commit({ ...draft, ...changes })}
onDelete={() => remove.mutate()}
onClose={() => setSettingsOpen(false)}
flush={flush}
/>
<DiscardDraftDialog
@@ -6,6 +6,7 @@ import type { MessageInfo, SettingDef, WidgetDef } from "@/client"
import { DEFAULT_RANGE, RANGES } from "@/components/Common/RangePicker"
import { screensOf } from "@/components/Devices/ports"
import { devicesQueryOptions } from "@/components/Devices/queries"
import { RenameDialog } from "@/components/Flow/RenameDialog"
import {
FieldLabel,
IdentitySection,
@@ -49,6 +50,7 @@ import {
SelectValue,
} from "@/components/ui/select"
import { Switch } from "@/components/ui/switch"
import useAuth from "@/hooks/useAuth"
import { cn } from "@/lib/utils"
import { MAX_SERIES, refreshFor } from "./ChartWidget"
import { COLOR_DTYPES, COLOR_FORMATS, colorFormatOf } from "./ColorWidget"
@@ -1922,6 +1924,7 @@ export function DashboardPanel({
onChange,
onDelete,
onClose,
flush,
}: {
open: boolean
dashboard: Dashboard
@@ -1929,8 +1932,12 @@ export function DashboardPanel({
onChange: (changes: Partial<Dashboard>) => void
onDelete: () => void
onClose: () => void
/** Sends the queued edit, before a rename plans on the stored document. */
flush: () => Promise<void>
}) {
const [confirmOpen, setConfirmOpen] = useState(false)
const [renameOpen, setRenameOpen] = useState(false)
const { user } = useAuth()
const canvas = canvasOf(dashboard)
const round = isRound(dashboard)
const theme = settingOf(dashboard, "theme")
@@ -1987,6 +1994,9 @@ export function DashboardPanel({
name={dashboard.name}
id={dashboard.id!}
help="The key is the dashboard's name. The id never changes: this page's address, the view link, devices and the API use it."
onRename={
user?.is_superuser ? () => setRenameOpen(true) : undefined
}
/>
<PanelSection
@@ -2223,6 +2233,15 @@ export function DashboardPanel({
</div>
</SidePanel>
<RenameDialog
kind="dashboard"
id={dashboard.id!}
name={dashboard.name}
open={renameOpen}
onOpenChange={setRenameOpen}
flush={flush}
/>
<Dialog open={confirmOpen} onOpenChange={setConfirmOpen}>
<DialogContent>
<DialogHeader>
@@ -49,6 +49,7 @@ export const messageHistoryQueryOptions = (message: string) => ({
export function useSaveDashboard(id: string) {
const queryClient = useQueryClient()
return useMutation({
meta: { autosave: true },
mutationFn: (body: DashboardDef_Input) =>
DashboardsService.saveDashboard({ dashboardId: id, requestBody: body }),
onSuccess: (saved) => {