| 1 | import { Outlet, data, type ShouldRevalidateFunctionArgs } from "react-router"; |
| 2 | |
| 3 | import type { ChatSidebar, WorkspaceAgent } from "@g1t/contracts"; |
| 4 | |
| 5 | import type { Route } from "./+types/layout"; |
| 6 | import type { Mentionable } from "../../../lib/chat"; |
| 7 | import { sidebarOrNull, workspacePeople } from "../../../lib/chat.server"; |
| 8 | import { 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 | */ |
| 15 | export type ChatLayoutData = { |
| 16 | slug: string; |
| 17 | /** The viewer's role: owners may make agents. */ |
| 18 | role: string; |
| 19 | me: { id: string; username: string; avatar: string | null }; |
| 20 | sidebar: ChatSidebar | null; |
| 21 | people: Mentionable[]; |
| 22 | agents: WorkspaceAgent[]; |
| 23 | }; |
| 24 | |
| 25 | export 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, agents }] = await Promise.all([sidebarOrNull(slug, viewer), workspacePeople(slug, viewer)]); |
| 31 | return { slug, role, me: { id: viewer.id, username: viewer.username, avatar: viewer.avatar ?? null }, sidebar, people, agents }; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Moving between conversations keeps the sidebar: it refreshes itself |
| 36 | * (components/chat/sidebar.tsx) and after anything is sent. |
| 37 | */ |
| 38 | export function shouldRevalidate({ currentParams, nextParams, formMethod, defaultShouldRevalidate }: ShouldRevalidateFunctionArgs) { |
| 39 | if (formMethod && formMethod !== "GET") return defaultShouldRevalidate; |
| 40 | return currentParams.owner !== nextParams.owner; |
| 41 | } |
| 42 | |
| 43 | export default function ChatLayout() { |
| 44 | return <Outlet />; |
| 45 | } |