Files
group-by-host/client/host-sidebar-view.tsx

509 lines
15 KiB
TypeScript

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<unknown>(null);
const hosts = useHosts();
const [searchText, setSearchText] = useState("");
const [activeOnly, setActiveOnly] = useState(false);
const [showAgents, setShowAgents] = useState(true);
const [collapsedHostIds, setCollapsedHostIds] = useState<ReadonlySet<string>>(
() => new Set(),
);
const [activeMenuKey, setActiveMenuKey] = useState<string | null>(null);
const [contextMenu, setContextMenu] = useState<ContextMenuRequest | null>(
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<string>();
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<string, readonly AgentRef[]>();
for (const group of agentGroups) {
map.set(group.key, group.items);
}
return map;
}, [agentGroups]);
const projectsByHost = useMemo(() => {
const map = new Map<string, readonly ProjectRef[]>();
for (const group of projectGroups) {
map.set(group.key, group.items);
}
return map;
}, [projectGroups]);
const hostEntries = useMemo<readonly HostEntry[]>(() => {
const workspacesByHost = new Map<string, readonly WorkspaceRef[]>();
for (const group of workspaceGroups) {
workspacesByHost.set(group.key, group.items);
}
const entries: HostEntry[] = [];
const seen = new Set<string>();
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 (
<View
ref={rootRef as never}
style={{ flex: 1, backgroundColor: theme.colors.surface0 }}
>
<View
style={{
paddingHorizontal: 10,
paddingTop: 8,
paddingBottom: 8,
gap: 8,
borderBottomWidth: 1,
borderBottomColor: theme.colors.border,
}}
>
<View
style={{
flexDirection: "row",
alignItems: "center",
gap: 6,
paddingHorizontal: 8,
paddingVertical: layout.compact ? 6 : 5,
borderRadius: 8,
borderWidth: 1,
borderColor: theme.colors.border,
backgroundColor: theme.colors.surface1,
}}
>
<Icon name="Search" size={14} color={theme.colors.foregroundMuted} />
<TextInput
value={searchText}
onChangeText={setSearchText}
placeholder="Filter hosts or workspaces..."
placeholderTextColor={theme.colors.foregroundMuted}
accessibilityLabel="Filter hosts or workspaces"
style={{
flex: 1,
fontSize: 12,
paddingVertical: 0,
color: theme.colors.foreground,
}}
/>
{searchText.length > 0 ? (
<Pressable
accessibilityRole="button"
accessibilityLabel="Clear filter"
onPress={() => setSearchText("")}
style={{ padding: 2 }}
>
<Icon name="X" size={13} color={theme.colors.foregroundMuted} />
</Pressable>
) : null}
</View>
<View
style={{
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
gap: 6,
}}
>
<View style={{ flexDirection: "row", alignItems: "center", gap: 6 }}>
<Pressable
accessibilityRole="button"
accessibilityLabel="Filter active workspaces"
onPress={() => 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,
}}
>
<Icon
name="Filter"
size={11}
color={
activeOnly
? theme.colors.accentForeground
: theme.colors.foregroundMuted
}
/>
<Text
style={{
fontSize: 11,
fontWeight: "600",
color: activeOnly
? theme.colors.accentForeground
: theme.colors.foreground,
}}
>
Active
</Text>
</Pressable>
<Pressable
accessibilityRole="button"
accessibilityLabel="Toggle agents list"
onPress={() => 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,
}}
>
<Icon
name="Bot"
size={11}
color={
showAgents
? theme.colors.foreground
: theme.colors.foregroundMuted
}
/>
<Text
style={{
fontSize: 11,
fontWeight: "600",
color: showAgents
? theme.colors.foreground
: theme.colors.foregroundMuted,
}}
>
Agents
</Text>
</Pressable>
</View>
{hostEntries.length > 1 ? (
<Pressable
accessibilityRole="button"
accessibilityLabel={
allCollapsed ? "Expand all hosts" : "Collapse all hosts"
}
onPress={toggleCollapseAll}
style={({ pressed }) => ({
flexDirection: "row",
alignItems: "center",
gap: 4,
paddingHorizontal: 6,
paddingVertical: 4,
borderRadius: 6,
backgroundColor: pressed ? theme.colors.surface1 : "transparent",
})}
>
<Icon
name={allCollapsed ? "ChevronsUpDown" : "ChevronsDownUp"}
size={13}
color={theme.colors.foregroundMuted}
/>
</Pressable>
) : null}
</View>
</View>
<ScrollView
style={{ flex: 1 }}
onScrollBeginDrag={closeContextMenu}
contentContainerStyle={{
padding: 10,
gap: 10,
}}
>
{hostEntries.length === 0 ? (
<View
style={{
paddingVertical: 24,
paddingHorizontal: 12,
alignItems: "center",
gap: 6,
}}
>
<Icon name="Server" size={20} color={theme.colors.foregroundMuted} />
<Text
style={{
fontSize: 13,
fontWeight: "500",
color: theme.colors.foreground,
textAlign: "center",
}}
>
{activeOnly
? "No active workspaces"
: normalizedQuery
? "No matching hosts or workspaces"
: "No connected hosts"}
</Text>
<Text
style={{
fontSize: 12,
color: theme.colors.foregroundMuted,
textAlign: "center",
}}
>
{activeOnly || normalizedQuery
? "Try clearing your filter to view all hosts."
: "Add or connect a Paseo host to see its workspaces here."}
</Text>
</View>
) : (
hostEntries.map((entry) => (
<HostGroup
key={entry.hostId}
hostId={entry.hostId}
hostLabel={entry.label}
hostStatus={entry.status}
workspaceRefs={entry.workspaceRefs}
projectRefs={entry.projectRefs}
agentsByWorkspace={agentsByWorkspace}
collapsed={collapsedHostIds.has(entry.hostId)}
onToggleCollapsed={toggleHostCollapsed}
theme={theme}
compact={layout.compact}
platform={layout.platform}
showAgents={showAgents}
activeWorkspaceKey={activeWorkspaceKey}
activeAgentKey={activeAgentKey}
activeMenuKey={activeMenuKey}
hasActiveWorkspaceInHost={activeHostId === entry.hostId}
onToggleMenu={toggleMenu}
onCloseMenu={closeMenu}
onOpenContextMenu={openContextMenu}
onOpenWorkspace={navigation.openWorkspace}
onOpenAgent={navigation.openAgent}
onOpenProject={navigation.openProject}
/>
))
)}
</ScrollView>
<ContextMenuOverlay
request={contextMenu}
containerRef={rootRef}
platform={layout.platform}
theme={theme}
onClose={closeContextMenu}
/>
</View>
);
}