File size: 2,629 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
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { useTranslate } from 'i18n-calypso';
import PopoverMenuItem from 'calypso/components/popover-menu/item';
import PluginCommonList from '../plugin-common/plugin-common-list';
import PluginManageConnection from '../plugin-manage-connection';
import PluginRowFormatter from '../plugin-row-formatter';
import RemovePlugin from '../remove-plugin';
import type { Columns, PluginRowFormatterArgs, PluginComponentProps } from '../types';
import type { SiteDetails } from '@automattic/data-stores';

import '../style.scss';

interface Props {
	selectedSite?: SiteDetails;
	items: Array< PluginComponentProps >;
	isLoading: boolean;
	columns: Columns;
	className?: string;
	removePluginNotice: ( plugin: PluginComponentProps ) => void;
	updatePlugin: ( plugin: PluginComponentProps ) => void;
}

export default function PluginsList( {
	selectedSite,
	removePluginNotice,
	updatePlugin,
	...rest
}: Props ) {
	const translate = useTranslate();

	const rowFormatter = ( props: PluginRowFormatterArgs ) => {
		return (
			<PluginRowFormatter
				{ ...props }
				selectedSite={ selectedSite }
				updatePlugin={ updatePlugin }
			/>
		);
	};

	const trackManagePlugin = ( pluginSlug: string ) => () => {
		recordTracksEvent( 'calypso_plugin_manage_click', {
			site: selectedSite?.ID,
			plugin: pluginSlug,
		} );
	};

	const trackRemovePlugin = ( pluginSlug: string ) => {
		recordTracksEvent( 'calypso_plugin_remove_click', {
			plugin: pluginSlug,
		} );
	};

	const onRemoveClick = ( plugin: PluginComponentProps ) => () => {
		removePluginNotice( plugin );
		trackRemovePlugin( plugin.slug );
	};

	const renderActions = ( plugin: PluginComponentProps ) => {
		return (
			<>
				<PopoverMenuItem
					className="plugin-management-v2__actions"
					icon="chevron-right"
					href={ `/plugins/${ plugin.slug }${ selectedSite ? `/${ selectedSite.domain }` : '' }` }
					onClick={ trackManagePlugin( plugin.slug ) }
				>
					{ translate( 'Manage plugin' ) }
				</PopoverMenuItem>
				{ selectedSite ? (
					<RemovePlugin site={ selectedSite } plugin={ plugin } />
				) : (
					<PopoverMenuItem
						icon="trash"
						className="plugin-management-v2__actions"
						onClick={ onRemoveClick( plugin ) }
					>
						{ translate( 'Remove' ) }
					</PopoverMenuItem>
				) }
				{ selectedSite && <PluginManageConnection site={ selectedSite } plugin={ plugin } /> }
			</>
		);
	};

	return (
		<PluginCommonList
			{ ...rest }
			selectedSite={ selectedSite }
			rowFormatter={ rowFormatter }
			primaryKey="id"
			renderActions={ renderActions }
		/>
	);
}