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>>({}) 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 } function Icon({ link, className }: IconProps) { const sources = iconSources(link) const [index, setIndex] = useState(0) const img = useRef(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 ( {link.name[0]} ) return ( ) } 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 ( {link.name} ) } /** 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 (
{up !== undefined && ( // at the lower right corner of the icon. Filled green or a red ring: // the shape differs too, not only the color. {!up && , not reachable} )}
) }