File size: 3,506 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { get, includes, reduce } from 'lodash';

export const APP_BANNER_DISMISS_TIMES_PREFERENCE = 'appBannerDismissTimes';
export const GUTENBERG = 'gutenberg-editor';
export const NOTES = 'notifications';
export const READER = 'reader';
export const STATS = 'stats';
export const HOME = 'home';
export const ALLOWED_SECTIONS = [ GUTENBERG, NOTES, READER, STATS, HOME ];
export const ONE_WEEK_IN_MILLISECONDS = 604800000;
export const ONE_MONTH_IN_MILLISECONDS = 2419200000; // 28 days

// Experiment Configuration
export const TWO_WEEKS_IN_MILLISECONDS = 1209600000;
export const ONE_DAY_IN_MILLISECONDS = 86400000;

const emptyBanner = {
	title: '',
	copy: '',
	icon: '',
};

export function getAppBannerData( translate, sectionName, isRTL ) {
	switch ( sectionName ) {
		case GUTENBERG:
			return {
				title: translate( 'Rich mobile publishing' ),
				copy: translate(
					'A streamlined editor with faster, simpler image uploading? Check and mate.'
				),
				icon: `/calypso/animations/app-promo/wp-to-jp${ isRTL ? '-rtl' : '' }.json`,
			};
		case NOTES:
			return {
				title: translate( 'Watch engagement happening' ),
				copy: translate(
					'Is your new post a hit? With push notifications, see reactions as they roll in.'
				),
				icon: `/calypso/animations/app-promo/jp-notifications${ isRTL ? '-rtl' : '' }.json`,
			};
		case READER:
			return {
				title: translate( 'Read posts, even offline' ),
				copy: translate( 'Catch up with new posts on the go or save them to read offline.' ),
				icon: `/calypso/animations/app-promo/jp-reader${ isRTL ? '-rtl' : '' }.json`,
			};
		case STATS:
			return {
				title: translate( 'Stats at your fingertips' ),
				copy: translate( 'See your real-time stats anytime, anywhere.' ),
				icon: `/calypso/animations/app-promo/jp-stats${ isRTL ? '-rtl' : '' }.json`,
			};
		case HOME:
			return {
				title: translate( 'The Jetpack app makes WordPress better' ),
				copy: translate( 'Everything you need to write, publish, and manage a world-class site.' ),
				icon: `/calypso/animations/app-promo/wp-to-jp${ isRTL ? '-rtl' : '' }.json`,
			};
		default:
			return emptyBanner;
	}
}

export function getCurrentSection( currentSection, isNotesOpen ) {
	if ( isNotesOpen ) {
		return NOTES;
	}

	if ( includes( ALLOWED_SECTIONS, currentSection ) ) {
		return currentSection;
	}

	return null;
}

function getDismissTimes() {
	const currentTime = Date.now();
	const longerTime = TWO_WEEKS_IN_MILLISECONDS;
	const shorterTime = ONE_DAY_IN_MILLISECONDS;

	return {
		longerDuration: currentTime + longerTime,
		shorterDuration: currentTime + shorterTime,
	};
}

export function getNewDismissTimes( dismissedSection, currentDismissTimes ) {
	const dismissTimes = getDismissTimes();

	return reduce(
		ALLOWED_SECTIONS,
		( result, section ) => {
			if ( section === dismissedSection ) {
				// Dismiss selected section for a longer period.
				result[ section ] = dismissTimes.longerDuration;
			} else {
				// Dismiss all other sections for a shorter period, but make sure that we preserve previous dismiss time
				// if it was longer than that (e.g. if other section was also dismissed for a month).
				result[ section ] =
					get( currentDismissTimes, section, -Infinity ) > dismissTimes.shorterDuration
						? get( currentDismissTimes, section )
						: dismissTimes.shorterDuration;
			}

			return result;
		},
		{}
	);
}

export const isDismissed = ( dismissedUntil, section ) =>
	get( dismissedUntil, section, -Infinity ) > Date.now();