|
| 1 | +// The Bot agency's roles (spec 26.5): filters, search and the cards, or one |
| 2 | +// role in full after "Learn more". Used in the window the crew's button |
| 3 | +// opens and, for a crew with only its chief, in the page itself. |
| 4 | + |
| 5 | +import { useState } from "react"; |
| 6 | +import { useT } from "../../i18n"; |
| 7 | +import { AVATAR_PALETTE, type BotTemplate, type Crew } from "../../lib/protocol.gen"; |
| 8 | +import { useApp } from "../../store/context"; |
| 9 | +import { AgencyCard } from "./AgencyCard"; |
| 10 | +import { RoleDetail } from "./RoleDetail"; |
| 11 | +import { roleText } from "./roleText"; |
| 12 | +import { useAddRole } from "./useAddRole"; |
| 13 | +import { useCatalog } from "./useCatalog"; |
| 14 | + |
| 15 | +type Category = BotTemplate["category"] | "all"; |
| 16 | + |
| 17 | +const CATEGORIES: Category[] = ["all", "code", "design", "content", "research", "business"]; |
| 18 | + |
| 19 | +/** A fixed color per role, so the mascots tell the cards apart. */ |
| 20 | +const colorOf = (index: number) => AVATAR_PALETTE[index % AVATAR_PALETTE.length] ?? "#FF7A59"; |
| 21 | + |
| 22 | +export function AgencyGrid({ crew, onLeave }: { crew: Crew; onLeave?: () => void }) { |
| 23 | + const t = useT(); |
| 24 | + const { roles, failed } = useCatalog(); |
| 25 | + const { add, added, busy } = useAddRole(crew); |
| 26 | + const selectBot = useApp((state) => state.selectBot); |
| 27 | + const setPanel = useApp((state) => state.setPanel); |
| 28 | + const [category, setCategory] = useState<Category>("all"); |
| 29 | + const [query, setQuery] = useState(""); |
| 30 | + const [open, setOpen] = useState<string | null>(null); |
| 31 | + |
| 32 | + if (failed) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + if (roles === null) { |
| 36 | + return <p className="text-muted text-sm">{t.catalog.loading}</p>; |
| 37 | + } |
| 38 | + |
| 39 | + const customize = (botId: string) => { |
| 40 | + selectBot(botId); |
| 41 | + setPanel(botId, "details"); |
| 42 | + onLeave?.(); |
| 43 | + }; |
| 44 | + |
| 45 | + const opened = roles.find((role) => role.id === open); |
| 46 | + if (opened) { |
| 47 | + const bot = added[opened.id]; |
| 48 | + return ( |
| 49 | + <RoleDetail |
| 50 | + template={opened} |
| 51 | + text={roleText(t, opened)} |
| 52 | + added={bot} |
| 53 | + busy={busy === opened.id} |
| 54 | + onAdd={() => add(opened)} |
| 55 | + onBack={() => setOpen(null)} |
| 56 | + onCustomize={() => bot && customize(bot.id)} |
| 57 | + /> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + const needle = query.trim().toLowerCase(); |
| 62 | + const shown = roles.filter((role) => { |
| 63 | + if (category !== "all" && role.category !== category) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + const text = roleText(t, role); |
| 67 | + return !needle || `${text.name} ${text.summary} ${text.role}`.toLowerCase().includes(needle); |
| 68 | + }); |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className="flex flex-col gap-3"> |
| 72 | + <div className="flex flex-wrap items-center gap-2"> |
| 73 | + <input |
| 74 | + type="search" |
| 75 | + value={query} |
| 76 | + onChange={(event) => setQuery(event.target.value)} |
| 77 | + aria-label={t.catalog.search} |
| 78 | + placeholder={t.catalog.search} |
| 79 | + className="h-8 w-48 rounded-lg border border-line-strong bg-canvas px-2.5 text-ink text-sm outline-none placeholder:text-muted focus:border-accent" |
| 80 | + /> |
| 81 | + <fieldset |
| 82 | + aria-label={t.catalog.filter} |
| 83 | + className="m-0 flex min-w-0 flex-wrap gap-1.5 border-0 p-0" |
| 84 | + > |
| 85 | + {CATEGORIES.map((id) => ( |
| 86 | + <button |
| 87 | + key={id} |
| 88 | + type="button" |
| 89 | + aria-pressed={category === id} |
| 90 | + onClick={() => setCategory(id)} |
| 91 | + className={`h-7 rounded-full border px-2.5 font-medium text-xs transition-colors ${ |
| 92 | + category === id |
| 93 | + ? "border-ink bg-ink text-canvas" |
| 94 | + : "border-line-strong text-ink-soft hover:bg-sunken hover:text-ink" |
| 95 | + }`} |
| 96 | + > |
| 97 | + {t.catalog.categories[id]} |
| 98 | + </button> |
| 99 | + ))} |
| 100 | + </fieldset> |
| 101 | + </div> |
| 102 | + {shown.length === 0 ? ( |
| 103 | + <p className="py-6 text-center text-muted text-sm">{t.catalog.none}</p> |
| 104 | + ) : ( |
| 105 | + <ul className="grid grid-cols-[repeat(auto-fill,minmax(230px,1fr))] gap-3"> |
| 106 | + {shown.map((role) => { |
| 107 | + const bot = added[role.id]; |
| 108 | + return ( |
| 109 | + <AgencyCard |
| 110 | + key={role.id} |
| 111 | + template={role} |
| 112 | + text={roleText(t, role)} |
| 113 | + color={colorOf(roles.indexOf(role))} |
| 114 | + added={bot} |
| 115 | + busy={busy === role.id} |
| 116 | + onAdd={() => add(role)} |
| 117 | + onLearnMore={() => setOpen(role.id)} |
| 118 | + onCustomize={() => bot && customize(bot.id)} |
| 119 | + /> |
| 120 | + ); |
| 121 | + })} |
| 122 | + </ul> |
| 123 | + )} |
| 124 | + </div> |
| 125 | + ); |
| 126 | +} |
0 commit comments