1+ import { useEffect , useState } from 'preact/hooks' ;
12import { purposesOnly } from '../utils/config' ;
2- import { useConfig , useManager , useTheme } from '../utils/hooks' ;
3+ import { useConfig , useManager , useTheme , useTranslations } from '../utils/hooks' ;
4+ import { template } from '../utils/template' ;
5+
6+ type ContextualNoticeState = 'enabled' | 'disabling' | 'disabled' ;
37
48interface ContextualNoticeContainerProps {
59 data : Record < string , string > ;
10+ isEnabled : boolean ;
611}
712
8- const ContextualNoticeContainer = ( { data} : ContextualNoticeContainerProps ) => {
13+ const ContextualNoticeContainer = ( {
14+ data,
15+ isEnabled
16+ } : ContextualNoticeContainerProps ) => {
917 const config = useConfig ( ) ;
1018 const manager = useManager ( ) ;
19+ const t = useTranslations ( ) ;
1120 const { ContextualNotice} = useTheme ( ) ;
21+ const [ state , setState ] = useState < ContextualNoticeState > (
22+ isEnabled ? 'enabled' : 'disabled'
23+ ) ;
24+
25+ useEffect ( ( ) => {
26+ if ( isEnabled && state !== 'enabled' ) {
27+ setState ( 'enabled' ) ;
28+ }
1229
13- if ( ! data ?. purpose ) {
30+ if ( ! isEnabled && state === 'enabled' ) {
31+ setState ( 'disabling' ) ;
32+ }
33+ } , [ isEnabled ] ) ;
34+
35+ if ( state === 'disabled' ) {
1436 return null ;
1537 }
1638
@@ -24,15 +46,30 @@ const ContextualNoticeContainer = ({data}: ContextualNoticeContainerProps) => {
2446
2547 const handleAccept = ( ) => {
2648 manager . setConsent ( purpose . id , true ) ;
49+ setState ( 'disabling' ) ;
50+ } ;
51+
52+ const handleFocusOut = ( ) => {
53+ setState ( 'disabled' ) ;
2754 } ;
2855
2956 return (
3057 < div className = "orejime-Env" >
31- < ContextualNotice
32- purpose = { purpose }
33- data = { data }
34- onAccept = { handleAccept }
35- > </ ContextualNotice >
58+ { state === 'enabled' ? (
59+ < ContextualNotice
60+ purpose = { purpose }
61+ data = { data }
62+ onAccept = { handleAccept }
63+ > </ ContextualNotice >
64+ ) : state === 'disabling' ? (
65+ < div
66+ tabIndex = { - 1 }
67+ title = { template ( t . contextual . accepted , {
68+ purpose : purpose . title
69+ } ) . join ( '' ) }
70+ onFocusOut = { handleFocusOut }
71+ />
72+ ) : null }
3673 </ div >
3774 ) ;
3875} ;
0 commit comments