SPB Git

Repository Public

nodes.tsx 1.3 KB 23 lines tsx

1"use client";2import Link from "next/link";3import { useMemo } from "react";4import { useApiList } from "@/api/query";5import { useSession } from "@/shell/session";6import type { ComboOption } from "@/ui";78/** Nodes as combobox options (alias shown, id as value). Empty while the endpoint is unavailable. */9export function useNodeOptions(): { options: ComboOption[]; byId: Map<string, string>; error: ReturnType<typeof useApiList>["error"]; loading: boolean } {10  const { can } = useSession();11  const list = useApiList("/nodes", { query: { limit: 200, sort: "alias" } }, { enabled: can("nodes.read") });12  return useMemo(() => {13    const options = list.items.map((n) => ({ value: n.id, label: n.alias, hint: [n.site, n.role].filter(Boolean).join(" · "), keywords: `${n.hostname} ${n.tags.join(" ")}` }));14    return { options, byId: new Map(list.items.map((n) => [n.id, n.alias])), error: list.error, loading: list.isLoading };15  }, [list.items, list.error, list.isLoading]);16}1718/** Node alias (monospace) linking to its detail screen. */19export function NodeLink({ id, alias }: { id?: string | undefined; alias?: string | undefined }) {20  if (!id) return <span className="mono">{alias ?? "—"}</span>;21  return <Link href={`/nodes/${id}`} className="mono">{alias ?? id.slice(0, 8)}</Link>;22}23
Files main
Menu

Theme