File size: 3,288 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
import { SyntheticEvent } from '@wordpress/element';
import { settings } from '@wordpress/icons';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { useState } from 'react';
import { connect } from 'react-redux';
import { withCurrentRoute } from 'calypso/components/route';
import GlobalSidebar from 'calypso/layout/global-sidebar';
import SidebarItem from 'calypso/layout/sidebar/item';
import SidebarMenu from 'calypso/layout/sidebar/menu';
import { getShouldShowCollapsedGlobalSidebar } from 'calypso/state/global-sidebar/selectors';
import { AppState } from 'calypso/types';
import { SidebarIconPlugins } from '../../sidebar/static-data/global-sidebar-menu';
import { SidebarIconCalendar } from './icons';
import './style.scss';

interface Props {
	path: string;
	isCollapsed: boolean;
}
const managePluginsPattern = /^\/plugins\/(manage|active|inactive|updates)/;

const PluginsSidebar = ( { path, isCollapsed }: Props ) => {
	const translate = useTranslate();

	const [ previousPath, setPreviousPath ] = useState( path );
	const isManagedPluginSelected =
		managePluginsPattern.test( path ) ||
		( path.startsWith( '/plugins/' ) &&
			managePluginsPattern.test( previousPath ) &&
			! path.startsWith( '/plugins/scheduled-updates' ) );

	return (
		<GlobalSidebar
			className={ clsx( 'sidebar--plugins', { 'is-collapsed': isCollapsed } ) }
			siteTitle={ ! isCollapsed && translate( 'Plugins' ) }
			requireBackLink
			backLinkHref="/sites"
			subHeading={
				! isCollapsed &&
				translate(
					"Enhance your site's features with plugins, or schedule updates to fit your needs."
				)
			}
		>
			<SidebarMenu>
				<SidebarItem
					className="sidebar__menu-item--plugins"
					link="/plugins"
					label={ translate( 'Marketplace' ) }
					tooltip={ isCollapsed && translate( 'Marketplace' ) }
					onNavigate={ ( _e: SyntheticEvent, link: string ) => setPreviousPath( link ) }
					selected={
						path.startsWith( '/plugins' ) &&
						! path.startsWith( '/plugins/scheduled-updates' ) &&
						! isManagedPluginSelected
					}
					customIcon={ <SidebarIconPlugins /> }
				/>

				<SidebarItem
					className="sidebar__menu-item--plugins"
					link="/plugins/manage/sites"
					label={ translate( 'Manage plugins' ) }
					tooltip={ isCollapsed && translate( 'Manage plugins' ) }
					selected={ isManagedPluginSelected }
					icon={ settings }
					onNavigate={ ( _e: SyntheticEvent, link: string ) => setPreviousPath( link ) }
				/>

				<SidebarItem
					className="sidebar__menu-item--plugins"
					link="/plugins/scheduled-updates"
					label={ translate( 'Scheduled updates' ) }
					tooltip={ isCollapsed && translate( 'Scheduled updates' ) }
					selected={ path.startsWith( '/plugins/scheduled-updates' ) }
					customIcon={ <SidebarIconCalendar /> }
				/>
			</SidebarMenu>
		</GlobalSidebar>
	);
};

export default withCurrentRoute(
	// eslint-disable-next-line @typescript-eslint/no-explicit-any
	connect( ( state: AppState, { currentSection, currentRoute }: any ) => {
		const shouldShowCollapsedGlobalSidebar = getShouldShowCollapsedGlobalSidebar( {
			state,
			siteId: null,
			section: currentSection,
			route: currentRoute,
		} );
		return {
			isCollapsed: shouldShowCollapsedGlobalSidebar,
		};
	} )( PluginsSidebar )
);