Skip to content

Commit 435b226

Browse files
fix(web): retain markdown image refreshes through pending requests
1 parent 70b6e81 commit 435b226

2 files changed

Lines changed: 45 additions & 18 deletions

File tree

‎apps/web/src/assets/assetUrls.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useAtomValue } from "@effect/atom-react";
22
import {
3-
type AssetUrlState,
3+
type AssetUrlState as RuntimeAssetUrlState,
44
assetUrlStateFromResult,
55
blockedAssetUrlState,
66
EMPTY_ASSET_URL_ATOM,
@@ -15,7 +15,9 @@ import { useFilesystemReadAccess } from "~/state/filesystem";
1515
import { usePreparedConnection } from "~/state/session";
1616
import { useAtomQueryRunner } from "~/state/use-atom-query-runner";
1717

18-
export { resolveAssetUrl, type AssetUrlState } from "@t3tools/client-runtime/state/assets";
18+
export { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
19+
20+
export type AssetUrlState = RuntimeAssetUrlState & { readonly waiting?: boolean };
1921

2022
export function useAssetUrlState(
2123
environmentId: EnvironmentId | null,
@@ -29,10 +31,13 @@ export function useAssetUrlState(
2931
: assetEnvironment.createUrl({ environmentId, input: { resource } }),
3032
);
3133
if (blocked !== null) return blocked;
32-
return assetUrlStateFromResult(
33-
result,
34-
preparedConnection._tag === "Some" ? preparedConnection.value.httpBaseUrl : null,
35-
);
34+
return {
35+
...assetUrlStateFromResult(
36+
result,
37+
preparedConnection._tag === "Some" ? preparedConnection.value.httpBaseUrl : null,
38+
),
39+
...(result.waiting ? { waiting: true } : {}),
40+
};
3641
}
3742

3843
export function useAssetUrlRefresh(

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ import { HighlightedCodeLines } from "./chat/HighlightedCodeLines";
160160
import { RenderErrorBoundary } from "./RenderErrorBoundary";
161161
import { MermaidDiagram } from "./chat/MermaidDiagram";
162162
import { useTheme } from "../hooks/useTheme";
163-
import { useWorkspaceMutationRefresh } from "../hooks/useWorkspaceMutationRefresh";
164163
import { getClientSettings, useClientSettings } from "../hooks/useSettings";
165164
import {
166165
chatMarkdownClipboardPayload,
@@ -1584,17 +1583,40 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
15841583
const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource);
15851584
const connectionReady = usePreparedConnection(props.environmentId)._tag === "Some";
15861585
const resource = props.resource;
1587-
// Only file previews pass a mutation id. Chat markdown must not re-mint URLs,
1588-
// and a preview must refresh once the connection is ready, so an edit made
1589-
// while offline still replaces a cached failure or stale signed URL.
1590-
useWorkspaceMutationRefresh({
1591-
enabled: props.workspaceMutationId !== undefined && connectionReady,
1592-
mutationId: props.workspaceMutationId ?? null,
1593-
resourceKey: JSON.stringify([props.environmentId, resource]),
1594-
refresh: () => {
1595-
void refreshAssetUrl().catch(() => undefined);
1596-
},
1597-
});
1586+
const refreshToken = JSON.stringify([props.environmentId, resource, props.workspaceMutationId]);
1587+
const handledRefreshToken = useRef<string | null | undefined>(undefined);
1588+
const assetLoading = assetUrl._tag === "Loading" || assetUrl.waiting === true;
1589+
useEffect(() => {
1590+
if (!connectionReady) {
1591+
handledRefreshToken.current = null;
1592+
return;
1593+
}
1594+
if (
1595+
props.workspaceMutationId === undefined ||
1596+
props.kind === "video" ||
1597+
(resource._tag !== "media-file" && resource._tag !== "workspace-file") ||
1598+
handledRefreshToken.current === refreshToken
1599+
) {
1600+
return;
1601+
}
1602+
if (assetLoading) {
1603+
// Only the initial mount shares a pending request; edits and reconnects need a new one.
1604+
if (handledRefreshToken.current === undefined) {
1605+
handledRefreshToken.current = refreshToken;
1606+
}
1607+
return;
1608+
}
1609+
handledRefreshToken.current = refreshToken;
1610+
void refreshAssetUrl().catch(() => undefined);
1611+
}, [
1612+
assetLoading,
1613+
connectionReady,
1614+
props.kind,
1615+
props.workspaceMutationId,
1616+
refreshAssetUrl,
1617+
refreshToken,
1618+
resource._tag,
1619+
]);
15981620
const path =
15991621
resource._tag === "media-file"
16001622
? resource.path

0 commit comments

Comments
 (0)