Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
mock createBlock, use to simplify aifilediff.preview.tsx
  • Loading branch information
sawka committed Mar 18, 2026
commit 4d357fdc075a708c50f97cbb3ad988c70e88e27e
19 changes: 18 additions & 1 deletion frontend/preview/mock/mockwaveenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,24 @@ export function makeMockWaveEnv(mockEnv?: MockEnv): MockWaveEnv {
mergedOverrides.createBlock ??
((blockDef: BlockDef, magnified?: boolean, ephemeral?: boolean) => {
console.log("[mock createBlock]", blockDef, { magnified, ephemeral });
return Promise.resolve(crypto.randomUUID());
const newBlockId = crypto.randomUUID();
const newBlock: Block = {
otype: "block",
oid: newBlockId,
version: 1,
meta: blockDef.meta ?? {},
};
mockWosFns.mockSetWaveObj(`block:${newBlockId}`, newBlock);
const tabORef = `tab:${tabId}`;
const tabAtom = getWaveObjectAtom<Tab>(tabORef);
const currentTab = globalStore.get(tabAtom);
if (currentTab != null) {
mockWosFns.mockSetWaveObj(tabORef, {
...currentTab,
blockids: [...(currentTab.blockids ?? []), newBlockId],
});
}
return Promise.resolve(newBlockId);
}),
showContextMenu: mergedOverrides.showContextMenu ?? showPreviewContextMenu,
getLocalHostDisplayNameAtom: () => {
Expand Down
82 changes: 37 additions & 45 deletions frontend/preview/previews/aifilediff.preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import { Block } from "@/app/block/block";
import { globalStore } from "@/app/store/jotaiStore";
import { useWaveEnv, WaveEnvContext } from "@/app/waveenv/waveenv";
import { useWaveEnv } from "@/app/waveenv/waveenv";
import type { NodeModel } from "@/layout/index";
import { atom } from "jotai";
import * as React from "react";
import { applyMockEnvOverrides } from "../mock/mockwaveenv";
import { useRpcOverride } from "../mock/use-rpc-override";
import {
DefaultAiFileDiffChatId,
DefaultAiFileDiffFileName,
Expand All @@ -16,23 +16,8 @@ import {
} from "./aifilediff.preview-util";

const PreviewNodeId = "preview-aifilediff-node";
const PreviewBlockId = crypto.randomUUID();

function makeMockBlock(): Block {
return {
otype: "block",
oid: PreviewBlockId,
version: 1,
meta: {
view: "aifilediff",
file: DefaultAiFileDiffFileName,
"aifilediff:chatid": DefaultAiFileDiffChatId,
"aifilediff:toolcallid": DefaultAiFileDiffToolCallId,
},
} as Block;
}

function makePreviewNodeModel(): NodeModel {
function makePreviewNodeModel(blockId: string): NodeModel {
const isFocusedAtom = atom(true);
const isMagnifiedAtom = atom(false);

Expand All @@ -42,7 +27,7 @@ function makePreviewNodeModel(): NodeModel {
blockNum: atom(1),
numLeafs: atom(1),
nodeId: PreviewNodeId,
blockId: PreviewBlockId,
blockId,
addEphemeralNodeToLayout: () => {},
animationTimeS: atom(0),
isResizing: atom(false),
Expand All @@ -65,38 +50,45 @@ function makePreviewNodeModel(): NodeModel {
}

export function AiFileDiffPreview() {
const baseEnv = useWaveEnv();
const nodeModel = React.useMemo(() => makePreviewNodeModel(), []);
const env = useWaveEnv();
const [blockId, setBlockId] = React.useState<string>(null);

const env = React.useMemo(() => {
return applyMockEnvOverrides(baseEnv, {
mockWaveObjs: {
[`block:${PreviewBlockId}`]: makeMockBlock(),
},
rpc: {
WaveAIGetToolDiffCommand: async (_client, data) => {
if (
data.chatid !== DefaultAiFileDiffChatId ||
data.toolcallid !== DefaultAiFileDiffToolCallId
) {
return null;
}
return makeMockAiFileDiffResponse();
useRpcOverride("WaveAIGetToolDiffCommand", async (_client, data) => {
if (data.chatid !== DefaultAiFileDiffChatId || data.toolcallid !== DefaultAiFileDiffToolCallId) {
return null;
}
return makeMockAiFileDiffResponse();
});

React.useEffect(() => {
env.createBlock(
{
meta: {
view: "aifilediff",
file: DefaultAiFileDiffFileName,
"aifilediff:chatid": DefaultAiFileDiffChatId,
"aifilediff:toolcallid": DefaultAiFileDiffToolCallId,
},
},
});
}, [baseEnv]);
false,
false
).then((id) => setBlockId(id));
}, []);
Comment on lines +29 to +42
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Missing error handling for createBlock promise.

If env.createBlock rejects, the error will be swallowed silently, leaving the component in a perpetual loading state. Consider adding error handling.

🛡️ Add error handling
     React.useEffect(() => {
         env.createBlock(
             {
                 meta: {
                     view: "aifilediff",
                     file: DefaultAiFileDiffFileName,
                     "aifilediff:chatid": DefaultAiFileDiffChatId,
                     "aifilediff:toolcallid": DefaultAiFileDiffToolCallId,
                 },
             },
             false,
             false
-        ).then((id) => setBlockId(id));
+        ).then((id) => setBlockId(id))
+         .catch((err) => console.error("[AiFileDiffPreview] createBlock failed:", err));
     }, []);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
React.useEffect(() => {
env.createBlock(
{
meta: {
view: "aifilediff",
file: DefaultAiFileDiffFileName,
"aifilediff:chatid": DefaultAiFileDiffChatId,
"aifilediff:toolcallid": DefaultAiFileDiffToolCallId,
},
},
});
}, [baseEnv]);
false,
false
).then((id) => setBlockId(id));
}, []);
React.useEffect(() => {
env.createBlock(
{
meta: {
view: "aifilediff",
file: DefaultAiFileDiffFileName,
"aifilediff:chatid": DefaultAiFileDiffChatId,
"aifilediff:toolcallid": DefaultAiFileDiffToolCallId,
},
},
false,
false
).then((id) => setBlockId(id))
.catch((err) => console.error("[AiFileDiffPreview] createBlock failed:", err));
}, []);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/preview/previews/aifilediff.preview.tsx` around lines 29 - 42, The
useEffect calling env.createBlock currently ignores rejection which can leave
the component stuck; update the React.useEffect to handle promise rejections
from env.createBlock by adding a .catch or using try/catch in an async IIFE, and
on error call a recovery path such as setting an error state or logging via
console.error/processLogger and avoid leaving blockId unset; target the
createBlock call and setBlockId usage inside the React.useEffect (or convert to
an async function) so failures are surfaced and the component can render an
error or retry UI.


const nodeModel = React.useMemo(() => (blockId != null ? makePreviewNodeModel(blockId) : null), [blockId]);

if (blockId == null || nodeModel == null) {
return null;
}

return (
<WaveEnvContext.Provider value={env}>
<div className="flex w-full max-w-[1120px] flex-col gap-2 px-6 py-6">
<div className="text-xs text-muted font-mono">full aifilediff block (mock WOS + mock WaveAI diff RPC)</div>
<div className="rounded-md border border-border bg-panel p-4">
<div className="h-[720px]">
<Block preview={false} nodeModel={nodeModel} />
</div>
<div className="flex w-full max-w-[1120px] flex-col gap-2 px-6 py-6">
<div className="text-xs text-muted font-mono">full aifilediff block (mock WOS + mock WaveAI diff RPC)</div>
<div className="rounded-md border border-border bg-panel p-4">
<div className="h-[720px]">
<Block preview={false} nodeModel={nodeModel} />
</div>
</div>
</WaveEnvContext.Provider>
</div>
);
}