|
| 1 | +import { CONSENT_KEY } from '@/lib/const/analytics' |
| 2 | +import { Box, Button, Flex, IconButton, Text } from '@chakra-ui/react' |
| 3 | +import posthog from 'posthog-js' |
| 4 | +import type { FC } from 'react' |
| 5 | +import { useEffect, useState } from 'react' |
| 6 | +import { useTranslation } from 'react-i18next' |
| 7 | +import { FaTimes as CloseIcon } from 'react-icons/fa' |
| 8 | + |
| 9 | +const AnalyticsConsent: FC = () => { |
| 10 | + const [showConsent, setShowConsent] = useState<boolean>(false) |
| 11 | + const { t } = useTranslation() |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + const storedConsent = localStorage.getItem(CONSENT_KEY) |
| 15 | + if (storedConsent === 'true') { |
| 16 | + setShowConsent(false) |
| 17 | + initializeAnalytics() |
| 18 | + } else if (storedConsent === 'false') { |
| 19 | + setShowConsent(false) |
| 20 | + } else { |
| 21 | + setShowConsent(true) |
| 22 | + } |
| 23 | + }, []) |
| 24 | + |
| 25 | + const initializeAnalytics = () => { |
| 26 | + if ( |
| 27 | + import.meta.env.PROD && |
| 28 | + import.meta.env.VITE_POSTHOG_API_KEY && |
| 29 | + import.meta.env.VITE_POSTHOG_HOST |
| 30 | + ) { |
| 31 | + posthog.init(import.meta.env.VITE_POSTHOG_API_KEY, { |
| 32 | + api_host: import.meta.env.VITE_POSTHOG_HOST, |
| 33 | + }) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + const handleAccept = () => { |
| 38 | + localStorage.setItem(CONSENT_KEY, 'true') |
| 39 | + setShowConsent(false) |
| 40 | + initializeAnalytics() |
| 41 | + } |
| 42 | + |
| 43 | + const handleDecline = () => { |
| 44 | + localStorage.setItem(CONSENT_KEY, 'false') |
| 45 | + setShowConsent(false) |
| 46 | + } |
| 47 | + |
| 48 | + const handleClose = () => { |
| 49 | + setShowConsent(false) |
| 50 | + } |
| 51 | + |
| 52 | + if (!showConsent) { |
| 53 | + return null |
| 54 | + } |
| 55 | + |
| 56 | + return ( |
| 57 | + <Box |
| 58 | + position="fixed" |
| 59 | + top={0} |
| 60 | + left={0} |
| 61 | + width="100dvw" |
| 62 | + height="100dvh" |
| 63 | + bg="blackAlpha.400" |
| 64 | + zIndex="overlay" |
| 65 | + display="flex" |
| 66 | + flexDirection="column" |
| 67 | + justifyContent="flex-end" |
| 68 | + alignItems="flex-end" |
| 69 | + pointerEvents="auto" |
| 70 | + > |
| 71 | + <Box |
| 72 | + maxW="sm" |
| 73 | + width="100%" |
| 74 | + bg="bg.100" |
| 75 | + color="fg.primary" |
| 76 | + p={6} |
| 77 | + borderRadius="lg" |
| 78 | + borderWidth="1px" |
| 79 | + borderColor="bg.100" |
| 80 | + display="flex" |
| 81 | + flexDirection="column" |
| 82 | + gap={3} |
| 83 | + position="relative" |
| 84 | + pointerEvents="auto" |
| 85 | + mb={{ base: 4, md: 8 }} |
| 86 | + mr={{ base: 4, md: 8 }} |
| 87 | + > |
| 88 | + <IconButton |
| 89 | + aria-label={t('general.actions.close')} |
| 90 | + size="sm" |
| 91 | + variant="ghost" |
| 92 | + color="fg.muted" |
| 93 | + position="absolute" |
| 94 | + top={2} |
| 95 | + right={2} |
| 96 | + onClick={handleClose} |
| 97 | + _hover={{ bg: 'bg.50' }} |
| 98 | + > |
| 99 | + <CloseIcon /> |
| 100 | + </IconButton> |
| 101 | + <Text fontWeight="bold" fontSize="lg" mb={1} display="flex" alignItems="center"> |
| 102 | + {t('components.analyticsConsent.title')}{' '} |
| 103 | + <Box as="span" ml={1}> |
| 104 | + 🍪 |
| 105 | + </Box> |
| 106 | + </Text> |
| 107 | + <Text fontSize="sm" color="fg.muted" mb={2}> |
| 108 | + {t('components.analyticsConsent.description')} |
| 109 | + </Text> |
| 110 | + <Flex gap={2} direction={{ base: 'column', md: 'row' }} justify="flex-end"> |
| 111 | + <Button |
| 112 | + size="sm" |
| 113 | + variant="outline" |
| 114 | + color="fg.primary" |
| 115 | + borderColor="bg.200" |
| 116 | + borderRadius="md" |
| 117 | + onClick={handleDecline} |
| 118 | + _hover={{ bg: 'bg.50', borderColor: 'bg.200' }} |
| 119 | + > |
| 120 | + {t('components.analyticsConsent.decline')} |
| 121 | + </Button> |
| 122 | + <Button |
| 123 | + size="sm" |
| 124 | + minW={28} |
| 125 | + bg="accent.purple.light" |
| 126 | + color="gray.800" |
| 127 | + borderRadius="md" |
| 128 | + onClick={handleAccept} |
| 129 | + _hover={{ bg: 'accent.purple.dark' }} |
| 130 | + fontWeight="semibold" |
| 131 | + > |
| 132 | + {t('components.analyticsConsent.accept')} |
| 133 | + </Button> |
| 134 | + </Flex> |
| 135 | + </Box> |
| 136 | + </Box> |
| 137 | + ) |
| 138 | +} |
| 139 | + |
| 140 | +export default AnalyticsConsent |
0 commit comments