| 1 | /** |
| 2 | * Customize (docs.g1t.sh/guides/customize/): what shapes how a workspace's |
| 3 | * agents work, in three tabs, Skills, Connectors and Plugins, each with |
| 4 | * what the workspace has (Yours) and what it can add (Discover), from the |
| 5 | * open plugin ecosystem as it is. Owners add; anyone else asks, and the |
| 6 | * agents service keeps the request. It is part of Apps, not a mode of its |
| 7 | * own: the Apps page and ⌘K lead here, and the Marketplace's old addresses |
| 8 | * redirect. |
| 9 | * |
| 10 | * This layout holds the heading, the tabs, the Yours | Discover switch and |
| 11 | * the search (both kept in the address), the owners' Add menu, and the |
| 12 | * viewer's requests, which every tab reads to say what is waiting. |
| 13 | */ |
| 14 | import { Blocks, FileUp, Inbox, PenLine, Plug, Plus, Search, Sparkles, Store } from "lucide-react"; |
| 15 | import { type ReactNode, useId, useState } from "react"; |
| 16 | import { Link, NavLink, Outlet, data, useLocation, useRouteLoaderData, useSearchParams } from "react-router"; |
| 17 | |
| 18 | import type { InstallRequests } from "@g1t/contracts"; |
| 19 | import { CUSTOMIZE, CUSTOMIZE_SECTIONS, type CustomizeSection, customizePath } from "@g1t/contracts/customize"; |
| 20 | |
| 21 | import type { Route } from "./+types/layout"; |
| 22 | import { useDialogFetcher } from "../../../components/agents/dialogs"; |
| 23 | import { skillsPath } from "../../../components/agents/skills"; |
| 24 | import { AddMarketplaceDialog } from "../../../components/customize"; |
| 25 | import { ButtonLink } from "../../../components/ui"; |
| 26 | import { Badge } from "../../../components/ui/badge"; |
| 27 | import { Button } from "../../../components/ui/button"; |
| 28 | import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "../../../components/ui/dropdown-menu"; |
| 29 | import { Input } from "../../../components/ui/input"; |
| 30 | import { TabStrip } from "../../../components/ui/tab-strip"; |
| 31 | import { ToggleGroup, ToggleGroupItem } from "../../../components/ui/toggle-group"; |
| 32 | import { connectorNewPath, nextParams, openRequests, readQuery, readView, tabOf } from "../../../lib/customize"; |
| 33 | import { page } from "../../../lib/meta"; |
| 34 | import { workspaceAgents } from "../../../lib/services.server"; |
| 35 | import { requireUser, roleIn } from "../../../lib/session.server"; |
| 36 | |
| 37 | export { shouldRevalidate } from "../../../lib/customize.server"; |
| 38 | |
| 39 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 40 | return page(args, { title: `Customize · ${params.owner} · g1t` }); |
| 41 | } |
| 42 | |
| 43 | export type CustomizeData = { |
| 44 | slug: string; |
| 45 | name: string; |
| 46 | owner: boolean; |
| 47 | username: string; |
| 48 | /** The viewer's requests (an owner's: everyone's); null when the agents service did not answer. */ |
| 49 | requests: InstallRequests | null; |
| 50 | }; |
| 51 | |
| 52 | export async function loader({ params, context, request }: Route.LoaderArgs): Promise<CustomizeData> { |
| 53 | const viewer = requireUser(context, request); |
| 54 | const slug = params.owner.toLowerCase(); |
| 55 | const role = roleIn(viewer, slug); |
| 56 | if (!role) throw data(null, { status: 404 }); |
| 57 | const membership = viewer.workspaces?.find((m) => m.slug === slug); |
| 58 | const requests = await workspaceAgents |
| 59 | .installRequests(slug, viewer) |
| 60 | .then((result) => (result.ok ? result.value : null)) |
| 61 | .catch(() => null); |
| 62 | return { slug, name: membership?.name?.trim() || slug, owner: role === "owner", username: viewer.username, requests }; |
| 63 | } |
| 64 | |
| 65 | /** Customize's own data, from any of its pages. */ |
| 66 | export function useCustomize(): CustomizeData { |
| 67 | return useRouteLoaderData("routes/workspace/customize/layout") as CustomizeData; |
| 68 | } |
| 69 | |
| 70 | const ICONS: Record<CustomizeSection, ReactNode> = { skills: <Sparkles size={14} />, connectors: <Plug size={14} />, plugins: <Blocks size={14} /> }; |
| 71 | |
| 72 | export default function CustomizeLayout({ loaderData }: Route.ComponentProps) { |
| 73 | const { slug, name, owner, requests } = loaderData; |
| 74 | const waiting = requests ? openRequests(requests.requests).length : 0; |
| 75 | const { pathname } = useLocation(); |
| 76 | const [params] = useSearchParams(); |
| 77 | const view = readView(params); |
| 78 | const tab = tabOf(pathname); |
| 79 | return ( |
| 80 | <main className="mx-auto max-w-6xl px-4 py-8 sm:px-8 sm:py-10"> |
| 81 | <div className="flex flex-wrap items-end justify-between gap-4"> |
| 82 | <div className="min-w-0"> |
| 83 | <h1 className="text-2xl font-semibold tracking-tight">Customize</h1> |
| 84 | <p className="mt-1 max-w-2xl text-sm text-muted"> |
| 85 | What shapes how {name}'s agents work: the skills they know, the connectors they reach, and plugins from any marketplace in the open plugin format.{" "} |
| 86 | {owner ? "As an owner, you add them for everyone." : "Owners add them; ask one for what you need, and they'll hear about it."} |
| 87 | </p> |
| 88 | </div> |
| 89 | <div className="flex shrink-0 flex-wrap items-center gap-2"> |
| 90 | <ButtonLink to={customizePath("marketplaces", slug)} variant="ghost" size="sm" prefetch="intent"> |
| 91 | <Store size={14} /> |
| 92 | Marketplaces |
| 93 | </ButtonLink> |
| 94 | <ButtonLink to={customizePath("requests", slug)} variant="ghost" size="sm" prefetch="intent"> |
| 95 | <Inbox size={14} /> |
| 96 | {owner ? "Requests" : "Your requests"} |
| 97 | {waiting > 0 && <Badge tone="warn">{waiting}</Badge>} |
| 98 | </ButtonLink> |
| 99 | {owner && <AddMenu slug={slug} />} |
| 100 | </div> |
| 101 | </div> |
| 102 | |
| 103 | <TabStrip label="Customize" className="mt-6 gap-1 border-b border-line"> |
| 104 | {CUSTOMIZE_SECTIONS.map((section) => { |
| 105 | const to = customizePath(section, slug); |
| 106 | return ( |
| 107 | <NavLink |
| 108 | key={section} |
| 109 | to={view === "discover" ? `${to}?view=discover` : to} |
| 110 | prefetch="intent" |
| 111 | data-active={pathname === to || pathname.startsWith(`${to}/`)} |
| 112 | className={({ isActive }) => |
| 113 | `-mb-px flex items-center gap-2 border-b-2 px-3 pb-3 text-sm whitespace-nowrap transition-colors ${isActive ? "border-accent font-medium text-fg" : "border-transparent text-muted hover:text-fg"}` |
| 114 | } |
| 115 | > |
| 116 | {ICONS[section]} |
| 117 | {CUSTOMIZE[section].title} |
| 118 | </NavLink> |
| 119 | ); |
| 120 | })} |
| 121 | </TabStrip> |
| 122 | |
| 123 | {tab && <Toolbar tab={tab} />} |
| 124 | |
| 125 | <div className={tab ? "mt-6" : "mt-8"}> |
| 126 | <Outlet /> |
| 127 | </div> |
| 128 | </main> |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | /** Yours | Discover and the search, each kept in the address so a view can be shared. */ |
| 133 | function Toolbar({ tab }: { tab: CustomizeSection }) { |
| 134 | const [params, setParams] = useSearchParams(); |
| 135 | const view = readView(params); |
| 136 | const [typed, setTyped] = useState(readQuery(params)); |
| 137 | const id = useId(); |
| 138 | const go = (changes: Record<string, string | null>) => setParams(nextParams(params, changes), { replace: true, preventScrollReset: true }); |
| 139 | return ( |
| 140 | <div className="mt-5 flex flex-wrap items-center gap-3"> |
| 141 | <ToggleGroup |
| 142 | type="single" |
| 143 | variant="outline" |
| 144 | value={view} |
| 145 | aria-label="What to show" |
| 146 | onValueChange={(next) => { |
| 147 | if (next && next !== view) go({ view: next }); |
| 148 | }} |
| 149 | > |
| 150 | <ToggleGroupItem value="yours">Yours</ToggleGroupItem> |
| 151 | <ToggleGroupItem value="discover">Discover</ToggleGroupItem> |
| 152 | </ToggleGroup> |
| 153 | <div className="relative min-w-0 grow basis-60 sm:max-w-sm"> |
| 154 | <label htmlFor={`${id}-search`} className="sr-only"> |
| 155 | Search {CUSTOMIZE[tab].title.toLowerCase()} |
| 156 | </label> |
| 157 | <Search size={15} className="pointer-events-none absolute top-1/2 left-3 -translate-y-1/2 text-faint" aria-hidden /> |
| 158 | <Input |
| 159 | id={`${id}-search`} |
| 160 | type="search" |
| 161 | value={typed} |
| 162 | onChange={(event) => { |
| 163 | setTyped(event.target.value); |
| 164 | go({ q: event.target.value.trim() || null }); |
| 165 | }} |
| 166 | placeholder={`Search ${CUSTOMIZE[tab].title.toLowerCase()}`} |
| 167 | autoComplete="off" |
| 168 | className="pl-9" |
| 169 | /> |
| 170 | </div> |
| 171 | </div> |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | /** The owners' Add menu: a skill of their own, a connector by address, or a marketplace to read plugins from. */ |
| 176 | function AddMenu({ slug }: { slug: string }) { |
| 177 | const marketplace = useDialogFetcher("add-marketplace"); |
| 178 | return ( |
| 179 | <> |
| 180 | <DropdownMenu> |
| 181 | <DropdownMenuTrigger asChild> |
| 182 | <Button type="button" variant="accent" size="sm"> |
| 183 | <Plus size={14} /> |
| 184 | Add |
| 185 | </Button> |
| 186 | </DropdownMenuTrigger> |
| 187 | <DropdownMenuContent align="end"> |
| 188 | <DropdownMenuItem asChild> |
| 189 | <Link to={skillsPath(slug, undefined, "/new")}> |
| 190 | <PenLine /> |
| 191 | Write a skill |
| 192 | </Link> |
| 193 | </DropdownMenuItem> |
| 194 | <DropdownMenuItem asChild> |
| 195 | <Link to={skillsPath(slug, undefined, "/import")}> |
| 196 | <FileUp /> |
| 197 | Import a skill |
| 198 | </Link> |
| 199 | </DropdownMenuItem> |
| 200 | <DropdownMenuSeparator /> |
| 201 | <DropdownMenuItem asChild> |
| 202 | <Link to={connectorNewPath(slug)}> |
| 203 | <Plug /> |
| 204 | Add a connector |
| 205 | </Link> |
| 206 | </DropdownMenuItem> |
| 207 | <DropdownMenuItem onSelect={() => marketplace.setOpen(true)}> |
| 208 | <Store /> |
| 209 | Add a marketplace |
| 210 | </DropdownMenuItem> |
| 211 | </DropdownMenuContent> |
| 212 | </DropdownMenu> |
| 213 | <AddMarketplaceDialog slug={slug} dialog={marketplace} /> |
| 214 | </> |
| 215 | ); |
| 216 | } |