|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { getStore, setStore } from 'utils/store'; |
| 4 | +import ReactGA from 'react-ga4'; |
| 5 | +import { |
| 6 | + Paper, |
| 7 | + Typography, |
| 8 | + Button, |
| 9 | + IconButton, |
| 10 | + Snackbar, |
| 11 | + Dialog, |
| 12 | + DialogTitle, |
| 13 | + DialogContent, |
| 14 | + DialogActions, |
| 15 | + TableContainer, |
| 16 | + TableRow, |
| 17 | + TableCell, |
| 18 | + Table, |
| 19 | + TableHead, |
| 20 | + TableBody, |
| 21 | + Link, |
| 22 | +} from '@mui/material'; |
| 23 | +import CloseIcon from '@mui/icons-material/Close'; |
| 24 | +import { useRouter } from 'next/router'; |
| 25 | + |
| 26 | +export const CookieNotice = (): JSX.Element => { |
| 27 | + const { t } = useTranslation(); |
| 28 | + const router = useRouter(); |
| 29 | + const [isCookieConsent, setIsCookieConsent] = useState<boolean | undefined>(undefined); |
| 30 | + const [gaTrackingCode, setGaTrackingCode] = useState<string | undefined>(undefined); |
| 31 | + const [openPolicyModal, setOpenPolicyModal] = useState(false); |
| 32 | + const [showNoticeSnackbar, setShowNoticeSnackbar] = useState(true); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + const fetchGaTrackingCode = async () => { |
| 36 | + try { |
| 37 | + const res = await fetch(`/ga.txt`); |
| 38 | + if (res.status !== 200) { |
| 39 | + setGaTrackingCode(undefined); |
| 40 | + return; |
| 41 | + } |
| 42 | + const gaCode = await res.text(); |
| 43 | + setGaTrackingCode(gaCode); |
| 44 | + } catch (e) { |
| 45 | + // network-related error |
| 46 | + setGaTrackingCode(undefined); |
| 47 | + } |
| 48 | + const store = getStore(); |
| 49 | + setIsCookieConsent(store.storedCookieConsent); |
| 50 | + }; |
| 51 | + fetchGaTrackingCode(); |
| 52 | + }, []); |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + if (!isCookieConsent || !gaTrackingCode) { |
| 56 | + ReactGA.reset(); |
| 57 | + return; |
| 58 | + } |
| 59 | + if (!ReactGA.isInitialized) { |
| 60 | + ReactGA.initialize(gaTrackingCode); |
| 61 | + console.log(`Google Analytics initialized - ${gaTrackingCode}`); |
| 62 | + } |
| 63 | + |
| 64 | + const handleRouteChange = (url: string) => { |
| 65 | + ReactGA.send({ hitType: 'pageview', page: url, title: window.document.title }); |
| 66 | + }; |
| 67 | + router.events.on('routeChangeComplete', handleRouteChange); |
| 68 | + |
| 69 | + return () => { |
| 70 | + router.events.off('routeChangeComplete', handleRouteChange); |
| 71 | + }; |
| 72 | + }, [isCookieConsent, gaTrackingCode, router.events]); |
| 73 | + |
| 74 | + const handleCookieAccept = () => { |
| 75 | + setIsCookieConsent(true); |
| 76 | + setShowNoticeSnackbar(false); |
| 77 | + setStore({ storedCookieConsent: true }); |
| 78 | + }; |
| 79 | + |
| 80 | + if (!isCookieConsent && gaTrackingCode) |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <Snackbar |
| 84 | + open={showNoticeSnackbar} |
| 85 | + autoHideDuration={null} |
| 86 | + anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }} |
| 87 | + > |
| 88 | + <Paper |
| 89 | + sx={{ |
| 90 | + p: 2, |
| 91 | + display: 'flex', |
| 92 | + alignItems: 'center', |
| 93 | + justifyContent: 'space-between', |
| 94 | + }} |
| 95 | + > |
| 96 | + <Typography variant="body2"> |
| 97 | + {t('cookie.INTRO_TEXT')} |
| 98 | + <Button color="primary" size="small" onClick={() => setOpenPolicyModal(true)}> |
| 99 | + {t('cookie.LEARN_MORE')} |
| 100 | + </Button> |
| 101 | + </Typography> |
| 102 | + <Button |
| 103 | + color="primary" |
| 104 | + size="small" |
| 105 | + variant="contained" |
| 106 | + onClick={() => { |
| 107 | + handleCookieAccept(); |
| 108 | + }} |
| 109 | + > |
| 110 | + {t('cookie.ACCEPT')} |
| 111 | + </Button> |
| 112 | + <IconButton |
| 113 | + size="small" |
| 114 | + color="inherit" |
| 115 | + sx={{ ml: 2 }} |
| 116 | + onClick={() => setShowNoticeSnackbar(false)} |
| 117 | + > |
| 118 | + <CloseIcon fontSize="small" /> |
| 119 | + </IconButton> |
| 120 | + </Paper> |
| 121 | + </Snackbar> |
| 122 | + <Dialog open={openPolicyModal} maxWidth="lg" onClose={() => setOpenPolicyModal(false)}> |
| 123 | + <DialogTitle>{t('cookiePolicy.TITLE')}</DialogTitle> |
| 124 | + <DialogContent> |
| 125 | + <> |
| 126 | + <Typography sx={{ marginBottom: '0.8rem' }} variant="body1"> |
| 127 | + {t('cookiePolicy.sections.cookies.WHAT_COOKIES')} |
| 128 | + </Typography> |
| 129 | + <Typography sx={{ marginBottom: '0.8rem' }} variant="body2"> |
| 130 | + {t('cookiePolicy.sections.cookies.WHAT_COOKIES_DESCRIPTION')} |
| 131 | + </Typography> |
| 132 | + <Typography sx={{ marginBottom: '0.8rem' }} variant="body1"> |
| 133 | + {t('cookiePolicy.sections.cookies.HOW_USE_TITLE')} |
| 134 | + </Typography> |
| 135 | + <TableContainer component={Paper} sx={{ marginBottom: '0.8rem' }}> |
| 136 | + <Table sx={{ minWidth: 650 }} aria-label="cookie-table"> |
| 137 | + <TableHead> |
| 138 | + <TableRow> |
| 139 | + <TableCell>{t('cookiePolicy.sections.cookies.SERVICE')}</TableCell> |
| 140 | + <TableCell>{t('cookiePolicy.sections.cookies.COOKIE_NAMES')}</TableCell> |
| 141 | + <TableCell>{t('cookiePolicy.sections.cookies.DURATION')}</TableCell> |
| 142 | + <TableCell>{t('cookiePolicy.sections.cookies.PURPOSE')}</TableCell> |
| 143 | + <TableCell>{t('cookiePolicy.sections.cookies.MORE_INFORMATION')}</TableCell> |
| 144 | + </TableRow> |
| 145 | + </TableHead> |
| 146 | + <TableBody> |
| 147 | + <TableRow key={1} sx={{ '&:last-child td, &:last-child th': { border: 0 } }}> |
| 148 | + <TableCell component="th" scope="row"> |
| 149 | + <b>{t('cookiePolicy.sections.cookies.googleAnalytics.SERVICE')}</b> |
| 150 | + </TableCell> |
| 151 | + <TableCell> |
| 152 | + <b>{t('cookiePolicy.sections.cookies.googleAnalytics.COOKIE_NAMES')}</b> |
| 153 | + </TableCell> |
| 154 | + <TableCell> |
| 155 | + <b>{t('cookiePolicy.sections.cookies.googleAnalytics.DURATION')}</b> |
| 156 | + </TableCell> |
| 157 | + <TableCell> |
| 158 | + <b>{t('cookiePolicy.sections.cookies.googleAnalytics.PURPOSE')}</b> |
| 159 | + </TableCell> |
| 160 | + <TableCell> |
| 161 | + <Link |
| 162 | + target="_blank" |
| 163 | + rel="noreferrer" |
| 164 | + href={t('cookiePolicy.sections.cookies.googleAnalytics.MORE_INFORMATION')} |
| 165 | + > |
| 166 | + <b>{t('cookiePolicy.sections.cookies.MORE_INFORMATION')}</b> |
| 167 | + </Link> |
| 168 | + </TableCell> |
| 169 | + </TableRow> |
| 170 | + </TableBody> |
| 171 | + </Table> |
| 172 | + </TableContainer> |
| 173 | + <Typography variant="body2"> |
| 174 | + {t('cookiePolicy.sections.cookies.CONCLUSION')} |
| 175 | + </Typography> |
| 176 | + </> |
| 177 | + </DialogContent> |
| 178 | + <DialogActions> |
| 179 | + <Button onClick={() => setOpenPolicyModal(false)} color="primary"></Button> |
| 180 | + </DialogActions> |
| 181 | + </Dialog> |
| 182 | + </> |
| 183 | + ); |
| 184 | + |
| 185 | + return <></>; |
| 186 | +}; |
0 commit comments