Skip to content

Commit 0e98aa1

Browse files
committed
feat(web): open diff files in external editor
1 parent 2460649 commit 0e98aa1

6 files changed

Lines changed: 162 additions & 45 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { ReactNode, Ref } from "react";
2+
import { Button } from "./ui/button";
3+
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
4+
5+
export function DiffFileHeaderActionButton({
6+
ref,
7+
ariaLabel,
8+
children,
9+
disabled,
10+
onClick,
11+
tooltip,
12+
}: {
13+
ref?: Ref<HTMLButtonElement>;
14+
ariaLabel: string;
15+
children: ReactNode;
16+
disabled?: boolean;
17+
onClick: () => void;
18+
tooltip: string;
19+
}) {
20+
return (
21+
<Tooltip>
22+
<TooltipTrigger
23+
render={
24+
<Button
25+
ref={ref}
26+
size="icon-micro"
27+
variant="ghost"
28+
className="text-muted-foreground [:hover,[data-pressed]]:bg-transparent"
29+
aria-label={ariaLabel}
30+
disabled={disabled}
31+
onClick={onClick}
32+
/>
33+
}
34+
>
35+
{children}
36+
</TooltipTrigger>
37+
<TooltipPopup>
38+
<p>{tooltip}</p>
39+
</TooltipPopup>
40+
</Tooltip>
41+
);
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ExternalLinkIcon } from "lucide-react";
2+
import { DiffFileHeaderActionButton } from "./DiffFileHeaderActionButton";
3+
4+
export function DiffFileOpenInEditorButton({
5+
filePath,
6+
disabled,
7+
onOpen,
8+
}: {
9+
filePath: string;
10+
disabled: boolean;
11+
onOpen: (filePath: string) => void;
12+
}) {
13+
return (
14+
<DiffFileHeaderActionButton
15+
ariaLabel={`Open ${filePath} in editor`}
16+
disabled={disabled}
17+
onClick={() => onOpen(filePath)}
18+
tooltip="Open in editor"
19+
>
20+
<ExternalLinkIcon aria-hidden />
21+
</DiffFileHeaderActionButton>
22+
);
23+
}

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {
66
showAnchoredCopyErrorToast,
77
showAnchoredCopySuccessToast,
88
} from "./ui/anchoredCopyToast";
9-
import { Button } from "./ui/button";
10-
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
9+
import { DiffFileHeaderActionButton } from "./DiffFileHeaderActionButton";
1110

1211
export function DiffFilePathCopyButton({ filePath }: { filePath: string }) {
1312
const ref = useRef<HTMLButtonElement>(null);
@@ -18,24 +17,13 @@ export function DiffFilePathCopyButton({ filePath }: { filePath: string }) {
1817
});
1918

2019
return (
21-
<Tooltip>
22-
<TooltipTrigger
23-
render={
24-
<Button
25-
ref={ref}
26-
size="icon-micro"
27-
variant="ghost"
28-
className="text-muted-foreground [:hover,[data-pressed]]:bg-transparent"
29-
aria-label="Copy file path"
30-
onClick={() => copyToClipboard(filePath, undefined)}
31-
/>
32-
}
33-
>
34-
{isCopied ? <CheckIcon className="size-3 text-success" /> : <CopyIcon className="size-3" />}
35-
</TooltipTrigger>
36-
<TooltipPopup>
37-
<p>{isCopied ? "Copied" : "Copy path"}</p>
38-
</TooltipPopup>
39-
</Tooltip>
20+
<DiffFileHeaderActionButton
21+
ref={ref}
22+
ariaLabel="Copy file path"
23+
onClick={() => copyToClipboard(filePath, undefined)}
24+
tooltip={isCopied ? "Copied" : "Copy path"}
25+
>
26+
{isCopied ? <CheckIcon aria-hidden className="text-success" /> : <CopyIcon aria-hidden />}
27+
</DiffFileHeaderActionButton>
4028
);
4129
}

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

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import * as Schema from "effect/Schema";
2626
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2727
import { useOpenInPreferredEditor } from "../editorPreferences";
2828
import { type DraftId } from "../composerDraftStore";
29-
import { openDiffFilePrimaryAction } from "../diffFileActions";
29+
import { openDiffFileInEditor, openDiffFilePrimaryAction } from "../diffFileActions";
3030
import { useCheckpointDiff } from "~/lib/checkpointDiffState";
3131
import { cn } from "~/lib/utils";
3232
import { selectThreadDiffPanelSelection, useDiffPanelStore } from "../diffPanelStore";
@@ -49,6 +49,7 @@ import { useProject, useThread } from "../state/entities";
4949
import { resolveThreadRouteRef } from "../threadRoutes";
5050
import { useClientSettings, useUpdateClientSettings } from "../hooks/useSettings";
5151
import { formatShortTimestamp } from "../timestampFormat";
52+
import { DiffFileOpenInEditorButton } from "./DiffFileOpenInEditorButton";
5253
import { DiffFilePathCopyButton } from "./DiffFilePathCopyButton";
5354
import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell";
5455
import { DiffStatLabel } from "./chat/DiffStatLabel";
@@ -475,34 +476,51 @@ export default function DiffPanel({
475476
[codeViewFiles, collapseScopeKey],
476477
);
477478

479+
const launchDiffFileInEditor = useCallback(
480+
(targetPath: string) => {
481+
void (async () => {
482+
const result = await openInPreferredEditor(targetPath);
483+
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
484+
console.warn("Failed to open diff file in editor.", {
485+
operation: "open-diff-file",
486+
...(routeThreadRef
487+
? {
488+
environmentId: routeThreadRef.environmentId,
489+
threadId: routeThreadRef.threadId,
490+
}
491+
: {}),
492+
...safeErrorLogAttributes(squashAtomCommandFailure(result)),
493+
});
494+
}
495+
})();
496+
},
497+
[openInPreferredEditor, routeThreadRef],
498+
);
478499
const openDiffFile = useCallback(
479500
(filePath: string) => {
480501
openDiffFilePrimaryAction({
481502
threadRef: routeThreadRef,
482503
filePath,
483504
activeCwd,
484505
repositoryRoot: activeRepositoryRoot,
485-
openInEditor: (targetPath) => {
486-
void (async () => {
487-
const result = await openInPreferredEditor(targetPath);
488-
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
489-
console.warn("Failed to open diff file in editor.", {
490-
operation: "open-diff-file",
491-
...(routeThreadRef
492-
? {
493-
environmentId: routeThreadRef.environmentId,
494-
threadId: routeThreadRef.threadId,
495-
}
496-
: {}),
497-
...safeErrorLogAttributes(squashAtomCommandFailure(result)),
498-
});
499-
}
500-
})();
501-
},
506+
openInEditor: launchDiffFileInEditor,
507+
});
508+
},
509+
[activeCwd, activeRepositoryRoot, launchDiffFileInEditor, routeThreadRef],
510+
);
511+
const openDiffFileExternally = useCallback(
512+
(filePath: string) => {
513+
openDiffFileInEditor({
514+
filePath,
515+
activeCwd,
516+
repositoryRoot: activeRepositoryRoot,
517+
openInEditor: launchDiffFileInEditor,
502518
});
503519
},
504-
[activeCwd, activeRepositoryRoot, openInPreferredEditor, routeThreadRef],
520+
[activeCwd, activeRepositoryRoot, launchDiffFileInEditor],
505521
);
522+
const canOpenDiffFileExternally =
523+
activeCwd != null && activeThread != null && (serverConfig?.availableEditors.length ?? 0) > 0;
506524
const toggleDiffFileCollapsed = useCallback(
507525
(fileKey: string) => {
508526
setCollapsedDiffFiles((current) => {
@@ -951,7 +969,8 @@ export default function DiffPanel({
951969
node instanceof HTMLElement && node.hasAttribute("data-title"),
952970
);
953971
const filePath = title?.textContent?.trim();
954-
// The filename remains the explicit "open in editor" affordance.
972+
// The filename is the internal file-viewer affordance. Header buttons keep
973+
// external actions separate and return above before reaching this handler.
955974
if (filePath) {
956975
openDiffFile(filePath);
957976
return;
@@ -979,9 +998,19 @@ export default function DiffPanel({
979998
sectionId={reviewSectionId}
980999
sectionTitle={reviewSectionTitle}
9811000
composerDraftTarget={composerDraftTarget}
982-
renderHeaderFilenameSuffix={(fileDiff) => (
983-
<DiffFilePathCopyButton filePath={resolveFileDiffPath(fileDiff)} />
984-
)}
1001+
renderHeaderFilenameSuffix={(fileDiff) => {
1002+
const filePath = resolveFileDiffPath(fileDiff);
1003+
return (
1004+
<span className="inline-flex items-center gap-0.5">
1005+
<DiffFilePathCopyButton filePath={filePath} />
1006+
<DiffFileOpenInEditorButton
1007+
filePath={filePath}
1008+
disabled={!canOpenDiffFileExternally}
1009+
onOpen={openDiffFileExternally}
1010+
/>
1011+
</span>
1012+
);
1013+
}}
9851014
renderHeaderPrefix={(fileDiff, fileKey, collapsed) => {
9861015
const filePath = resolveFileDiffPath(fileDiff);
9871016
return (

‎apps/web/src/diffFileActions.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment";
22
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
33
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
44

5-
import { openDiffFilePrimaryAction, resolveDiffPathForWorkspace } from "./diffFileActions";
5+
import {
6+
openDiffFileInEditor,
7+
openDiffFilePrimaryAction,
8+
resolveDiffPathForWorkspace,
9+
} from "./diffFileActions";
610
import { selectThreadRightPanelState, useRightPanelStore } from "./rightPanelStore";
711

812
const THREAD_REF = scopeThreadRef(
@@ -69,6 +73,19 @@ describe("openDiffFilePrimaryAction", () => {
6973
expect(openInEditor).not.toHaveBeenCalled();
7074
});
7175

76+
it("can explicitly open a repository-relative diff file in the editor", () => {
77+
const openInEditor = vi.fn();
78+
79+
openDiffFileInEditor({
80+
filePath: "frontend/Dockerfile",
81+
activeCwd: "/repo/frontend",
82+
repositoryRoot: "/repo",
83+
openInEditor,
84+
});
85+
86+
expect(openInEditor).toHaveBeenCalledWith("/repo/frontend/Dockerfile");
87+
});
88+
7289
it("preserves repository-relative paths in a separate worktree", () => {
7390
expect(
7491
resolveDiffPathForWorkspace({

‎apps/web/src/diffFileActions.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ export function resolveDiffPathForWorkspace(input: {
7777
return relativeSegments.length > 0 ? relativeSegments.join("/") : null;
7878
}
7979

80+
type OpenDiffFileInEditorInput = Omit<OpenDiffFilePrimaryActionInput, "threadRef">;
81+
82+
export function openDiffFileInEditor({
83+
filePath,
84+
activeCwd,
85+
repositoryRoot,
86+
openInEditor,
87+
}: OpenDiffFileInEditorInput): void {
88+
const workspaceFilePath = resolveDiffPathForWorkspace({
89+
filePath,
90+
workspaceRoot: activeCwd,
91+
repositoryRoot,
92+
});
93+
if (!workspaceFilePath) return;
94+
95+
openInEditor(activeCwd ? resolvePathLinkTarget(workspaceFilePath, activeCwd) : workspaceFilePath);
96+
}
97+
8098
export function openDiffFilePrimaryAction({
8199
threadRef,
82100
filePath,

0 commit comments

Comments
 (0)