Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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