JSONViewer.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import React from 'react';
  2. import JSONTree from 'react-json-tree';
  3. import useDataSaver from 'lib/hooks/useDataSaver';
  4. import theme from './themes/google';
  5. import DynamicButton from './DynamicButton';
  6. interface JSONViewerProps {
  7. data: {
  8. [key: string]: string;
  9. };
  10. }
  11. const JSONViewer: React.FC<JSONViewerProps> = ({ data }) => {
  12. const { copyToClipboard, saveFile } = useDataSaver();
  13. const copyButtonHandler = () => {
  14. copyToClipboard(JSON.stringify(data));
  15. };
  16. const buttonClasses = 'button is-link is-outlined is-small is-centered';
  17. return (
  18. <div>
  19. <JSONTree
  20. data={data}
  21. theme={theme}
  22. shouldExpandNode={() => true}
  23. hideRoot
  24. />
  25. <div className="field has-addons is-justify-content-flex-end">
  26. <DynamicButton
  27. callback={copyButtonHandler}
  28. classes={`${buttonClasses} mr-1`}
  29. title="Copy the message to the clipboard"
  30. text={{ default: 'Copy', dynamic: 'Copied!' }}
  31. >
  32. <span className="icon">
  33. <i className="far fa-clipboard" />
  34. </span>
  35. </DynamicButton>
  36. <button
  37. className={buttonClasses}
  38. title="Download the message as a .json/.txt file"
  39. type="button"
  40. onClick={() => saveFile(JSON.stringify(data), `topic-message`)}
  41. >
  42. <span className="icon">
  43. <i className="fas fa-file-download" />
  44. </span>
  45. <span>Save</span>
  46. </button>
  47. </div>
  48. </div>
  49. );
  50. };
  51. export default JSONViewer;