Skip to content

Commit c73d67c

Browse files
feat(web): let unlabelled pull request links follow the link target setting
CI check details, a pull request that has no checked-out project to open in the panel, and the repository a publish just created are ordinary links beside the thread, so they follow "Open links in" like chat links do. Controls that name their destination — Open on GitHub, the external-link icons, the in-app browser's own escape hatch — keep opening the system browser.
1 parent 33e3bb4 commit c73d67c

7 files changed

Lines changed: 118 additions & 38 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { ScopedThreadRef } from "@t3tools/contracts";
2+
import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime";
3+
import { useCallback } from "react";
4+
5+
import { readLocalApi } from "~/localApi";
6+
import { previewEnvironment } from "~/state/preview";
7+
import { useAtomCommand } from "~/state/use-atom-command";
8+
9+
import {
10+
canOpenLinksInApp,
11+
getBrowserLinkTargetPreference,
12+
resolveLinkTarget,
13+
} from "./browserLinkTarget";
14+
import { openUrlInPreview } from "./openFileInPreview";
15+
16+
const NO_MODIFIER = { metaKey: false, ctrlKey: false } as const;
17+
18+
/**
19+
* Opens a URL where the "Open links in" setting says, for buttons that sit
20+
* beside a thread but are not markdown anchors: CI check details, a pull
21+
* request that has no project to open in the panel. Without a thread there is
22+
* nowhere to put an in-app tab, so the link goes to the system browser.
23+
*
24+
* The in-app open falling over is reported through the returned promise, so
25+
* the caller decides whether that deserves a toast; the system-browser path
26+
* rejects the same way `shell.openExternal` does.
27+
*/
28+
export function useOpenLink(
29+
threadRef: ScopedThreadRef | null | undefined,
30+
): (
31+
url: string,
32+
event?: { readonly metaKey: boolean; readonly ctrlKey: boolean },
33+
) => Promise<void> {
34+
const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
35+
return useCallback(
36+
async (url, event = NO_MODIFIER) => {
37+
const target = resolveLinkTarget({
38+
url,
39+
event,
40+
preference: getBrowserLinkTargetPreference(),
41+
canOpenInApp: canOpenLinksInApp(Boolean(threadRef)),
42+
});
43+
if (target === "app" && threadRef) {
44+
const result = await openUrlInPreview({ threadRef, url, openPreview });
45+
if (result._tag === "Success" || isAtomCommandInterrupted(result)) return;
46+
throw new Error("Unable to open the link in T3 Code.", { cause: result.cause });
47+
}
48+
const api = readLocalApi();
49+
if (!api) throw new Error("Link opening is unavailable.");
50+
await api.shell.openExternal(url);
51+
},
52+
[openPreview, threadRef],
53+
);
54+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7289,6 +7289,7 @@ function ChatViewContent(props: ChatViewProps) {
72897289
<PullRequestDetailPanel
72907290
key={`${renderedRightPanelSurface.repository}#${renderedRightPanelSurface.number}`}
72917291
environmentId={activeThread.environmentId}
7292+
threadRef={activeThreadRef}
72927293
reference={{
72937294
projectId: renderedRightPanelSurface.projectId as ProjectId,
72947295
repository: renderedRightPanelSurface.repository,

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
ChevronDownIcon,
3333
CloudDownloadIcon,
3434
CloudUploadIcon,
35-
ExternalLinkIcon,
3635
GitBranchPlusIcon,
3736
GitCommitIcon,
3837
InfoIcon,
@@ -97,9 +96,9 @@ import { vcsEnvironment } from "~/state/vcs";
9796
import { randomUUID } from "~/lib/utils";
9897
import { resolvePathLinkTarget } from "~/terminal-links";
9998
import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
100-
import { readLocalApi } from "~/localApi";
10199
import { getSourceControlPresentation } from "~/sourceControlPresentation";
102-
import { openPullRequestLink, useOpenPrLink } from "~/lib/openPullRequestLink";
100+
import { useOpenLink } from "~/browser/useOpenLink";
101+
import { useOpenPrLink } from "~/lib/openPullRequestLink";
103102

104103
interface GitActionsControlProps {
105104
gitCwd: string | null;
@@ -384,10 +383,13 @@ interface PublishRepositoryDialogProps {
384383
readonly open: boolean;
385384
readonly onOpenChange: (open: boolean) => void;
386385
readonly environmentId: ScopedThreadRef["environmentId"] | null;
386+
/** Thread the dialog was opened from, so the new repository can open beside it. */
387+
readonly threadRef: ScopedThreadRef | null;
387388
readonly gitCwd: string;
388389
}
389390

390391
function PublishRepositoryDialog(props: PublishRepositoryDialogProps) {
392+
const openLink = useOpenLink(props.threadRef);
391393
const navigate = useNavigate();
392394
const sourceControlDiscovery = useEnvironmentQuery(
393395
props.environmentId === null
@@ -911,12 +913,9 @@ function PublishRepositoryDialog(props: PublishRepositoryDialogProps) {
911913
size="sm"
912914
className="w-full"
913915
onClick={() => {
914-
const api = readLocalApi();
915-
if (!api) return;
916-
void api.shell.openExternal(publishResult.repository.url);
916+
void openLink(publishResult.repository.url).catch(() => undefined);
917917
}}
918918
>
919-
<ExternalLinkIcon className="size-3.5" aria-hidden />
920919
Open on {publishProviderLabel}
921920
</Button>
922921
</>
@@ -1004,6 +1003,7 @@ export default function GitActionsControl({
10041003
[activeThreadRef],
10051004
);
10061005
const openPrLink = useOpenPrLink(activeThreadRef ?? undefined);
1006+
const openLink = useOpenLink(activeThreadRef);
10071007
const activeDraftThread = useComposerDraftStore((store) =>
10081008
draftId
10091009
? store.getDraftSession(draftId)
@@ -1238,15 +1238,6 @@ export default function GitActionsControl({
12381238
onOpenPullRequest(openPr.number);
12391239
return;
12401240
}
1241-
const api = readLocalApi();
1242-
if (!api) {
1243-
toastManager.add({
1244-
type: "error",
1245-
title: "Link opening is unavailable.",
1246-
data: threadToastData,
1247-
});
1248-
return;
1249-
}
12501241
const prUrl = openPr?.url ?? null;
12511242
if (!prUrl) {
12521243
toastManager.add({
@@ -1256,7 +1247,7 @@ export default function GitActionsControl({
12561247
});
12571248
return;
12581249
}
1259-
void openPullRequestLink(api.shell, prUrl).catch((err: unknown) => {
1250+
void openLink(prUrl).catch((err: unknown) => {
12601251
console.error(err);
12611252
toastManager.add(
12621253
stackedThreadToast({
@@ -1267,7 +1258,7 @@ export default function GitActionsControl({
12671258
}),
12681259
);
12691260
});
1270-
}, [gitStatusForActions, onOpenPullRequest, threadToastData]);
1261+
}, [gitStatusForActions, onOpenPullRequest, openLink, threadToastData]);
12711262

12721263
runGitActionWithToast = useEffectEvent(
12731264
async ({
@@ -2010,6 +2001,7 @@ export default function GitActionsControl({
20102001
open={isPublishDialogOpen}
20112002
onOpenChange={setIsPublishDialogOpen}
20122003
environmentId={activeEnvironmentId}
2004+
threadRef={activeThreadRef}
20132005
gitCwd={gitCwd}
20142006
/>
20152007

‎apps/web/src/components/pullRequest/PullRequestChecksPopover.tsx‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import type {
33
PullRequestCheck,
44
PullRequestChecksState,
55
PullRequestRef,
6+
ScopedThreadRef,
67
} from "@t3tools/contracts";
78

8-
import { readLocalApi } from "~/localApi";
9+
import { useOpenLink } from "~/browser/useOpenLink";
910
import { cn } from "~/lib/utils";
1011
import { pullRequestEnvironment } from "~/state/pullRequests";
1112
import { useEnvironmentQuery } from "~/state/query";
@@ -27,9 +28,11 @@ import {
2728
function LazyChecksBody({
2829
environmentId,
2930
reference,
31+
threadRef,
3032
}: {
3133
environmentId: EnvironmentId;
3234
reference: PullRequestRef;
35+
threadRef: ScopedThreadRef | null;
3336
}) {
3437
const detailQuery = useEnvironmentQuery(
3538
pullRequestEnvironment.detail({ environmentId, input: reference }),
@@ -44,10 +47,17 @@ function LazyChecksBody({
4447
</p>
4548
);
4649
}
47-
return <ChecksBody checks={detailQuery.data.checks} />;
50+
return <ChecksBody checks={detailQuery.data.checks} threadRef={threadRef} />;
4851
}
4952

50-
function ChecksBody({ checks }: { checks: ReadonlyArray<PullRequestCheck> }) {
53+
function ChecksBody({
54+
checks,
55+
threadRef,
56+
}: {
57+
checks: ReadonlyArray<PullRequestCheck>;
58+
threadRef: ScopedThreadRef | null;
59+
}) {
60+
const openLink = useOpenLink(threadRef);
5161
if (checks.length === 0) {
5262
return <p className="text-muted-foreground text-xs">No checks reported</p>;
5363
}
@@ -71,7 +81,9 @@ function ChecksBody({ checks }: { checks: ReadonlyArray<PullRequestCheck> }) {
7181
<button
7282
type="button"
7383
className="shrink-0 text-primary hover:underline"
74-
onClick={() => void readLocalApi()?.shell.openExternal(check.url ?? "")}
84+
onClick={() => {
85+
if (check.url) void openLink(check.url).catch(() => undefined);
86+
}}
7587
>
7688
Details
7789
</button>
@@ -94,13 +106,16 @@ export function PullRequestChecksPopover({
94106
checks,
95107
environmentId,
96108
reference,
109+
threadRef = null,
97110
className,
98111
}: {
99112
checksState: PullRequestChecksState;
100113
/** The checks already in hand, for the detail header. Absent on a listing row. */
101114
checks?: ReadonlyArray<PullRequestCheck>;
102115
environmentId?: EnvironmentId;
103116
reference?: PullRequestRef;
117+
/** Thread the popover sits beside; a listing row has none. */
118+
threadRef?: ScopedThreadRef | null;
104119
className?: string;
105120
}) {
106121
const presentation = pullRequestChecksStatePresentation(checksState);
@@ -129,9 +144,13 @@ export function PullRequestChecksPopover({
129144
<p className="mb-2 font-medium text-sm">{presentation.label}</p>
130145
{summary === null ? null : <p className="mb-2 text-muted-foreground text-xs">{summary}</p>}
131146
{checks !== undefined ? (
132-
<ChecksBody checks={checks} />
147+
<ChecksBody checks={checks} threadRef={threadRef} />
133148
) : environmentId !== undefined && reference !== undefined ? (
134-
<LazyChecksBody environmentId={environmentId} reference={reference} />
149+
<LazyChecksBody
150+
environmentId={environmentId}
151+
reference={reference}
152+
threadRef={threadRef}
153+
/>
135154
) : null}
136155
</PopoverPopup>
137156
</Popover>

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ function PullRequestBaseFreshnessWarning({
445445

446446
export function PullRequestDetailPanel({
447447
environmentId,
448+
threadRef = null,
448449
reference,
449450
refreshToken: forcedRefreshToken = 0,
450451
onActed,
@@ -454,6 +455,13 @@ export function PullRequestDetailPanel({
454455
composerDraftTarget,
455456
}: {
456457
environmentId: EnvironmentId;
458+
/**
459+
* The thread this panel sits beside, if any. Links that are not the pull
460+
* request itself (check details, host permalinks) can open in that thread's
461+
* in-app browser when the user has asked for it; the page has no thread, so
462+
* there they always go to the system browser.
463+
*/
464+
threadRef?: ScopedThreadRef | null;
457465
reference: PullRequestRef;
458466
/**
459467
* Bumped by whatever holds the panel when a reader asks for everything on screen to be read
@@ -2027,7 +2035,11 @@ export function PullRequestDetailPanel({
20272035
aria-label={checksSummary ? `Checks: ${checksSummary}` : "Checks"}
20282036
>
20292037
{checksState !== null ? (
2030-
<PullRequestChecksPopover checks={detail.checks} checksState={checksState} />
2038+
<PullRequestChecksPopover
2039+
checks={detail.checks}
2040+
checksState={checksState}
2041+
threadRef={threadRef}
2042+
/>
20312043
) : (
20322044
<CircleDotIcon aria-hidden className="size-3.5" />
20332045
)}
@@ -2151,6 +2163,7 @@ export function PullRequestDetailPanel({
21512163
<div className={cn("absolute inset-0", tab !== "summary" && "invisible")}>
21522164
<PullRequestSummaryTab
21532165
environmentId={environmentId}
2166+
threadRef={threadRef}
21542167
reference={reference}
21552168
detail={detail}
21562169
activityPending={activityPending}

‎apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
PullRequestComment,
55
PullRequestDetailView,
66
PullRequestRef,
7+
ScopedThreadRef,
78
} from "@t3tools/contracts";
89
import {
910
ArrowDownUpIcon,
@@ -23,7 +24,7 @@ import { useRef, useState, type ReactNode } from "react";
2324
import { useAtomCommand } from "~/state/use-atom-command";
2425
import { pullRequestEnvironment } from "~/state/pullRequests";
2526
import { cn } from "~/lib/utils";
26-
import { readLocalApi } from "~/localApi";
27+
import { useOpenLink } from "~/browser/useOpenLink";
2728
import { formatRelativeTimeLabel } from "~/timestampFormat";
2829

2930
import { Button } from "../ui/button";
@@ -423,6 +424,7 @@ const COMMENT_PAGE = 30;
423424

424425
export function PullRequestSummaryTab({
425426
environmentId,
427+
threadRef,
426428
reference,
427429
detail,
428430
activityPending,
@@ -436,6 +438,7 @@ export function PullRequestSummaryTab({
436438
onRefresh,
437439
}: {
438440
environmentId: EnvironmentId;
441+
threadRef: ScopedThreadRef | null;
439442
reference: PullRequestRef;
440443
detail: PullRequestDetailView;
441444
activityPending: boolean;
@@ -508,8 +511,12 @@ export function PullRequestSummaryTab({
508511
),
509512
);
510513

514+
const openLink = useOpenLink(threadRef);
511515
const openCheck = (url: string) => {
512-
void readLocalApi()?.shell.openExternal(url);
516+
void openLink(url).catch((error: unknown) => {
517+
console.error(error);
518+
toastManager.add({ type: "error", title: "Unable to open check details" });
519+
});
513520
};
514521

515522
const update = useAtomCommand(pullRequestEnvironment.update, { reportFailure: false });

‎apps/web/src/lib/openPullRequestLink.ts‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { type MouseEvent, useCallback } from "react";
1111

1212
import { pullRequestHostOf, type SourceControlProviderKind } from "@t3tools/contracts";
1313

14+
import { useOpenLink } from "../browser/useOpenLink";
1415
import { stackedThreadToast, toastManager } from "../components/ui/toast";
15-
import { readLocalApi } from "../localApi";
1616
import { useRightPanelStore } from "../rightPanelStore";
1717
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
1818

@@ -328,6 +328,7 @@ export function useOpenChangeRequestLink(
328328

329329
export function useOpenPrLink(threadRef?: ScopedThreadRef) {
330330
const openChangeRequest = useOpenChangeRequestLink(threadRef);
331+
const openLink = useOpenLink(threadRef);
331332
return useCallback(
332333
(event: MouseEvent<HTMLElement>, prUrl: string, targetThreadRef?: ScopedThreadRef) => {
333334
event.stopPropagation();
@@ -342,16 +343,9 @@ export function useOpenPrLink(threadRef?: ScopedThreadRef) {
342343
event.preventDefault();
343344
if (!openInBrowser && openChangeRequest(event, prUrl, targetThreadRef)) return true;
344345

345-
const api = readLocalApi();
346-
if (!api) {
347-
toastManager.add({
348-
type: "error",
349-
title: "Link opening is unavailable.",
350-
});
351-
return false;
352-
}
353-
354-
void openPullRequestLink(api.shell, prUrl).catch((error) => {
346+
// No project to show it in, so it is an ordinary link and follows the
347+
// "Open links in" setting; the modifier still forces the system browser.
348+
void openLink(prUrl, event).catch((error: unknown) => {
355349
console.error(error);
356350
toastManager.add(
357351
stackedThreadToast({

0 commit comments

Comments
 (0)