Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree mode option addded for API embeded Iframe #395

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/pages/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import { useRouter } from "next/router";
import { useMantineColorScheme } from "@mantine/core";
import { ThemeProvider } from "styled-components";
import toast from "react-hot-toast";
import { CanvasDirection } from "reaflow/dist/layout/elkLayout";
import { darkTheme, lightTheme } from "src/constants/theme";
import { Toolbar } from "src/containers/Toolbar";
import { TreeView } from "src/containers/Views/TreeView";
import { ViewMode } from "src/enums/viewMode.enum";
import useConfig from "src/store/useConfig";
import useGraph from "src/modules/GraphView/stores/useGraph";
import useFile from "src/store/useFile";

type Options = {
theme: "light" | "dark";
direction: CanvasDirection;
viewMode: ViewMode.Graph | ViewMode.Tree;
};
interface EmbedMessage {
data: {
json?: string;
options?: {
theme?: "light" | "dark";
direction?: "LEFT" | "RIGHT" | "DOWN" | "UP";
};
options?: Options;
};
}

Expand All @@ -28,10 +34,12 @@ const WidgetPage = () => {
const { query, push, isReady } = useRouter();
const { setColorScheme } = useMantineColorScheme();
const [theme, setTheme] = React.useState<"dark" | "light">("dark");
const viewMode = useConfig(state => state.viewMode);
const checkEditorSession = useFile(state => state.checkEditorSession);
const setContents = useFile(state => state.setContents);
const setDirection = useGraph(state => state.setDirection);
const clearGraph = useGraph(state => state.clearGraph);
const setViewMode = useConfig(state => state.setViewMode);

React.useEffect(() => {
if (isReady) {
Expand All @@ -50,6 +58,13 @@ const WidgetPage = () => {
setTheme(event.data.options.theme);
}

if (
event.data?.options?.viewMode === ViewMode.Graph ||
event.data?.options?.viewMode === ViewMode.Tree
) {
setViewMode(event.data?.options.viewMode);
}

setContents({ contents: event.data.json, hasChanges: false });
setDirection(event.data.options?.direction || "RIGHT");
} catch (error) {
Expand All @@ -60,7 +75,7 @@ const WidgetPage = () => {

window.addEventListener("message", handler);
return () => window.removeEventListener("message", handler);
}, [setColorScheme, setContents, setDirection, theme]);
}, [setColorScheme, setContents, setDirection, setViewMode, theme]);

React.useEffect(() => {
setColorScheme(theme);
Expand All @@ -73,7 +88,8 @@ const WidgetPage = () => {
</Head>
<ThemeProvider theme={theme === "dark" ? darkTheme : lightTheme}>
<Toolbar isWidget />
<GraphView isWidget />
{viewMode === ViewMode.Graph && <GraphView isWidget />}
{viewMode === ViewMode.Tree && <TreeView />}
</ThemeProvider>
</>
);
Expand Down