index.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import React from "react";
  2. import { ReactZoomPanPinchRef, TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";
  3. import { Canvas, Edge, ElkRoot } from "reaflow";
  4. import { CustomNode } from "src/components/CustomNode";
  5. import useGraph from "src/store/useGraph";
  6. import useUser from "src/store/useUser";
  7. import { getNodePath } from "src/utils/getNodePath";
  8. import styled from "styled-components";
  9. import { Loading } from "../Loading";
  10. import { ErrorView } from "./ErrorView";
  11. import { PremiumView } from "./PremiumView";
  12. interface GraphProps {
  13. isWidget?: boolean;
  14. openNodeModal: () => void;
  15. }
  16. const StyledEditorWrapper = styled.div<{ widget: boolean }>`
  17. position: absolute;
  18. width: 100%;
  19. height: ${({ widget }) => (widget ? "calc(100vh - 36px)" : "calc(100vh - 65px)")};
  20. background: ${({ theme }) => theme.BACKGROUND_SECONDARY};
  21. background-image: ${({ theme }) =>
  22. `radial-gradient(#505050 1px, ${theme.BACKGROUND_SECONDARY} 1px)`};
  23. background-size: 25px 25px;
  24. :active {
  25. cursor: move;
  26. }
  27. .dragging,
  28. .dragging button {
  29. pointer-events: none;
  30. }
  31. rect {
  32. fill: ${({ theme }) => theme.BACKGROUND_NODE};
  33. }
  34. @media only screen and (max-width: 1440px) {
  35. background-image: ${({ theme }) =>
  36. `radial-gradient(#505050 0.5px, ${theme.BACKGROUND_SECONDARY} 0.5px)`};
  37. background-size: 15px 15px;
  38. }
  39. @media only screen and (max-width: 320px) {
  40. height: 100vh;
  41. }
  42. `;
  43. const GraphComponent = ({ isWidget = false, openNodeModal }: GraphProps) => {
  44. const isPremium = useUser(state => state.isPremium);
  45. const setLoading = useGraph(state => state.setLoading);
  46. const setZoomPanPinch = useGraph(state => state.setZoomPanPinch);
  47. const centerView = useGraph(state => state.centerView);
  48. const setSelectedNode = useGraph(state => state.setSelectedNode);
  49. const loading = useGraph(state => state.loading);
  50. const direction = useGraph(state => state.direction);
  51. const nodes = useGraph(state => state.nodes);
  52. const edges = useGraph(state => state.edges);
  53. const [size, setSize] = React.useState({
  54. width: 1,
  55. height: 1,
  56. });
  57. const handleNodeClick = React.useCallback(
  58. (_: React.MouseEvent<SVGElement>, data: NodeData) => {
  59. if (setSelectedNode)
  60. setSelectedNode({ node: data.text, path: getNodePath(nodes, edges, data.id) });
  61. if (openNodeModal) openNodeModal();
  62. },
  63. [edges, nodes, openNodeModal, setSelectedNode]
  64. );
  65. const onInit = React.useCallback(
  66. (ref: ReactZoomPanPinchRef) => {
  67. setZoomPanPinch(ref);
  68. },
  69. [setZoomPanPinch]
  70. );
  71. const onLayoutChange = React.useCallback(
  72. (layout: ElkRoot) => {
  73. if (layout.width && layout.height) {
  74. const areaSize = layout.width * layout.height;
  75. const changeRatio = Math.abs((areaSize * 100) / (size.width * size.height) - 100);
  76. setSize({
  77. width: (layout.width as number) + 400,
  78. height: (layout.height as number) + 400,
  79. });
  80. requestAnimationFrame(() => {
  81. setTimeout(() => {
  82. setLoading(false);
  83. setTimeout(() => {
  84. if (changeRatio > 70 || isWidget) centerView();
  85. });
  86. });
  87. });
  88. }
  89. },
  90. [centerView, isWidget, setLoading, size.height, size.width]
  91. );
  92. const onCanvasClick = React.useCallback(() => {
  93. const input = document.querySelector("input:focus") as HTMLInputElement;
  94. if (input) input.blur();
  95. }, []);
  96. if (nodes.length > 8_000) return <ErrorView />;
  97. if (nodes.length > 1_000 && !isWidget) {
  98. if (!isPremium()) return <PremiumView />;
  99. }
  100. return (
  101. <StyledEditorWrapper onContextMenu={e => e.preventDefault()} widget={isWidget}>
  102. <Loading message="Painting graph..." loading={loading} />
  103. <TransformWrapper
  104. maxScale={2}
  105. minScale={0.05}
  106. initialScale={0.4}
  107. wheel={{ step: 0.08 }}
  108. zoomAnimation={{ animationType: "linear" }}
  109. doubleClick={{ disabled: true }}
  110. onInit={onInit}
  111. onPanning={ref => ref.instance.wrapperComponent?.classList.add("dragging")}
  112. onPanningStop={ref => ref.instance.wrapperComponent?.classList.remove("dragging")}
  113. >
  114. <TransformComponent
  115. wrapperStyle={{
  116. width: "100%",
  117. height: "100%",
  118. overflow: "hidden",
  119. display: loading ? "none" : "block",
  120. }}
  121. >
  122. <Canvas
  123. className="jsoncrack-canvas"
  124. nodes={nodes}
  125. edges={edges}
  126. maxWidth={size.width}
  127. maxHeight={size.height}
  128. direction={direction}
  129. onLayoutChange={onLayoutChange}
  130. onCanvasClick={onCanvasClick}
  131. zoomable={false}
  132. animated={false}
  133. readonly={true}
  134. dragEdge={null}
  135. dragNode={null}
  136. fit={true}
  137. key={direction}
  138. node={props => <CustomNode {...props} onClick={handleNodeClick} />}
  139. edge={props => <Edge {...props} containerClassName={`edge-${props.id}`} />}
  140. />
  141. </TransformComponent>
  142. </TransformWrapper>
  143. </StyledEditorWrapper>
  144. );
  145. };
  146. export const Graph = React.memo(GraphComponent);