Node.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React from "react";
  2. import { Handle, Position } from "react-flow-renderer";
  3. import styled from "styled-components";
  4. type Data = { label: string | object };
  5. const StyledWrapper = styled.div<{
  6. isArray?: boolean;
  7. isElement?: boolean;
  8. circle?: boolean;
  9. }>`
  10. background: ${({ theme }) => theme.BLACK_PRIMARY};
  11. border: 1px solid ${({ theme }) => theme.BLACK};
  12. border-radius: 5px;
  13. padding: 16px;
  14. color: ${({ theme, isArray, isElement }) =>
  15. isArray ? theme.SEAGREEN : isElement && theme.ORANGE};
  16. width: ${({ circle }) => (circle ? "20px" : "auto")};
  17. height: ${({ circle }) => (circle ? "20px" : "auto")};
  18. min-width: ${({ circle }) => !circle && "100px"};
  19. max-width: 400px;
  20. text-align: ${({ isArray }) => isArray && "center"};
  21. border-radius: ${({ circle }) => circle && "50%"};
  22. `;
  23. const StyledKey = styled.span`
  24. color: ${({ theme }) => theme.BLURPLE};
  25. `;
  26. const StyledLineWrapper = styled.div`
  27. overflow: hidden;
  28. text-overflow: ellipsis;
  29. white-space: nowrap;
  30. `;
  31. export const CustomNodeComponent: React.FC<{ data: Data; id: string }> = ({
  32. id,
  33. data,
  34. }) => {
  35. const { label: json } = data;
  36. if (Object.keys(json).length === 0) {
  37. return (
  38. <StyledWrapper circle>
  39. <Handle type="source" position={Position.Left} />
  40. <Handle type="target" position={Position.Right} />
  41. </StyledWrapper>
  42. );
  43. }
  44. if (typeof json === "string") {
  45. return (
  46. <StyledWrapper isArray>
  47. {json}
  48. <Handle type="source" position={Position.Left} />
  49. <Handle type="target" position={Position.Right} />
  50. </StyledWrapper>
  51. );
  52. }
  53. if (json instanceof Object) {
  54. const keyPairs = Object.entries(json);
  55. // temporary solution for array items
  56. if (Object.keys(json).every((val) => !isNaN(+val))) {
  57. return (
  58. <StyledWrapper isElement>
  59. {Object.values(json).join("")}
  60. <Handle type="source" position={Position.Left} />
  61. <Handle type="target" position={Position.Right} />
  62. </StyledWrapper>
  63. );
  64. }
  65. return (
  66. <StyledWrapper>
  67. {keyPairs.map((entries) => {
  68. const isValidValue =
  69. typeof entries[1] === "string" || typeof entries[1] === "number";
  70. return (
  71. <StyledLineWrapper key={entries[0]}>
  72. <StyledKey>{entries[0]}: </StyledKey>
  73. {isValidValue && entries[1]}
  74. </StyledLineWrapper>
  75. );
  76. })}
  77. <Handle type="source" id={id} position={Position.Left} />
  78. <Handle type="target" id={id} position={Position.Right} />
  79. </StyledWrapper>
  80. );
  81. }
  82. return null;
  83. };