Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): prevent artifact version flicker (#80229)

Shy Alterb5eb82be967c
diff --git a/products/desktop/packages/ui/src/features/sessions/components/ArtifactPreview.test.tsx b/products/desktop/packages/ui/src/features/sessions/components/ArtifactPreview.test.tsx
index bde016c75a1c..4c322c4276f0 100644
--- a/products/desktop/packages/ui/src/features/sessions/components/ArtifactPreview.test.tsx
+++ b/products/desktop/packages/ui/src/features/sessions/components/ArtifactPreview.test.tsx
@@ -250,7 +250,7 @@ describe("ArtifactPreview", () => {
vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => undefined);
});
- it("scopes cached previews to the authenticated identity", () => {
+ it("scopes cached previews to the identity and artifact name", () => {
render(
<ArtifactPreview
taskId="task-1"
@@ -266,6 +266,7 @@ describe("ArtifactPreview", () => {
"artifactPreview",
"auth-1",
"task-1",
+ "report.html",
"run-1",
"artifact-1",
],
@@ -273,6 +274,24 @@ describe("ArtifactPreview", () => {
meta: { authScoped: true },
}),
);
+ const queryOptions = useQuery.mock.calls[0]?.[0] as {
+ placeholderData: (
+ previousData: unknown,
+ previousQuery: { queryKey: unknown[] },
+ ) => unknown;
+ };
+ expect(
+ queryOptions.placeholderData("previous preview", {
+ queryKey: [
+ "artifactPreview",
+ "auth-1",
+ "task-1",
+ "other.html",
+ "run-1",
+ "artifact-0",
+ ],
+ }),
+ ).toBeUndefined();
});
it("starts the manifest and preview URL requests together", async () => {
@@ -1024,13 +1043,50 @@ describe("ArtifactPreview", () => {
],
},
];
- useQuery.mockReturnValue({
- data: editablePreview(),
- isLoading: false,
- isError: false,
+ const currentPreview = editablePreview({
+ storage_path: "runs/1/report-v2.md",
+ });
+ const olderPreview = editablePreview({
+ id: "artifact-0",
+ storage_path: "runs/1/report-v1.md",
+ uploaded_at: "2026-08-06T10:00:00Z",
});
+ let olderVersionLoaded = false;
+ useQuery.mockImplementation(
+ (options: {
+ queryKey: unknown[];
+ placeholderData?: (
+ previousData: unknown,
+ previousQuery: { queryKey: unknown[] },
+ ) => unknown;
+ }) => {
+ const isOlderVersion = options.queryKey.includes("artifact-0");
+ const data =
+ isOlderVersion && !olderVersionLoaded
+ ? options.placeholderData?.(currentPreview, {
+ queryKey: [
+ "artifactPreview",
+ "auth-1",
+ "task-1",
+ "report.md",
+ "run-1",
+ "artifact-1",
+ ],
+ })
+ : isOlderVersion
+ ? olderPreview
+ : currentPreview;
+ return {
+ data,
+ isLoading: data === undefined,
+ isError: false,
+ isPlaceholderData:
+ isOlderVersion && !olderVersionLoaded && data !== undefined,
+ };
+ },
+ );
- render(
+ const { rerender } = render(
<ArtifactPreview
taskId="task-1"
runId="run-1"
@@ -1040,16 +1096,40 @@ describe("ArtifactPreview", () => {
);
expect(screen.getByText("v2/2")).toBeInTheDocument();
+ expect(screen.getByRole("button", { name: "Edit" })).toBeInTheDocument();
expect(
screen.getByRole("button", { name: "Newer version" }),
).toHaveAttribute("aria-disabled", "true");
fireEvent.click(screen.getByRole("button", { name: "Older version" }));
- expect(screen.getByText("v1/2")).toBeInTheDocument();
+ expect(screen.getByText("v2/2")).toBeInTheDocument();
+ expect(screen.getByText("Report")).toBeInTheDocument();
+ expect(screen.queryByRole("button", { name: "Edit" })).toBeNull();
expect(
screen.getByRole("button", { name: "Older version" }),
).toHaveAttribute("aria-disabled", "true");
+ expect(
+ screen.getByRole("button", { name: "Newer version" }),
+ ).toHaveAttribute("aria-disabled", "true");
+ expect(
+ screen.getByRole("button", { name: "Comment…" }),
+ ).toBeInTheDocument();
+
+ olderVersionLoaded = true;
+ rerender(
+ <ArtifactPreview
+ taskId="task-1"
+ runId="run-1"
+ artifactId="artifact-1"
+ name="report.md"
+ />,
+ );
+
+ expect(screen.getByText("v1/2")).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Newer version" }),
+ ).not.toHaveAttribute("aria-disabled", "true");
let lastCall = useQuery.mock.calls.at(-1)?.[0] as {
queryKey: unknown[];
};
diff --git a/products/desktop/packages/ui/src/features/sessions/components/ArtifactPreview.tsx b/products/desktop/packages/ui/src/features/sessions/components/ArtifactPreview.tsx
index 273b23b690d7..c232f55300a4 100644
--- a/products/desktop/packages/ui/src/features/sessions/components/ArtifactPreview.tsx
+++ b/products/desktop/packages/ui/src/features/sessions/components/ArtifactPreview.tsx
@@ -122,19 +122,42 @@ export function ArtifactPreview({
null,
);
const activeArtifactId = selectedVersionId ?? artifactId;
- const versionIndex = versions.findIndex(
+ const activeVersionIndex = versions.findIndex(
(version) => version.id === activeArtifactId,
);
const activeRunId =
- versionIndex >= 0 ? (versions[versionIndex]?.runId ?? runId) : runId;
+ activeVersionIndex >= 0
+ ? (versions[activeVersionIndex]?.runId ?? runId)
+ : runId;
const markdownRootRef = useRef<HTMLDivElement>(null);
const markdownContainerRef = useRef<HTMLDivElement>(null);
const [imageError, setImageError] = useState(false);
const [imageCommenting, setImageCommenting] = useState(false);
const authIdentity = useAuthStateValue(getAuthIdentity);
+ const {
+ artifactResult,
+ previewData,
+ previewUrl,
+ isLoading,
+ isError,
+ isPlaceholderData,
+ } = useArtifactPreviewData({
+ sessionService,
+ authIdentity,
+ taskId,
+ runId: activeRunId,
+ artifactId: activeArtifactId,
+ name,
+ });
+ const displayedArtifactId = artifactResult?.artifact.id ?? activeArtifactId;
+ const versionIndex = versions.findIndex(
+ (version) => version.id === displayedArtifactId,
+ );
+ const displayedRunId =
+ versionIndex >= 0 ? (versions[versionIndex]?.runId ?? runId) : runId;
const commentTarget = useMemo<CommentTarget>(
- () => ({ scope: "task_artifact", itemId: activeArtifactId }),
- [activeArtifactId],
+ () => ({ scope: "task_artifact", itemId: displayedArtifactId }),
+ [displayedArtifactId],
);
const commentsQuery = useCommentsQuery(commentTarget, taskId, {
enabled: commentsEnabled,
@@ -159,24 +182,15 @@ export function ArtifactPreview({
}
setSelectedVersionId(focus.target.itemId);
}, [activeArtifactId, focus, versions]);
- const { artifactResult, previewData, previewUrl, isLoading, isError } =
- useArtifactPreviewData({
- sessionService,
- authIdentity,
- taskId,
- runId: activeRunId,
- artifactId: activeArtifactId,
- name,
- });
const editing = useArtifactEditing({
sessionService,
artifactResult,
versions:
versions.length > 0 ? versions : (artifactResult?.artifacts ?? []),
- versionsLoading: runsLoading,
+ versionsLoading: runsLoading || isPlaceholderData,
refreshVersions,
taskId,
- runId: activeRunId,
+ runId: displayedRunId,
name,
authIdentity,
openArtifactTab,
@@ -252,7 +266,7 @@ export function ArtifactPreview({
size="icon"
variant="default"
aria-label="Older version"
- disabled={versionIndex >= versions.length - 1}
+ disabled={isPlaceholderData || versionIndex >= versions.length - 1}
onClick={() => {
const older = versions[versionIndex + 1];
if (older?.id) setSelectedVersionId(older.id);
@@ -267,7 +281,7 @@ export function ArtifactPreview({
size="icon"
variant="default"
aria-label="Newer version"
- disabled={versionIndex <= 0}
+ disabled={isPlaceholderData || versionIndex <= 0}
onClick={() => {
const newer = versions[versionIndex - 1];
if (newer?.id) setSelectedVersionId(newer.id);
diff --git a/products/desktop/packages/ui/src/features/sessions/components/useArtifactPreviewData.ts b/products/desktop/packages/ui/src/features/sessions/components/useArtifactPreviewData.ts
index 3a23663e3011..197ad2ef8647 100644
--- a/products/desktop/packages/ui/src/features/sessions/components/useArtifactPreviewData.ts
+++ b/products/desktop/packages/ui/src/features/sessions/components/useArtifactPreviewData.ts
@@ -89,11 +89,19 @@ export function useArtifactPreviewData({
previewUrl: string | null;
isLoading: boolean;
isError: boolean;
+ isPlaceholderData: boolean;
} {
- const { data, isLoading, isError } = useQuery<
+ const { data, isLoading, isError, isPlaceholderData } = useQuery<
PreviewData | ArtifactPreviewResult
>({
- queryKey: ["artifactPreview", authIdentity, taskId, runId, artifactId],
+ queryKey: [
+ "artifactPreview",
+ authIdentity,
+ taskId,
+ name,
+ runId,
+ artifactId,
+ ],
queryFn: async () => {
const [artifacts, url] = await Promise.all([
sessionService.getCloudRunArtifacts(taskId, runId),
@@ -135,6 +143,12 @@ export function useArtifactPreviewData({
},
enabled: authIdentity !== null,
staleTime: Infinity,
+ placeholderData: (previousData, previousQuery) =>
+ previousQuery?.queryKey[1] === authIdentity &&
+ previousQuery.queryKey[2] === taskId &&
+ previousQuery.queryKey[3] === name
+ ? previousData
+ : undefined,
retry: false,
meta: AUTH_SCOPED_QUERY_META,
});
@@ -154,5 +168,12 @@ export function useArtifactPreviewData({
return () => URL.revokeObjectURL(objectUrl);
}, [previewData]);
- return { artifactResult, previewData, previewUrl, isLoading, isError };
+ return {
+ artifactResult,
+ previewData,
+ previewUrl,
+ isLoading,
+ isError,
+ isPlaceholderData,
+ };
}