Open History to see the last commit
1import { AuthPreviewOperateScope, type ScopedThreadRef } from "@t3tools/contracts";
2import {
3 isAtomCommandInterrupted,
4 squashAtomCommandFailure,
5} from "@t3tools/client-runtime/state/runtime";
6import { useCallback } from "react";
7
8import { recordVisitForThread } from "~/browserHistoryStore";
9import { readLocalApi } from "~/localApi";
10import { previewEnvironment } from "~/state/preview";
11import { readEnvironmentScope } from "~/state/session";
12import { useAtomCommand } from "~/state/use-atom-command";
13
14import {
15 canOpenLinksInApp,
16 resolveBrowserLinkTargetPreference,
17 resolveLinkTarget,
18} from "./browserLinkTarget";
19import { BrowserSettingsReadError, openUrlInPreview } from "./openFileInPreview";
20
21const NO_MODIFIER = { metaKey: false, ctrlKey: false } as const;
22
23/**
24 * Opens a URL where the "Open links in" setting says, for buttons that sit
25 * beside a thread but are not markdown anchors: CI check details, a pull
26 * request that has no project to open in the panel. Without a thread there is
27 * nowhere to put an in-app tab, so the link goes to the system browser.
28 *
29 * An in-app open that fails falls back to the system browser rather than
30 * dropping the click: the user asked for the link, and the setting only says
31 * where it should go first. Failed settings reads reject without opening a
32 * browser. The promise also rejects if the system-browser fallback fails.
33 */
34export function useOpenLink(threadRef: ScopedThreadRef | null | undefined): (
35 url: string,
36 options?: {
37 readonly event?: {
38 readonly metaKey: boolean;
39 readonly ctrlKey: boolean;
40 readonly button?: number | undefined;
41 };
42 /** Thread to open beside when it is not the hook's own, e.g. a sidebar row's. */
43 readonly threadRef?: ScopedThreadRef | undefined;
44 },
45) => Promise<void> {
46 const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
47 return useCallback(
48 async (url, options = {}) => {
49 const targetThreadRef = options.threadRef ?? threadRef;
50 const target = resolveLinkTarget({
51 url,
52 event: options.event ?? NO_MODIFIER,
53 preference: await resolveBrowserLinkTargetPreference(),
54 canOpenInApp:
55 targetThreadRef != null &&
56 readEnvironmentScope(targetThreadRef.environmentId, AuthPreviewOperateScope) &&
57 canOpenLinksInApp(targetThreadRef),
58 });
59 if (target === "app" && targetThreadRef) {
60 const result = await openUrlInPreview({ threadRef: targetThreadRef, url, openPreview });
61 if (isAtomCommandInterrupted(result)) return;
62 if (result._tag === "Success") {
63 recordVisitForThread(targetThreadRef, url);
64 return;
65 }
66 const failure = squashAtomCommandFailure(result);
67 if (failure instanceof BrowserSettingsReadError) throw failure;
68 console.error(result.cause);
69 }
70 const api = readLocalApi();
71 if (!api) throw new Error("Link opening is unavailable.");
72 await api.shell.openExternal(url);
73 },
74 [openPreview, threadRef],
75 );
76}