| 1 | import { Outlet, data, type ShouldRevalidateFunctionArgs } from "react-router"; |
| 2 | |
| 3 | import type { WorkspaceAgent } from "@g1t/contracts"; |
| 4 | |
| 5 | import type { Route } from "./+types/layout"; |
| 6 | import { workspaceAgents } from "../../../lib/services.server"; |
| 7 | import { 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. */ |
| 10 | export type AgentsLayoutData = { slug: string; agents: WorkspaceAgent[] | null }; |
| 11 | |
| 12 | export 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. */ |
| 23 | export function shouldRevalidate({ currentParams, nextParams, formMethod, defaultShouldRevalidate }: ShouldRevalidateFunctionArgs) { |
| 24 | if (formMethod && formMethod !== "GET") return defaultShouldRevalidate; |
| 25 | return currentParams.owner !== nextParams.owner; |
| 26 | } |
| 27 | |
| 28 | export default function AgentsLayout() { |
| 29 | return <Outlet />; |
| 30 | } |