22
33import { X } from 'lucide-react' ;
44import { useTranslations } from 'next-intl' ;
5- import { useEffect , useState } from 'react' ;
5+ import { useEffect , useState } from 'react' ;
66
77import { Button } from '@/components/ui/button' ;
88import { Link } from '@/i18n/routing' ;
99
1010export function CookieBanner ( ) {
1111 const t = useTranslations ( 'CookieBanner' ) ;
12- const [ isVisible , setIsVisible ] = useState ( false ) ;
13- const [ isMounted , setIsMounted ] = useState ( false ) ;
12+ const [ isVisible , setIsVisible ] = useState < boolean | null > ( null ) ;
1413
1514 useEffect ( ( ) => {
16- setIsMounted ( true ) ;
1715 const consent = localStorage . getItem ( 'cookie-consent' ) ;
1816 if ( ! consent ) {
1917 const timer = setTimeout ( ( ) => setIsVisible ( true ) , 500 ) ;
2018 return ( ) => clearTimeout ( timer ) ;
19+ } else {
20+ const timer = setTimeout ( ( ) => setIsVisible ( false ) , 0 ) ;
21+ return ( ) => clearTimeout ( timer ) ;
2122 }
2223 } , [ ] ) ;
2324
24- const handleAccept = ( ) => {
25- try {
26- localStorage . setItem ( 'cookie-consent' , 'accepted' ) ;
27- } catch ( error ) {
28- console . error ( 'Failed to save cookie consent:' , error ) ;
29- }
30- setIsVisible ( false ) ;
31- } ;
25+ if ( isVisible === null || ! isVisible ) return null ;
3226
33- const handleDecline = ( ) => {
27+ const handleAction = ( type : 'accepted' | 'declined' ) => {
3428 try {
35- localStorage . setItem ( 'cookie-consent' , 'declined' ) ;
29+ localStorage . setItem ( 'cookie-consent' , type ) ;
3630 } catch ( error ) {
3731 console . error ( 'Failed to save cookie consent:' , error ) ;
3832 }
3933 setIsVisible ( false ) ;
4034 } ;
4135
42- if ( ! isMounted || ! isVisible ) return null ;
43-
4436 return (
4537 < div className = "animate-in slide-in-from-bottom-full fade-in fixed right-0 bottom-0 left-0 z-[100] p-4 duration-700 md:p-6" >
4638 < div className = "mx-auto max-w-4xl rounded-2xl border border-gray-200 bg-white/90 p-5 shadow-2xl backdrop-blur-md md:flex md:items-center md:justify-between md:gap-6 dark:border-gray-800 dark:bg-gray-900/90" >
@@ -64,23 +56,23 @@ export function CookieBanner() {
6456 < Button
6557 variant = "outline"
6658 size = "sm"
67- onClick = { handleDecline }
59+ onClick = { ( ) => handleAction ( 'declined' ) }
6860 className = "w-full sm:w-auto"
6961 >
7062 { t ( 'decline' ) }
7163 </ Button >
7264 < Button
7365 variant = "primary"
7466 size = "sm"
75- onClick = { handleAccept }
67+ onClick = { ( ) => handleAction ( 'accepted' ) }
7668 className = "w-full shadow-lg shadow-blue-500/20 sm:w-auto"
7769 >
7870 { t ( 'accept' ) }
7971 </ Button >
8072 </ div >
8173
8274 < button
83- onClick = { handleDecline }
75+ onClick = { ( ) => handleAction ( 'declined' ) }
8476 className = "absolute top-2 right-2 rounded-full p-1 text-gray-400 transition-colors hover:bg-gray-100 hover:text-gray-600 md:hidden dark:hover:bg-gray-800 dark:hover:text-gray-300"
8577 aria-label = { t ( 'decline' ) }
8678 >
0 commit comments