waveterm/frontend/app/tab/tabcontent.tsx

84 lines
2.6 KiB
TypeScript
Raw Normal View History

// Copyright 2024, Command Line Inc.
2024-06-18 06:50:33 +02:00
// SPDX-License-Identifier: Apache-2.0
2024-08-02 00:35:13 +02:00
import { Block } from "@/app/block/block";
import { CenteredDiv } from "@/element/quickelems";
import { ContentRenderer, NodeModel, PreviewRenderer, TileLayout } from "@/layout/index";
import { TileLayoutContents } from "@/layout/lib/types";
import { atoms, getApi } from "@/store/global";
2024-06-18 06:50:33 +02:00
import * as services from "@/store/services";
import * as WOS from "@/store/wos";
import { atom, useAtomValue } from "jotai";
import * as React from "react";
2024-06-26 18:31:43 +02:00
import { useMemo } from "react";
import "./tabcontent.scss";
2024-06-18 06:50:33 +02:00
const tileGapSizeAtom = atom((get) => {
const settings = get(atoms.settingsAtom);
return settings["window:tilegapsize"];
});
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 tabData = useAtomValue(tabAtom);
const tileGapSize = useAtomValue(tileGapSizeAtom);
2024-06-18 06:50:33 +02:00
2024-06-26 18:31:43 +02:00
const tileLayoutContents = useMemo(() => {
const renderContent: ContentRenderer = (nodeModel: NodeModel) => {
return <Block key={nodeModel.blockId} nodeModel={nodeModel} preview={false} />;
};
2024-06-18 06:50:33 +02:00
const renderPreview: PreviewRenderer = (nodeModel: NodeModel) => {
return <Block key={nodeModel.blockId} nodeModel={nodeModel} 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,
renderPreview,
tabId,
onNodeDelete,
gapSizePx: tileGapSize,
} as TileLayoutContents;
}, [tabId, tileGapSize]);
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}
tabAtom={tabAtom}
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 };