DataPanel.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Typography, useTheme } from '@mui/material';
  2. import SyntaxHighlighter from 'react-syntax-highlighter';
  3. import { useTranslation } from 'react-i18next';
  4. import { vs2015, github } from 'react-syntax-highlighter/dist/esm/styles/hljs';
  5. import { GraphNode } from '../../types';
  6. const DataPanel = (props: { node: GraphNode; color: any }) => {
  7. // i18n
  8. const { t: searchTrans } = useTranslation('search');
  9. // theme
  10. const theme = useTheme();
  11. // props
  12. const { node, color } = props;
  13. const data = node.data;
  14. // format data to json
  15. const json = JSON.stringify(data, null, 2);
  16. const image = [];
  17. // loop through the object find any value is an image url, add it into an image array;
  18. for (const key in data) {
  19. if (
  20. typeof data[key] === 'string' &&
  21. data[key].match(/\.(jpeg|jpg|gif|png)$/) != null
  22. ) {
  23. image.push(data[key]);
  24. }
  25. }
  26. const styleObj = node.nodeY
  27. ? {
  28. top: node.nodeY + 16,
  29. left: node.nodeX! + 16,
  30. right: 'auto',
  31. position: 'absolute',
  32. borderColor: color(node.color + ''),
  33. }
  34. : { borderColor: color(node.color + '') };
  35. return (
  36. <div key={node.id} className="nodeInfo" style={styleObj as any}>
  37. <div className="wrapper">
  38. {image.map((url, i) => (
  39. <a key={i} href={url} target="_blank">
  40. <img src={url} />
  41. </a>
  42. ))}
  43. </div>
  44. <SyntaxHighlighter
  45. language="json"
  46. style={theme.palette.mode === 'dark' ? vs2015 : github}
  47. customStyle={{ fontSize: 11, margin: 0 }}
  48. wrapLines={true}
  49. wrapLongLines={true}
  50. showLineNumbers={false}
  51. >
  52. {json}
  53. </SyntaxHighlighter>
  54. {node.nodeY && (
  55. <Typography className="tip">
  56. {searchTrans('graphNodeHoverTip')}
  57. </Typography>
  58. )}
  59. </div>
  60. );
  61. };
  62. export default DataPanel;