|
3 | 3 | isAtomCommandInterrupted, |
4 | 4 | squashAtomCommandFailure, |
5 | 5 | } from "@t3tools/client-runtime/state/runtime"; |
6 | | -import { useCallback } from "react"; |
7 | | - |
8 | 6 | import { recordVisitForThread } from "~/browserHistoryStore"; |
9 | 7 | import { readLocalApi } from "~/localApi"; |
10 | 8 | import { previewEnvironment } from "~/state/preview"; |
@@ -36,33 +34,30 @@ export function useOpenLink(threadRef: ScopedThreadRef | null | undefined): ( |
36 | 34 | }, |
37 | 35 | ) => Promise<void> { |
38 | 36 | const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false }); |
39 | | - return useCallback( |
40 | | - async (url, options = {}) => { |
41 | | - const targetThreadRef = options.threadRef ?? threadRef; |
42 | | - const target = resolveLinkTarget({ |
43 | | - url, |
44 | | - event: options.event ?? NO_MODIFIER, |
45 | | - preference: await resolveBrowserLinkTargetPreference(), |
46 | | - canOpenInApp: |
47 | | - targetThreadRef != null && |
48 | | - readEnvironmentScope(targetThreadRef.environmentId, AuthPreviewOperateScope) && |
49 | | - canOpenLinksInApp(targetThreadRef), |
50 | | - }); |
51 | | - if (target === "app" && targetThreadRef) { |
52 | | - const result = await openUrlInPreview({ threadRef: targetThreadRef, url, openPreview }); |
53 | | - if (isAtomCommandInterrupted(result)) return; |
54 | | - if (result._tag === "Success") { |
55 | | - recordVisitForThread(targetThreadRef, url); |
56 | | - return; |
57 | | - } |
58 | | - const failure = squashAtomCommandFailure(result); |
59 | | - if (failure instanceof BrowserSettingsReadError) throw failure; |
60 | | - console.error(result.cause); |
| 37 | + return async (url, options = {}) => { |
| 38 | + const targetThreadRef = options.threadRef ?? threadRef; |
| 39 | + const target = resolveLinkTarget({ |
| 40 | + url, |
| 41 | + event: options.event ?? NO_MODIFIER, |
| 42 | + preference: await resolveBrowserLinkTargetPreference(), |
| 43 | + canOpenInApp: |
| 44 | + targetThreadRef != null && |
| 45 | + readEnvironmentScope(targetThreadRef.environmentId, AuthPreviewOperateScope) && |
| 46 | + canOpenLinksInApp(targetThreadRef), |
| 47 | + }); |
| 48 | + if (target === "app" && targetThreadRef) { |
| 49 | + const result = await openUrlInPreview({ threadRef: targetThreadRef, url, openPreview }); |
| 50 | + if (isAtomCommandInterrupted(result)) return; |
| 51 | + if (result._tag === "Success") { |
| 52 | + recordVisitForThread(targetThreadRef, url); |
| 53 | + return; |
61 | 54 | } |
62 | | - const api = readLocalApi(); |
63 | | - if (!api) throw new Error("Link opening is unavailable."); |
64 | | - await api.shell.openExternal(url); |
65 | | - }, |
66 | | - [openPreview, threadRef], |
67 | | - ); |
| 55 | + const failure = squashAtomCommandFailure(result); |
| 56 | + if (failure instanceof BrowserSettingsReadError) throw failure; |
| 57 | + console.error(result.cause); |
| 58 | + } |
| 59 | + const api = readLocalApi(); |
| 60 | + if (!api) throw new Error("Link opening is unavailable."); |
| 61 | + await api.shell.openExternal(url); |
| 62 | + }; |
68 | 63 | } |
0 commit comments