|
| 1 | +// Copyright 2026, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { Block } from "@/app/block/block"; |
| 5 | +import { globalStore } from "@/app/store/jotaiStore"; |
| 6 | +import { getTabModelByTabId, TabModelContext } from "@/app/store/tab-model"; |
| 7 | +import { useWaveEnv, WaveEnvContext } from "@/app/waveenv/waveenv"; |
| 8 | +import type { NodeModel } from "@/layout/index"; |
| 9 | +import { atom } from "jotai"; |
| 10 | +import * as React from "react"; |
| 11 | +import { applyMockEnvOverrides, MockWaveEnv } from "../mock/mockwaveenv"; |
| 12 | +import { |
| 13 | + DefaultAiFileDiffChatId, |
| 14 | + DefaultAiFileDiffFileName, |
| 15 | + DefaultAiFileDiffToolCallId, |
| 16 | + makeMockAiFileDiffResponse, |
| 17 | +} from "./aifilediff.preview-util"; |
| 18 | + |
| 19 | +const PreviewWorkspaceId = "preview-aifilediff-workspace"; |
| 20 | +const PreviewTabId = "preview-aifilediff-tab"; |
| 21 | +const PreviewNodeId = "preview-aifilediff-node"; |
| 22 | +const PreviewBlockId = "preview-aifilediff-block"; |
| 23 | + |
| 24 | +function makeMockWorkspace(): Workspace { |
| 25 | + return { |
| 26 | + otype: "workspace", |
| 27 | + oid: PreviewWorkspaceId, |
| 28 | + version: 1, |
| 29 | + name: "Preview Workspace", |
| 30 | + tabids: [PreviewTabId], |
| 31 | + activetabid: PreviewTabId, |
| 32 | + meta: {}, |
| 33 | + } as Workspace; |
| 34 | +} |
| 35 | + |
| 36 | +function makeMockTab(): Tab { |
| 37 | + return { |
| 38 | + otype: "tab", |
| 39 | + oid: PreviewTabId, |
| 40 | + version: 1, |
| 41 | + name: "AI File Diff Preview", |
| 42 | + blockids: [PreviewBlockId], |
| 43 | + meta: {}, |
| 44 | + } as Tab; |
| 45 | +} |
| 46 | + |
| 47 | +function makeMockBlock(): Block { |
| 48 | + return { |
| 49 | + otype: "block", |
| 50 | + oid: PreviewBlockId, |
| 51 | + version: 1, |
| 52 | + meta: { |
| 53 | + view: "aifilediff", |
| 54 | + file: DefaultAiFileDiffFileName, |
| 55 | + "aifilediff:chatid": DefaultAiFileDiffChatId, |
| 56 | + "aifilediff:toolcallid": DefaultAiFileDiffToolCallId, |
| 57 | + }, |
| 58 | + } as Block; |
| 59 | +} |
| 60 | + |
| 61 | +function makePreviewNodeModel(): NodeModel { |
| 62 | + const isFocusedAtom = atom(true); |
| 63 | + const isMagnifiedAtom = atom(false); |
| 64 | + |
| 65 | + return { |
| 66 | + additionalProps: atom({} as any), |
| 67 | + innerRect: atom({ width: "1000px", height: "640px" }), |
| 68 | + blockNum: atom(1), |
| 69 | + numLeafs: atom(1), |
| 70 | + nodeId: PreviewNodeId, |
| 71 | + blockId: PreviewBlockId, |
| 72 | + addEphemeralNodeToLayout: () => {}, |
| 73 | + animationTimeS: atom(0), |
| 74 | + isResizing: atom(false), |
| 75 | + isFocused: isFocusedAtom, |
| 76 | + isMagnified: isMagnifiedAtom, |
| 77 | + anyMagnified: atom(false), |
| 78 | + isEphemeral: atom(false), |
| 79 | + ready: atom(true), |
| 80 | + disablePointerEvents: atom(false), |
| 81 | + toggleMagnify: () => { |
| 82 | + globalStore.set(isMagnifiedAtom, !globalStore.get(isMagnifiedAtom)); |
| 83 | + }, |
| 84 | + focusNode: () => { |
| 85 | + globalStore.set(isFocusedAtom, true); |
| 86 | + }, |
| 87 | + onClose: () => {}, |
| 88 | + dragHandleRef: { current: null }, |
| 89 | + displayContainerRef: { current: null }, |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +function AiFileDiffPreviewInner() { |
| 94 | + const baseEnv = useWaveEnv(); |
| 95 | + const nodeModel = React.useMemo(() => makePreviewNodeModel(), []); |
| 96 | + |
| 97 | + const env = React.useMemo<MockWaveEnv>(() => { |
| 98 | + const mockWaveObjs: Record<string, WaveObj> = { |
| 99 | + [`workspace:${PreviewWorkspaceId}`]: makeMockWorkspace(), |
| 100 | + [`tab:${PreviewTabId}`]: makeMockTab(), |
| 101 | + [`block:${PreviewBlockId}`]: makeMockBlock(), |
| 102 | + }; |
| 103 | + |
| 104 | + return applyMockEnvOverrides(baseEnv, { |
| 105 | + tabId: PreviewTabId, |
| 106 | + mockWaveObjs, |
| 107 | + atoms: { |
| 108 | + workspaceId: atom(PreviewWorkspaceId), |
| 109 | + staticTabId: atom(PreviewTabId), |
| 110 | + }, |
| 111 | + rpc: { |
| 112 | + WaveAIGetToolDiffCommand: async (_client, data) => { |
| 113 | + if ( |
| 114 | + data.chatid !== DefaultAiFileDiffChatId || |
| 115 | + data.toolcallid !== DefaultAiFileDiffToolCallId |
| 116 | + ) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + return makeMockAiFileDiffResponse(); |
| 120 | + }, |
| 121 | + }, |
| 122 | + }); |
| 123 | + }, [baseEnv]); |
| 124 | + |
| 125 | + const tabModel = React.useMemo(() => getTabModelByTabId(PreviewTabId, env), [env]); |
| 126 | + |
| 127 | + return ( |
| 128 | + <WaveEnvContext.Provider value={env}> |
| 129 | + <TabModelContext.Provider value={tabModel}> |
| 130 | + <div className="flex w-full max-w-[1120px] flex-col gap-2 px-6 py-6"> |
| 131 | + <div className="text-xs text-muted font-mono">full aifilediff block (mock WOS + mock WaveAI diff RPC)</div> |
| 132 | + <div className="rounded-md border border-border bg-panel p-4"> |
| 133 | + <div className="h-[720px]"> |
| 134 | + <Block preview={false} nodeModel={nodeModel} /> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + </TabModelContext.Provider> |
| 139 | + </WaveEnvContext.Provider> |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +export function AiFileDiffPreview() { |
| 144 | + return <AiFileDiffPreviewInner />; |
| 145 | +} |
0 commit comments