Skip to content

Commit 8466395

Browse files
fix(web): refresh embedded assets in markdown file previews
1 parent 5753207 commit 8466395

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ interface ChatMarkdownProps {
235235
/** Directory that anchors relative links and images; defaults to `cwd`. Set
236236
to the file's own directory when rendering a markdown file. */
237237
imageBaseDir?: string | undefined;
238+
/** File previews refresh embedded host assets when opened or after workspace edits. */
239+
workspaceMutationId?: string | null | undefined;
238240
onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
239241
extraRemarkPlugins?: NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
240242
/** Renders a `t3-context://` link as a chip; without it the link shows its label as text. */
@@ -1598,11 +1600,29 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
15981600
too old to know this resource. Only safe when the client can reach it directly. */
15991601
readonly fallbackSrc?: string | undefined;
16001602
readonly workspaceRoot?: string | undefined;
1603+
readonly workspaceMutationId?: string | null | undefined;
16011604
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
16021605
}) {
16031606
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
16041607
const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource);
16051608
const resource = props.resource;
1609+
const refreshToken = JSON.stringify([props.environmentId, resource, props.workspaceMutationId]);
1610+
const handledRefreshToken = useRef<string | null>(null);
1611+
useEffect(() => {
1612+
if (
1613+
props.workspaceMutationId === undefined ||
1614+
(resource._tag !== "media-file" && resource._tag !== "workspace-file") ||
1615+
handledRefreshToken.current === refreshToken
1616+
) {
1617+
return;
1618+
}
1619+
handledRefreshToken.current = refreshToken;
1620+
// A first request already reads the current file. Cached URLs and failures
1621+
// need a new signature after a file is created or atomically replaced.
1622+
if (assetUrl._tag !== "Loading") {
1623+
void refreshAssetUrl().catch(() => undefined);
1624+
}
1625+
}, [assetUrl._tag, props.workspaceMutationId, refreshAssetUrl, refreshToken, resource._tag]);
16061626
const path =
16071627
resource._tag === "media-file"
16081628
? resource.path
@@ -2316,6 +2336,7 @@ function useChatMarkdownState({
23162336
onUseArtifactTemplate,
23172337
onRunShellCommand,
23182338
imageBaseDir,
2339+
workspaceMutationId,
23192340
onImageExpand,
23202341
renderContextReference,
23212342
headingLevelOffset = 0,
@@ -2730,6 +2751,7 @@ function useChatMarkdownState({
27302751
renderContextReference,
27312752
headingLevelOffset,
27322753
imageBaseDir,
2754+
workspaceMutationId,
27332755
inlineCodeFileLinkMetaByText,
27342756
isStreaming,
27352757
linkTargetPreference,
@@ -2763,6 +2785,7 @@ function useChatMarkdownState({
27632785
renderContextReference,
27642786
headingLevelOffset,
27652787
imageBaseDir,
2788+
workspaceMutationId,
27662789
inlineCodeFileLinkMetaByText,
27672790
isStreaming,
27682791
linkTargetPreference,
@@ -3219,6 +3242,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
32193242
githubMedia,
32203243
imageBaseDir,
32213244
threadRef,
3245+
workspaceMutationId,
32223246
renderContextReference,
32233247
} = use(ChatMarkdownRendererContext);
32243248
const imageExpand = use(MarkdownLinkContext) ? undefined : expandMedia;
@@ -3334,6 +3358,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
33343358
standalone={standalone}
33353359
style={authoredSizeStyle}
33363360
workspaceRoot={cwd}
3361+
workspaceMutationId={workspaceMutationId}
33373362
onImageExpand={imageExpand}
33383363
/>
33393364
);

‎apps/web/src/components/files/FileMarkdownPreview.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function FileMarkdownPreview(props: {
88
readonly relativePath: string;
99
readonly text: string;
1010
readonly threadRef: ScopedThreadRef;
11+
readonly workspaceMutationId?: string | null;
1112
readonly onTaskListChange?:
1213
| ((input: { readonly markerOffset: number; readonly checked: boolean }) => void)
1314
| undefined;
@@ -27,6 +28,7 @@ export function FileMarkdownPreview(props: {
2728
cwd={props.cwd}
2829
imageBaseDir={imageBaseDir}
2930
threadRef={props.threadRef}
31+
workspaceMutationId={props.workspaceMutationId ?? null}
3032
className="mx-auto max-w-4xl px-6 py-5"
3133
onTaskListChange={props.onTaskListChange}
3234
/>

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ function RenderedMarkdownSurface({
964964
relativePath,
965965
contents,
966966
threadRef,
967+
workspaceMutationId,
967968
readOnly,
968969
onPendingChange,
969970
}: Omit<
@@ -977,6 +978,7 @@ function RenderedMarkdownSurface({
977978
> & {
978979
threadRef: ScopedThreadRef;
979980
readOnly: boolean;
981+
workspaceMutationId: string | null;
980982
}) {
981983
const saveCoordinator = useFileSaveCoordinator({
982984
environmentId,
@@ -992,6 +994,7 @@ function RenderedMarkdownSurface({
992994
cwd={cwd}
993995
relativePath={relativePath}
994996
threadRef={threadRef}
997+
workspaceMutationId={workspaceMutationId}
995998
onTaskListChange={
996999
readOnly
9971000
? undefined
@@ -1441,6 +1444,7 @@ export default function FilePreviewPanel({
14411444
relativePath={relativePath}
14421445
threadRef={threadRef}
14431446
contents={file.data.contents}
1447+
workspaceMutationId={workspaceMutationId}
14441448
readOnly={isHostFile || !canWriteFiles}
14451449
onPendingChange={onPendingChange}
14461450
/>

0 commit comments

Comments
 (0)