File size: 4,115 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
import page from '@automattic/calypso-router';
import { SubscriptionManager } from '@automattic/data-stores';
import { useTranslate } from 'i18n-calypso';
import React, { useEffect, useMemo } from 'react';
import DocumentHead from 'calypso/components/data/document-head';
import Main from 'calypso/components/main';
import NavigationHeader from 'calypso/components/navigation-header';
import Nav from 'calypso/components/section-nav';
import NavItem from 'calypso/components/section-nav/item';
import NavTabs from 'calypso/components/section-nav/tabs';
import {
	SubscriptionsPortal,
	SubscriptionManagerContextProvider,
} from 'calypso/landing/subscriptions/components/subscription-manager-context';
import { SubscriptionsEllipsisMenu } from 'calypso/landing/subscriptions/components/subscriptions-ellipsis-menu';
import { useDispatch } from 'calypso/state';
import { markFollowsAsStale } from 'calypso/state/reader/follows/actions';
import './style.scss';

const useMarkFollowsAsStaleOnUnmount = () => {
	const dispatch = useDispatch();
	useEffect( () => {
		return () => {
			dispatch( markFollowsAsStale() );
		};
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [] );
};

const getSelectedTab = ( pathname: string ) => {
	if ( pathname.includes( 'comments' ) ) {
		return 'comments';
	}
	if ( pathname.includes( 'pending' ) ) {
		return 'pending';
	}
	return 'sites';
};

type SubscriptionsManagerWrapperProps = {
	actionButton?: React.ReactNode;
	children: React.ReactNode;
	ellipsisMenuItems?: React.ReactNode;
	headerText: string;
	subHeaderText: React.ReactNode;
};

const SubscriptionsManagerWrapper = ( {
	actionButton,
	children,
	ellipsisMenuItems,
	headerText,
	subHeaderText,
}: SubscriptionsManagerWrapperProps ) => {
	const translate = useTranslate();
	const { data: counts } = SubscriptionManager.useSubscriptionsCountQuery();
	const selectedTab = getSelectedTab( page.current );

	// Mark follows as stale on unmount to ensure that the reader
	// redux store is in a consistent state when the user navigates.
	// This is necessary because the subscription manager does not
	// sync its subscriptions state with the reader redux store.
	useMarkFollowsAsStaleOnUnmount();

	const selectedTabText = useMemo( () => {
		switch ( selectedTab ) {
			case 'comments':
				return translate( 'Comments' );
			case 'pending':
				return translate( 'Pending' );
			default:
				return translate( 'Sites' );
		}
	}, [ selectedTab ] );

	return (
		<SubscriptionManagerContextProvider portal={ SubscriptionsPortal.Reader }>
			<Main className="site-subscriptions-manager">
				<DocumentHead title={ translate( 'Manage subscriptions' ) } />
				<NavigationHeader
					title={ headerText }
					subtitle={
						<>
							{ subHeaderText }{ ' ' }
							<a href="/me/notifications/subscriptions?referrer=management">
								{ translate( 'Manage notification settings' ) }
							</a>
						</>
					}
				>
					{ actionButton }

					{ ellipsisMenuItems && (
						<SubscriptionsEllipsisMenu
							toggleTitle={ translate( 'More' ) }
							popoverClassName="site-subscriptions-manager__import-export-popover"
						>
							{ ellipsisMenuItems }
						</SubscriptionsEllipsisMenu>
					) }
				</NavigationHeader>

				<Nav className="site-subscriptions-manager__nav" selectedText={ selectedTabText }>
					<NavTabs>
						<NavItem
							count={ counts?.blogs }
							selected={ selectedTab === 'sites' }
							path="/reader/subscriptions"
						>
							{ translate( 'Sites' ) }
						</NavItem>
						<NavItem
							count={ counts?.comments }
							selected={ selectedTab === 'comments' }
							path="/reader/subscriptions/comments"
						>
							{ translate( 'Comments' ) }
						</NavItem>
						{ !! counts?.pending && (
							<NavItem
								count={ counts?.pending }
								selected={ selectedTab === 'pending' }
								path="/reader/subscriptions/pending"
							>
								{ translate( 'Pending' ) }
							</NavItem>
						) }
					</NavTabs>
				</Nav>

				{ children }
			</Main>
		</SubscriptionManagerContextProvider>
	);
};

export default SubscriptionsManagerWrapper;