Skip to content
35 linesCodeBlameRaw
1import { data } from "react-router";
2
3import type { Route } from "./+types/person";
4import { buildCard } from "../../../lib/hovercard";
5import { identity, repos } from "../../../lib/services.server";
6import { requireUser, roleIn } from "../../../lib/session.server";
7
8/**
9 * A person's card in Chat (components/chat/profile-card.tsx): their public
10 * profile as the hovercard has it, and the teams they are on in this
11 * workspace that the viewer can see. Kept a minute by the browser.
12 */
13export async function loader({ params, context, request }: Route.LoaderArgs) {
14 const viewer = requireUser(context, request);
15 if (!roleIn(viewer, params.owner)) throw data(null, { status: 404 });
16 const [card, teams] = await Promise.all([
17 buildCard(
18 params.username,
19 viewer,
20 null,
21 {
22 profile: (username) => identity.profile(username),
23 accountId: async (username) => (await identity.userByUsername(username))?.id ?? null,
24 publicNamespaces: (id) => repos.publicNamespaces(id),
25 profileWorkspaces: (username, who, publicIn) => identity.profileWorkspaces(username, who, publicIn),
26 contributors: (path, who) => repos.contributors(path, who),
27 },
28 Date.now(),
29 ).catch(() => null),
30 identity.userTeams(viewer, params.owner, params.username).catch(() => null),
31 ]);
32 const headers = { "cache-control": "private, max-age=60", vary: "Cookie" };
33 if (!card || card.kind !== "user") return Response.json({ error: "not_found" }, { status: 404, headers });
34 return Response.json({ ...card, teams: teams?.ok ? teams.value.map((team) => ({ slug: team.slug, name: team.name })) : [] }, { headers });
35}