frontPanel/src/pages/Questions/BranchingMap/CsComponent.tsx

150 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-01-05 16:48:35 +00:00
import { devlog } from "@frontend/kitui";
2023-12-20 10:46:38 +00:00
import { AnyTypedQuizQuestion } from "@model/questionTypes/shared";
2024-01-05 16:48:35 +00:00
import { Box, Button } from "@mui/material";
2024-01-10 18:23:38 +00:00
import { clearRuleForAll } from "@root/questions/actions";
2023-11-29 15:45:15 +00:00
import { useQuestionsStore } from "@root/questions/store";
2024-01-05 16:48:35 +00:00
import { updateRootContentId } from "@root/quizes/actions";
import { useCurrentQuiz } from "@root/quizes/hooks";
import {
cleardragQuestionContentId,
setModalQuestionParentContentId,
setModalQuestionTargetContentId,
2023-12-31 02:53:25 +00:00
updateOpenedModalSettingsId,
} from "@root/uiTools/actions";
2024-01-05 16:48:35 +00:00
import { useUiTools } from "@root/uiTools/store";
2024-01-10 18:23:38 +00:00
import type { Core } from "cytoscape";
2024-01-05 16:48:35 +00:00
import { enqueueSnackbar } from "notistack";
2024-01-10 18:23:38 +00:00
import { useEffect, useLayoutEffect, useMemo, useRef } from "react";
2024-01-05 16:48:35 +00:00
import CytoscapeComponent from "react-cytoscapejs";
import { withErrorBoundary } from "react-error-boundary";
import { DeleteNodeModal } from "../DeleteNodeModal";
2024-01-17 15:42:25 +00:00
import CsNodeButtons from "./CsNodeButtons";
2024-01-10 18:23:38 +00:00
import { addNode, layoutOptions, storeToNodes } from "./helper";
import { useRemoveNode } from "./hooks/useRemoveNode";
import "./style/styles.css";
2024-01-05 16:48:35 +00:00
import { stylesheet } from "./style/stylesheet";
2023-11-29 15:45:15 +00:00
2024-01-05 16:48:35 +00:00
function CsComponent() {
const desireToOpenABranchingModal = useUiTools((state) => state.desireToOpenABranchingModal);
const modalQuestionParentContentId = useUiTools((state) => state.modalQuestionParentContentId);
const modalQuestionTargetContentId = useUiTools((state) => state.modalQuestionTargetContentId);
const trashQuestions = useQuestionsStore((state) => state.questions);
2023-11-29 15:45:15 +00:00
const cyRef = useRef<Core | null>(null);
const { removeNode } = useRemoveNode({ cyRef });
const csElements = useMemo(() => {
const questions = trashQuestions.filter(
(question): question is AnyTypedQuizQuestion => question.type !== null && question.type !== "result"
);
2023-12-31 10:36:30 +00:00
return storeToNodes(questions);
}, [trashQuestions]);
2023-12-31 10:36:30 +00:00
useLayoutEffect(() => {
2023-12-31 02:53:25 +00:00
const cy = cyRef?.current;
if (desireToOpenABranchingModal) {
setTimeout(() => {
cy?.getElementById(desireToOpenABranchingModal)?.data("eroticeyeblink", true);
2023-12-31 02:53:25 +00:00
}, 250);
} else {
2023-12-31 02:53:25 +00:00
cy?.elements().data("eroticeyeblink", false);
}
2023-12-31 02:53:25 +00:00
}, [desireToOpenABranchingModal]);
2024-01-05 16:48:35 +00:00
useEffect(() => {
if (modalQuestionTargetContentId.length !== 0 && modalQuestionParentContentId.length !== 0) {
if (!cyRef.current) return;
2024-01-05 16:48:35 +00:00
2023-12-31 02:53:25 +00:00
addNode({
parentNodeContentId: modalQuestionParentContentId,
targetNodeContentId: modalQuestionTargetContentId,
});
}
2023-12-31 02:53:25 +00:00
setModalQuestionParentContentId("");
setModalQuestionTargetContentId("");
}, [modalQuestionTargetContentId]);
useEffect(function onMount() {
updateOpenedModalSettingsId();
document.addEventListener("pointerup", cleardragQuestionContentId);
2023-11-29 15:45:15 +00:00
return () => {
document.removeEventListener("pointerup", cleardragQuestionContentId);
2023-11-29 15:45:15 +00:00
};
}, []);
2024-01-05 16:48:35 +00:00
useEffect(
function rerunLayout() {
cyRef.current?.layout(layoutOptions).run();
cyRef.current?.fit(undefined, 70);
},
[csElements]
);
2023-11-29 15:45:15 +00:00
return (
<>
<Box mb="20px">
<Button
sx={{
height: "27px",
color: "#7E2AEA",
textDecoration: "underline",
fontSize: "16px",
}}
variant="text"
onClick={() => {
cyRef.current?.fit(undefined, 70);
}}
>
Выровнять
</Button>
</Box>
<Box
sx={{
position: "relative",
height: "480px",
background: "#F2F3F7",
}}
>
<CytoscapeComponent
wheelSensitivity={0.1}
elements={csElements}
style={{
height: "100%",
width: "100%",
overflow: "hidden",
}}
stylesheet={stylesheet}
layout={layoutOptions}
cy={(cy) => {
cyRef.current = cy;
}}
autoungrabify={true}
autounselectify={true}
boxSelectionEnabled={false}
/>
<CsNodeButtons csElements={csElements} cyRef={cyRef} />
</Box>
2023-12-21 14:56:29 +00:00
<DeleteNodeModal removeNode={removeNode} />
</>
2023-11-29 15:45:15 +00:00
);
2023-12-31 02:53:25 +00:00
}
function Clear() {
const quiz = useCurrentQuiz();
2023-12-21 14:56:29 +00:00
if (quiz) {
updateRootContentId(quiz?.id, "");
}
2023-12-31 02:53:25 +00:00
clearRuleForAll();
return <></>;
}
export default withErrorBoundary(CsComponent, {
fallback: <Clear />,
onError: (error, info) => {
2023-12-31 02:53:25 +00:00
enqueueSnackbar("Дерево порвалось");
devlog(info);
devlog(error);
},
});