File size: 12,897 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { LoadingPlaceholder } from '@automattic/components';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Modal, Button, __experimentalHStack as HStack } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import clsx from 'clsx';
import { getLocaleSlug } from 'i18n-calypso';
import React, { useMemo, useState, ComponentType, useEffect, useCallback } from 'react';
import { useSelector } from 'react-redux';
import { AnyAction } from 'redux';
import ConnectedReaderSubscriptionListItem from 'calypso/blocks/reader-subscription-list-item/connected';
import wpcom from 'calypso/lib/wp';
import { trackScrollPage } from 'calypso/reader/controller-helper';
import { READER_ONBOARDING_TRACKS_EVENT_PREFIX } from 'calypso/reader/onboarding/constants';
import { curatedBlogs } from 'calypso/reader/onboarding/curated-blogs';
import Stream from 'calypso/reader/stream';
import { useDispatch } from 'calypso/state';
import { isCurrentUserEmailVerified } from 'calypso/state/current-user/selectors';
import { requestFollows } from 'calypso/state/reader/follows/actions';
import { getReaderFollows } from 'calypso/state/reader/follows/selectors';
import {
	requestPage,
	clearStream,
	requestPaginatedStream,
} from 'calypso/state/reader/streams/actions';
import { getReaderFollowedTags } from 'calypso/state/reader/tags/selectors';
import SubscribeVerificationNudge from './verificationNudge';

import './style.scss';

interface SubscribeModalProps {
	isOpen: boolean;
	onClose: () => void;
}

interface CardData {
	feed_ID: number;
	site_ID: number;
	site_URL: string;
	site_name: string;
}

interface Card {
	type: string;
	data: CardData[];
}

interface StreamProps {
	streamKey: string;
	className?: string;
	followSource?: string;
	useCompactCards?: boolean;
	trackScrollPage?: (
		path: string,
		title: string,
		category: string,
		readerView: string,
		pageNum: number
	) => void;
}

const TypedStream: ComponentType< StreamProps > = Stream as ComponentType< StreamProps >;

const SubscribeModal: React.FC< SubscribeModalProps > = ( { isOpen, onClose } ) => {
	const followedTags = useSelector( getReaderFollowedTags );

	const followedTagSlugs = useMemo( () => {
		return ( followedTags || [] ).map( ( tag ) => tag.slug );
	}, [ followedTags ] );

	const promptVerification = ! useSelector( isCurrentUserEmailVerified );

	const [ currentPage, setCurrentPage ] = useState( 0 );
	const [ selectedSite, setSelectedSite ] = useState< CardData | null >( null );
	const dispatch = useDispatch();
	const currentLocale = getLocaleSlug();
	const SITES_PER_PAGE = 6;
	const queryClient = useQueryClient();

	const { data: apiRecommendedSites = [], isLoading } = useQuery( {
		queryKey: [ 'reader-onboarding-recommended-sites', followedTagSlugs, currentLocale ],
		queryFn: () =>
			wpcom.req.get(
				{
					path: '/read/tags/cards',
					apiNamespace: 'wpcom/v2',
				},
				{
					tags: followedTagSlugs,
					site_recs_per_card: 18,
					tag_recs_per_card: 0,
				}
			),
		refetchOnMount: 'always',
		refetchOnWindowFocus: true,
		select: ( data: { cards: Card[] } ) => {
			const recommendedBlogsCard = data.cards.find(
				( card: Card ) => card.type === 'recommended_blogs'
			);

			return recommendedBlogsCard
				? recommendedBlogsCard.data.map( ( site: CardData & { URL?: string } ) => ( {
						...site,
						site_URL: site.URL || site.site_URL,
				  } ) )
				: [];
		},
		staleTime: Infinity,
		enabled: followedTagSlugs.length > 0,
	} );

	const combinedRecommendations = useMemo( () => {
		if ( isLoading ) {
			return [];
		}
		const isEnglish = currentLocale?.startsWith( 'en' );

		// Get list of curated recommendations only if the language is English.
		const curatedRecommendations = isEnglish
			? followedTagSlugs
					.flatMap( ( tag ) => curatedBlogs[ tag ] || [] )
					.map( ( blog ) => ( { ...blog, weight: 1, isCurated: true } ) )
			: [];

		// Get list of API recommended blogs.
		const apiRecommendations = apiRecommendedSites.map( ( site ) => ( {
			...site,
			weight: 1,
			isCurated: false,
		} ) );

		// Combine all recommendations.
		const allRecommendations = [ ...curatedRecommendations, ...apiRecommendations ];

		// Increase "weight" for blogs that match multiple tags.
		const blogWeights = allRecommendations.reduce< Record< number, number > >( ( acc, blog ) => {
			acc[ blog.feed_ID ] = ( acc[ blog.feed_ID ] || 0 ) + blog.weight;
			return acc;
		}, {} );

		// Remove duplicates, prioritizing curated blogs.
		const uniqueRecommendations = Object.values(
			allRecommendations.reduce<
				Record< number, CardData & { weight: number; isCurated: boolean } >
			>( ( acc, blog ) => {
				if ( ! acc[ blog.feed_ID ] || blog.isCurated ) {
					acc[ blog.feed_ID ] = { ...blog, weight: blogWeights[ blog.feed_ID ] };
				}
				return acc;
			}, {} )
		);

		// Sort recommendations: curated first, then by "weight" (i.e. how many tags it matches).
		const sortedRecommendations = uniqueRecommendations.sort( ( a, b ) => {
			if ( a.isCurated !== b.isCurated ) {
				return a.isCurated ? -1 : 1;
			}
			return b.weight - a.weight;
		} );

		// Limit to 18 recommendations.
		return sortedRecommendations.slice( 0, 18 );
	}, [ followedTagSlugs, apiRecommendedSites, isLoading, currentLocale ] );

	const maxPages = Math.ceil( combinedRecommendations.length / SITES_PER_PAGE ) - 1; // -1 because pages are 0-based.

	const displayedRecommendations = useMemo( () => {
		// Show all items up to the current page.
		return combinedRecommendations.slice( 0, ( currentPage + 1 ) * SITES_PER_PAGE );
	}, [ combinedRecommendations, currentPage ] );

	const handleLoadMore = useCallback( () => {
		recordTracksEvent( `${ READER_ONBOARDING_TRACKS_EVENT_PREFIX }clicked_load_more`, {
			page: currentPage,
		} );
		// Only increment the page if we haven't reached the end.
		setCurrentPage( ( prevPage ) => ( prevPage < maxPages ? prevPage + 1 : prevPage ) );
	}, [ maxPages, currentPage ] );

	// Prefetch the first blog's feed. Only fetch one because it happens every time a tag changes.
	useEffect( () => {
		if ( combinedRecommendations.length > 0 ) {
			dispatch(
				// eslint-disable-next-line @typescript-eslint/no-explicit-any
				requestPage( { streamKey: `feed:${ combinedRecommendations[ 0 ].feed_ID }` } as any )
			);
		}
	}, [ combinedRecommendations, dispatch ] );

	// Prefetch all feed streams when the modal is opened.
	useEffect( () => {
		if ( isOpen && combinedRecommendations.length > 0 ) {
			combinedRecommendations.forEach( ( site ) => {
				// eslint-disable-next-line @typescript-eslint/no-explicit-any
				dispatch( requestPage( { streamKey: `feed:${ site.feed_ID }` } as any ) );
			} );
		}
	}, [ isOpen, combinedRecommendations, dispatch ] );

	// Reset the page and selected site when the followed tags change.
	useEffect( () => {
		setCurrentPage( 0 );
		setSelectedSite( null );
	}, [ followedTagSlugs ] );

	// Select the first site by default when recommendations are loaded.
	useEffect( () => {
		if ( displayedRecommendations.length > 0 && ! selectedSite ) {
			setSelectedSite( displayedRecommendations[ 0 ] );
		}
	}, [ displayedRecommendations, selectedSite ] );

	const handleItemClick = useCallback(
		( site: CardData ) => {
			// Only reset scroll position if selecting a different site.
			if ( site.feed_ID !== selectedSite?.feed_ID ) {
				const previewContainer = document.querySelector(
					'.subscribe-modal__preview-stream-container'
				);
				if ( previewContainer ) {
					previewContainer.scrollTop = 0;
				}
			}
			setSelectedSite( site );
		},
		[ selectedSite ]
	);

	const follows = useSelector( getReaderFollows );

	const handleFollowToggle = useCallback(
		async ( site: CardData, following: boolean ) => {
			const isFollowingSite = ( site: CardData ) =>
				follows.some(
					( follow ) => follow.feed_ID === site.feed_ID || follow.blog_ID === site.site_ID
				);

			// Exit early if the follow state already matches what we want.
			if ( following === isFollowingSite( site ) ) {
				return;
			}

			// Maximum number of retries
			const MAX_RETRIES = 3;

			for ( let attempt = 0; attempt < MAX_RETRIES; attempt++ ) {
				// Update the subscriptions list behind the modal.
				await dispatch( requestFollows() );

				// Delay the next attempt.
				await new Promise( ( resolve ) => setTimeout( resolve, 300 ) );

				if ( following === isFollowingSite( site ) ) {
					return;
				}
			}
		},
		[ follows, dispatch ]
	);

	const formatUrl = ( url: string ): string => {
		return url
			.replace( /^(https?:\/\/)?(www\.)?/, '' ) // Remove protocol and www
			.replace( /\/$/, '' ); // Remove trailing slash
	};

	const handleClose = useCallback( () => {
		dispatch( clearStream( { streamKey: 'following' } ) );
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		dispatch( requestPage( { streamKey: 'following' } as any ) );

		onClose();
	}, [ dispatch, onClose ] );

	const handleContinue = useCallback( () => {
		// Invalidate the subscriptions count query to refresh the Recent stream.
		queryClient.invalidateQueries( {
			queryKey: [ 'read', 'subscriptions-count' ],
		} );

		// Refresh the Recent stream data.
		dispatch(
			requestPaginatedStream( {
				streamKey: 'recent',
				page: 1,
				perPage: 10,
			} ) as AnyAction
		);

		handleClose();
	}, [ dispatch, handleClose, queryClient ] );

	return (
		isOpen && (
			<Modal
				onRequestClose={ handleClose }
				size="fill"
				className={ clsx( 'subscribe-modal', {
					'is-disabled': promptVerification,
				} ) }
			>
				<div className="subscribe-modal__container">
					{ promptVerification && <SubscribeVerificationNudge /> }
					<div className="subscribe-modal__content">
						<div className="subscribe-modal__site-list-column">
							<h2 className="subscribe-modal__title">
								{ __( "Discover sites that you'll love" ) }
							</h2>
							<p className="subscribe-modal__description">
								{ __(
									'Preview sites by clicking below, then subscribe to any site that inspires you.'
								) }
							</p>
							{ isLoading && <LoadingPlaceholder /> }
							{ ! isLoading && combinedRecommendations.length === 0 && (
								<p>{ __( 'No recommendations available at the moment.' ) }</p>
							) }
							{ ! isLoading && combinedRecommendations.length > 0 && (
								<div className="subscribe-modal__recommended-sites">
									{ displayedRecommendations.map( ( site: CardData ) => (
										<ConnectedReaderSubscriptionListItem
											key={ site.feed_ID }
											feedId={ site.feed_ID }
											siteId={ site.site_ID }
											site={ site }
											url={ site.site_URL }
											showLastUpdatedDate={ false }
											showNotificationSettings={ false }
											showFollowedOnDate={ false }
											followSource="reader-onboarding-modal"
											disableSuggestedFollows
											replaceStreamClickWithItemClick
											onItemClick={ () => handleItemClick( site ) }
											isSelected={ selectedSite?.feed_ID === site.feed_ID }
											onFollowToggle={ ( following: boolean ) =>
												handleFollowToggle( site, following )
											}
										/>
									) ) }
								</div>
							) }
							{ currentPage < maxPages && (
								<Button
									className="subscribe-modal__load-more-button"
									onClick={ handleLoadMore }
									variant="link"
								>
									{ __( 'Load more recommendations' ) }
								</Button>
							) }
						</div>
						<div className="subscribe-modal__preview-column">
							<div className="subscribe-modal__preview-placeholder">
								{ selectedSite && (
									<>
										<div className="subscribe-modal__preview-stream-header">
											<h3>{ formatUrl( selectedSite.site_URL ) }</h3>
										</div>
										<div className="subscribe-modal__preview-stream-container">
											<TypedStream
												streamKey={ `feed:${ selectedSite.feed_ID }` }
												className="is-site-stream subscribe-modal__preview-stream"
												followSource="reader_subscribe_modal"
												useCompactCards
												trackScrollPage={ trackScrollPage.bind( null ) }
											/>
										</div>
									</>
								) }
							</div>
						</div>
						<div className="reader-onboarding-modal__footer">
							<HStack justify="right" className="reader-onboarding-modal__footer-actions">
								<Button __next40pxDefaultSize variant="tertiary" onClick={ handleClose }>
									{ __( 'Cancel' ) }
								</Button>
								<Button
									__next40pxDefaultSize
									onClick={ handleContinue }
									variant="primary"
									disabled={ promptVerification }
									accessibleWhenDisabled
								>
									{ __( 'Continue' ) }
								</Button>
							</HStack>
						</div>
					</div>
				</div>
			</Modal>
		)
	);
};

export default SubscribeModal;