@@ -76,7 +76,8 @@ import { mediaFileReference, mediaUrlReference } from "@t3tools/client-runtime/m
7676import { MISSING_FILE_ACCESS_MESSAGE } from "@t3tools/client-runtime/state/assets" ;
7777import { mediaKindFromPath , mediaMimeTypeFromExtension } from "@t3tools/shared/filePreview" ;
7878import * as Cause from "effect/Cause" ;
79- import { AsyncResult } from "effect/reactivity" ;
79+ import * as Option from "effect/Option" ;
80+ import { AsyncResult , Atom } from "effect/reactivity" ;
8081import React , {
8182 Children ,
8283 Suspense ,
@@ -184,7 +185,11 @@ import { readThreadShell, useProjects } from "../state/entities";
184185import { serverEnvironment } from "../state/server" ;
185186import { shellEnvironment } from "../state/shell" ;
186187import { assetEnvironment } from "../state/assets" ;
187- import { readEnvironmentScope , usePreparedConnection , useEnvironmentScope } from "../state/session" ;
188+ import {
189+ readEnvironmentScope ,
190+ readPreparedConnection ,
191+ useEnvironmentScope ,
192+ } from "../state/session" ;
188193import { previewEnvironment } from "../state/preview" ;
189194import { useAtomCommand } from "../state/use-atom-command" ;
190195import { useAtomQueryRunner } from "../state/use-atom-query-runner" ;
@@ -2194,6 +2199,8 @@ function areMarkdownFileLinkPropsEqual(
21942199
21952200const NO_MARKDOWN_EDITORS : ReadonlyArray < EditorId > = [ ] ;
21962201
2202+ type MarkdownServerConfigFields = ReturnType < typeof markdownServerConfigFields > ;
2203+
21972204/** The server config fields markdown reads. */
21982205function markdownServerConfigFields ( config : ServerConfig | null ) {
21992206 return {
@@ -2208,6 +2215,29 @@ function markdownServerConfigFields(config: ServerConfig | null) {
22082215 } ;
22092216}
22102217
2218+ function markdownServerConfigFieldsEqual (
2219+ previous : MarkdownServerConfigFields ,
2220+ next : MarkdownServerConfigFields ,
2221+ ) : boolean {
2222+ return (
2223+ previous . pullRequestsEnabled === next . pullRequestsEnabled &&
2224+ previous . revealInFileManagerLabel === next . revealInFileManagerLabel &&
2225+ previous . availableEditors . length === next . availableEditors . length &&
2226+ previous . availableEditors . every ( ( editor , index ) => editor === next . availableEditors [ index ] )
2227+ ) ;
2228+ }
2229+
2230+ // The config object changes with every provider status push and snapshot. The
2231+ // atom keeps its previous value while the fields markdown reads are equal, so
2232+ // those pushes don't re-render every visible reply.
2233+ const markdownServerConfigFieldsAtom = Atom . family ( ( environmentId : EnvironmentId | null ) =>
2234+ Atom . make ( ( get ) => {
2235+ const next = markdownServerConfigFields ( get ( serverEnvironment . configValueAtom ( environmentId ) ) ) ;
2236+ const previous = Option . getOrNull ( get . self < MarkdownServerConfigFields > ( ) ) ;
2237+ return previous !== null && markdownServerConfigFieldsEqual ( previous , next ) ? previous : next ;
2238+ } ) . pipe ( Atom . withLabel ( "markdown-server-config-fields" ) ) ,
2239+ ) ;
2240+
22112241function useChatMarkdownState ( {
22122242 text,
22132243 cwd,
@@ -2279,7 +2309,6 @@ function useChatMarkdownState({
22792309 const canUseShellActions =
22802310 canOperateHost &&
22812311 canUseMarkdownFileShellActions ( environmentId , remoteOpen . state . mode , remoteOpen . isResolved ) ;
2282- const preparedConnection = usePreparedConnection ( environmentId ) ;
22832312 const openMarkdownMedia = (
22842313 source : string ,
22852314 resolvedFilePath ?: string ,
@@ -2291,8 +2320,10 @@ function useChatMarkdownState({
22912320 resolvedFilePath,
22922321 cwd,
22932322 threadRef,
2323+ // Read at click time: a reconnect replaces the prepared connection, and
2324+ // subscribing would hand every rendered markdown element a new context.
22942325 httpBaseUrl :
2295- preparedConnection . _tag === "Some" ? preparedConnection . value . httpBaseUrl : undefined ,
2326+ environmentId === null ? undefined : readPreparedConnection ( environmentId ) ?. httpBaseUrl ,
22962327 createAssetUrl,
22972328 onOpenFile : threadRef
22982329 ? ( path ) => useRightPanelStore . getState ( ) . openFile ( threadRef , path )
@@ -2323,20 +2354,9 @@ function useChatMarkdownState({
23232354 } ,
23242355 ) ;
23252356 } ;
2326- // Only the parts of the server config markdown reads. The config object itself
2327- // changes with every provider status push, which would re-render every
2328- // visible reply.
2329- const serverConfig = useAtomValue ( serverEnvironment . configValueAtom ( environmentId ) ) ;
2330- const nextConfigFields = markdownServerConfigFields ( serverConfig ) ;
2331- const [ configFields , setConfigFields ] = useState ( nextConfigFields ) ;
2332- if (
2333- configFields . pullRequestsEnabled !== nextConfigFields . pullRequestsEnabled ||
2334- configFields . availableEditors !== nextConfigFields . availableEditors ||
2335- configFields . revealInFileManagerLabel !== nextConfigFields . revealInFileManagerLabel
2336- ) {
2337- setConfigFields ( nextConfigFields ) ;
2338- }
2339- const { pullRequestsEnabled, availableEditors, revealInFileManagerLabel } = configFields ;
2357+ const { pullRequestsEnabled, availableEditors, revealInFileManagerLabel } = useAtomValue (
2358+ markdownServerConfigFieldsAtom ( environmentId ) ,
2359+ ) ;
23402360 const projects = useProjects ( ) ;
23412361 const [ preferredEditor ] = usePreferredEditor ( availableEditors ) ;
23422362 const preferredEditorMenuLabel = openInEditorMenuLabel ( preferredEditor ) ;
@@ -2480,7 +2500,8 @@ function useChatMarkdownState({
24802500 } ) ;
24812501 } ;
24822502 const openMarkdownFileInPreview = ( path : string ) => {
2483- if ( ! threadRef || ! canOperatePreview || preparedConnection . _tag === "None" ) {
2503+ const preparedConnection = threadRef ? readPreparedConnection ( threadRef . environmentId ) : null ;
2504+ if ( ! threadRef || ! canOperatePreview || preparedConnection === null ) {
24842505 return Promise . resolve (
24852506 AsyncResult . failure < void , BrowserPreviewUnavailableError > (
24862507 Cause . fail (
@@ -2495,7 +2516,7 @@ function useChatMarkdownState({
24952516 threadRef,
24962517 filePath : path ,
24972518 workspaceRoot : cwd ,
2498- httpBaseUrl : preparedConnection . value . httpBaseUrl ,
2519+ httpBaseUrl : preparedConnection . httpBaseUrl ,
24992520 createAssetUrl,
25002521 openPreview,
25012522 } ) ;
0 commit comments