import type { PluginProjectSnapshot, PluginWorkspaceSnapshot, } from "@getpaseo/plugin"; import type { AgentRef, PluginHostSummary, PluginSidebarViewProps, ProjectRef, WorkspaceRef, } from "@getpaseo/plugin/client"; import { useAgents, useHosts, useProjects, useWorkspaces, } from "@getpaseo/plugin/client"; import { Icon, ScrollView, TextInput } from "@getpaseo/plugin/client/ui"; import { useCallback, useMemo, useRef, useState } from "react"; import { Pressable, Text, View } from "react-native"; import { ContextMenuOverlay, type ContextMenuRequest, } from "./context-menu"; import { compareAgents, compareWorkspaces, isWorkspaceActive, } from "./grouping"; import { HostGroup } from "./host-group"; interface HostEntry { readonly hostId: string; readonly label: string; readonly status: PluginHostSummary["status"]; readonly workspaceRefs: readonly WorkspaceRef[]; readonly projectRefs: readonly ProjectRef[]; } export function HostGroupedSidebarView({ theme, layout, active, navigation, }: PluginSidebarViewProps) { const rootRef = useRef(null); const hosts = useHosts(); const [searchText, setSearchText] = useState(""); const [activeOnly, setActiveOnly] = useState(false); const [showAgents, setShowAgents] = useState(true); const [collapsedHostIds, setCollapsedHostIds] = useState>( () => new Set(), ); const [activeMenuKey, setActiveMenuKey] = useState(null); const [contextMenu, setContextMenu] = useState( null, ); const toggleMenu = useCallback((key: string) => { setContextMenu(null); setActiveMenuKey((previous) => (previous === key ? null : key)); }, []); const closeMenu = useCallback(() => { setActiveMenuKey(null); }, []); const openContextMenu = useCallback((request: ContextMenuRequest) => { setActiveMenuKey(null); setContextMenu(request); }, []); const closeContextMenu = useCallback(() => { setContextMenu(null); }, []); const normalizedQuery = searchText.trim().toLowerCase(); const matchingHostIds = useMemo(() => { if (!normalizedQuery) return null; const matched = new Set(); for (const host of hosts) { if ( host.label.toLowerCase().includes(normalizedQuery) || host.serverId.toLowerCase().includes(normalizedQuery) ) { matched.add(host.serverId); } } return matched; }, [hosts, normalizedQuery]); const workspaceQuery = useMemo( () => ({ where: (workspace: PluginWorkspaceSnapshot) => { if (workspace.background || workspace.archivingAt !== null) { return false; } if (activeOnly && !isWorkspaceActive(workspace)) { return false; } if (!normalizedQuery) { return true; } if (matchingHostIds?.has(workspace.hostId)) { return true; } const fields = [ workspace.title, workspace.name, workspace.projectDisplayName, workspace.branch, ]; return fields.some( (field) => field !== null && field.toLowerCase().includes(normalizedQuery), ); }, orderBy: compareWorkspaces, groupBy: (workspace: PluginWorkspaceSnapshot) => workspace.hostId, }), [activeOnly, matchingHostIds, normalizedQuery], ); const agentQuery = useMemo( () => ({ where: (agent: { readonly background: boolean }) => !agent.background, orderBy: compareAgents, groupBy: (agent: { readonly workspace: WorkspaceRef }) => agent.workspace.key, }), [], ); const projectQuery = useMemo( () => ({ orderBy: (left: PluginProjectSnapshot, right: PluginProjectSnapshot) => left.displayName.localeCompare(right.displayName), groupBy: (project: PluginProjectSnapshot) => project.hostId, }), [], ); const workspaceGroups = useWorkspaces(workspaceQuery); const agentGroups = useAgents(agentQuery); const projectGroups = useProjects(projectQuery); const agentsByWorkspace = useMemo(() => { const map = new Map(); for (const group of agentGroups) { map.set(group.key, group.items); } return map; }, [agentGroups]); const projectsByHost = useMemo(() => { const map = new Map(); for (const group of projectGroups) { map.set(group.key, group.items); } return map; }, [projectGroups]); const hostEntries = useMemo(() => { const workspacesByHost = new Map(); for (const group of workspaceGroups) { workspacesByHost.set(group.key, group.items); } const entries: HostEntry[] = []; const seen = new Set(); for (const host of hosts) { seen.add(host.serverId); const hostWorkspaces = workspacesByHost.get(host.serverId) ?? []; const hostMatchesQuery = !normalizedQuery || Boolean(matchingHostIds?.has(host.serverId)) || hostWorkspaces.length > 0; const matchesFilter = !activeOnly || hostWorkspaces.length > 0; if (hostMatchesQuery && matchesFilter) { entries.push({ hostId: host.serverId, label: host.label || host.serverId, status: host.status, workspaceRefs: hostWorkspaces, projectRefs: projectsByHost.get(host.serverId) ?? [], }); } } const extraHostIds = [...workspacesByHost.keys()] .filter((hostId) => !seen.has(hostId)) .sort((a, b) => a.localeCompare(b)); for (const hostId of extraHostIds) { entries.push({ hostId, label: hostId, status: "online", workspaceRefs: workspacesByHost.get(hostId) ?? [], projectRefs: projectsByHost.get(hostId) ?? [], }); } return entries; }, [ activeOnly, hosts, matchingHostIds, normalizedQuery, projectsByHost, workspaceGroups, ]); const allCollapsed = hostEntries.length > 0 && hostEntries.every((entry) => collapsedHostIds.has(entry.hostId)); const toggleHostCollapsed = useCallback((hostId: string) => { setCollapsedHostIds((previous) => { const next = new Set(previous); if (next.has(hostId)) { next.delete(hostId); } else { next.add(hostId); } return next; }); }, []); const toggleCollapseAll = useCallback(() => { setCollapsedHostIds((previous) => { if ( hostEntries.length > 0 && hostEntries.every((entry) => previous.has(entry.hostId)) ) { return new Set(); } return new Set(hostEntries.map((entry) => entry.hostId)); }); }, [hostEntries]); const activeWorkspaceKey = active.workspace?.key ?? null; const activeAgentKey = active.agent?.key ?? null; const activeHostId = active.workspace?.hostId ?? active.agent?.hostId ?? null; return ( {searchText.length > 0 ? ( setSearchText("")} style={{ padding: 2 }} > ) : null} setActiveOnly((prev) => !prev)} style={{ flexDirection: "row", alignItems: "center", gap: 4, paddingHorizontal: 8, paddingVertical: 4, borderRadius: 999, borderWidth: 1, borderColor: activeOnly ? theme.colors.accent : theme.colors.border, backgroundColor: activeOnly ? theme.colors.accent : theme.colors.surface1, }} > Active setShowAgents((prev) => !prev)} style={{ flexDirection: "row", alignItems: "center", gap: 4, paddingHorizontal: 8, paddingVertical: 4, borderRadius: 999, borderWidth: 1, borderColor: showAgents ? theme.colors.accent : theme.colors.border, backgroundColor: showAgents ? theme.colors.surface2 : theme.colors.surface1, }} > Agents {hostEntries.length > 1 ? ( ({ flexDirection: "row", alignItems: "center", gap: 4, paddingHorizontal: 6, paddingVertical: 4, borderRadius: 6, backgroundColor: pressed ? theme.colors.surface1 : "transparent", })} > ) : null} {hostEntries.length === 0 ? ( {activeOnly ? "No active workspaces" : normalizedQuery ? "No matching hosts or workspaces" : "No connected hosts"} {activeOnly || normalizedQuery ? "Try clearing your filter to view all hosts." : "Add or connect a Paseo host to see its workspaces here."} ) : ( hostEntries.map((entry) => ( )) )} ); }