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.

132 linesCodeBlameRaw
1/**
2 * Apps: the integrations the workspace connected that you can use, the ones
3 * pinned to your rail first. Built-in apps are always in the
4 * rail, and their own pages are in their sidebars; Apps are only what was
5 * added. Pins are yours alone, per workspace, kept with your account so
6 * every device shows the same dock (lib/dock.server.ts), with a copy in a
7 * cookie for when the account's cannot be read (lib/apps.ts). The pin
8 * buttons post here, and the launcher reads its list from here.
9 */
10import { Plug } from "lucide-react";
11import { data } from "react-router";
12
13import type { Route } from "./+types/apps";
14import { AppTile, NoApps, useAppPins } from "../../components/apps";
15import { ButtonLink } from "../../components/ui";
16import { DOCK_COOKIE, appData, appPinFromForm, dockCookie, installedApps, pinsToShow, withPin, writeDock } from "../../lib/apps";
17import { connectedStates } from "../../lib/connected.server";
18import { savePins, savedPins } from "../../lib/dock.server";
19import { page } from "../../lib/meta";
20import { readCookie } from "../../lib/mission";
21import { assertSameOrigin, getViewer, requireUser } from "../../lib/session.server";
22import { Card } from "../../components/ui/card";
23import { Unavailable } from "../../components/boundary";
24import { isFailed, part } from "../../lib/partial";
25
26export function meta({ params, ...args }: Route.MetaArgs) {
27 return page(args, { title: `Apps · ${params.owner} · g1t` });
28}
29
30export async function loader({ params, context, request }: Route.LoaderArgs) {
31 const viewer = getViewer(context);
32 const slug = params.owner.toLowerCase();
33 const membership = viewer?.workspaces?.find((m) => m.slug === slug);
34 if (!viewer || !membership) throw data(null, { status: 404 });
35 const [connected, saved] = await Promise.all([part(connectedStates(slug, viewer), "What is installed"), savedPins(viewer, slug)]);
36 return {
37 slug,
38 name: membership.name?.trim() || slug,
39 /** Failed when what is installed could not be read. */
40 apps: isFailed(connected) ? connected : installedApps(connected).map((app) => appData(app, slug)),
41 pins: pinsToShow(saved, readCookie(request.headers.get("cookie"), DOCK_COOKIE), slug),
42 };
43}
44
45export async function action({ params, context, request }: Route.ActionArgs) {
46 assertSameOrigin(request);
47 const user = requireUser(context, request);
48 const slug = params.owner.toLowerCase();
49 if (!user.workspaces?.some((m) => m.slug === slug)) throw data(null, { status: 404 });
50 const change = appPinFromForm(await request.formData());
51 if (!change) return data({ error: "Nothing to do." }, { status: 400 });
52 const cookie = readCookie(request.headers.get("cookie"), DOCK_COOKIE);
53 // The change is made to the pins as the account keeps them; pins only
54 // ever kept in this device's cookie are carried over by the first one.
55 const current = pinsToShow(await savedPins(user, slug), cookie, slug);
56 const pins = await savePins(user, slug, withPin(current, change.app, change.pinned));
57 if (!pins) return data({ error: "Your pins could not be saved. Try again.", pins: current }, { status: 503 });
58 // This device's copy, drawn from when the account's cannot be read.
59 const secure = new URL(request.url).protocol === "https:";
60 return data({ error: null, pins }, { headers: { "Set-Cookie": dockCookie(writeDock(cookie, slug, pins), secure) } });
61}
62
63export default function Apps({ loaderData }: Route.ComponentProps) {
64 const { slug, name, apps } = loaderData;
65 const { pins, toggle } = useAppPins(slug, loaderData.pins);
66 const usable = (isFailed(apps) ? [] : apps).filter((app) => app.usable);
67 const pinned = pins.map((key) => usable.find((app) => app.key === key)).filter((app) => app != null);
68 return (
69 <main className="mx-auto max-w-5xl px-4 py-8 sm:px-8 sm:py-10">
70 <div className="flex flex-wrap items-end justify-between gap-4">
71 <div>
72 <h1 className="text-2xl font-semibold tracking-tight">Apps</h1>
73 <p className="mt-1 max-w-xl text-sm text-muted">
74 The integrations {name} connected. Pin the ones you want in your rail: your rail is yours, and nobody else sees your pins.
75 </p>
76 </div>
77 <ButtonLink to={`/${slug}/-/integrations`} prefetch="intent" variant="outline">
78 <Plug size={15} />
79 Integrations
80 </ButtonLink>
81 </div>
82
83 {isFailed(apps) ? (
84 <div className="mt-8">
85 <Unavailable what={apps.what} />
86 </div>
87 ) : apps.length === 0 ? (
88 <div className="mt-8">
89 <NoApps slug={slug} />
90 </div>
91 ) : (
92 <>
93 <section aria-labelledby="pinned" className="mt-8">
94 <div className="flex items-baseline justify-between gap-3">
95 <h2 id="pinned" className="text-sm font-semibold">
96 Pinned to your rail
97 </h2>
98 <span className="text-xs text-faint">{pinned.length === 0 ? "None yet" : `${pinned.length} pinned`}</span>
99 </div>
100 {pinned.length === 0 ? (
101 <Card asChild tone="plain" className="mt-3 border-dashed px-4 py-6 text-center text-sm text-muted">
102 <p>
103 Nothing pinned yet. Pin an app below and it shows in your rail, under the built-in ones.
104 </p>
105 </Card>
106 ) : (
107 <div className="mt-3 grid grid-cols-[repeat(auto-fill,minmax(15rem,1fr))] gap-2">
108 {pinned.map((app) => (
109 <AppTile key={app.key} app={app} pinned onToggle={toggle} card />
110 ))}
111 </div>
112 )}
113 </section>
114
115 <section aria-labelledby="installed" className="mt-10">
116 <div className="flex items-baseline justify-between gap-3">
117 <h2 id="installed" className="text-sm font-semibold">
118 Installed
119 </h2>
120 <span className="text-xs text-faint">{apps.length}</span>
121 </div>
122 <div className="mt-3 grid grid-cols-[repeat(auto-fill,minmax(15rem,1fr))] gap-2">
123 {apps.map((app) => (
124 <AppTile key={app.key} app={app} pinned={pins.includes(app.key)} onToggle={toggle} card />
125 ))}
126 </div>
127 </section>
128 </>
129 )}
130 </main>
131 );
132}