|
| 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 { handleWaveEvent } from "@/app/store/wps"; |
| 8 | +import { useWaveEnv, WaveEnvContext } from "@/app/waveenv/waveenv"; |
| 9 | +import type { NodeModel } from "@/layout/index"; |
| 10 | +import { atom } from "jotai"; |
| 11 | +import * as React from "react"; |
| 12 | +import { applyMockEnvOverrides, MockWaveEnv } from "../mock/mockwaveenv"; |
| 13 | +import { |
| 14 | + DefaultSysinfoHistoryPoints, |
| 15 | + makeMockSysinfoEvent, |
| 16 | + makeMockSysinfoHistory, |
| 17 | + MockSysinfoConnection, |
| 18 | +} from "./sysinfo.preview-util"; |
| 19 | + |
| 20 | +const PreviewWorkspaceId = "preview-sysinfo-workspace"; |
| 21 | +const PreviewTabId = "preview-sysinfo-tab"; |
| 22 | +const PreviewNodeId = "preview-sysinfo-node"; |
| 23 | +const PreviewBlockId = "preview-sysinfo-block"; |
| 24 | + |
| 25 | +function makeMockWorkspace(): Workspace { |
| 26 | + return { |
| 27 | + otype: "workspace", |
| 28 | + oid: PreviewWorkspaceId, |
| 29 | + version: 1, |
| 30 | + name: "Preview Workspace", |
| 31 | + tabids: [PreviewTabId], |
| 32 | + activetabid: PreviewTabId, |
| 33 | + meta: {}, |
| 34 | + } as Workspace; |
| 35 | +} |
| 36 | + |
| 37 | +function makeMockTab(): Tab { |
| 38 | + return { |
| 39 | + otype: "tab", |
| 40 | + oid: PreviewTabId, |
| 41 | + version: 1, |
| 42 | + name: "Sysinfo Preview", |
| 43 | + blockids: [PreviewBlockId], |
| 44 | + meta: {}, |
| 45 | + } as Tab; |
| 46 | +} |
| 47 | + |
| 48 | +function makeMockBlock(): Block { |
| 49 | + return { |
| 50 | + otype: "block", |
| 51 | + oid: PreviewBlockId, |
| 52 | + version: 1, |
| 53 | + meta: { |
| 54 | + view: "sysinfo", |
| 55 | + connection: MockSysinfoConnection, |
| 56 | + "sysinfo:type": "CPU + Mem", |
| 57 | + "graph:numpoints": 90, |
| 58 | + }, |
| 59 | + } as Block; |
| 60 | +} |
| 61 | + |
| 62 | +function makePreviewNodeModel(): NodeModel { |
| 63 | + const isFocusedAtom = atom(true); |
| 64 | + const isMagnifiedAtom = atom(false); |
| 65 | + |
| 66 | + return { |
| 67 | + additionalProps: atom({} as any), |
| 68 | + innerRect: atom({ width: "920px", height: "560px" }), |
| 69 | + blockNum: atom(1), |
| 70 | + numLeafs: atom(2), |
| 71 | + nodeId: PreviewNodeId, |
| 72 | + blockId: PreviewBlockId, |
| 73 | + addEphemeralNodeToLayout: () => {}, |
| 74 | + animationTimeS: atom(0), |
| 75 | + isResizing: atom(false), |
| 76 | + isFocused: isFocusedAtom, |
| 77 | + isMagnified: isMagnifiedAtom, |
| 78 | + anyMagnified: atom(false), |
| 79 | + isEphemeral: atom(false), |
| 80 | + ready: atom(true), |
| 81 | + disablePointerEvents: atom(false), |
| 82 | + toggleMagnify: () => { |
| 83 | + globalStore.set(isMagnifiedAtom, !globalStore.get(isMagnifiedAtom)); |
| 84 | + }, |
| 85 | + focusNode: () => { |
| 86 | + globalStore.set(isFocusedAtom, true); |
| 87 | + }, |
| 88 | + onClose: () => {}, |
| 89 | + dragHandleRef: { current: null }, |
| 90 | + displayContainerRef: { current: null }, |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +function SysinfoPreviewInner() { |
| 95 | + const baseEnv = useWaveEnv(); |
| 96 | + const historyRef = React.useRef(makeMockSysinfoHistory()); |
| 97 | + const nodeModel = React.useMemo(() => makePreviewNodeModel(), []); |
| 98 | + |
| 99 | + const env = React.useMemo<MockWaveEnv>(() => { |
| 100 | + const mockWaveObjs: Record<string, WaveObj> = { |
| 101 | + [`workspace:${PreviewWorkspaceId}`]: makeMockWorkspace(), |
| 102 | + [`tab:${PreviewTabId}`]: makeMockTab(), |
| 103 | + [`block:${PreviewBlockId}`]: makeMockBlock(), |
| 104 | + }; |
| 105 | + |
| 106 | + return applyMockEnvOverrides(baseEnv, { |
| 107 | + tabId: PreviewTabId, |
| 108 | + mockWaveObjs, |
| 109 | + atoms: { |
| 110 | + workspaceId: atom(PreviewWorkspaceId), |
| 111 | + staticTabId: atom(PreviewTabId), |
| 112 | + }, |
| 113 | + rpc: { |
| 114 | + EventReadHistoryCommand: async (_client, data) => { |
| 115 | + if (data.event !== "sysinfo" || data.scope !== MockSysinfoConnection) { |
| 116 | + return []; |
| 117 | + } |
| 118 | + const maxItems = data.maxitems ?? historyRef.current.length; |
| 119 | + return historyRef.current.slice(-maxItems); |
| 120 | + }, |
| 121 | + }, |
| 122 | + }); |
| 123 | + }, [baseEnv]); |
| 124 | + |
| 125 | + const tabModel = React.useMemo(() => getTabModelByTabId(PreviewTabId, env), [env]); |
| 126 | + |
| 127 | + React.useEffect(() => { |
| 128 | + let nextStep = historyRef.current.length; |
| 129 | + let nextTs = (historyRef.current[historyRef.current.length - 1]?.data?.ts ?? Date.now()) + 1000; |
| 130 | + const intervalId = window.setInterval(() => { |
| 131 | + const nextEvent = makeMockSysinfoEvent(nextTs, nextStep); |
| 132 | + historyRef.current = [...historyRef.current.slice(-(DefaultSysinfoHistoryPoints - 1)), nextEvent]; |
| 133 | + handleWaveEvent(nextEvent); |
| 134 | + nextStep++; |
| 135 | + nextTs += 1000; |
| 136 | + }, 1000); |
| 137 | + |
| 138 | + return () => { |
| 139 | + window.clearInterval(intervalId); |
| 140 | + }; |
| 141 | + }, []); |
| 142 | + |
| 143 | + return ( |
| 144 | + <WaveEnvContext.Provider value={env}> |
| 145 | + <TabModelContext.Provider value={tabModel}> |
| 146 | + <div className="flex w-full max-w-[980px] flex-col gap-2 px-6 py-6"> |
| 147 | + <div className="text-xs text-muted font-mono">full sysinfo block (mock WOS + FE-only WPS events)</div> |
| 148 | + <div className="rounded-md border border-border bg-panel p-4"> |
| 149 | + <div className="h-[620px]"> |
| 150 | + <Block preview={false} nodeModel={nodeModel} /> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + </TabModelContext.Provider> |
| 155 | + </WaveEnvContext.Provider> |
| 156 | + ); |
| 157 | +} |
| 158 | + |
| 159 | +export default function SysinfoPreview() { |
| 160 | + return <SysinfoPreviewInner />; |
| 161 | +} |
0 commit comments