Files
pi2d/web/src/pages/workspaces.tsx
T
yuez 88e73c7737 feat(web): port the pi2-web-ui-design v0.1.1 UI
Sync the React console with the v0.1.1 prototype (20 CHGs): Quiet Workbench
shell, Chat IA v3, tool-family rendering, and the mobile page pill.

Shell / tokens (CHG-01/04/15/20):
- tokens.css rewritten from the prototype (three-layer light/dark/system),
  `--tabbar-h` removed, new faint/border-subtle/surface-1-2/fs-*/dur-*/reading-max
- 56px icon-only nav rail/sidebar (44x44 hit targets, CSS tooltips, active is a
  solid primary fill, no left bar), 52px topbar, merged preferences menu,
  ⌘K/Cmd-K palette
- `<600` gets a topbar page pill + page sheet; the bottom tabbar is gone

Pages (CHG-01): workspaces becomes a card grid with whole-card links and a
320px recent-activity column; status gets four metric tiles + a zero-primary
layout; connect gets a two-column hero, one primary per state and a collapsed
manual form.

Chat IA v3 (CHG-02/10-14/16-19): two columns, single-workspace tree with a
workspace switch, single-line rows with attention, body-level row popover,
session tray (todo/tasks) replacing the right panel, context ring moved into a
composer telemetry line, right-aligned user messages, queued messages as one
stream entity plus a composer status bar, and an in-place subagent child view.

Tool rendering (CHG-05/06/09): new `features/chat/tools` module — one row per
call, DTO adapter for the daemon's block shape, 11 kind families, state
semantics ("one state, one meaning"), 44px touch targets and the diff row
contrast fix.

i18n: 729 keys per language (zh/en parity checked), 0 unresolved keys.

Verification: web typecheck + 297 vitest + vite build green, root typecheck
green, `scripts/smoke.sh` green, and a new visual harness
(`scripts/ui-mock-server.mjs` + `scripts/ui-shots.mjs`, docs/ui-parity.md)
shoots 4 pages x 3 viewports x 2 themes = 24 screenshots clean with
overflow/console/primary/tabbar assertions. `scripts/e2e/ui-e2e.mjs` selectors
are updated to the new DOM; that suite still needs a real pi + nvim to run.
2026-10-26 20:28:47 +08:00

226 lines
9.4 KiB
TypeScript

import { useQueryClient } from "@tanstack/react-query";
import { useNavigate } from "@tanstack/react-router";
import { useMemo, useState, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { Icon } from "@/components/icons";
import { PageHeader } from "@/components/shared/page-header";
import { Button } from "@/components/ui/button";
import { Empty } from "@/components/ui/empty";
import { toast } from "@/components/ui/toast";
import { deriveServerIdentity, type InfoExtras } from "@/features/status/server";
import { useInfoQuery, useWorkspacesQuery } from "@/features/status/queries";
import { ActivityColumn } from "@/features/workspaces/activity-column";
import { openWorkspace, workspaceSessionCount } from "@/features/workspaces/commands";
import { NewWorkspaceDialog, type NewWorkspaceInput } from "@/features/workspaces/new-workspace-dialog";
import { WorkspaceStatusLine } from "@/features/workspaces/status-line";
import { countSecondary, partitionWorkspaces, readShowAll, writeShowAll } from "@/features/workspaces/visibility";
import { WorkspaceCard, WorkspacesSkeleton } from "@/features/workspaces/workspace-card";
import { ApiError, errorI18nKey } from "@/lib/api";
import type { WorkspaceInfo } from "@/lib/protocol";
import { useConnectionStore } from "@/stores/connection";
import "@/styles/pages/workspaces.css";
function compareWorkspaces(a: WorkspaceInfo, b: WorkspaceInfo): number {
const pinned = Number(Boolean(b.pinned)) - Number(Boolean(a.pinned));
if (pinned !== 0) return pinned;
return (b.lastActiveAt ?? 0) - (a.lastActiveAt ?? 0);
}
function basename(path: string): string {
const segments = path.replace(/\/+$/, "").split("/");
return segments[segments.length - 1] || path;
}
/**
* Workspaces page — pi2-web-ui-design v0.1.1 "Quiet Workbench" (§5.1):
* page head (the only primary is "new workspace") → single-line server status
* strip → card grid (2 columns ≥1100px) + 320px recent-activity column.
*
* The page owns layout and queries only; rows, the status line and the activity
* column live in `features/workspaces/`.
*/
export function WorkspacesPage() {
const { t } = useTranslation();
const navigate = useNavigate();
const queryClient = useQueryClient();
const [dialogOpen, setDialogOpen] = useState(false);
const endpoint = useConnectionStore((state) => state.endpoint);
const wsState = useConnectionStore((state) => state.wsState);
const storedServer = useConnectionStore((state) => state.server);
const workspaces = useWorkspacesQuery();
const info = useInfoQuery();
// Persisted "show N hidden" toggle (localStorage, default off): secondary
// rows (hidden/ghost/temp/…) stay out of the list until asked for.
const [showAll, setShowAll] = useState<boolean>(() => readShowAll());
function toggleShowAll() {
setShowAll((previous) => {
const next = !previous;
writeShowAll(next);
return next;
});
}
const all = useMemo(() => (workspaces.data ?? []).slice().sort(compareWorkspaces), [workspaces.data]);
const secondaryCount = useMemo(() => countSecondary(all), [all]);
const { primary, secondary } = useMemo(() => partitionWorkspaces(all, showAll), [all, showAll]);
const items = showAll ? [...primary, ...secondary].sort(compareWorkspaces) : primary;
const sessionCount = useMemo(
() => all.reduce((total, workspace) => total + workspaceSessionCount(workspace), 0),
[all],
);
const runningCount = useMemo(() => all.filter((workspace) => (workspace.liveCount || 0) > 0).length, [all]);
const server = deriveServerIdentity({
info: info.data as InfoExtras | undefined,
endpoint,
wsState,
fetchedAt: info.dataUpdatedAt || null,
nameFallback: storedServer?.name,
status: info.isError ? "offline" : undefined,
});
async function createWorkspace(input: NewWorkspaceInput) {
const workspaceId = await openWorkspace(input);
await queryClient.invalidateQueries({ queryKey: ["workspaces"] });
toast.success(t("workspaces.opened", { name: input.name?.trim() || basename(input.cwd) }));
navigate({ href: `/chat?workspace=${encodeURIComponent(workspaceId)}` });
}
const errorText =
workspaces.error instanceof ApiError ? t(errorI18nKey(workspaces.error.code)) : t("workspaces.loadErrorText");
const hasCards = workspaces.isSuccess && items.length > 0;
// Empty/loading/error states own the primary, so a screen never shows two.
const showHiddenToggle = items.length > 0 && secondaryCount > 0;
let main: ReactNode;
if (workspaces.isPending) {
main = (
<div className="ws-grid">
<WorkspacesSkeleton />
<WorkspacesSkeleton />
<WorkspacesSkeleton />
</div>
);
} else if (workspaces.isError) {
main = (
<section className="ws-empty">
<Empty
icon={<Icon name="alert" size={22} />}
title={t("workspaces.loadErrorTitle")}
description={`${t("workspaces.loadErrorText")} (${errorText})`}
action={
<Button variant="ghost" onClick={() => void workspaces.refetch()}>
<Icon name="refresh" />
{t("common.retry")}
</Button>
}
/>
</section>
);
} else if (all.length === 0) {
main = (
<section className="ws-empty">
<Empty
icon={<Icon name="folder" size={22} />}
title={t("workspaces.emptyTitle")}
description={t("workspaces.emptyText")}
action={
<Button variant="primary" onClick={() => setDialogOpen(true)}>
<Icon name="plus" />
{t("workspaces.new")}
</Button>
}
/>
</section>
);
} else if (primary.length === 0 && !showAll) {
main = (
<section className="ws-empty">
<Empty
icon={<Icon name="eye" size={22} />}
title={t("workspaces.allHidden")}
description={t("workspaces.allHiddenText", { n: secondaryCount })}
action={
<Button
variant="ghost"
aria-pressed={false}
aria-label={t("workspaces.showHiddenAria")}
data-testid="workspaces-show-all"
onClick={toggleShowAll}
>
<Icon name="eye" />
{t("workspaces.showHidden", { n: secondaryCount })}
</Button>
}
/>
</section>
);
} else {
main = (
<>
<div className="ws-grid">
{items.map((workspace) => (
<WorkspaceCard key={workspace.workspaceId} workspace={workspace} />
))}
</div>
{showHiddenToggle ? (
<div className="ws-hidden-bar">
<button
type="button"
className="ws-hidden-toggle"
aria-pressed={showAll}
aria-label={t(showAll ? "workspaces.hideHidden" : "workspaces.showHiddenAria")}
data-testid="workspaces-show-all"
onClick={toggleShowAll}
>
<Icon name="eye" />
<span>{t(showAll ? "workspaces.hideHidden" : "workspaces.showHidden", { n: secondaryCount })}</span>
<Icon name="chevron-right" className={showAll ? "ws-hidden-toggle__chevron is-open" : "ws-hidden-toggle__chevron"} />
</button>
{showAll ? null : <span className="ws-hidden-bar__hint faint">{t("workspaces.hiddenHint")}</span>}
</div>
) : null}
</>
);
}
return (
<div className="ws-page">
<PageHeader
title={t("workspaces.title")}
subtitle={t("workspaces.subtitle")}
docTitle={t("nav.workspaces")}
actions={
hasCards ? (
<Button variant="primary" onClick={() => setDialogOpen(true)}>
<Icon name="plus" />
{t("workspaces.new")}
</Button>
) : null
}
/>
<WorkspaceStatusLine
server={server}
workspaceCount={items.length}
sessionCount={sessionCount}
runningCount={runningCount}
/>
<div className="ws-layout">
<div className="ws-main">{main}</div>
<ActivityColumn workspaces={items} />
</div>
<NewWorkspaceDialog open={dialogOpen} onOpenChange={setDialogOpen} onCreate={createWorkspace} />
</div>
);
}