Skip to content

Commit 3130480

Browse files
committed
fix(web): simplify workspace card rows
1 parent cfbe90f commit 3130480

13 files changed

Lines changed: 308 additions & 274 deletions

‎apps/web/src/components/BranchToolbar.logic.test.ts‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
resolveBranchToolbarPrBranch,
1414
resolveBranchToolbarValue,
1515
resolveLockedWorkspaceLabel,
16-
resolveWorkspaceDisplayName,
1716
resolveLocalCheckoutBranchMismatch,
1817
resolvePreviousWorktreeLabel,
1918
resolvePreviousWorktreeSeed,
@@ -568,18 +567,6 @@ describe("resolveLockedWorkspaceLabel", () => {
568567
});
569568
});
570569

571-
describe("resolveWorkspaceDisplayName", () => {
572-
it("returns the final folder for POSIX and Windows paths", () => {
573-
expect(resolveWorkspaceDisplayName("/repo/.t3/worktrees/feature-a")).toBe("feature-a");
574-
expect(resolveWorkspaceDisplayName("C:\\code\\project\\feature-b\\")).toBe("feature-b");
575-
});
576-
577-
it("handles missing and root paths", () => {
578-
expect(resolveWorkspaceDisplayName(null)).toBeNull();
579-
expect(resolveWorkspaceDisplayName("/")).toBe("/");
580-
});
581-
});
582-
583570
describe("deriveLocalBranchNameFromRemoteRef", () => {
584571
it("strips the remote prefix from a remote ref", () => {
585572
expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");

‎apps/web/src/components/BranchToolbar.logic.ts‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ export function resolveLockedWorkspaceLabel(
118118
return effectiveEnvMode === "worktree" ? resolveEnvModeLabel("worktree") : "Local checkout";
119119
}
120120

121-
export function resolveWorkspaceDisplayName(path: string | null): string | null {
122-
if (!path) return null;
123-
const normalizedPath = path.replace(/[\\/]+$/, "");
124-
if (normalizedPath.length === 0) return path;
125-
return normalizedPath.split(/[\\/]/).at(-1) ?? normalizedPath;
126-
}
127-
128121
export interface PreviousWorktreeSeed {
129122
branch: string | null;
130123
worktreePath: string;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// @vitest-environment jsdom
2+
3+
import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
4+
import { act } from "react";
5+
import { createRoot } from "react-dom/client";
6+
import { expect, it, vi } from "vite-plus/test";
7+
8+
vi.mock("./BranchToolbarBranchSelector", () => ({ BranchToolbarBranchSelector: () => null }));
9+
vi.mock("../composerDraftStore", () => ({
10+
useComposerDraftStore: (select: (store: unknown) => unknown) =>
11+
select({ getDraftThreadByRef: () => null, setDraftThreadContext: vi.fn() }),
12+
}));
13+
vi.mock("../state/entities", () => ({
14+
useThreadShell: () => ({
15+
environmentId: "local",
16+
projectId: "project",
17+
worktreePath: "/tmp/worktree",
18+
}),
19+
useProject: () => ({ workspaceRoot: "/tmp/project" }),
20+
useThreadShellsForProjectRefs: () => [],
21+
}));
22+
23+
import { BranchToolbar } from "./BranchToolbar";
24+
25+
it("keeps machine choices usable when the combined row's workspace is locked", async () => {
26+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
27+
const container = document.createElement("div");
28+
document.body.append(container);
29+
const root = createRoot(container);
30+
const onEnvironmentChange = vi.fn();
31+
try {
32+
await act(async () => {
33+
root.render(
34+
<BranchToolbar
35+
layout="panel"
36+
panelSection="workspace"
37+
environmentId={EnvironmentId.make("local")}
38+
threadId={ThreadId.make("thread")}
39+
showGitControls
40+
envMode="local"
41+
envLocked={false}
42+
startFromOrigin={false}
43+
onStartFromOriginChange={vi.fn()}
44+
onEnvModeChange={vi.fn()}
45+
onEnvironmentChange={onEnvironmentChange}
46+
availableEnvironments={["local", "remote"].map((id) => ({
47+
environmentId: EnvironmentId.make(id),
48+
projectId: ProjectId.make("project"),
49+
label: id,
50+
isPrimary: id === "local",
51+
machine: "server",
52+
}))}
53+
/>,
54+
);
55+
});
56+
const trigger = container.querySelector("button")!;
57+
expect(trigger.textContent).toBe("local");
58+
expect(container.querySelectorAll("button")).toHaveLength(1);
59+
await act(async () => {
60+
trigger.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, ctrlKey: true }));
61+
});
62+
expect(document.querySelector('[role="menu"]')).toBeNull();
63+
await act(async () => trigger.click());
64+
const items = [...document.querySelectorAll<HTMLElement>('[role="menuitemradio"]')];
65+
expect(
66+
items.find((item) => item.textContent === "New worktree")?.getAttribute("aria-disabled"),
67+
).toBe("true");
68+
await act(async () => items.find((item) => item.textContent === "remote")!.click());
69+
expect(onEnvironmentChange).toHaveBeenCalledWith("remote");
70+
} finally {
71+
await act(async () => root.unmount());
72+
container.remove();
73+
vi.unstubAllGlobals();
74+
}
75+
});

‎apps/web/src/components/BranchToolbar.tsx‎

Lines changed: 123 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
2+
import { readLocalApi } from "../localApi";
3+
import { stackedThreadToast, toastManager } from "./ui/toast";
14
import { ComposerContextLabel } from "./ComposerContextLabel";
25
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
36
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
@@ -10,6 +13,7 @@ import {
1013
} from "lucide-react";
1114
import {
1215
type Ref,
16+
type MouseEvent as ReactMouseEvent,
1317
memo,
1418
useImperativeHandle,
1519
useCallback,
@@ -41,6 +45,11 @@ import {
4145
import { BranchToolbarEnvironmentSelector } from "./BranchToolbarEnvironmentSelector";
4246
import { BranchToolbarEnvModeSelector } from "./BranchToolbarEnvModeSelector";
4347
import { PreviousWorktreeItemContent } from "./PreviousWorktreeItemContent";
48+
import { ThreadDetailsControl } from "./chat/ThreadDetailsControl";
49+
import {
50+
THREAD_DETAILS_PANEL_ICON_CLASS,
51+
THREAD_DETAILS_PANEL_LOCKED_ROW_CLASS,
52+
} from "./chat/threadDetailsPanelStyles";
4453
import { ComposerControl } from "./chat/ComposerControl";
4554
import {
4655
Menu,
@@ -93,7 +102,44 @@ interface BranchToolbarProps {
93102
contextStripVisible?: boolean;
94103
}
95104

96-
interface MobileRunContextSelectorProps {
105+
function showWorkspaceContextMenu(event: ReactMouseEvent, workspacePath: string) {
106+
const api = readLocalApi();
107+
if (!api) return;
108+
event.preventDefault();
109+
event.stopPropagation();
110+
void api.contextMenu
111+
.show([{ id: "copy-path", label: "Copy full path", icon: "copy" }], {
112+
x: event.clientX,
113+
y: event.clientY,
114+
})
115+
.then((action) => {
116+
if (action !== "copy-path") return;
117+
void writeTextToClipboard(workspacePath, "workspace path").then(
118+
(didCopy) => {
119+
if (didCopy) {
120+
toastManager.add({
121+
type: "success",
122+
title: "Path copied",
123+
description: workspacePath,
124+
});
125+
}
126+
},
127+
(error: unknown) => {
128+
toastManager.add(
129+
stackedThreadToast({
130+
type: "error",
131+
title: "Failed to copy path",
132+
description: error instanceof Error ? error.message : "An error occurred.",
133+
}),
134+
);
135+
},
136+
);
137+
});
138+
}
139+
140+
interface RunContextSelectorProps {
141+
displayMode?: "toolbar" | "panel";
142+
workspaceRoot?: string;
97143
forceNewWorktree: boolean;
98144
autoEnvironmentLabel?: string | undefined;
99145
onAutoEnvironment?: (() => void) | undefined;
@@ -112,7 +158,9 @@ interface MobileRunContextSelectorProps {
112158
onUsePreviousWorktree: () => void;
113159
}
114160

115-
const MobileRunContextSelector = memo(function MobileRunContextSelector({
161+
const RunContextSelector = memo(function RunContextSelector({
162+
displayMode = "toolbar",
163+
workspaceRoot,
116164
forceNewWorktree,
117165
autoEnvironmentLabel,
118166
onAutoEnvironment,
@@ -129,7 +177,7 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
129177
previousWorktreeLabel,
130178
previousWorktreeBranch,
131179
onUsePreviousWorktree,
132-
}: MobileRunContextSelectorProps) {
180+
}: RunContextSelectorProps) {
133181
const composerFloatingLayerProps = useComposerMenuProps();
134182
const activeEnvironment = useMemo(
135183
() => availableEnvironments?.find((env) => env.environmentId === environmentId) ?? null,
@@ -148,13 +196,32 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
148196
: effectiveEnvMode === "worktree"
149197
? resolveEnvModeLabel("worktree")
150198
: resolveCurrentWorkspaceLabel(activeWorktreePath);
151-
const isLocked = envLocked || envModeLocked;
199+
const isPanel = displayMode === "panel";
200+
const isLocked = envLocked || (envModeLocked && (!isPanel || !showEnvironmentPicker));
201+
const workspacePath = forceNewWorktree ? null : (activeWorktreePath ?? workspaceRoot);
202+
const handleContextMenu = (event: ReactMouseEvent) => {
203+
if (isPanel && workspacePath) {
204+
showWorkspaceContextMenu(event, workspacePath);
205+
}
206+
};
152207
const workspaceIcon = (
153208
<Tooltip>
154209
<TooltipTrigger render={<span className="inline-flex shrink-0" />}>
155-
<WorkspaceIcon className={cn("size-3 shrink-0", showEnvironmentIndicator && "mx-0!")} />
210+
<WorkspaceIcon
211+
className={
212+
isPanel
213+
? THREAD_DETAILS_PANEL_ICON_CLASS
214+
: cn("size-3 shrink-0", showEnvironmentIndicator && "mx-0!")
215+
}
216+
/>
156217
</TooltipTrigger>
157-
<TooltipPopup>{workspaceLabel}</TooltipPopup>
218+
<TooltipPopup>
219+
{isPanel
220+
? forceNewWorktree
221+
? "Each model starts in its own worktree."
222+
: (workspacePath ?? workspaceLabel)
223+
: workspaceLabel}
224+
</TooltipPopup>
158225
</Tooltip>
159226
);
160227
const icon = showEnvironmentIndicator ? (
@@ -164,11 +231,14 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
164231
<Tooltip>
165232
<TooltipTrigger render={<span className="inline-flex shrink-0" />}>
166233
{autoEnvironmentLabel ? (
167-
<ScaleIcon className="size-3 shrink-0 mx-0!" aria-hidden="true" />
234+
<ScaleIcon
235+
className={isPanel ? THREAD_DETAILS_PANEL_ICON_CLASS : "size-3 shrink-0 mx-0!"}
236+
aria-hidden="true"
237+
/>
168238
) : (
169239
<EnvironmentMachineIcon
170240
kind={activeEnvironment?.machine ?? "server"}
171-
className="size-3 shrink-0 mx-0!"
241+
className={isPanel ? THREAD_DETAILS_PANEL_ICON_CLASS : "size-3 shrink-0 mx-0!"}
172242
/>
173243
)}
174244
</TooltipTrigger>
@@ -182,7 +252,7 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
182252
const triggerContent = (
183253
<>
184254
{icon}
185-
<ComposerContextLabel>
255+
<ComposerContextLabel displayMode={displayMode}>
186256
{autoEnvironmentLabel ??
187257
(showEnvironmentIndicator ? (activeEnvironment?.label ?? "Run on") : workspaceLabel)}
188258
</ComposerContextLabel>
@@ -192,7 +262,13 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
192262
if (isLocked) {
193263
return (
194264
<span
195-
className="inline-flex h-7 min-w-0 max-w-[48%] flex-initial items-center justify-start gap-1 rounded-md border border-transparent px-1.75 font-normal text-muted-foreground/70 text-xs sm:h-6"
265+
className={cn(
266+
"inline-flex h-7 min-w-0 max-w-[48%] flex-initial items-center justify-start gap-1 rounded-md border border-transparent px-1.75 font-normal text-muted-foreground/70 text-xs sm:h-6",
267+
isPanel && "max-w-full",
268+
isPanel && THREAD_DETAILS_PANEL_LOCKED_ROW_CLASS,
269+
)}
270+
onContextMenu={handleContextMenu}
271+
aria-label={isPanel ? "Run context" : undefined}
196272
data-composer-context-control
197273
>
198274
{triggerContent}
@@ -203,22 +279,37 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
203279
return (
204280
<Menu>
205281
<MenuTrigger
206-
render={<ComposerControl size="xs" />}
207-
className="min-w-0 max-w-[48%] flex-initial justify-start"
282+
render={isPanel ? <ThreadDetailsControl part="select" /> : <ComposerControl size="xs" />}
283+
className={isPanel ? undefined : "min-w-0 max-w-[48%] flex-initial justify-start"}
284+
onContextMenu={handleContextMenu}
285+
onMouseDownCapture={
286+
isPanel
287+
? (event) => {
288+
if (event.button !== 0 || event.ctrlKey) event.stopPropagation();
289+
}
290+
: undefined
291+
}
292+
aria-label={isPanel ? "Run context" : undefined}
208293
data-composer-context-control
209294
data-composer-shortcut={[
210295
showEnvironmentPicker && !envLocked ? "composer.host" : "",
211296
!envModeLocked ? "composer.workspace" : "",
212297
].join(" ")}
213298
>
214299
{triggerContent}
215-
<ChevronDownIcon className="size-3 shrink-0 opacity-50" />
300+
{!isPanel ? <ChevronDownIcon className="size-3 shrink-0 opacity-50" /> : null}
216301
</MenuTrigger>
217302
<MenuPopup
218303
align="start"
219-
side="top"
220-
className={previousWorktreeLabel ? "w-[min(21rem,calc(100vw-2rem))]" : undefined}
221-
{...composerFloatingLayerProps}
304+
side={isPanel ? "bottom" : "top"}
305+
className={
306+
isPanel
307+
? "w-(--anchor-width)"
308+
: previousWorktreeLabel
309+
? "w-[min(21rem,calc(100vw-2rem))]"
310+
: undefined
311+
}
312+
{...(!isPanel ? composerFloatingLayerProps : {})}
222313
>
223314
{showEnvironmentPicker && availableEnvironments && onEnvironmentChange ? (
224315
<>
@@ -618,13 +709,25 @@ export const BranchToolbar = memo(function BranchToolbar({
618709
return (
619710
<div className="flex w-full flex-col" data-thread-panel-run-context>
620711
{panelSection !== "branch" ? (
621-
<BranchToolbarEnvModeSelector
712+
<RunContextSelector
622713
displayMode="panel"
623-
envLocked={envModeLocked}
714+
workspaceRoot={activeProject.workspaceRoot}
715+
forceNewWorktree={forceNewWorktree}
716+
autoEnvironmentLabel={autoEnvironmentLabel}
717+
onAutoEnvironment={onAutoEnvironment}
718+
envLocked={envLocked}
719+
envModeLocked={envModeLocked}
720+
environmentId={environmentId}
721+
availableEnvironments={availableEnvironments}
722+
showEnvironmentPicker={showEnvironmentPicker}
723+
showEnvironmentIndicator={activeEnvironmentOption !== null}
724+
onEnvironmentChange={onEnvironmentChange}
624725
effectiveEnvMode={effectiveEnvMode}
625726
activeWorktreePath={activeWorktreePath}
626-
workspaceRoot={activeProject.workspaceRoot}
627727
onEnvModeChange={onEnvModeChange}
728+
previousWorktreeLabel={previousWorktreeLabel}
729+
previousWorktreeBranch={previousWorktreeSeed?.branch ?? null}
730+
onUsePreviousWorktree={onUsePreviousWorktree}
628731
/>
629732
) : null}
630733
{panelSection !== "workspace" ? (
@@ -662,7 +765,7 @@ export const BranchToolbar = memo(function BranchToolbar({
662765
>
663766
{showGitControls ? (
664767
<div className="contents @3xl/composer-surface:hidden">
665-
<MobileRunContextSelector
768+
<RunContextSelector
666769
forceNewWorktree={forceNewWorktree}
667770
autoEnvironmentLabel={autoEnvironmentLabel}
668771
onAutoEnvironment={onAutoEnvironment}

‎apps/web/src/components/BranchToolbarBranchSelector.tsx‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ import { threadEnvironment } from "../state/threads";
4242
import { useAtomCommand } from "../state/use-atom-command";
4343
import { vcsEnvironment } from "../state/vcs";
4444
import { cn } from "../lib/utils";
45-
import {
46-
THREAD_DETAILS_PANEL_CHEVRON_CLASS,
47-
THREAD_DETAILS_PANEL_ICON_CLASS,
48-
} from "./chat/threadDetailsPanelStyles";
45+
import { THREAD_DETAILS_PANEL_ICON_CLASS } from "./chat/threadDetailsPanelStyles";
4946
import { ThreadDetailsPrRows } from "./chat/ThreadDetailsPrRows";
5047
import { parsePullRequestReference } from "../pullRequestReference";
5148
import { getSourceControlPresentation } from "../sourceControlPresentation";
@@ -784,13 +781,9 @@ export function BranchToolbarBranchSelector({
784781
<ComposerContextLabel displayMode={displayMode}>
785782
<MiddleTruncate value={triggerLabel} className="w-full" />
786783
</ComposerContextLabel>
787-
{displayMode === "panel" ? (
788-
<span data-slot="select-icon">
789-
<ChevronDownIcon className={THREAD_DETAILS_PANEL_CHEVRON_CLASS} />
790-
</span>
791-
) : (
784+
{displayMode !== "panel" ? (
792785
<ChevronDownIcon className="size-3 shrink-0 opacity-50" />
793-
)}
786+
) : null}
794787
</ComboboxTrigger>
795788
</span>
796789
{displayMode === "panel" && prNumber !== undefined && prUrl !== undefined ? (

0 commit comments

Comments
 (0)