11import { scopeProjectRef , scopeThreadRef } from "@t3tools/client-runtime/environment" ;
22import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" ;
3+ import type { EnvironmentId , ScopedThreadRef } from "@t3tools/contracts" ;
4+ import { useRouter } from "@tanstack/react-router" ;
35import { useEffect , useEffectEvent , useRef } from "react" ;
46
57import { openFileInPreview , openUrlInPreview } from "../../browser/openFileInPreview" ;
8+ import { isPreviewAvailableFor } from "../../browser/previewRuntime" ;
9+ import { useComposerDraftStore } from "../../composerDraftStore" ;
610import { useNewThreadHandler } from "../../hooks/useHandleNewThread" ;
711import { useScratchProject } from "../../hooks/useScratchProject" ;
8- import { useRightPanelStore } from "../../rightPanelStore" ;
12+ import { selectActiveRightPanelSurface , useRightPanelStore } from "../../rightPanelStore" ;
13+ import { resolveThreadRouteTarget } from "../../threadRoutes" ;
914import { assetEnvironment } from "../../state/assets" ;
1015import { useEnvironmentHttpBaseUrl , usePrimaryEnvironment } from "../../state/environments" ;
1116import { previewEnvironment } from "../../state/preview" ;
1217import { useAtomCommand } from "../../state/use-atom-command" ;
1318import { useAtomQueryRunner } from "../../state/use-atom-query-runner" ;
1419import { stackedThreadToast , toastManager } from "../ui/toast" ;
1520
21+ /**
22+ * The thread on screen when its browser panel is showing, so a link opens as
23+ * another tab there, the way a browser window takes new links. HTML files are
24+ * served by this machine's environment, so only its threads can take them.
25+ */
26+ function threadShowingBrowser (
27+ routeParams : Record < string , string | undefined > ,
28+ fileEnvironmentId : EnvironmentId | null ,
29+ ) : ScopedThreadRef | null {
30+ const target = resolveThreadRouteTarget ( routeParams ) ;
31+ if ( target === null ) return null ;
32+ let threadRef = target . kind === "server" ? target . threadRef : null ;
33+ if ( target . kind === "draft" ) {
34+ const session = useComposerDraftStore . getState ( ) . getDraftSession ( target . draftId ) ;
35+ if ( ! session || session . promotedTo ) return null ;
36+ threadRef = scopeThreadRef ( session . environmentId , session . threadId ) ;
37+ }
38+ if ( threadRef === null || ! isPreviewAvailableFor ( threadRef . environmentId ) ) return null ;
39+ if ( fileEnvironmentId !== null && threadRef . environmentId !== fileEnvironmentId ) return null ;
40+ const surface = selectActiveRightPanelSurface (
41+ useRightPanelStore . getState ( ) . byThreadKey ,
42+ threadRef ,
43+ ) ;
44+ return surface ?. kind === "preview" ? threadRef : null ;
45+ }
46+
1647/**
1748 * Opens each web link or HTML file the OS hands T3 Code as the default browser
18- * (macOS) in a new thread without a project, its browser panel maximized on the page.
49+ * (macOS). With a browser panel on screen the page opens as a new tab there;
50+ * otherwise it starts a thread without a project, its browser panel maximized.
1951 */
2052export function DesktopWebLinkCoordinator ( ) {
53+ const router = useRouter ( ) ;
2154 const primaryEnvironment = usePrimaryEnvironment ( ) ;
2255 const { scratchEnvironmentId, openScratchProject } = useScratchProject ( ) ;
2356 const openThread = useNewThreadHandler ( ) ;
@@ -34,20 +67,29 @@ export function DesktopWebLinkCoordinator() {
3467 primaryEnvironment ?. connection . phase === "connected" &&
3568 primaryEnvironment . serverConfig !== null ;
3669
37- const openLink = useEffectEvent ( async ( url : string ) => {
70+ const startThread = async ( ) : Promise < ScopedThreadRef | null > => {
3871 const environmentId = scratchEnvironmentId ( primaryEnvironment ?. environmentId ?? null ) ;
39- if ( environmentId === null ) return ;
72+ if ( environmentId === null ) return null ;
4073 const project = await openScratchProject ( environmentId , "Could not open the link" ) ;
41- if ( ! project ) return ;
42- // Each link is its own thread, even before the user types in the last one.
43- const opened = await openThread ( scopeProjectRef ( project . environmentId , project . id ) , {
44- fresh : true ,
45- } ) ;
46- if ( ! opened ) return ;
74+ if ( ! project ) return null ;
75+ const opened = await openThread ( scopeProjectRef ( project . environmentId , project . id ) ) ;
76+ if ( ! opened ) return null ;
4777 const threadRef = scopeThreadRef ( project . environmentId , opened . threadId ) ;
4878 useRightPanelStore . getState ( ) . requestMaximize ( threadRef ) ;
79+ return threadRef ;
80+ } ;
81+
82+ const openLink = useEffectEvent ( async ( url : string ) => {
83+ const isFile = url . startsWith ( "file:" ) ;
84+ const routeParams = router . state . matches . at ( - 1 ) ?. params ?? { } ;
85+ const threadRef =
86+ threadShowingBrowser (
87+ routeParams ,
88+ isFile ? ( primaryEnvironment ?. environmentId ?? null ) : null ,
89+ ) ?? ( await startThread ( ) ) ;
90+ if ( threadRef === null ) return ;
4991 // An HTML file is served from the environment, which runs on this machine.
50- const result = url . startsWith ( "file:" )
92+ const result = isFile
5193 ? httpBaseUrl === null
5294 ? null
5395 : await openFileInPreview ( {
0 commit comments