index.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { decompress } from "compress-json";
  2. import dynamic from "next/dynamic";
  3. import { useRouter } from "next/router";
  4. import React from "react";
  5. import { defaultJson } from "src/constants/data";
  6. import { isValidJson } from "src/utils/isValidJson";
  7. import styled from "styled-components";
  8. const Graph = dynamic<any>(
  9. () => import("src/components/Graph").then((c) => c.Graph),
  10. { ssr: false }
  11. );
  12. const StyledAttribute = styled.a`
  13. position: fixed;
  14. bottom: 0;
  15. right: 0;
  16. color: ${({ theme }) => theme.INTERACTIVE_NORMAL};
  17. background: ${({ theme }) => theme.SILVER_DARK};
  18. padding: 4px 8px;
  19. font-size: 14px;
  20. font-weight: 500;
  21. border-radius: 3px 0 0 0;
  22. opacity: 0.8;
  23. `;
  24. function inIframe() {
  25. try {
  26. return window.self !== window.top;
  27. } catch (e) {
  28. return true;
  29. }
  30. }
  31. const WidgetPage = () => {
  32. const { query, push } = useRouter();
  33. const [json, setJson] = React.useState("");
  34. React.useEffect(() => {
  35. if (!query.json) {
  36. setJson(JSON.stringify(defaultJson));
  37. } else {
  38. const jsonURI = decodeURIComponent(query.json as string);
  39. const isJsonValid = isValidJson(jsonURI);
  40. if (isJsonValid) {
  41. const jsonDecoded = decompress(JSON.parse(isJsonValid));
  42. const jsonString = JSON.stringify(jsonDecoded);
  43. setJson(jsonString);
  44. }
  45. }
  46. if (!inIframe()) push("/");
  47. }, [query?.json, push]);
  48. return (
  49. <div>
  50. <Graph json={json} isWidget />
  51. <StyledAttribute
  52. href={`https://jsonvisio.com/editor?json=${query.json}`}
  53. target="_blank"
  54. rel="noreferrer"
  55. >
  56. jsonvisio.com
  57. </StyledAttribute>
  58. </div>
  59. );
  60. };
  61. export default WidgetPage;