waveterm/frontend/app/tab/tabcontent.tsx

93 lines
3.0 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, LayoutComponentModel } from "@/app/block/block";
import { getApi } from "@/store/global";
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 } from "@/element/quickelems";
2024-07-23 22:50:23 +02:00
import { TileLayout } from "frontend/layout/index";
import { getLayoutStateAtomForTab } from "frontend/layout/lib/layoutAtom";
2024-06-18 06:50:33 +02:00
import { useAtomValue } from "jotai";
2024-06-26 18:31:43 +02:00
import { useMemo } from "react";
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,
onMagnifyToggle: () => void,
2024-06-20 08:00:57 +02:00
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;
}
const layoutModel: LayoutComponentModel = {
2024-07-09 00:04:48 +02:00
onClose: onClose,
onMagnifyToggle: onMagnifyToggle,
2024-07-09 00:04:48 +02:00
dragHandleRef: dragHandleRef,
};
return <Block key={tabData.blockId} blockId={tabData.blockId} layoutModel={layoutModel} preview={false} />;
2024-06-26 18:31:43 +02:00
}
2024-06-18 06:50:33 +02:00
2024-06-26 18:31:43 +02:00
function renderPreview(tabData: TabLayoutData) {
2024-07-09 00:04:48 +02:00
return <Block key={tabData.blockId} blockId={tabData.blockId} layoutModel={null} preview={true} />;
2024-06-26 18:31:43 +02:00
}
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 (
<div className="tabcontent">
<CenteredDiv>Tab Loading</CenteredDiv>
</div>
);
2024-06-18 06:50:33 +02:00
}
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 };