@@ -941,15 +941,22 @@ export default function FilePreviewPanel({
941941 // A file outside the workspace (an absolute path) is shown, never edited.
942942 const isHostFile =
943943 attachment !== undefined || ( relativePath !== null && isAbsolutePath ( relativePath ) ) ;
944- const file = useProjectFileQuery (
945- environmentId ,
946- cwd ,
947- relativePath ,
948- attachment === undefined && ! isMedia && ! isPdf ,
949- ) ;
944+ // Media and PDFs render from their absolute path, so their contents are never
945+ // shown. The read still runs: a folder named `assets.png` is only knowable as a
946+ // folder from the read failure, and the server stats before reading, so a folder
947+ // costs an open and a stat and returns no body.
948+ const file = useProjectFileQuery ( environmentId , cwd , relativePath , attachment === undefined ) ;
949+ // A chat link cannot tell a folder from a file, so a folder arrives here as
950+ // a file surface and the read fails. Keep the breadcrumbs, drop the preview
951+ // pane, and let the tree fill the surface with the folder revealed. Mutation
952+ // refresh stays on so the surface notices if the path becomes a file. A host
953+ // path cannot be revealed in the workspace tree, so it keeps the read error.
954+ const isDirectory = file . isNotFile && ! isHostFile ;
955+ // Everything preview-related keys off previewPath; a folder has no preview.
956+ const previewPath = isDirectory ? null : relativePath ;
950957 const [ explorerOpen , setExplorerOpen ] = useState ( initialExplorerOpen ) ;
951958 const showExplorer = shouldShowFileExplorer ( {
952- relativePath,
959+ relativePath : previewPath ,
953960 explorerOpen,
954961 attachmentOpen : attachment !== undefined ,
955962 } ) ;
@@ -977,9 +984,9 @@ export default function FilePreviewPanel({
977984 null ,
978985 ) ;
979986 const breadcrumbRef = useRef < HTMLDivElement > ( null ) ;
980- const isMarkdown = relativePath ? isMarkdownPreviewFile ( relativePath ) : false ;
987+ const isMarkdown = previewPath ? isMarkdownPreviewFile ( previewPath ) : false ;
981988 const tableDelimiter =
982- relativePath && attachment === undefined ? filePreviewDelimiter ( { name : relativePath } ) : null ;
989+ previewPath && attachment === undefined ? filePreviewDelimiter ( { name : previewPath } ) : null ;
983990 // A reveal still wins over the preference: the line only exists in the source.
984991 const revealHandled =
985992 revealLine === null ||
@@ -994,12 +1001,13 @@ export default function FilePreviewPanel({
9941001 : isHtml
9951002 ? ( "html" as const )
9961003 : null ;
997- const canToggleRendered = attachment === undefined && renderedMode !== null ;
1004+ const canToggleRendered =
1005+ previewPath !== null && attachment === undefined && renderedMode !== null ;
9981006 const updateClientSettings = useUpdateClientSettings ( ) ;
9991007 // Word wrap only reaches the text bodies. A rendered Markdown document, a table and the
10001008 // browser frame all lay themselves out, so the toggle stays hidden rather than inert.
10011009 const showsRawText =
1002- relativePath !== null &&
1010+ previewPath !== null &&
10031011 file . data !== null &&
10041012 ! ( isMarkdown && renderMarkdown ) &&
10051013 ! ( tableDelimiter && renderTable ) &&
@@ -1011,20 +1019,22 @@ export default function FilePreviewPanel({
10111019 ? setRenderTablePreferred
10121020 : setRenderBrowserFilePreferred ;
10131021 const canOpenInBrowser =
1014- relativePath !== null &&
1022+ previewPath !== null &&
10151023 attachment === undefined &&
10161024 ! isVideo &&
10171025 isPreviewSupportedInRuntime ( ) &&
1018- isBrowserPreviewFile ( relativePath ) ;
1026+ isBrowserPreviewFile ( previewPath ) ;
10191027 const absolutePath =
10201028 relativePath && attachment === undefined ? resolvePathLinkTarget ( relativePath , cwd ) : null ;
10211029 const onFilePostRender = useFileLineReveal ( relativePath , revealLine , revealRequestId ) ;
10221030 useWorkspaceMutationRefresh ( {
10231031 enabled :
10241032 attachment === undefined &&
10251033 relativePath !== null &&
1026- ! isMedia &&
1027- ! isPdf &&
1034+ // Media and PDFs never show their contents, so re-reading them on every
1035+ // workspace mutation is waste. A folder named like one still re-reads, so
1036+ // it notices when the path becomes a file.
1037+ ( isDirectory || ( ! isMedia && ! isPdf ) ) &&
10281038 ! selectedFilePending ,
10291039 mutationId : workspaceMutationId ,
10301040 refresh : file . refresh ,
@@ -1145,7 +1155,7 @@ export default function FilePreviewPanel({
11451155 < Globe2 className = "size-3.5" />
11461156 </ FileSurfaceAction >
11471157 ) : null }
1148- { ! isHostFile ? (
1158+ { ! isHostFile && previewPath !== null ? (
11491159 < FileSurfaceAction
11501160 label = { explorerOpen ? "Hide file explorer" : "Show file explorer" }
11511161 pressed = { explorerOpen }
@@ -1156,7 +1166,7 @@ export default function FilePreviewPanel({
11561166 ) : null }
11571167 </ div >
11581168 ) : null }
1159- { relativePath &&
1169+ { previewPath &&
11601170 attachment === undefined &&
11611171 ! isMedia &&
11621172 ! renderBrowserFile &&
@@ -1167,12 +1177,9 @@ export default function FilePreviewPanel({
11671177 ) : null }
11681178 < div className = "flex min-h-0 flex-1 overflow-hidden" >
11691179 < div
1170- className = { cn (
1171- "min-w-0 flex-1 flex-col overflow-hidden" ,
1172- relativePath ? "flex" : "hidden" ,
1173- ) }
1180+ className = { cn ( "min-w-0 flex-1 flex-col overflow-hidden" , previewPath ? "flex" : "hidden" ) }
11741181 >
1175- { relativePath && attachment ? (
1182+ { isDirectory ? null : relativePath && attachment ? (
11761183 < AttachmentFilePreview
11771184 key = { `${ environmentId } :${ attachment . id } ` }
11781185 name = { attachment . name }
@@ -1279,7 +1286,7 @@ export default function FilePreviewPanel({
12791286 < aside
12801287 className = { cn (
12811288 "flex min-h-0 shrink-0 bg-background" ,
1282- relativePath
1289+ previewPath
12831290 ? "w-[min(22rem,46%)] min-w-64 border-l border-border/60"
12841291 : "min-w-0 flex-1" ,
12851292 ) }
@@ -1293,7 +1300,7 @@ export default function FilePreviewPanel({
12931300 selectedPathRevealId = { revealRequestId }
12941301 onOpenFile = { onOpenFile }
12951302 workspaceMutationId = { workspaceMutationId }
1296- { ...( relativePath && ! isMedia && ! isPdf
1303+ { ...( previewPath && ! isMedia && ! isPdf
12971304 ? { onRefreshSelectedFile : file . refresh }
12981305 : { } ) }
12991306 />
0 commit comments