import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { SearchInput } from "@/components/ui/input"; import { LanguageSwitcher } from "@/components/LanguageSwitcher"; import { SegmentedControl } from "@/components/ui/segmented-control"; import { ToggleButton } from "@/components/ui/toggle-button"; import { ThemeProvider, useTheme } from "@/hooks/useTheme"; import { cn } from "@/lib/utils"; import { foundationExamples } from "./foundations"; import { sharedExamples } from "./shared-views"; import { settingsExamples } from "./settings"; import { providerExamples } from "./providers"; import { conversationExamples } from "./conversation"; import { shellExamples } from "./shell"; import { standaloneExamples } from "./standalone"; import { remoteExamples } from "./remote"; import { contentExamples } from "./content"; import { runtimeSettingsExamples } from "./runtime-settings"; import { channelExamples, withChannelEntries } from "./channels"; import { feishuWhatsAppChannelExamples } from "./channels-feishu-whatsapp"; import { linearChannelExamples } from "./channels-linear"; import { weixinChannelExamples } from "./channels-weixin"; import { foundationPath, type ComponentSource } from "./inventory"; import { useGalleryTranslation } from "./i18n"; export const examples = [...foundationExamples, ...sharedExamples, ...settingsExamples, ...providerExamples, ...conversationExamples, ...shellExamples, ...standaloneExamples, ...remoteExamples, ...contentExamples, ...runtimeSettingsExamples, ...channelExamples, ...withChannelEntries([...feishuWhatsAppChannelExamples, ...linearChannelExamples, ...weixinChannelExamples])]; const examplesByPath = new Map(examples.flatMap((example) => example.files.map((file) => [file, example] as const))); const groupKeys: Record = { All: "all", "Shared views & shell": "shared", Icons: "icons", Hosts: "hosts", Settings: "settings", "Settings / shared": "settingsShared", Conversation: "conversation", Foundations: "foundations", Workbench: "workbench", }; function selectedPath(): string { return new URLSearchParams(window.location.hash.slice(1)).get("component") ?? `${foundationPath}button.tsx`; } export function Gallery() { const { theme, setTheme } = useTheme(); const { t } = useGalleryTranslation(); const [inventory, setInventory] = useState([]); const [error, setError] = useState(false); const [loading, setLoading] = useState(true); const [reload, setReload] = useState(0); const [query, setQuery] = useState(""); const [group, setGroup] = useState("All"); const [selected, setSelected] = useState(selectedPath); const [view, setView] = useState<"previews" | "all">(() => examplesByPath.has(selectedPath()) ? "previews" : "all"); useEffect(() => { const controller = new AbortController(); setLoading(true); setError(false); void fetch("/__nanobot_components", { signal: controller.signal }) .then(async (response) => { if (!response.ok) throw new Error(`Inventory request failed: ${response.status}`); const data: ComponentSource[] = await response.json(); setInventory(data); }) .catch(() => { if (!controller.signal.aborted) setError(true); }) .finally(() => { if (!controller.signal.aborted) setLoading(false); }); return () => controller.abort(); }, [reload]); useEffect(() => { const change = () => { const next = selectedPath(); setSelected(next); if (!examplesByPath.has(next)) setView("all"); }; window.addEventListener("hashchange", change); return () => window.removeEventListener("hashchange", change); }, []); useEffect(() => { document.title = t("ui.pageTitle"); }, [t]); const exampleTitle = (example: (typeof examples)[number]) => t(example.title, example.titleValues); const groupLabel = (name: string): string => { return name.startsWith("Channels / ") ? t("ui.groups.channel", { channel: name.slice("Channels / ".length) }) : t(`ui.groups.${groupKeys[name]}`); }; const previewCount = inventory.filter((item) => examplesByPath.has(item.path)).length; const browsable = view === "previews" ? inventory.filter((item) => examplesByPath.has(item.path)) : inventory; const groups = ["All", ...new Set(browsable.map((item) => item.group))]; const search = query.trim().toLowerCase(); const filtered = browsable.filter((item) => { const example = examplesByPath.get(item.path); return (group === "All" || item.group === group) && `${item.path} ${item.exports.join(" ")} ${example ? exampleTitle(example) : ""}`.toLowerCase().includes(search); }); const current = browsable.find((item) => item.path === selected) ?? browsable[0]; const example = current && examplesByPath.get(current.path); const relatedExamples = current && !example ? examples.filter((item) => item.files.some((file) => current.usedBy.includes(file) || inventory.find((source) => source.path === file)?.usedBy.includes(current.path))) : []; return

{t("ui.title")}

{t("ui.subtitle")}

{error ?
{t("ui.inventoryFailure")}
: null} {current ?

{groupLabel(current.group)}

{example ? exampleTitle(example) : current.name}

{current.path}

{example ? <>

{t(example.description)}

{t("ui.usage")}

{t(example.options)}

{example.code}
{example.files.length > 1 ?

{t("ui.composedModules", { modules: example.files.map((file) => file.split("/").pop()).join(", ") })}

: null}
:

{t("ui.sourceOnlyTitle")}

{t("ui.sourceOnlyDescription")}

{relatedExamples.length > 0 ?

{t("ui.relatedPreviews")}

: null}
}

{t("ui.exports")}

{current.exports.join(", ") || t("ui.internalModule")}

{t("ui.references")}

{t("ui.referencesDescription")}

{current.usedBy.length ?
    {current.usedBy.map((file) =>
  • {file}
  • )}
:

{t("ui.noReferences")}

}
{t("ui.sourceCode")}
{current.source}
: !error ?

{t(loading ? "ui.loadingComponents" : "ui.noComponentFiles")}

: null}
; }