TextNode.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React from "react";
  2. import { MdCompareArrows } from "react-icons/md";
  3. import { ConditionalWrapper, CustomNodeProps } from "src/components/CustomNode";
  4. import useConfig from "src/hooks/store/useConfig";
  5. import useGraph from "src/hooks/store/useGraph";
  6. import useStored from "src/hooks/store/useStored";
  7. import styled from "styled-components";
  8. import * as Styled from "./styles";
  9. const StyledExpand = styled.button`
  10. pointer-events: all;
  11. position: absolute;
  12. display: flex;
  13. align-items: center;
  14. justify-content: center;
  15. top: 0;
  16. right: 0;
  17. padding: 0;
  18. color: ${({ theme }) => theme.TEXT_NORMAL};
  19. background: rgba(0, 0, 0, 0.1);
  20. min-height: 0;
  21. height: 100%;
  22. width: 40px;
  23. border-radius: 0;
  24. border-left: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
  25. `;
  26. const TextNode: React.FC<
  27. CustomNodeProps<string> & { node: NodeData; hasCollapse: boolean }
  28. > = ({
  29. node,
  30. width,
  31. height,
  32. value,
  33. isParent = false,
  34. hasCollapse = false,
  35. x,
  36. y,
  37. }) => {
  38. const performanceMode = useConfig((state) => state.performanceMode);
  39. const hideCollapse = useStored((state) => state.hideCollapse);
  40. const expandNodes = useGraph((state) => state.expandNodes);
  41. const collapseNodes = useGraph((state) => state.collapseNodes);
  42. const [isExpanded, setIsExpanded] = React.useState(true);
  43. const handleExpand = (e: React.MouseEvent<HTMLButtonElement>) => {
  44. e.stopPropagation();
  45. setIsExpanded(!isExpanded);
  46. if (isExpanded) collapseNodes(node.id);
  47. else expandNodes(node.id);
  48. };
  49. return (
  50. <Styled.StyledForeignObject
  51. width={width}
  52. height={height}
  53. x={0}
  54. y={0}
  55. data-nodeid={node.id}
  56. >
  57. <ConditionalWrapper condition={performanceMode}>
  58. <Styled.StyledText
  59. hideCollapse={hideCollapse}
  60. hasCollapse={isParent && hasCollapse}
  61. width={width}
  62. height={height}
  63. >
  64. <Styled.StyledKey
  65. data-x={x}
  66. data-y={y}
  67. data-key={JSON.stringify(value)}
  68. parent={isParent}
  69. >
  70. <Styled.StyledLinkItUrl>
  71. {JSON.stringify(value).replaceAll('"', "")}
  72. </Styled.StyledLinkItUrl>
  73. </Styled.StyledKey>
  74. </Styled.StyledText>
  75. </ConditionalWrapper>
  76. {isParent && hasCollapse && !hideCollapse && (
  77. <StyledExpand onClick={handleExpand}>
  78. <MdCompareArrows size={18} />
  79. </StyledExpand>
  80. )}
  81. </Styled.StyledForeignObject>
  82. );
  83. };
  84. export default TextNode;