File size: 2,908 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
import { useTranslate } from 'i18n-calypso';
import { useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import AllSites from 'calypso/blocks/all-sites';
import Site from 'calypso/blocks/site';
import { SidebarV2Header as SidebarHeader } from 'calypso/layout/sidebar-v2';
import { recordTracksEvent } from 'calypso/state/analytics/actions/record';
import { setLayoutFocus } from 'calypso/state/ui/layout-focus/actions';
import { getCurrentLayoutFocus } from 'calypso/state/ui/layout-focus/selectors';
import getSelectedSiteId from 'calypso/state/ui/selectors/get-selected-site-id';
import { AppState } from 'calypso/types';
import JetpackLogo from './jetpack-logo.svg';
import ProfileDropdown from './profile-dropdown';

type Props = {
	forceAllSitesView?: boolean;
};

const AllSitesIcon = () => (
	<img
		className="jetpack-cloud-sidebar__all-sites-icon"
		src={ JetpackLogo }
		alt=""
		role="presentation"
	/>
);

// NOTE: This hook is a little hacky, to get around the "outside click"
// close behavior that happens inside `<SiteSelector />`. Instead of
// using the `getCurrentLayoutFocus` selector, we use internal state to
// derive whether or not the site selector should be visible.
const useShowSiteSelector = ( {
	forceAllSitesView,
	selectedSiteId,
}: {
	forceAllSitesView: boolean;
	selectedSiteId: number | null;
} ) => {
	const SITES_FOCUS = 'sites';

	const isSiteSelectorVisible = useSelector(
		( state: AppState ) => getCurrentLayoutFocus( state ) === SITES_FOCUS
	);
	const dispatch = useDispatch();

	return useCallback( () => {
		dispatch(
			forceAllSitesView
				? recordTracksEvent( 'calypso_jetpack_sidebar_switch_site_all_click' )
				: recordTracksEvent( 'calypso_jetpack_sidebar_switch_site_single_click', {
						site_id: selectedSiteId,
				  } )
		);

		// NOTE: If the selector is visible, dismissal will happen automatically
		// when the user clicks outside the bounds of its root element
		if ( ! isSiteSelectorVisible ) {
			dispatch( setLayoutFocus( SITES_FOCUS ) );
		}
	}, [ dispatch, forceAllSitesView, isSiteSelectorVisible, selectedSiteId ] );
};

const Header = ( { forceAllSitesView = false }: Props ) => {
	const translate = useTranslate();
	const selectedSiteId = useSelector( getSelectedSiteId );

	const showSiteSelector = useShowSiteSelector( { forceAllSitesView, selectedSiteId } );

	return (
		<SidebarHeader className="jetpack-cloud-sidebar__header">
			{ forceAllSitesView ? (
				<AllSites
					showIcon
					showChevronDownIcon
					showCount={ false }
					icon={ <AllSitesIcon /> }
					title={ translate( 'All Sites' ) }
					onSelect={ showSiteSelector }
				/>
			) : (
				<Site
					showChevronDownIcon
					className="jetpack-cloud-sidebar__selected-site"
					siteId={ selectedSiteId }
					onSelect={ showSiteSelector }
				/>
			) }
			<ProfileDropdown />
		</SidebarHeader>
	);
};

export default Header;