Skip to content
130 linesCodeBlameRaw
1import { Lock, Plus, Search } from "lucide-react";
2import { data, useSearchParams, type ShouldRevalidateFunctionArgs } from "react-router";
3
4import { type Team, mayCreateTeams } from "@g1t/contracts";
5
6import type { Route } from "./+types/teams";
7import { page } from "../../lib/meta";
8import { TeamRow } from "../../components/teams";
9import { ButtonLink, EmptyState, notACredential } from "../../components/ui";
10import { Button } from "../../components/ui/button";
11import { Card } from "../../components/ui/card";
12import { filterTeams, splitTeams } from "../../lib/teams";
13import { agentsOn } from "../../lib/people";
14import { identity, workspaceAgents } from "../../lib/services.server";
15import { getViewer, roleIn, unwrap } from "../../lib/session.server";
16
17export function meta({ params, ...args }: Route.MetaArgs) {
18 return page(args, { title: `Teams · ${params.owner} · g1t` });
19}
20
21export async function loader({ params, context }: Route.LoaderArgs) {
22 const viewer = getViewer(context);
23 // A workspace's teams are its members' business.
24 const role = roleIn(viewer, params.owner);
25 if (!role) throw data(null, { status: 404 });
26 const [teams, workspace, directory, agents] = await Promise.all([
27 identity.listTeams(viewer, params.owner).then(unwrap),
28 identity.getWorkspace(params.owner).catch(() => null),
29 // Which agents are on each team: its agent members that are still here.
30 identity.peopleDirectory(viewer, params.owner).catch(() => null),
31 workspaceAgents.list(params.owner, viewer!).catch(() => null),
32 ]);
33 const listed = agents?.ok ? agents.value : [];
34 const agentCounts = Object.fromEntries(
35 (directory?.ok ? directory.value.teams : []).map((team) => [team.slug, agentsOn(team, listed).length]),
36 );
37 // Who may create one is the workspace's to say (its settings).
38 return { teams, agentCounts, slug: params.owner.toLowerCase(), canCreate: mayCreateTeams(workspace?.teamCreation, role) };
39}
40
41/** Searching changes only the address: the list is already here. */
42export function shouldRevalidate({ currentUrl, nextUrl, formMethod, defaultShouldRevalidate }: ShouldRevalidateFunctionArgs) {
43 if (!formMethod && currentUrl.pathname === nextUrl.pathname) return false;
44 return defaultShouldRevalidate;
45}
46
47export default function WorkspaceTeams({ loaderData }: Route.ComponentProps) {
48 const { teams, agentCounts, slug, canCreate } = loaderData;
49 const [params, setParams] = useSearchParams();
50 const query = params.get("q") ?? "";
51 const { mine, others } = splitTeams(filterTeams(teams, query));
52 const setQuery = (value: string) =>
53 setParams(
54 (current) => {
55 const next = new URLSearchParams(current);
56 if (value) next.set("q", value);
57 else next.delete("q");
58 return next;
59 },
60 { replace: true, preventScrollReset: true },
61 );
62
63 return (
64 <div className="space-y-8">
65 <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
66 <label className="relative block sm:w-72">
67 <span className="sr-only">Find a team</span>
68 <Search size={14} className="pointer-events-none absolute top-1/2 left-3 -translate-y-1/2 text-faint" />
69 <input
70 type="search"
71 value={query}
72 onChange={(event) => setQuery(event.target.value)}
73 placeholder="Find a team"
74 {...notACredential()}
75 className="w-full rounded-md border border-line bg-bg py-1.5 pr-3 pl-8 text-sm outline-none transition-colors placeholder:text-faint hover:border-line-strong focus:border-accent-dim"
76 />
77 </label>
78 {canCreate ? (
79 <ButtonLink to={`/${slug}/-/teams/new`}>
80 <Plus size={15} />
81 New team
82 </ButtonLink>
83 ) : (
84 <p className="flex items-center gap-1.5 text-xs text-muted">
85 <Lock size={13} className="shrink-0 text-faint" />
86 Only owners can create teams in this workspace.
87 </p>
88 )}
89 </div>
90
91 {teams.length === 0 ? (
92 <EmptyState title="No teams yet">
93 A team brings people and agents together: a lead, a channel, roles on repositories and a budget for its agents.
94 Mention it as <span className="font-mono text-fg">@{slug}/team</span>, or ask it to review a pull request.
95 </EmptyState>
96 ) : mine.length + others.length === 0 ? (
97 <Card asChild tone="plain" className="border-dashed px-4 py-8 text-center text-sm text-muted">
98 <p>
99 No teams match.{" "}
100 <Button type="button" variant="link" size="inline" onClick={() => setQuery("")}>
101 Show them all
102 </Button>
103 </p>
104 </Card>
105 ) : (
106 <>
107 {mine.length > 0 && <TeamList title="Your teams" teams={mine} agents={agentCounts} />}
108 {others.length > 0 && <TeamList title={mine.length > 0 ? "Other teams" : "All teams"} teams={others} agents={agentCounts} />}
109 </>
110 )}
111 </div>
112 );
113}
114
115function TeamList({ title, teams, agents }: { title: string; teams: Team[]; agents: Record<string, number> }) {
116 return (
117 <section aria-label={title}>
118 <h2 className="mb-3 text-sm font-medium text-muted">
119 {title} <span className="text-faint">{teams.length}</span>
120 </h2>
121 <Card asChild tone="plain" divided className="overflow-hidden">
122 <ul>
123 {teams.map((team) => (
124 <TeamRow key={team.id} team={team} agents={agents[team.slug]} />
125 ))}
126 </ul>
127 </Card>
128 </section>
129 );
130}