2024-05-13 23:40:18 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import * as React from "react";
|
|
|
|
import * as jotai from "jotai";
|
2024-05-16 09:29:58 +02:00
|
|
|
import { atoms, blockDataMap } from "@/store/global";
|
2024-05-13 23:40:18 +02:00
|
|
|
|
2024-05-14 08:45:41 +02:00
|
|
|
import { TerminalView } from "@/app/view/term";
|
|
|
|
import { PreviewView } from "@/app/view/preview";
|
2024-05-14 21:29:41 +02:00
|
|
|
import { CenteredLoadingDiv } from "@/element/quickelems";
|
2024-05-13 23:40:18 +02:00
|
|
|
|
2024-05-14 08:45:41 +02:00
|
|
|
import "./block.less";
|
2024-05-13 23:40:18 +02:00
|
|
|
|
2024-05-14 00:10:31 +02:00
|
|
|
const Block = ({ blockId }: { blockId: string }) => {
|
2024-05-13 23:40:18 +02:00
|
|
|
const blockRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
const [dims, setDims] = React.useState({ width: 0, height: 0 });
|
2024-05-14 00:40:52 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!blockRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
2024-05-13 23:40:18 +02:00
|
|
|
const rect = blockRef.current.getBoundingClientRect();
|
2024-05-14 08:45:41 +02:00
|
|
|
const newWidth = Math.floor(rect.width);
|
|
|
|
const newHeight = Math.floor(rect.height);
|
2024-05-13 23:40:18 +02:00
|
|
|
if (newWidth !== dims.width || newHeight !== dims.height) {
|
|
|
|
setDims({ width: newWidth, height: newHeight });
|
|
|
|
}
|
2024-05-14 00:40:52 +02:00
|
|
|
}, [blockRef.current]);
|
2024-05-14 08:45:41 +02:00
|
|
|
let blockElem: JSX.Element = null;
|
2024-05-16 09:29:58 +02:00
|
|
|
const blockAtom = blockDataMap.get(blockId);
|
2024-05-14 08:45:41 +02:00
|
|
|
const blockData = jotai.useAtomValue(blockAtom);
|
|
|
|
if (blockData.view === "term") {
|
|
|
|
blockElem = <TerminalView blockId={blockId} />;
|
|
|
|
} else if (blockData.view === "preview") {
|
|
|
|
blockElem = <PreviewView blockId={blockId} />;
|
|
|
|
}
|
2024-05-13 23:40:18 +02:00
|
|
|
return (
|
|
|
|
<div className="block" ref={blockRef}>
|
|
|
|
<div key="header" className="block-header">
|
2024-05-14 00:10:31 +02:00
|
|
|
<div className="text-fixed">
|
|
|
|
Block [{blockId.substring(0, 8)}] {dims.width}x{dims.height}
|
2024-05-13 23:40:18 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-05-14 08:45:41 +02:00
|
|
|
<div key="content" className="block-content">
|
2024-05-14 21:29:41 +02:00
|
|
|
<React.Suspense fallback={<CenteredLoadingDiv />}>{blockElem}</React.Suspense>
|
2024-05-14 08:45:41 +02:00
|
|
|
</div>
|
2024-05-13 23:40:18 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { Block };
|