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