Introduce Contentpass layer and OneTrust CMP adapters#48
Conversation
b1ed3dd to
6ac2466
Compare
6ac2466 to
9f3eddf
Compare
| const [cmpFailed, setCmpFailed] = useState(false); | ||
| const [cmpAdapter, setCmpAdapter] = useState<CmpAdapter | null>(null); | ||
|
|
||
| useEffect(() => { |
There was a problem hiding this comment.
cleaner:
useEffect(() => {
const initCmp = async () => {
try {
await OTPublishersNativeSDK.startSDK(
ONETRUST_CDN_LOCATION,
ONETRUST_APP_ID,
ONETRUST_LANGUAGE_CODE,
{},
false
);
const onetrustCmpAdapter: CmpAdapter =
await createOnetrustCmpAdapter(OTPublishersNativeSDK);
setCmpAdapter(onetrustCmpAdapter);
setCmpReady(true);
} catch (error: any) {
console.error('Failed to initialize CMP', error);
setCmpFailed(true);
}
};
initCmp();
}, []);
| export default function App() { | ||
| const [cmpReady, setCmpReady] = useState(false); | ||
| const [cmpFailed, setCmpFailed] = useState(false); | ||
| const [cmpAdapter, setCmpAdapter] = useState<CmpAdapter | null>(null); |
There was a problem hiding this comment.
maybe cleaner to have just type CmpStatus = 'loading' | 'ready' | 'error';
and
const [status, setStatus] = useState('loading'); ad cmpFailed and cmpAdapter can be removed?
and then:
if (status === 'loading') {
return (
<View style={styles.container}>
<Text>Loading CMP...</Text>
</View>
);
}
if (status === 'error' || !cmpAdapter) {
return (
<View style={styles.container}>
<Text>Failed to load CMP</Text>
</View>
);
}
| return ( | ||
| <ContentpassSdkProvider contentpassConfig={contentpassConfig}> | ||
| <ContentpassConsentGate | ||
| cmpAdapter={cmpAdapter!} |
There was a problem hiding this comment.
no need for explanation mark with if above
| hideAppWhenVisible={false} | ||
| > | ||
| <View style={styles.container}> | ||
| <Content cmpAdapter={cmpAdapter!} /> |
| <ContentpassSdkProvider contentpassConfig={contentpassConfig}> | ||
| <ContentpassConsentGate | ||
| cmpAdapter={cmpAdapter!} | ||
| contentpassConfig={contentpassConfig} |
There was a problem hiding this comment.
nit: ContentpassSdkProvider could expose contentpassConfig so no need to pass it to ContentpassConsentGate
| const [isVisible, setIsVisible] = useState(false); | ||
| const [cpAuthState, setCpAuthState] = useState<ContentpassState | null>(null); | ||
| const [isShowingSecondLayer, setIsShowingSecondLayer] = useState(false); | ||
| const [isShowingContentpass, setIsShowingContentpass] = useState(false); |
There was a problem hiding this comment.
do we need to have isShowingSecondLayer and isShowingContentpass as separated states? Is it possible for example to have them true in the same time? Maybe we could have one state with enum for it?
|
|
||
| // Policy for setting the visibility of the consent layer | ||
| useEffect(() => { | ||
| const invalidStates = [ |
There was a problem hiding this comment.
invalidStates doesn't have to be in effect
| isShowingSecondLayer, | ||
| isVisible, | ||
| onVisibilityChange, | ||
| ]); |
There was a problem hiding this comment.
this whole useEffect is complex and not needed. We can remove:
const [isVisible, setIsVisible] = useState(false);
calculate isVisible in memo:
const isVisible = useMemo(() => {
const invalidStates = [
ContentpassStateType.INITIALISING,
ContentpassStateType.ERROR,
];
if (!cmpReady || !cpAuthState || invalidStates.includes(cpAuthState.state)) {
return false;
}
if (isShowingSecondLayer || isShowingContentpass) {
return false;
}
const isFine =
cpAuthState.state === ContentpassStateType.AUTHENTICATED || hasFullConsent;
return !isFine;
}, [
cmpReady,
cpAuthState,
hasFullConsent,
isShowingSecondLayer,
isShowingContentpass,
]);
have prev visibility on ref : const prevVisibleRef = React.useRef<boolean | null>(null);
and then simplify effect to:
useEffect(() => {
if (prevVisibleRef.current === null) {
prevVisibleRef.current = isVisible;
return;
}
if (prevVisibleRef.current !== isVisible) {
prevVisibleRef.current = isVisible;
onVisibilityChange?.(isVisible);
}
}, [isVisible, onVisibilityChange]);
| } | ||
|
|
||
| cmpAdapter?.onConsentStatusChange?.((v: boolean) => setHasFullConsent(v)); | ||
| cmpAdapter.getRequiredPurposes().then((v: string[]) => setPurposesList(v)); |
There was a problem hiding this comment.
shoudn;t we recalculate it after consent changes?
| return <>{children}</>; | ||
| } | ||
|
|
||
| function renderContentpassLayer() { |
There was a problem hiding this comment.
renderContentpassLayer() is a function declared on every render; Would be better to extract it to separated compnent
No description provided.