index.tsx 1.8 KB

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