Files
paseo/packages/app/src/hooks/use-aggregated-agents.ts
T
Mohamed BoudraandClaude a01c3f9b73 Flatten home screen agent list and sort by activity
The agent list on the home screen now displays all agents in a single flat
list rather than grouped by host. Each row shows a host badge next to the
agent title so users can still identify which host an agent belongs to.
Agents are sorted globally by status (running first) then by most recent
activity, providing a unified view across all hosts.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 20:18:49 +01:00

68 lines
2.2 KiB
TypeScript

import { useMemo } from "react";
import type { Agent } from "@/contexts/session-context";
import { useDaemonConnections } from "@/contexts/daemon-connections-context";
import { useSessionDirectory } from "@/hooks/use-session-directory";
export interface AggregatedAgent extends Agent {
serverId: string;
serverLabel: string;
}
export interface AggregatedAgentsResult {
agents: AggregatedAgent[];
isLoading: boolean;
}
export function useAggregatedAgents(): AggregatedAgentsResult {
const { connectionStates, isLoading: registryLoading } = useDaemonConnections();
const sessionDirectory = useSessionDirectory();
return useMemo(() => {
const allAgents: AggregatedAgent[] = [];
sessionDirectory.forEach((session, serverId) => {
if (!session) {
return;
}
const serverLabel = connectionStates.get(serverId)?.daemon.label ?? serverId;
for (const agent of session.agents.values()) {
allAgents.push({
...agent,
serverId,
serverLabel,
});
}
});
// Sort by: running agents first, then by most recent activity
allAgents.sort((left, right) => {
const leftRunning = left.status === "running";
const rightRunning = right.status === "running";
if (leftRunning && !rightRunning) {
return -1;
}
if (!leftRunning && rightRunning) {
return 1;
}
const leftTime = (left.lastUserMessageAt ?? left.lastActivityAt).getTime();
const rightTime = (right.lastUserMessageAt ?? right.lastActivityAt).getTime();
return rightTime - leftTime;
});
// Loading if registry is still loading, or if any host is connecting and hasn't
// reported agents yet (idle hosts that haven't connected don't block loading)
let isLoading = registryLoading;
if (!isLoading && connectionStates.size > 0) {
for (const [serverId, record] of connectionStates) {
const hasSession = sessionDirectory.has(serverId);
if (record.status === "connecting" && !hasSession) {
isLoading = true;
break;
}
}
}
return { agents: allAgents, isLoading };
}, [sessionDirectory, connectionStates, registryLoading]);
}