With STATUS_CHECKS=true the server requests the address of every link of the open dashboard once a minute and the tile shows a green dot or a red ring. Off by default: users choose the addresses, so a user could find out which hosts and ports the server reaches.
147 lines
4.7 KiB
TypeScript
147 lines
4.7 KiB
TypeScript
import { createContext, useContext, useEffect, useRef, useState } from "react"
|
|
import { useDndContext } from "@dnd-kit/core"
|
|
import { useSortable } from "@dnd-kit/sortable"
|
|
import { CSS } from "@dnd-kit/utilities"
|
|
import { Pencil } from "lucide-react"
|
|
import { iconSources } from "@/lib/icons"
|
|
import { cn } from "@/vendor/collabry/lib/cn"
|
|
|
|
export type HomeLink = {
|
|
id: string
|
|
cardId: string
|
|
name: string
|
|
url: string
|
|
icon: string | null
|
|
/** the library's light version of the icon, when the link has no icon of its own */
|
|
lightIcon: string | null
|
|
}
|
|
|
|
/** Per link id: does its address answer? No entry: not checked (yet). */
|
|
export const StatusContext = createContext<Partial<Record<string, boolean>>>({})
|
|
|
|
type IconProps = { link: HomeLink; className?: string }
|
|
|
|
/** Starts the fallback chain again when the link changes. */
|
|
export function LinkIcon(props: IconProps) {
|
|
const { id, name, url, icon, lightIcon } = props.link
|
|
return <Icon key={[id, name, url, icon, lightIcon].join("|")} {...props} />
|
|
}
|
|
|
|
function Icon({ link, className }: IconProps) {
|
|
const sources = iconSources(link)
|
|
const [index, setIndex] = useState(0)
|
|
const img = useRef<HTMLImageElement>(null)
|
|
const next = () => setIndex((i) => i + 1)
|
|
|
|
// an image that failed before hydration never fires onError
|
|
useEffect(() => {
|
|
if (img.current?.complete && img.current.naturalWidth === 0) next()
|
|
}, [index])
|
|
|
|
if (index >= sources.length)
|
|
return (
|
|
<span
|
|
className={cn(
|
|
"grid place-items-center rounded-2xl bg-white/15 font-serif text-2xl text-white uppercase",
|
|
className
|
|
)}
|
|
>
|
|
{link.name[0]}
|
|
</span>
|
|
)
|
|
return (
|
|
<img
|
|
ref={img}
|
|
src={sources[index]}
|
|
alt=""
|
|
referrerPolicy="no-referrer"
|
|
draggable={false}
|
|
onError={next}
|
|
className={cn("rounded-2xl object-contain drop-shadow-lg", className)}
|
|
/>
|
|
)
|
|
}
|
|
|
|
const tile =
|
|
"flex w-24 flex-col items-center gap-2.5 rounded-2xl px-2 py-3 text-base text-white/90"
|
|
|
|
/** Icon and name. Also what follows the pointer while a link is dragged. */
|
|
export function TileFace({
|
|
link,
|
|
className,
|
|
}: {
|
|
link: HomeLink
|
|
className?: string
|
|
}) {
|
|
return (
|
|
<span className={cn(tile, className)}>
|
|
<LinkIcon link={link} className="size-14" />
|
|
<span className="line-clamp-2 w-full text-center leading-4.5 break-words [text-shadow:0_1px_8px_rgb(0_0_0/0.6)]">
|
|
{link.name}
|
|
</span>
|
|
</span>
|
|
)
|
|
}
|
|
|
|
/** A link in a card. Drag it (mouse, long press, or Space and the arrow keys) to move it. */
|
|
export function LinkTile({
|
|
link,
|
|
onEdit,
|
|
}: {
|
|
link: HomeLink
|
|
onEdit: () => void
|
|
}) {
|
|
// while a card is dragged, the arrow keys go from card to card, not through the links
|
|
const cardDragged = !!useDndContext().active?.data.current?.card
|
|
const {
|
|
attributes,
|
|
listeners,
|
|
setNodeRef,
|
|
transform,
|
|
transition,
|
|
isDragging,
|
|
} = useSortable({ id: link.id, disabled: { droppable: cardDragged } })
|
|
const up = useContext(StatusContext)[link.id]
|
|
return (
|
|
<div
|
|
ref={setNodeRef}
|
|
style={{ transform: CSS.Translate.toString(transform), transition }}
|
|
// flex: the link fills the row's height, also next to a two line name
|
|
className={cn("group relative flex", isDragging && "opacity-30")}
|
|
>
|
|
<a
|
|
href={link.url}
|
|
rel="noreferrer"
|
|
// the browser's own link drag would win over ours
|
|
draggable={false}
|
|
aria-describedby={attributes["aria-describedby"]}
|
|
{...listeners}
|
|
className="block touch-manipulation rounded-2xl transition-colors outline-none select-none [-webkit-touch-callout:none] hover:bg-white/10 focus-visible:bg-white/10 focus-visible:ring-2 focus-visible:ring-white/60"
|
|
>
|
|
<TileFace link={link} />
|
|
{up !== undefined && (
|
|
// at the lower right corner of the icon. Filled green or a red ring:
|
|
// the shape differs too, not only the color.
|
|
<span
|
|
title={up ? "Reachable" : "Not reachable"}
|
|
className={cn(
|
|
"absolute top-[58px] right-[18px] size-3 rounded-full border-2",
|
|
up ? "border-black/70 bg-success" : "border-danger bg-black/80"
|
|
)}
|
|
>
|
|
{!up && <span className="sr-only">, not reachable</span>}
|
|
</span>
|
|
)}
|
|
</a>
|
|
<button
|
|
type="button"
|
|
aria-label={`Edit ${link.name}`}
|
|
onClick={onEdit}
|
|
className="absolute top-1 right-1 grid size-6 place-items-center rounded-full bg-black/70 text-white opacity-0 transition-opacity outline-none group-hover:opacity-100 focus-visible:opacity-100 focus-visible:ring-2 focus-visible:ring-white/60 pointer-coarse:opacity-80"
|
|
>
|
|
<Pencil className="size-3" />
|
|
</button>
|
|
</div>
|
|
)
|
|
}
|