@@ -119,7 +119,7 @@ import { LRUCache } from "../lib/lruCache";
119119import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting" ;
120120import { RenderErrorBoundary } from "./RenderErrorBoundary" ;
121121import { useTheme } from "../hooks/useTheme" ;
122- import { getClientSettings } from "../hooks/useSettings" ;
122+ import { getClientSettings , useClientSettings } from "../hooks/useSettings" ;
123123import {
124124 chatMarkdownClipboardPayload ,
125125 serializeTableElementToCsv ,
@@ -173,6 +173,7 @@ import {
173173 openUrlInPreview ,
174174 BrowserPreviewUnavailableError ,
175175} from "../browser/openFileInPreview" ;
176+ import { resolveLinkTarget } from "../browser/browserLinkTarget" ;
176177
177178interface ChatMarkdownProps {
178179 text : string ;
@@ -2120,6 +2121,10 @@ function ChatMarkdown({
21202121 event . clipboardData . setData ( "text/html" , payload . html ) ;
21212122 } , [ ] ) ;
21222123 const openChangeRequestLink = useOpenChangeRequestLink ( threadRef ) ;
2124+ // Subscribed rather than read at click time: the anchor has to decide
2125+ // synchronously whether to intercept its `_blank`, and a subscription is what
2126+ // makes a persisted "app" apply once settings hydrate after launch.
2127+ const linkTargetPreference = useClientSettings ( ( settings ) => settings . browserLinkTarget ) ;
21232128 const resolveThreadPullRequest = useCallback (
21242129 ( href : string ) : ThreadLinkedPullRequest | null => {
21252130 if (
@@ -2472,9 +2477,35 @@ function ChatMarkdown({
24722477 }
24732478 // A link to a change request in a workspace project opens beside the
24742479 // conversation instead of in a browser: it is the thing being talked about, and
2475- // the panel it opens offers the browser as one of its actions. Anything else is
2476- // an ordinary link and keeps the `_blank` the shell already handles.
2477- if ( href ) openChangeRequestLink ( event , href ) ;
2480+ // the panel it opens offers the browser as one of its actions.
2481+ if ( ! href || openChangeRequestLink ( event , href ) ) return ;
2482+ // Anything else follows the "Open links in" setting. The system browser
2483+ // keeps the `_blank` the shell already handles; the in-app browser needs
2484+ // the click intercepted here. A modifier click is the way out of the
2485+ // in-app default, so it is left to the shell too.
2486+ if (
2487+ event . defaultPrevented ||
2488+ resolveLinkTarget ( {
2489+ url : href ,
2490+ event,
2491+ preference : linkTargetPreference ,
2492+ canOpenInApp : canOpenInPreview ,
2493+ } ) !== "app"
2494+ ) {
2495+ return ;
2496+ }
2497+ event . preventDefault ( ) ;
2498+ event . stopPropagation ( ) ;
2499+ // The click was taken from the shell, so an in-app open that fails
2500+ // hands the link to the system browser instead of dropping it.
2501+ void openExternalLinkInPreview ( href ) . then ( ( result ) => {
2502+ if ( result . _tag === "Success" || isAtomCommandInterrupted ( result ) ) return ;
2503+ reportMarkdownActionFailure (
2504+ { operation : "open-link-in-preview" , target : href } ,
2505+ result . cause ,
2506+ ) ;
2507+ void readLocalApi ( ) ?. shell . openExternal ( href ) ;
2508+ } ) ;
24782509 } }
24792510 onContextMenu = { ( event ) => {
24802511 if ( ! href || ! faviconHost ) return ;
@@ -2711,6 +2742,7 @@ function ChatMarkdown({
27112742 inlineCodeFileLinkMetaByText ,
27122743 imageBaseDir ,
27132744 isStreaming ,
2745+ linkTargetPreference ,
27142746 markdownFileLinkMetaByHref ,
27152747 onTaskListChange ,
27162748 onUseArtifactTemplate ,
0 commit comments