Files
pi2d/web/src/features/workspaces/workspace-row.tsx
T
yuez 64a1a98c1f feat(daemon): collapse workspace noise into a "show hidden" section
Discovery returns every pi session directory ever seen, so the console opened on
47 rows on this machine: 20 ghosts (cwd deleted) and 21 scratch dirs under /tmp
next to the real projects. Every row now carries a derived, non-persisted
`visibility`/`visibilityReason`; nothing is filtered server-side, so
`/workspace/<id>` deep links and the chat page's id lookup still resolve.

Precedence: explicit `hidden` wins; a live instance, `pinned` or `trusted`
rescues a row; then temp paths (`PI2_WORKSPACE_HIDE_TEMP`, on), ghosts
(`PI2_WORKSPACE_HIDE_GHOSTS`, on), outside roots (`PI2_WORKSPACE_HIDE_OUTSIDE_ROOTS`,
off) and staleness (`PI2_WORKSPACE_STALE_DAYS`, off). Measured on real data:
47 → 26 default rows, 0 openable rows lost.

The browser collapses the secondary rows behind a persisted toggle and dims them
with a reason badge; `workspace.add` now clears `hidden`.
2026-10-23 10:08:15 +08:00

140 lines
5.9 KiB
TypeScript

import { Link } from "@tanstack/react-router";
import { useTranslation } from "react-i18next";
import { Icon } from "@/components/icons";
import { RelativeTime } from "@/components/shared/relative-time";
import { Badge } from "@/components/ui/badge";
import { StatusDot, type DotStatus } from "@/components/ui/status-dot";
import type { ServerIdentity } from "@/features/status/server";
import { plural } from "@/lib/i18n";
import type { WorkspaceInfo } from "@/lib/protocol";
import { workspaceName } from "./commands";
import { isSecondary, reasonLabelKey } from "./visibility";
function serverDot(status: ServerIdentity["status"]): DotStatus {
if (status === "online") return "ok";
if (status === "degraded") return "attention";
return "exited";
}
export interface ServerContextBarProps {
server: ServerIdentity;
workspaceCount: number;
sessionCount: number;
}
/** Single-server identity bar: dot + name + hostname:port + counts + status link. */
export function ServerContextBar({ server, workspaceCount, sessionCount }: ServerContextBarProps) {
const { t } = useTranslation();
const hostPort = `${server.hostname}${server.port != null ? `:${server.port}` : ""}`;
return (
<section className="ws-context" aria-label={t("workspaces.contextAria")}>
<StatusDot status={serverDot(server.status)} title={t(`state.${server.status}`)} />
<span className="ws-context__name truncate">{server.name}</span>
<span className="ws-context__host mono muted">{hostPort}</span>
<Badge tone="info">{plural("workspaces.count", workspaceCount)}</Badge>
<Badge>{plural("workspaces.sessionCount", sessionCount)}</Badge>
<span className="spacer" />
<Link
to="/status"
className="ws-context__link"
title={t("workspaces.contextAria")}
aria-label={`${t("workspaces.contextAria")}: ${server.name} — ${t("workspaces.openStatus")}`}
>
{t("workspaces.openStatus")}
<Icon name="chevron-right" />
</Link>
</section>
);
}
function instanceDot(workspace: WorkspaceInfo): { status: DotStatus; labelKey: string } {
if (!workspace.exists) return { status: "err", labelKey: "workspaces.instanceMissing" };
const status = workspace.instance?.status;
switch (status) {
case "ready":
return { status: "ok", labelKey: "workspaces.instanceReady" };
case "starting":
return { status: "busy", labelKey: "workspaces.instanceStarting" };
case "degraded":
return { status: "attention", labelKey: "workspaces.instanceDegraded" };
case "stopping":
return { status: "attention", labelKey: "workspaces.instanceStopping" };
case "unmanaged":
return { status: "idle", labelKey: "workspaces.instanceUnmanaged" };
case "stopped":
default:
return { status: "exited", labelKey: "workspaces.instanceStopped" };
}
}
export interface WorkspaceRowProps {
workspace: WorkspaceInfo;
}
/** One flat workspace row (folder + name + full cwd + count + rel + dot + enter). */
export function WorkspaceRow({ workspace }: WorkspaceRowProps) {
const { t } = useTranslation();
const dot = instanceDot(workspace);
const count = Math.max(workspace.liveCount || 0, workspace.sessionCount || 0);
// Secondary rows are dimmed and carry the policy reason; the page decides
// *whether* they render (see features/workspaces/visibility.ts).
const secondary = isSecondary(workspace);
const reasonKey = secondary ? reasonLabelKey(workspace) : null;
return (
<div
className={secondary ? "list-row list-row--secondary" : "list-row"}
style={secondary ? { opacity: 0.55 } : undefined}
data-workspace-id={workspace.workspaceId}
data-testid="workspace-row"
data-visibility={secondary ? "secondary" : "default"}
data-visibility-reason={reasonKey ? workspace.visibilityReason ?? "hidden" : undefined}
>
<span className="ico list-row__icon">
<Icon name="folder" />
</span>
<div className="list-row__main">
<div className="list-row__name">{workspaceName(workspace, t("workspaces.unnamed"))}</div>
<div className="list-row__cwd mono muted truncate">{workspace.cwd}</div>
</div>
{reasonKey ? (
<Badge className="list-row__count" data-testid="workspace-reason">
{t(reasonKey)}
</Badge>
) : null}
<Badge tone="info" className="list-row__count">
{plural("workspaces.sessionCount", count)}
</Badge>
<RelativeTime ts={workspace.lastActiveAt} className="list-row__rel muted" />
<StatusDot status={dot.status} title={t(dot.labelKey)} />
<a
className="btn btn--sm btn--primary list-row__go"
data-testid="workspace-enter"
href={`/chat?workspace=${encodeURIComponent(workspace.workspaceId)}`}
>
{t("workspaces.enter")}
</a>
</div>
);
}
export function WorkspacesSkeleton() {
const { t } = useTranslation();
return (
<div className="list-row list-row--skeleton" role="status" aria-label={t("common.loading")}>
<span className="skeleton skeleton--icon" />
<div className="list-row__main">
<span className="skeleton skeleton--name" />
<span className="skeleton skeleton--cwd" />
</div>
<span className="skeleton skeleton--badge" />
<span className="skeleton skeleton--time" />
<span className="skeleton skeleton--dot" />
<span className="skeleton skeleton--go" />
</div>
);
}