waveterm/frontend/app/tab/tabcontent.tsx

90 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-06-18 06:50:33 +02:00
// Copyright 2023, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import { Block, BlockFrame } from "@/app/block/block";
2024-06-18 06:50:33 +02:00
import * as services from "@/store/services";
import * as WOS from "@/store/wos";
2024-06-26 18:31:43 +02:00
import * as React from "react";
2024-06-18 06:50:33 +02:00
import { CenteredDiv, CenteredLoadingDiv } from "@/element/quickelems";
import { TileLayout } from "@/faraday/index";
import { getLayoutStateAtomForTab } from "@/faraday/lib/layoutAtom";
import { useAtomValue } from "jotai";
2024-06-26 18:31:43 +02:00
import { useMemo } from "react";
import { getApi } from "../store/global";
2024-06-18 06:50:33 +02:00
import "./tabcontent.less";
2024-06-26 18:31:43 +02:00
const TabContent = React.memo(({ tabId }: { tabId: string }) => {
2024-06-18 06:50:33 +02:00
const oref = useMemo(() => WOS.makeORef("tab", tabId), [tabId]);
const loadingAtom = useMemo(() => WOS.getWaveObjectLoadingAtom(oref), [oref]);
const tabLoading = useAtomValue(loadingAtom);
const tabAtom = useMemo(() => WOS.getWaveObjectAtom<Tab>(oref), [oref]);
const layoutStateAtom = useMemo(() => getLayoutStateAtomForTab(tabId, tabAtom), [tabAtom, tabId]);
const tabData = useAtomValue(tabAtom);
2024-06-26 18:31:43 +02:00
const tileLayoutContents = useMemo(() => {
function renderBlock(
2024-06-20 08:00:57 +02:00
tabData: TabLayoutData,
ready: boolean,
onClose: () => void,
dragHandleRef: React.RefObject<HTMLDivElement>
2024-06-26 18:31:43 +02:00
) {
2024-06-20 08:00:57 +02:00
if (!tabData.blockId || !ready) {
return null;
}
2024-06-26 18:31:43 +02:00
return (
<Block
key={tabData.blockId}
blockId={tabData.blockId}
onClose={onClose}
dragHandleRef={dragHandleRef}
/>
);
}
2024-06-18 06:50:33 +02:00
2024-06-26 18:31:43 +02:00
function renderPreview(tabData: TabLayoutData) {
return <BlockFrame key={tabData.blockId} blockId={tabData.blockId} preview={true} />;
}
2024-06-18 06:50:33 +02:00
2024-06-26 18:31:43 +02:00
function onNodeDelete(data: TabLayoutData) {
return services.ObjectService.DeleteBlock(data.blockId);
}
2024-06-18 06:50:33 +02:00
2024-06-26 18:31:43 +02:00
return {
renderContent: renderBlock,
renderPreview: renderPreview,
tabId: tabId,
onNodeDelete: onNodeDelete,
};
}, []);
2024-06-18 06:50:33 +02:00
if (tabLoading) {
return <CenteredLoadingDiv />;
}
if (!tabData) {
return (
<div className="tabcontent">
<CenteredDiv>Tab Not Found</CenteredDiv>
</div>
);
}
2024-06-21 23:44:11 +02:00
if (tabData?.blockids?.length == 0) {
return <div className="tabcontent tabcontent-empty"></div>;
}
2024-06-18 06:50:33 +02:00
return (
<div className="tabcontent">
<TileLayout
key={tabId}
2024-06-26 18:31:43 +02:00
contents={tileLayoutContents}
2024-06-18 06:50:33 +02:00
layoutTreeStateAtom={layoutStateAtom}
2024-06-26 18:31:43 +02:00
getCursorPoint={getApi().getCursorPoint}
2024-06-18 06:50:33 +02:00
/>
</div>
);
2024-06-26 18:31:43 +02:00
});
2024-06-18 06:50:33 +02:00
export { TabContent };