Add a colour-wheel widget to the dashboard
A custom hue ring — a conic gradient, not a canvas — with saturation and brightness sliders beside or under it depending on the tile's shape, sized for a wall panel and reachable from a keyboard. It publishes [h, s, v] by default, which is what the reference installation's DMX encoders read, and `format` switches that to [r, g, b] or "#rrggbb". `usePublish` moves to its own module so a widget in a file of its own can reach it without importing `widgets.tsx` back.
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
import type { ApiError, WidgetDef } from "@/client"
|
||||
import { useLiveValue } from "@/components/Flow/liveStore"
|
||||
import useCustomToast from "@/hooks/useCustomToast"
|
||||
import { handleError } from "@/utils"
|
||||
// The transmit overlay's rule lives beside the other widget CSS, and CSS is
|
||||
// chunked per entry — so the sheet is pulled in wherever the pulse is drawn.
|
||||
import "./dashboard.css"
|
||||
import { usePublishMessage } from "./queries"
|
||||
|
||||
/**
|
||||
* How long a control shows what it sent before falling back to the engine.
|
||||
*
|
||||
* ponytail: a flat 3 s rather than anything the engine tells us. A publish the
|
||||
* server takes but nothing ever echoes — a message no flow consumes — would
|
||||
* otherwise leave the tile holding a value that is not the truth, forever.
|
||||
*/
|
||||
const HOLD_MS = 3000
|
||||
|
||||
/** Whether what came back over the socket is what this control sent. */
|
||||
function confirms(live: unknown, sent: unknown): boolean {
|
||||
// Readings are scalars nearly always; a dropdown may carry a record and a
|
||||
// colour wheel an array, and comparing those as text is cheaper than a deep
|
||||
// walk for the same answer.
|
||||
return live === sent || JSON.stringify(live) === JSON.stringify(sent)
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishing, with the value shown as sent until the engine confirms it.
|
||||
*
|
||||
* A control publishes over HTTP and reads the result back over the socket, so
|
||||
* between the two the live value is still the old one — a handle let go of
|
||||
* would snap back to it. The hold ends when the echo matches, when the publish
|
||||
* is refused, or on `HOLD_MS`; success is silent, because the echo is the
|
||||
* confirmation.
|
||||
*
|
||||
* Its own module rather than `widgets.tsx`, which every widget file is
|
||||
* imported *by*: a control drawn in a file of its own can only reach this
|
||||
* without closing that circle if it does not sit there.
|
||||
*/
|
||||
export function usePublish(widget: WidgetDef, dashboard: string) {
|
||||
const cfg = (widget.config ?? {}) as Record<string, unknown>
|
||||
const target = cfg.target == null ? "" : String(cfg.target)
|
||||
const publish = usePublishMessage()
|
||||
const live = useLiveValue(target || undefined)
|
||||
const { showErrorToast } = useCustomToast()
|
||||
// Boxed: holding `false` or `null` is not the same as holding nothing.
|
||||
const [held, setHeld] = useState<{ value: unknown } | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!held) return
|
||||
const timer = setTimeout(() => setHeld(null), HOLD_MS)
|
||||
return () => clearTimeout(timer)
|
||||
}, [held])
|
||||
|
||||
useEffect(() => {
|
||||
if (held && confirms(live?.value, held.value)) setHeld(null)
|
||||
}, [held, live])
|
||||
|
||||
return {
|
||||
target,
|
||||
/** What the control draws: what it sent, until the engine answers. */
|
||||
value: held ? held.value : live?.value,
|
||||
send: (value: unknown) => {
|
||||
if (!target) return
|
||||
setHeld({ value })
|
||||
publish.mutate(
|
||||
{
|
||||
name: target,
|
||||
value,
|
||||
dashboard,
|
||||
widget: widget.id,
|
||||
label: widget.title || widget.id,
|
||||
kind: widget.type,
|
||||
},
|
||||
{
|
||||
onError: (error) => {
|
||||
// Back to the engine's own value, and say which message refused it
|
||||
// — a panel showing several controls cannot tell them apart.
|
||||
setHeld(null)
|
||||
handleError.call(
|
||||
(detail: string) => showErrorToast(`${target}: ${detail}`),
|
||||
error as ApiError,
|
||||
)
|
||||
},
|
||||
},
|
||||
)
|
||||
},
|
||||
/**
|
||||
* The in-flight pulse, drawn over the whole tile.
|
||||
*
|
||||
* Absolutely positioned and inert, so it neither resizes the widget nor
|
||||
* moves anything around it. Every control renders it; the frame is what it
|
||||
* hangs off, which is why it must not be put inside a child that positions
|
||||
* itself.
|
||||
*/
|
||||
pulse: publish.isPending ? (
|
||||
<span aria-hidden className="widget-transmit" />
|
||||
) : null,
|
||||
pending: publish.isPending,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user