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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user