import type { PluginTheme } from "@getpaseo/plugin";
import type { AgentRef, WorkspaceRef } from "@getpaseo/plugin/client";
import { useAgent, usePaseo, useWorkspace } from "@getpaseo/plugin/client";
import { copyText, Icon, useToast } from "@getpaseo/plugin/client/ui";
import { useMemo, useState } from "react";
import { Pressable, Text, View } from "react-native";
import {
type ContextMenuRequest,
useContextMenuTarget,
} from "./context-menu";
import {
getAgentStatusColor,
getAgentSubtitle,
getWorkspaceIndicatorColor,
} from "./grouping";
import {
ActionPill,
ConfirmArchiveModal,
RenameWorkspaceModal,
RowIconButton,
} from "./workspace-actions";
interface AgentRowProps {
agentRef: AgentRef;
theme: PluginTheme;
platform: "ios" | "android" | "web";
isActive: boolean;
actionsOpen: boolean;
onToggleActions(key: string): void;
onCloseActions(): void;
onOpenContextMenu(request: ContextMenuRequest): void;
onOpenAgent(ref: AgentRef): void;
}
function AgentRow({
agentRef,
theme,
platform,
isActive,
actionsOpen,
onToggleActions,
onCloseActions,
onOpenContextMenu,
onOpenAgent,
}: AgentRowProps) {
const agent = useAgent(agentRef);
const paseo = usePaseo();
const toast = useToast();
const [archiveOpen, setArchiveOpen] = useState(false);
const [busy, setBusy] = useState(false);
const contextMenuRef = useContextMenuTarget(openContextMenu);
if (!agent) return null;
const dotColor = getAgentStatusColor(agent, theme);
const label = agent.title?.trim() || agent.provider;
const subtitle = getAgentSubtitle(agent);
const menuKey = `agent:${agent.ref.key}`;
function openContextMenu(clientX: number, clientY: number) {
if (!agent) return;
onOpenContextMenu({
clientX,
clientY,
title: label,
items: [
{
id: "open",
icon: "ExternalLink",
label: "Open agent",
onSelect: () => onOpenAgent(agent.ref),
},
{
id: "copy-id",
icon: "Copy",
label: "Copy ID",
disabled: busy,
onSelect: () => handleCopy(agent.id, "Copied agent ID"),
},
{
id: "copy-cwd",
icon: "Copy",
label: "Copy CWD",
disabled: busy,
onSelect: () => handleCopy(agent.cwd, "Copied working directory"),
},
{
id: "archive",
icon: "Archive",
label: "Archive",
tone: "danger",
disabled: busy,
separatorBefore: true,
onSelect: () => setArchiveOpen(true),
},
],
});
}
async function handleCopy(value: string, successMessage: string) {
try {
await copyText(value);
toast.show(successMessage, { variant: "success" });
onCloseActions();
} catch (error) {
toast.error(error instanceof Error ? error.message : "Failed to copy");
}
}
async function handleArchiveAgent() {
if (!agent || busy) return;
setBusy(true);
try {
await paseo.agents.ref(agent.ref).archive();
setArchiveOpen(false);
toast.show(`Archived ${label}`, { variant: "success" });
onCloseActions();
} catch (error) {
toast.error(
error instanceof Error ? error.message : "Failed to archive agent",
);
} finally {
setBusy(false);
}
}
return (
onOpenAgent(agent.ref)}
onLongPress={
platform !== "web" ? () => openContextMenu(0, 0) : undefined
}
style={({ pressed }) => ({
flex: 1,
minWidth: 0,
flexDirection: "row",
alignItems: "flex-start",
gap: 8,
paddingVertical: 5,
paddingLeft: 26,
paddingRight: 4,
borderRadius: 6,
backgroundColor:
!isActive && pressed ? theme.colors.surface1 : "transparent",
})}
>
{label}
{subtitle ? (
{subtitle}
) : null}
setArchiveOpen(true)}
/>
onToggleActions(menuKey)}
/>
{actionsOpen ? (
handleCopy(agent.id, "Copied agent ID")}
/>
handleCopy(agent.cwd, "Copied working directory")}
/>
setArchiveOpen(true)}
/>
) : null}
setArchiveOpen(false)}
onConfirm={handleArchiveAgent}
/>
);
}
export interface WorkspaceRowProps {
workspaceRef: WorkspaceRef;
agentRefs: readonly AgentRef[];
theme: PluginTheme;
compact: boolean;
platform: "ios" | "android" | "web";
showAgents: boolean;
activeWorkspaceKey: string | null;
activeAgentKey: string | null;
activeMenuKey: string | null;
onToggleMenu(key: string): void;
onCloseMenu(): void;
onOpenContextMenu(request: ContextMenuRequest): void;
onOpenWorkspace(ref: WorkspaceRef): void;
onOpenAgent(ref: AgentRef): void;
}
export function WorkspaceRow({
workspaceRef,
agentRefs,
theme,
compact,
platform,
showAgents,
activeWorkspaceKey,
activeAgentKey,
activeMenuKey,
onToggleMenu,
onCloseMenu,
onOpenContextMenu,
onOpenWorkspace,
onOpenAgent,
}: WorkspaceRowProps) {
const workspace = useWorkspace(workspaceRef);
const paseo = usePaseo();
const toast = useToast();
const [agentsCollapsed, setAgentsCollapsed] = useState(false);
const [renameOpen, setRenameOpen] = useState(false);
const [archiveOpen, setArchiveOpen] = useState(false);
const [busy, setBusy] = useState(false);
const contextMenuRef = useContextMenuTarget(openContextMenu);
const menuKey = `workspace:${workspaceRef.key}`;
const actionsOpen = activeMenuKey === menuKey;
const isActiveWorkspace = activeWorkspaceKey === workspaceRef.key;
const hasAgents = agentRefs.length > 0;
const agentsExpanded = showAgents && hasAgents && !agentsCollapsed;
const metaLine = useMemo(() => {
if (!workspace) return "";
const parts: string[] = [];
if (workspace.projectDisplayName) {
parts.push(workspace.projectDisplayName);
}
if (workspace.branch && workspace.branch !== workspace.name) {
parts.push(workspace.branch);
}
if (workspace.indicatorLabel) {
parts.push(workspace.indicatorLabel);
}
return parts.join(" ยท ");
}, [workspace]);
if (!workspace) return null;
const title = workspace.title?.trim() || workspace.name;
const indicatorColor = getWorkspaceIndicatorColor(workspace.indicator, theme);
const diffAdditions = workspace.diffStat?.additions ?? 0;
const diffDeletions = workspace.diffStat?.deletions ?? 0;
const hasDiff = diffAdditions > 0 || diffDeletions > 0;
async function handleTogglePin() {
if (!workspace || busy) return;
const nextPinned = workspace.pinnedAt === null;
setBusy(true);
try {
await paseo.workspaces.ref(workspace.ref).setPinned(nextPinned);
toast.show(nextPinned ? `Pinned ${title}` : `Unpinned ${title}`, {
variant: "success",
});
onCloseMenu();
} catch (error) {
toast.error(
error instanceof Error ? error.message : "Failed to update pin state",
);
} finally {
setBusy(false);
}
}
async function handleRename(nextTitle: string | null) {
if (!workspace || busy) return;
setBusy(true);
try {
await paseo.workspaces.ref(workspace.ref).setTitle(nextTitle);
setRenameOpen(false);
onCloseMenu();
toast.show(
nextTitle ? `Renamed to "${nextTitle}"` : "Reset workspace title",
{ variant: "success" },
);
} catch (error) {
toast.error(
error instanceof Error ? error.message : "Failed to rename workspace",
);
} finally {
setBusy(false);
}
}
async function handleCopyDirectory() {
if (!workspace) return;
try {
await copyText(workspace.directory);
toast.show("Copied workspace directory", { variant: "success" });
onCloseMenu();
} catch (error) {
toast.error(error instanceof Error ? error.message : "Failed to copy");
}
}
async function handleArchiveWorkspace() {
if (!workspace || busy) return;
setBusy(true);
try {
const result = await paseo.workspaces.ref(workspace.ref).archive();
if (result.error) {
toast.error(result.error);
return;
}
setArchiveOpen(false);
toast.show(`Archived ${title}`, { variant: "success" });
onCloseMenu();
} catch (error) {
toast.error(
error instanceof Error ? error.message : "Failed to archive workspace",
);
} finally {
setBusy(false);
}
}
function openContextMenu(clientX: number, clientY: number) {
if (!workspace) return;
onOpenContextMenu({
clientX,
clientY,
title,
items: [
{
id: "open",
icon: "ExternalLink",
label: "Open workspace",
onSelect: () => onOpenWorkspace(workspace.ref),
},
{
id: "rename",
icon: "Pencil",
label: "Rename",
disabled: busy,
onSelect: () => setRenameOpen(true),
},
{
id: "pin",
icon: workspace.pinnedAt ? "PinOff" : "Pin",
label: workspace.pinnedAt ? "Unpin" : "Pin",
disabled: busy,
onSelect: handleTogglePin,
},
{
id: "copy-path",
icon: "Copy",
label: "Copy path",
disabled: busy,
onSelect: handleCopyDirectory,
},
{
id: "archive",
icon: "Archive",
label: "Archive",
tone: "danger",
disabled: busy,
separatorBefore: true,
onSelect: () => setArchiveOpen(true),
},
],
});
}
return (
onOpenWorkspace(workspace.ref)}
onLongPress={
platform !== "web" ? () => openContextMenu(0, 0) : undefined
}
style={({ pressed }) => ({
flex: 1,
minWidth: 0,
flexDirection: "row",
alignItems: "flex-start",
gap: 8,
paddingVertical: compact ? 8 : 6,
paddingLeft: 10,
paddingRight: 4,
borderRadius: 8,
backgroundColor:
!isActiveWorkspace && pressed
? theme.colors.surface1
: "transparent",
})}
>
{title}
{workspace.pinnedAt ? (
) : null}
{hasDiff ? (
{diffAdditions > 0 ? (
+{diffAdditions}
) : null}
{diffDeletions > 0 ? (
-{diffDeletions}
) : null}
) : null}
{metaLine ? (
{metaLine}
) : null}
{hasAgents ? (
{
if (showAgents) {
setAgentsCollapsed((prev) => !prev);
} else {
onOpenWorkspace(workspace.ref);
}
}}
style={({ pressed }) => ({
flexDirection: "row",
alignItems: "center",
gap: 3,
paddingHorizontal: 5,
paddingVertical: 6,
borderRadius: 6,
backgroundColor: pressed ? theme.colors.surface1 : "transparent",
})}
>
{agentRefs.length}
{showAgents ? (
) : null}
) : null}
setRenameOpen(true)}
/>
setArchiveOpen(true)}
/>
onToggleMenu(menuKey)}
/>
{actionsOpen ? (
setRenameOpen(true)}
/>
setArchiveOpen(true)}
/>
) : null}
{agentsExpanded ? (
{agentRefs.map((agentRef) => (
))}
) : null}
setRenameOpen(false)}
onSave={handleRename}
/>
setArchiveOpen(false)}
onConfirm={handleArchiveWorkspace}
/>
);
}