File size: 1,705 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { useEffect, useRef } from 'react';
import { useDispatch, useSelector } from 'calypso/state';
import { getCurrentUserDate } from 'calypso/state/current-user/selectors';
import { savePreference } from 'calypso/state/preferences/actions';
import { getPreference, hasReceivedRemotePreferences } from 'calypso/state/preferences/selectors';
import { isA8cTeamMember } from 'calypso/state/teams/selectors';
import ErrorBoundary from './error-boundary';
import CrowdsignalPollComponent from './main';

import './style.scss';

const READER_CROWDSIGNAL_POLL_VIEWED_PREFERENCE = 'reader-crowdsignal-poll-viewed';
const REGISTRATION_CUTOFF_DATE = new Date( '2025-01-01T00:00:00Z' );

const CrowdsignalPoll = () => {
	const dispatch = useDispatch();

	const remotePrefsLoaded = useSelector( hasReceivedRemotePreferences );
	const isAutomattician = useSelector( isA8cTeamMember );
	const userRegistrationDate = useSelector( getCurrentUserDate );
	const hasViewedPollPref = useSelector( ( state ): boolean | undefined | null =>
		getPreference( state, READER_CROWDSIGNAL_POLL_VIEWED_PREFERENCE )
	);
	const hasViewedPoll = useRef( hasViewedPollPref ); // Show the poll when the component first mounts, but not subsequently

	const shouldNotRender =
		( remotePrefsLoaded && hasViewedPoll.current ) ||
		new Date( userRegistrationDate ) < REGISTRATION_CUTOFF_DATE ||
		isAutomattician;

	useEffect( () => {
		if ( ! hasViewedPoll.current ) {
			dispatch( savePreference( READER_CROWDSIGNAL_POLL_VIEWED_PREFERENCE, true ) );
		}
	}, [ dispatch ] );

	if ( shouldNotRender ) {
		return null;
	}

	return (
		<ErrorBoundary>
			<CrowdsignalPollComponent />
		</ErrorBoundary>
	);
};

export default CrowdsignalPoll;