|
1 | 1 | import type { AppProps } from "next/app" |
2 | 2 | import Head from "next/head" |
3 | | -import { useEffect } from "react" |
| 3 | +import { useEffect, useRef, useState } from "react" |
| 4 | +import CookieConsent, { getCookieConsentValue } from "react-cookie-consent" |
4 | 5 | import "tailwindcss/tailwind.css" |
5 | 6 | import Footer from "../components/Footer" |
6 | 7 | import NavBar from "../components/NavBar" |
7 | 8 | import "../public/global.css" |
8 | 9 | import * as gtag from "../utils/gtag" |
| 10 | +import Script from "next/script" |
9 | 11 |
|
10 | 12 | export default function MyApp({ Component, pageProps, router }: AppProps) { |
| 13 | + const [consented, setConsented] = useState(null as boolean | undefined | null) |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + const value = getCookieConsentValue() |
| 17 | + if (value == "true") |
| 18 | + setConsented(true) |
| 19 | + else if (value == "true") |
| 20 | + setConsented(false) |
| 21 | + else |
| 22 | + setConsented(undefined) |
| 23 | + }, []) |
| 24 | + |
11 | 25 | useEffect(() => { |
| 26 | + if (!consented) |
| 27 | + return |
| 28 | + |
12 | 29 | const handleRouteChange = (url: string) => { |
13 | 30 | gtag.pageview(url) |
14 | 31 | } |
15 | 32 | router.events.on("routeChangeComplete", handleRouteChange) |
16 | 33 | return () => { |
17 | 34 | router.events.off("routeChangeComplete", handleRouteChange) |
18 | 35 | } |
19 | | - }, [router.events]) |
| 36 | + }, [router.events, consented]) |
20 | 37 |
|
21 | 38 | return <div className="bg-slate-50 dark:bg-slate-700 min-h-screen flex flex-col items-center justify-between text-slate-900 dark:text-slate-100"> |
22 | 39 | <Head> |
23 | 40 | <title>{router.pathname.substring(1).replace(/^\w/, w => w.toUpperCase())} | Hu Tao</title> |
24 | 41 | <link rel="icon" href="/favicon.ico" /> |
25 | 42 | <meta httpEquiv="content-language" content="en-us"></meta> |
| 43 | + |
| 44 | + {consented && <Script async |
| 45 | + src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`} |
| 46 | + />} |
26 | 47 | </Head> |
27 | 48 |
|
28 | | - <div className="w-full"> |
| 49 | + <div className="w-full flex-1"> |
| 50 | + <div className="absolute z-10"> |
| 51 | + <CookieConsent |
| 52 | + buttonText="Accept all cookies" |
| 53 | + declineButtonText="Refuse non-essential cookies" |
| 54 | + enableDeclineButton |
| 55 | + onAccept={() => { |
| 56 | + setConsented(true) |
| 57 | + }} |
| 58 | + onDecline={() => { |
| 59 | + setConsented(false) |
| 60 | + }} |
| 61 | + > |
| 62 | + This website uses cookies to enhance the user experience. |
| 63 | + <div style={{ fontSize: "12px" }}>Currently we only use these for analytic purposes, intended to figure out where to focus our efforts.</div> |
| 64 | + </CookieConsent> |
| 65 | + </div> |
| 66 | + |
29 | 67 | <NavBar location={router.asPath} /> |
30 | 68 | <div className="p-4 flex flex-col w-full flex-1 px-1 lg:px-20 items-center justify-center"> |
31 | 69 | <Component {...pageProps} location={router.asPath} /> |
32 | 70 | </div> |
33 | 71 |
|
| 72 | + <Footer location={router.asPath} marginBottom={consented || consented == null ? 3 : 80} /> |
34 | 73 | </div> |
35 | | - <Footer location={router.asPath} /> |
36 | 74 | </div> |
37 | 75 | } |
0 commit comments