index.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. @media only screen and (max-width: 768px) {
  24. font-size: 12px;
  25. }
  26. `;
  27. function inIframe() {
  28. try {
  29. return window.self !== window.top;
  30. } catch (e) {
  31. return true;
  32. }
  33. }
  34. const WidgetPage = () => {
  35. const { query, push } = useRouter();
  36. const [json, setJson] = React.useState("");
  37. React.useEffect(() => {
  38. if (!query.json) {
  39. setJson(JSON.stringify(defaultJson));
  40. } else {
  41. const jsonURI = decodeURIComponent(query.json as string);
  42. const isJsonValid = isValidJson(jsonURI);
  43. if (isJsonValid) {
  44. const jsonDecoded = decompress(JSON.parse(isJsonValid));
  45. const jsonString = JSON.stringify(jsonDecoded);
  46. setJson(jsonString);
  47. }
  48. }
  49. if (!inIframe()) push("/");
  50. }, [query?.json, push]);
  51. return (
  52. <>
  53. <Graph json={json} isWidget />
  54. <StyledAttribute
  55. href={`https://jsonvisio.com/editor?json=${query.json}`}
  56. target="_blank"
  57. rel="noreferrer"
  58. >
  59. jsonvisio.com
  60. </StyledAttribute>
  61. </>
  62. );
  63. };
  64. export default WidgetPage;