Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk Plugin Management: Use list view for small viewports #98551

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { isDesktop, subscribeIsDesktop } from '@automattic/viewport';
import { filterSortAndPaginate } from '@wordpress/dataviews';
import { useTranslate } from 'i18n-calypso';
import { useMemo, useState, useCallback } from 'react';
import { initialDataViewsState } from 'calypso/a8c-for-agencies/components/items-dashboard/constants';
import { useMemo, useState, useCallback, useEffect } from 'react';
import {
DATAVIEWS_LIST,
DATAVIEWS_TABLE,
initialDataViewsState,
} from 'calypso/a8c-for-agencies/components/items-dashboard/constants';
import { DataViewsState } from 'calypso/a8c-for-agencies/components/items-dashboard/items-dataviews/interfaces';
import QueryUserPurchases from 'calypso/components/data/query-user-purchases';
import { DataViews } from 'calypso/components/dataviews';
Expand Down Expand Up @@ -31,8 +36,10 @@ interface Props {
export default function SitesWithInstalledPluginsList( { sites, plugin, isWpCom }: Props ) {
const translate = useTranslate();
const dispatch = useDispatch();
const isDesktopView = isDesktop();
const shouldUseListView = ! isDesktopView;

const compareBooleans =
const compareBooleans = useCallback(
( fieldName: keyof PluginSite ) => ( a: SiteDetails, b: SiteDetails, direction: string ) => {
const aValue = plugin.sites[ a.ID ][ fieldName ];
const bValue = plugin.sites[ b.ID ][ fieldName ];
Expand All @@ -43,7 +50,9 @@ export default function SitesWithInstalledPluginsList( { sites, plugin, isWpCom
return aValue ? 1 : -1;
}
return aValue ? -1 : 1;
};
},
[ plugin ]
);

const renderActions = useCallback(
( site: SiteDetails ) => {
Expand Down Expand Up @@ -155,11 +164,12 @@ export default function SitesWithInstalledPluginsList( { sites, plugin, isWpCom
enableSorting: false,
},
],
[ translate, plugin, sites.length, dispatch, renderActions ]
[ translate, compareBooleans, plugin, sites.length, dispatch, renderActions ]
);

const [ dataViewsState, setDataViewsState ] = useState< DataViewsState >( () => ( {
...initialDataViewsState,
type: shouldUseListView ? DATAVIEWS_LIST : DATAVIEWS_TABLE,
fields: [ 'domain', 'activate', 'autoupdate', 'update', 'actions' ],
items: sites,
} ) );
Expand All @@ -168,6 +178,25 @@ export default function SitesWithInstalledPluginsList( { sites, plugin, isWpCom
getSitesWithSecondarySites( state, sites )
);

useEffect( () => {
// Sets the correct fields when route changes or viewport changes
setDataViewsState( ( dVwState ) => ( {
...dVwState,
type: shouldUseListView ? DATAVIEWS_LIST : DATAVIEWS_TABLE,
} ) );

// Subscribe to viewport changes
const unsubscribe = subscribeIsDesktop( ( matches ) => {
const shouldUseListView = ! matches;
setDataViewsState( ( dVwState ) => ( {
...dVwState,
type: shouldUseListView ? DATAVIEWS_LIST : DATAVIEWS_TABLE,
} ) );
} );

return () => unsubscribe();
}, [ plugin.slug, shouldUseListView ] );

if ( ! sitesWithSecondarySites?.length ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,13 @@
justify-content: flex-end;
}
}
// Hiding this here because in this version the mediaField is not hiddable
// https://github.com/WordPress/gutenberg/tree/d59faffa3d270dbc7716f653b4cdab6020812658/packages/dataviews#properties-of-layout
// In future versions, a showMedia property can be used
.dataviews-view-list {
.dataviews-view-list__media-wrapper {
background-color: transparent;
display: none;
}
}
}
5 changes: 3 additions & 2 deletions client/my-sites/plugins/plugins-dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { PluginActionName, PluginActions, Site } from '../hooks/types';
import { withShowPluginActionDialog } from '../hooks/use-show-plugin-action-dialog';
import PluginAvailableOnSitesList from '../plugin-management-v2/plugin-details-v2/plugin-available-on-sites-list';
import SitesWithInstalledPluginsList from '../plugin-management-v2/plugin-details-v2/sites-with-installed-plugin-list';
import { PluginComponentProps } from '../plugin-management-v2/types';
import PluginsListDataViews from '../plugins-list/plugins-list-dataviews';
import type { SiteDetails } from '@automattic/data-stores';
import type { Plugin } from 'calypso/state/plugins/installed/types';
Expand Down Expand Up @@ -115,8 +116,8 @@ const PluginsDashboard = ( {
const productsList = useSelector( ( state ) => getProductsList( state ) );
const { data: dotComPlugins }: { data: Plugin[] | undefined } = useWPCOMPluginsList( 'all' );
const allPlugins = useSelector( ( state ) =>
getPlugins( state, siteIds, 'all' ).map( ( plugin: Plugin ) => {
let dotComPluginData: Plugin | undefined;
getPlugins( state, siteIds, 'all' ).map( ( plugin: PluginComponentProps ) => {
let dotComPluginData: PluginComponentProps | undefined;
if ( dotComPlugins ) {
dotComPluginData = dotComPlugins.find(
( dotComPlugin ) => dotComPlugin.slug === plugin.slug
Expand Down
1 change: 0 additions & 1 deletion client/state/plugins/installed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export type Plugin = {
wporg?: boolean;
status?: Array< number >;
allStatuses?: Array< CurrentSiteStatus >;
isMarketplaceProduct?: boolean;
};

export type PluginSite = {
Expand Down
Loading