Skip to content
30 linesCodeBlameRaw
1import { Outlet, data, type ShouldRevalidateFunctionArgs } from "react-router";
2
3import type { WorkspaceAgent } from "@g1t/contracts";
4
5import type { Route } from "./+types/layout";
6import { workspaceAgents } from "../../../lib/services.server";
7import { getViewer, roleIn } from "../../../lib/session.server";
8
9/** Agents mode: the workspace's agents, for its sidebar; null when the agents service does not answer. */
10export type AgentsLayoutData = { slug: string; agents: WorkspaceAgent[] | null };
11
12export async function loader({ params, context }: Route.LoaderArgs): Promise<AgentsLayoutData> {
13 const viewer = getViewer(context);
14 if (!roleIn(viewer, params.owner)) throw data(null, { status: 404 });
15 const listed = await workspaceAgents.list(params.owner.toLowerCase(), viewer!).catch(() => null);
16 return {
17 slug: params.owner.toLowerCase(),
18 agents: listed?.ok ? listed.value.filter((agent) => !agent.archived_at).sort((a, b) => a.display_name.localeCompare(b.display_name)) : null,
19 };
20}
21
22/** The list changes when an agent is made or changed, not when moving between them. */
23export function shouldRevalidate({ currentParams, nextParams, formMethod, defaultShouldRevalidate }: ShouldRevalidateFunctionArgs) {
24 if (formMethod && formMethod !== "GET") return defaultShouldRevalidate;
25 return currentParams.owner !== nextParams.owner;
26}
27
28export default function AgentsLayout() {
29 return <Outlet />;
30}