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