_app.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React from "react";
  2. import type { AppProps } from "next/app";
  3. import { useRouter } from "next/router";
  4. import { init } from "@sentry/nextjs";
  5. import { decompress } from "compress-json";
  6. import { Toaster } from "react-hot-toast";
  7. import { GoogleAnalytics } from "src/components/GoogleAnalytics";
  8. import { SupportButton } from "src/components/SupportButton";
  9. import GlobalStyle from "src/constants/globalStyle";
  10. import { darkTheme, lightTheme } from "src/constants/theme";
  11. import useConfig from "src/hooks/store/useConfig";
  12. import useStored from "src/hooks/store/useStored";
  13. import { isValidJson } from "src/utils/isValidJson";
  14. import { ThemeProvider } from "styled-components";
  15. if (process.env.NODE_ENV !== "development") {
  16. init({
  17. dsn: "https://[email protected]/6495191",
  18. tracesSampleRate: 0.5,
  19. });
  20. }
  21. function JsonCrack({ Component, pageProps }: AppProps) {
  22. const { query, pathname } = useRouter();
  23. const lightmode = useStored(state => state.lightmode);
  24. const setJson = useConfig(state => state.setJson);
  25. const [isRendered, setRendered] = React.useState(false);
  26. React.useEffect(() => {
  27. try {
  28. if (pathname !== "editor") return;
  29. const isJsonValid =
  30. typeof query.json === "string" &&
  31. isValidJson(decodeURIComponent(query.json));
  32. if (isJsonValid) {
  33. const jsonDecoded = decompress(JSON.parse(isJsonValid));
  34. const jsonString = JSON.stringify(jsonDecoded);
  35. setJson(jsonString);
  36. }
  37. } catch (error) {
  38. console.error(error);
  39. }
  40. }, [pathname, query.json, setJson]);
  41. React.useEffect(() => {
  42. setRendered(true);
  43. }, []);
  44. if (isRendered)
  45. return (
  46. <>
  47. <GoogleAnalytics />
  48. <ThemeProvider theme={lightmode ? lightTheme : darkTheme}>
  49. <GlobalStyle />
  50. <Component {...pageProps} />
  51. <Toaster
  52. position="bottom-right"
  53. toastOptions={{
  54. style: {
  55. background: "#4D4D4D",
  56. color: "#B9BBBE",
  57. },
  58. }}
  59. />
  60. <SupportButton />
  61. </ThemeProvider>
  62. </>
  63. );
  64. }
  65. export default JsonCrack;