mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 01:39:51 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - The board UI reads the company project list in many places: the
sidebar, starred projects, the command palette, the new-task dialog, and
archive-aware pages such as the Mine inbox
> - Active-only consumers read `GET /projects`, and archive-aware ones
read `GET /projects?includeArchived=true`, under two React Query keys
> - A Mine load therefore fetched the same large payload twice (580–590
KB each on our dev instance)
> - The two responses differ only by archived rows; the server's active
list is exactly the rows where `archivedAt` is null
> - This pull request makes every consumer share the
`includeArchived=true` query, and active-only consumers filter it with
`select`
> - The benefit is one project read per page instead of two
## Linked Issues or Issue Description
**What existing behavior does this improve?**
Duplicate project list reads on page load.
**Subsystem affected**
Board UI project queries (`ui/src/lib/company-projects-query.ts` and its
consumers).
**Current behavior**
A cold Mine load sends both `/companies/:id/projects` (sidebar) and
`/companies/:id/projects?includeArchived=true` (inbox).
**Proposed behavior**
One `includeArchived=true` read serves both. Active-only consumers apply
`selectActiveProjects`.
**Reason and benefit**
The project list is one of the largest shell payloads. Reading it once
saves a large response on every page that has both kinds of consumer.
## What Changed
- New `companyProjectsQuery(companyId, { includeArchived? })` in
`ui/src/lib/company-projects-query.ts`. It always uses the
`includeArchived: true` key and fetch. For active-only use it adds a
stable, module-level `select` that keeps rows without `archivedAt`.
- 22 files that read the active list now spread
`companyProjectsQuery(...)`, and so do the plugin SDK project picker and
16 archive-aware pages that wrote the shared key and fetcher inline.
Their `enabled` and other options are unchanged. `OnboardingWizard`'s
one imperative read is unchanged. `pages/Inbox.tsx` already reads the
same key and fetcher, so it shares the entry without an edit.
- Invalidation: project mutations and live events invalidate
`queryKeys.projects.all(companyId)`, which matches the shared key. The
onboarding wizard invalidated the old active-only key; it now
invalidates `projects.all`.
- Tests: new `company-projects-query.test.tsx` (one request for both
consumers, correct filtering, and one refetch after `projects.all`
invalidation). Three existing tests (`CommandPalette`, `CompanyExport`,
`GitHubBotManagement`) now seed or expect the shared key.
- New browser check
`tests/perf/collections/shared-project-list.spec.ts`: after the Mine
inbox read completes, the page has sent no active-only project request.
## Verification
- `pnpm verify` passes.
- `ui`: the consumer suites (`CoordinatorSidebar`, `SidebarProjects`,
`SidebarStarredProjects`, `CommandPalette`, `NewIssueDialog`,
`AgentWorkPanels`, `CoordinatorNeedsYou`, `IssueDetail`,
`ProjectDetail`, `Projects`, `CompanyExport`, `ChatExecutionDefaults`,
`GatewaysList`, `Search`, `IssueProperties`, `persisted-query-cache`)
pass.
- Browser check:
`PAPERCLIP_COLLECTION_PERF_BASE_URL=http://127.0.0.1:<port> pnpm exec
playwright test --config tests/perf/collections/playwright.config.ts
shared-project-list.spec.ts`. It passes on this branch and fails on
`master`.
## Risks
- A page that needs only active projects now downloads archived rows
too. For most companies that is a small difference; the duplicate read
it removes is a full copy.
- The server does not order either list, and consumers sort for display,
so filtering on the client keeps behavior.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, via Claude Code with
tool use (shell, file edits, browser). 1M-token context window. Extended
thinking was off.
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
263 lines
9.6 KiB
TypeScript
263 lines
9.6 KiB
TypeScript
import { companyProjectsQuery } from "@/lib/company-projects-query";
|
||
import { useEffect, useMemo, useState } from "react";
|
||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||
import { Pencil, Send } from "lucide-react";
|
||
import type { ToolMcpGatewayTokenCreated } from "@paperclipai/shared";
|
||
import { Link, Navigate, useNavigate, useParams } from "@/lib/router";
|
||
import { useCompany } from "@/context/CompanyContext";
|
||
import { useBreadcrumbs } from "@/context/BreadcrumbContext";
|
||
import { useToast } from "@/context/ToastContext";
|
||
import { queryKeys } from "@/lib/queryKeys";
|
||
import { toolsApi } from "@/api/tools";
|
||
import { agentsApi } from "@/api/agents";
|
||
import { QueryErrorState, useQueryView } from "@/components/QueryView";
|
||
import { Button } from "@/components/ui/button";
|
||
import { Skeleton } from "@/components/ui/skeleton";
|
||
import { cn } from "@/lib/utils";
|
||
import { GATEWAY_TABS, gatewayTabHref, isGatewayTabKey, type GatewayTabKey } from "./gateway-tabs";
|
||
import { gatewaysQueryKey } from "./NewGatewayDialog";
|
||
import { ConnectClientDialog } from "./ConnectClientDialog";
|
||
import { EditGatewayDialog } from "./EditGatewayDialog";
|
||
import { deriveGatewayApps, isGatewayOn } from "./gateway-helpers";
|
||
import { OverviewPanel } from "./panels/OverviewPanel";
|
||
import { AppsToolsPanel } from "./panels/AppsToolsPanel";
|
||
import { TokensPanel } from "./panels/TokensPanel";
|
||
import { GatewayActivityPanel } from "./panels/GatewayActivityPanel";
|
||
import { GatewayAdvancedPanel } from "./panels/GatewayAdvancedPanel";
|
||
import { CopyableGatewayUrl } from "./CopyableGatewayUrl";
|
||
import { describeError } from "@/api/errors";
|
||
|
||
export function GatewayDetail() {
|
||
const { gatewayId = "", tab } = useParams<{ gatewayId: string; tab?: string }>();
|
||
const navigate = useNavigate();
|
||
const queryClient = useQueryClient();
|
||
const { pushToast } = useToast();
|
||
const { selectedCompanyId } = useCompany();
|
||
const { setBreadcrumbs } = useBreadcrumbs();
|
||
const [snippetOpen, setSnippetOpen] = useState(false);
|
||
const [editing, setEditing] = useState(false);
|
||
const [createdTokens, setCreatedTokens] = useState<ToolMcpGatewayTokenCreated[]>([]);
|
||
|
||
const activeTab: GatewayTabKey | null = isGatewayTabKey(tab) ? tab : null;
|
||
|
||
const gatewaysQuery = useQuery({
|
||
queryKey: gatewaysQueryKey(selectedCompanyId ?? "__none__"),
|
||
queryFn: () => toolsApi.listGateways(selectedCompanyId!),
|
||
enabled: !!selectedCompanyId,
|
||
});
|
||
const profilesQuery = useQuery({
|
||
queryKey: queryKeys.tools.profiles(selectedCompanyId ?? "__none__"),
|
||
queryFn: () => toolsApi.listProfiles(selectedCompanyId!),
|
||
enabled: !!selectedCompanyId,
|
||
});
|
||
const connectionsQuery = useQuery({
|
||
queryKey: queryKeys.tools.connections(selectedCompanyId ?? "__none__"),
|
||
queryFn: () => toolsApi.listConnections(selectedCompanyId!),
|
||
enabled: !!selectedCompanyId,
|
||
});
|
||
const applicationsQuery = useQuery({
|
||
queryKey: queryKeys.tools.applications(selectedCompanyId ?? "__none__"),
|
||
queryFn: () => toolsApi.listApplications(selectedCompanyId!),
|
||
enabled: !!selectedCompanyId,
|
||
});
|
||
const agentsQuery = useQuery({
|
||
queryKey: queryKeys.agents.list(selectedCompanyId ?? "__none__"),
|
||
queryFn: () => agentsApi.list(selectedCompanyId!),
|
||
enabled: !!selectedCompanyId,
|
||
});
|
||
const projectsQuery = useQuery({
|
||
...companyProjectsQuery(selectedCompanyId ?? "__none__", { includeArchived: true }),
|
||
enabled: !!selectedCompanyId,
|
||
});
|
||
const gatewaysView = useQueryView(gatewaysQuery);
|
||
|
||
const gateway = useMemo(
|
||
() => (gatewaysQuery.data?.gateways ?? []).find((g) => g.id === gatewayId),
|
||
[gatewaysQuery.data, gatewayId],
|
||
);
|
||
const profile = useMemo(
|
||
() => (profilesQuery.data?.profiles ?? []).find((p) => p.id === gateway?.profileId),
|
||
[profilesQuery.data, gateway?.profileId],
|
||
);
|
||
const apps = useMemo(
|
||
() =>
|
||
deriveGatewayApps(
|
||
profile,
|
||
applicationsQuery.data?.applications ?? [],
|
||
connectionsQuery.data?.connections ?? [],
|
||
),
|
||
[profile, applicationsQuery.data, connectionsQuery.data],
|
||
);
|
||
const agentNames = useMemo(
|
||
() => new Map((agentsQuery.data ?? []).map((agent) => [agent.id, agent.name])),
|
||
[agentsQuery.data],
|
||
);
|
||
const projectNames = useMemo(
|
||
() => new Map((projectsQuery.data ?? []).map((project) => [project.id, project.name])),
|
||
[projectsQuery.data],
|
||
);
|
||
|
||
function rememberCreatedToken(token: ToolMcpGatewayTokenCreated) {
|
||
setCreatedTokens((current) => [token, ...current.filter((item) => item.id !== token.id)]);
|
||
}
|
||
|
||
useEffect(() => {
|
||
if (!gateway) return;
|
||
setBreadcrumbs([
|
||
{ label: "Connectors", href: "/apps" },
|
||
{ label: "Gateways", href: "/apps/gateways" },
|
||
{ label: gateway.name },
|
||
]);
|
||
return () => setBreadcrumbs([]);
|
||
}, [setBreadcrumbs, gateway]);
|
||
|
||
const toggleMutation = useMutation({
|
||
mutationFn: () =>
|
||
toolsApi.updateGateway(selectedCompanyId!, gatewayId, {
|
||
status: gateway && isGatewayOn(gateway) ? "disabled" : "active",
|
||
}),
|
||
onSuccess: async (updated) => {
|
||
pushToast({
|
||
title: updated.status === "active" ? "Gateway on" : "Gateway off",
|
||
body:
|
||
updated.status === "active"
|
||
? `${updated.name} is exposing its tools again.`
|
||
: `${updated.name} is off — every client goes silent.`,
|
||
tone: "success",
|
||
});
|
||
await queryClient.invalidateQueries({ queryKey: gatewaysQueryKey(selectedCompanyId!) });
|
||
},
|
||
onError: (error) =>
|
||
pushToast({
|
||
title: "Couldn't update the gateway",
|
||
body: describeError(error).body,
|
||
tone: "error",
|
||
}),
|
||
});
|
||
|
||
if (!selectedCompanyId) {
|
||
return <div className="p-6 text-sm text-muted-foreground">Select an organization to manage gateways.</div>;
|
||
}
|
||
if (!activeTab) {
|
||
return <Navigate replace to={gatewayTabHref(gatewayId, "overview")} />;
|
||
}
|
||
if (gatewaysQuery.isLoading || gatewaysView.kind === "reconnecting") {
|
||
return (
|
||
<div className="max-w-4xl space-y-4">
|
||
<Skeleton className="h-10 w-64" />
|
||
<Skeleton className="h-9 w-full max-w-lg" />
|
||
<Skeleton className="h-64 w-full" />
|
||
</div>
|
||
);
|
||
}
|
||
if (gatewaysView.kind === "error") {
|
||
return (
|
||
<QueryErrorState
|
||
size="page"
|
||
error={gatewaysQuery.error}
|
||
action="load the gateway"
|
||
onRetry={gatewaysView.retry}
|
||
retrying={gatewaysView.isFetching}
|
||
/>
|
||
);
|
||
}
|
||
if (!gateway) {
|
||
return (
|
||
<div className="max-w-3xl p-6">
|
||
<p className="text-sm text-muted-foreground">We couldn’t find that gateway.</p>
|
||
<Button className="mt-4" variant="outline" onClick={() => navigate("/apps/gateways")}>
|
||
Back to gateways
|
||
</Button>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
return (
|
||
<div className="max-w-4xl space-y-5 pb-12">
|
||
<div className="flex flex-wrap items-start justify-between gap-3">
|
||
<div className="min-w-0">
|
||
<div className="text-xs text-muted-foreground">
|
||
<Link to="/apps/gateways" className="hover:underline">
|
||
Apps · Gateways
|
||
</Link>
|
||
</div>
|
||
<h1 className="mt-1 text-2xl font-bold tracking-tight">{gateway.name}</h1>
|
||
<CopyableGatewayUrl endpointPath={gateway.endpointPath} className="mt-1 max-w-xl" />
|
||
</div>
|
||
<div className="flex items-center gap-2">
|
||
<Button variant="outline" onClick={() => setEditing(true)}>
|
||
<Pencil className="mr-1.5 h-4 w-4" />
|
||
Edit
|
||
</Button>
|
||
<Button onClick={() => setSnippetOpen(true)}>
|
||
<Send className="mr-1.5 h-4 w-4" />
|
||
Client snippets
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
|
||
<nav className="flex items-center gap-6 overflow-x-auto border-b border-border text-sm" aria-label="Gateway tabs">
|
||
{GATEWAY_TABS.map((item) => {
|
||
const isActive = item.key === activeTab;
|
||
return (
|
||
<Link
|
||
key={item.key}
|
||
to={gatewayTabHref(gatewayId, item.key)}
|
||
className={cn(
|
||
"-mb-px shrink-0 border-b-2 pb-2.5 pt-1 font-medium transition-colors",
|
||
isActive
|
||
? "border-foreground text-foreground"
|
||
: "border-transparent text-muted-foreground hover:text-foreground",
|
||
)}
|
||
aria-current={isActive ? "page" : undefined}
|
||
>
|
||
{item.label}
|
||
</Link>
|
||
);
|
||
})}
|
||
</nav>
|
||
|
||
{activeTab === "overview" && (
|
||
<OverviewPanel
|
||
gateway={gateway}
|
||
profile={profile}
|
||
apps={apps}
|
||
agentNames={agentNames}
|
||
projectNames={projectNames}
|
||
toggleDisabled={toggleMutation.isPending}
|
||
onToggle={() => toggleMutation.mutate()}
|
||
/>
|
||
)}
|
||
{activeTab === "apps" && <AppsToolsPanel apps={apps} profile={profile} />}
|
||
{activeTab === "tokens" && (
|
||
<TokensPanel
|
||
companyId={selectedCompanyId}
|
||
gateway={gateway}
|
||
onTokenCreated={rememberCreatedToken}
|
||
/>
|
||
)}
|
||
{activeTab === "activity" && (
|
||
<GatewayActivityPanel companyId={selectedCompanyId} gateway={gateway} />
|
||
)}
|
||
{activeTab === "advanced" && (
|
||
<GatewayAdvancedPanel companyId={selectedCompanyId} gateway={gateway} />
|
||
)}
|
||
|
||
<ConnectClientDialog
|
||
gateway={gateway}
|
||
open={snippetOpen}
|
||
onOpenChange={setSnippetOpen}
|
||
createdTokens={createdTokens}
|
||
onTokenCreated={rememberCreatedToken}
|
||
/>
|
||
<EditGatewayDialog
|
||
companyId={selectedCompanyId}
|
||
gateway={gateway}
|
||
profiles={profilesQuery.data?.profiles ?? []}
|
||
open={editing}
|
||
onOpenChange={setEditing}
|
||
/>
|
||
</div>
|
||
);
|
||
}
|