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
59 linesCodeBlameRaw
1import { Outlet, data, type ShouldRevalidateFunctionArgs } from "react-router";
2
3import { type ChatSidebar, shownUsername } from "@g1t/contracts";
4
5import type { Route } from "./+types/layout";
6import type { Mentionable } from "../../../lib/chat";
7import { sidebarOrNull, workspacePeople } from "../../../lib/chat.server";
8import { requireUser, roleIn } from "../../../lib/session.server";
9
10/**
11 * Chat mode: what its sidebar shows (the shell draws it, from this data)
12 * and who can be mentioned or messaged. Each part is empty rather than an
13 * error when its service does not answer.
14 */
15export type ChatLayoutData = {
16 slug: string;
17 /** The viewer's role in the workspace. */
18 role: string;
19 /** The viewer, named as chat names them (`memberName`). */
20 me: { id: string; username: string; display_username: string | null; display_name: string; avatar: string | null };
21 sidebar: ChatSidebar | null;
22 people: Mentionable[];
23};
24
25export async function loader({ params, context, request }: Route.LoaderArgs): Promise<ChatLayoutData> {
26 const viewer = requireUser(context, request);
27 const role = roleIn(viewer, params.owner);
28 if (!role) throw data(null, { status: 404 });
29 const slug = params.owner.toLowerCase();
30 const [sidebar, people] = await Promise.all([sidebarOrNull(slug, viewer), workspacePeople(slug, viewer)]);
31 const self = people.find((person) => person.name === viewer.username.toLowerCase());
32 const display_username = viewer.display_username ?? self?.display_username ?? null;
33 return {
34 slug,
35 role,
36 me: {
37 id: viewer.id,
38 username: viewer.username,
39 display_username,
40 display_name: self?.display_name ?? shownUsername({ username: viewer.username, display_username }),
41 avatar: viewer.avatar ?? null,
42 },
43 sidebar,
44 people,
45 };
46}
47
48/**
49 * Moving between conversations keeps the sidebar: it refreshes itself
50 * (components/chat/sidebar.tsx) and after anything is sent.
51 */
52export function shouldRevalidate({ currentParams, nextParams, formMethod, defaultShouldRevalidate }: ShouldRevalidateFunctionArgs) {
53 if (formMethod && formMethod !== "GET") return defaultShouldRevalidate;
54 return currentParams.owner !== nextParams.owner;
55}
56
57export default function ChatLayout() {
58 return <Outlet />;
59}