waveterm/frontend/app/block/block.tsx

212 lines
7.5 KiB
TypeScript
Raw Normal View History

// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
2024-08-02 00:35:13 +02:00
import { BlockComponentModel, BlockProps } from "@/app/block/blocktypes";
2024-08-01 22:06:18 +02:00
import { PlotView } from "@/app/view/plotview/plotview";
2024-08-01 21:53:49 +02:00
import { PreviewView, makePreviewModel } from "@/app/view/preview/preview";
import { ErrorBoundary } from "@/element/errorboundary";
2024-05-17 07:48:23 +02:00
import { CenteredDiv } from "@/element/quickelems";
2024-08-02 00:35:13 +02:00
import { atoms, setBlockFocus, useBlockAtom } from "@/store/global";
2024-05-28 21:12:28 +02:00
import * as WOS from "@/store/wos";
2024-06-21 23:44:11 +02:00
import * as util from "@/util/util";
2024-08-01 22:06:18 +02:00
import { CpuPlotView, makeCpuPlotViewModel } from "@/view/cpuplot/cpuplot";
import { HelpView } from "@/view/helpview/helpview";
import { TerminalView, makeTerminalModel } from "@/view/term/term";
2024-08-01 22:06:18 +02:00
import { WaveAi, makeWaveAiViewModel } from "@/view/waveai/waveai";
import { WebView, makeWebViewModel } from "@/view/webview/webview";
import * as jotai from "jotai";
2024-05-28 21:12:28 +02:00
import * as React from "react";
2024-08-02 00:35:13 +02:00
import { BlockFrame } from "./blockframe";
import { blockViewToIcon, blockViewToName } from "./blockutil";
import "./block.less";
2024-07-09 00:04:48 +02:00
function getViewElemAndModel(
blockId: string,
blockView: string,
blockRef: React.RefObject<HTMLDivElement>
): { viewModel: ViewModel; viewElem: JSX.Element } {
let viewElem: JSX.Element = null;
let viewModel: ViewModel = null;
if (util.isBlank(blockView)) {
viewElem = <CenteredDiv>No View</CenteredDiv>;
viewModel = makeDefaultViewModel(blockId);
} else if (blockView === "term") {
const termViewModel = makeTerminalModel(blockId);
viewElem = <TerminalView key={blockId} blockId={blockId} model={termViewModel} />;
viewModel = termViewModel;
2024-07-09 00:04:48 +02:00
} else if (blockView === "preview") {
const previewModel = makePreviewModel(blockId);
viewElem = <PreviewView key={blockId} blockId={blockId} model={previewModel} />;
viewModel = previewModel;
} else if (blockView === "plot") {
viewElem = <PlotView key={blockId} />;
} else if (blockView === "web") {
2024-07-15 18:40:28 +02:00
const webviewModel = makeWebViewModel(blockId);
viewElem = <WebView key={blockId} parentRef={blockRef} model={webviewModel} />;
viewModel = webviewModel;
2024-07-09 00:04:48 +02:00
} else if (blockView === "waveai") {
const waveAiModel = makeWaveAiViewModel(blockId);
viewElem = <WaveAi key={blockId} model={waveAiModel} />;
viewModel = waveAiModel;
} else if (blockView === "cpuplot") {
const cpuPlotModel = makeCpuPlotViewModel(blockId);
viewElem = <CpuPlotView key={blockId} model={cpuPlotModel} />;
viewModel = cpuPlotModel;
} else if (blockView == "help") {
viewElem = <HelpView key={blockId} />;
viewModel = makeDefaultViewModel(blockId);
2024-06-26 18:31:43 +02:00
}
2024-07-09 00:04:48 +02:00
if (viewModel == null) {
viewElem = <CenteredDiv>Invalid View "{blockView}"</CenteredDiv>;
2024-07-09 00:04:48 +02:00
viewModel = makeDefaultViewModel(blockId);
}
return { viewElem, viewModel };
2024-06-26 18:31:43 +02:00
}
2024-07-09 00:04:48 +02:00
function makeDefaultViewModel(blockId: string): ViewModel {
const blockDataAtom = WOS.getWaveObjectAtom<Block>(WOS.makeORef("block", blockId));
let viewModel: ViewModel = {
viewIcon: jotai.atom((get) => {
const blockData = get(blockDataAtom);
return blockViewToIcon(blockData?.meta?.view);
2024-07-09 00:04:48 +02:00
}),
viewName: jotai.atom((get) => {
const blockData = get(blockDataAtom);
return blockViewToName(blockData?.meta?.view);
2024-07-09 00:04:48 +02:00
}),
viewText: jotai.atom((get) => {
const blockData = get(blockDataAtom);
return blockData?.meta?.title;
}),
preIconButton: jotai.atom(null),
endIconButtons: jotai.atom(null),
2024-07-09 00:04:48 +02:00
};
return viewModel;
}
const BlockPreview = React.memo(({ blockId, layoutModel }: BlockProps) => {
const [blockData] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
2024-07-09 00:04:48 +02:00
if (!blockData) {
return null;
}
let { viewModel } = getViewElemAndModel(blockId, blockData?.meta?.view, null);
2024-07-09 00:04:48 +02:00
return (
<BlockFrame
key={blockId}
blockId={blockId}
layoutModel={layoutModel}
preview={true}
blockModel={null}
viewModel={viewModel}
/>
);
});
const BlockFull = React.memo(({ blockId, layoutModel }: BlockProps) => {
const focusElemRef = React.useRef<HTMLInputElement>(null);
const blockRef = React.useRef<HTMLDivElement>(null);
const [blockClicked, setBlockClicked] = React.useState(false);
const [blockData, blockDataLoading] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
const [focusedChild, setFocusedChild] = React.useState(null);
const isFocusedAtom = useBlockAtom<boolean>(blockId, "isFocused", () => {
return jotai.atom((get) => {
const winData = get(atoms.waveWindow);
return winData.activeblockid === blockId;
});
});
const isFocused = jotai.useAtomValue(isFocusedAtom);
React.useLayoutEffect(() => {
setBlockClicked(isFocused);
}, [isFocused]);
React.useLayoutEffect(() => {
if (!blockClicked) {
return;
}
setBlockClicked(false);
const focusWithin = blockRef.current?.contains(document.activeElement);
if (!focusWithin) {
setFocusTarget();
}
setBlockFocus(blockId);
}, [blockClicked]);
React.useLayoutEffect(() => {
if (focusedChild == null) {
return;
}
setBlockFocus(blockId);
}, [focusedChild, blockId]);
// treat the block as clicked on creation
2024-06-26 18:31:43 +02:00
const setBlockClickedTrue = React.useCallback(() => {
setBlockClicked(true);
}, []);
let { viewElem, viewModel } = React.useMemo(
() => getViewElemAndModel(blockId, blockData?.meta?.view, blockRef),
[blockId, blockData?.meta?.view, blockRef]
);
const determineFocusedChild = React.useCallback(
(event: React.FocusEvent<HTMLDivElement, Element>) => {
setFocusedChild(event.target);
},
[setFocusedChild]
);
const setFocusTarget = React.useCallback(() => {
const ok = viewModel?.giveFocus?.();
if (ok) {
return;
}
2024-07-31 07:53:50 +02:00
focusElemRef.current?.focus({ preventScroll: true });
}, []);
2024-07-09 00:04:48 +02:00
if (!blockId || !blockData) return null;
2024-07-09 00:04:48 +02:00
if (blockDataLoading) {
2024-07-09 00:04:48 +02:00
viewElem = <CenteredDiv>Loading...</CenteredDiv>;
}
2024-07-09 00:04:48 +02:00
const blockModel: BlockComponentModel = {
onClick: setBlockClickedTrue,
onFocusCapture: determineFocusedChild,
blockRef: blockRef,
};
return (
<BlockFrame
2024-06-26 18:31:43 +02:00
key={blockId}
blockId={blockId}
2024-07-09 00:04:48 +02:00
layoutModel={layoutModel}
preview={false}
2024-07-09 00:04:48 +02:00
blockModel={blockModel}
viewModel={viewModel}
>
<div key="focuselem" className="block-focuselem">
<input type="text" value="" ref={focusElemRef} id={`${blockId}-dummy-focus`} onChange={() => {}} />
</div>
<div
key="content"
className="block-content"
style={{ pointerEvents: layoutModel?.disablePointerEvents ? "none" : undefined }}
>
<ErrorBoundary>
2024-07-09 00:04:48 +02:00
<React.Suspense fallback={<CenteredDiv>Loading...</CenteredDiv>}>{viewElem}</React.Suspense>
</ErrorBoundary>
</div>
</BlockFrame>
);
2024-06-26 18:31:43 +02:00
});
2024-07-09 00:04:48 +02:00
const Block = React.memo((props: BlockProps) => {
if (props.preview) {
return <BlockPreview {...props} />;
}
return <BlockFull {...props} />;
});
export { Block };