Skip to content

Commit

Permalink
Hosting dashboard: Enable sorting for Plan and Status (#94646)
Browse files Browse the repository at this point in the history
* Enable sorting for Plan and Status.

* Add sorting by plan.

* Add status sorting.

* Fix coming soon status sort.

* Make plan property optional.

* Use getSiteLaunchStatus.
  • Loading branch information
allilevine committed Sep 20, 2024
1 parent 69ad0ae commit 3a76c46
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
2 changes: 2 additions & 0 deletions client/hosting/sites/components/sites-dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ const siteSortingKeys = [
{ dataView: 'site', sortKey: 'alphabetically' },
{ dataView: 'last-publish', sortKey: 'updatedAt' },
{ dataView: 'last-interacted', sortKey: 'lastInteractedWith' },
{ dataView: 'plan', sortKey: 'plan' },
{ dataView: 'status', sortKey: 'status' },
];

const DEFAULT_PER_PAGE = 50;
Expand Down
4 changes: 2 additions & 2 deletions client/hosting/sites/components/sites-dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ const DotcomSitesDataViews = ( {
<SitePlan site={ item } userId={ userId } />
),
enableHiding: false,
enableSorting: false,
enableSorting: true,
},
{
id: 'status',
label: __( 'Status' ),
render: ( { item }: { item: SiteExcerptData } ) => <SiteStatus site={ item } />,
enableHiding: false,
enableSorting: false,
enableSorting: true,
elements: siteStatusGroups,
filterBy: {
operators: [ 'is' ],
Expand Down
3 changes: 3 additions & 0 deletions packages/sites/src/site-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ export interface MinimumSite {
is_redirect?: boolean;
updated_at?: string;
};
plan?: {
product_name_short: string;
};
}
70 changes: 67 additions & 3 deletions packages/sites/src/use-sites-list-sorting.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
import { createHigherOrderComponent } from '@wordpress/compose';
import { useMemo } from 'react';
import { getSiteLaunchStatus } from './site-status';
import { MinimumSite } from './site-type';

type SiteDetailsForSortingWithOptionalUserInteractions = Pick<
MinimumSite,
'title' | 'user_interactions' | 'options' | 'is_wpcom_staging_site' | 'ID'
| 'title'
| 'user_interactions'
| 'options'
| 'is_wpcom_staging_site'
| 'ID'
| 'plan'
| 'is_deleted'
| 'is_coming_soon'
| 'is_private'
| 'launch_status'
>;

type SiteDetailsForSortingWithUserInteractions = Pick<
MinimumSite,
'title' | 'options' | 'is_wpcom_staging_site' | 'ID'
| 'title'
| 'options'
| 'is_wpcom_staging_site'
| 'ID'
| 'plan'
| 'is_deleted'
| 'is_coming_soon'
| 'is_private'
| 'launch_status'
> &
Required< Pick< MinimumSite, 'user_interactions' > >;

export type SiteDetailsForSorting =
| SiteDetailsForSortingWithOptionalUserInteractions
| SiteDetailsForSortingWithUserInteractions;

const validSortKeys = [ 'lastInteractedWith', 'updatedAt', 'alphabetically' ] as const;
const validSortKeys = [
'lastInteractedWith',
'updatedAt',
'alphabetically',
'plan',
'status',
] as const;
const validSortOrders = [ 'asc', 'desc' ] as const;

export type SitesSortKey = ( typeof validSortKeys )[ number ];
Expand Down Expand Up @@ -52,6 +76,10 @@ export function useSitesListSorting< T extends SiteDetailsForSorting >(
return sortSitesAlphabetically( allSites, sortOrder );
case 'updatedAt':
return sortSitesByLastPublish( allSites, sortOrder );
case 'plan':
return sortSitesByPlan( allSites, sortOrder );
case 'status':
return sortSitesByStatus( allSites, sortOrder );
default:
return allSites;
}
Expand Down Expand Up @@ -280,6 +308,28 @@ function sortByLastPublish< T extends SiteDetailsForSorting >(
return 0;
}

function sortByPlan< T extends SiteDetailsForSorting >( a: T, b: T, sortOrder: SitesSortOrder ) {
const planA = a.plan?.product_name_short;
const planB = b.plan?.product_name_short;

if ( ! planA || ! planB ) {
return 0;
}

return sortOrder === 'asc' ? planA.localeCompare( planB ) : planB.localeCompare( planA );
}

function sortByStatus< T extends SiteDetailsForSorting >( a: T, b: T, sortOrder: SitesSortOrder ) {
const statusA = getSiteLaunchStatus( a );
const statusB = getSiteLaunchStatus( b );

if ( ! statusA || ! statusB ) {
return 0;
}

return sortOrder === 'asc' ? statusA.localeCompare( statusB ) : statusB.localeCompare( statusA );
}

function sortSitesAlphabetically< T extends SiteDetailsForSorting >(
sites: T[],
sortOrder: SitesSortOrder
Expand All @@ -294,6 +344,20 @@ function sortSitesByLastPublish< T extends SiteDetailsForSorting >(
return [ ...sites ].sort( ( a, b ) => sortByLastPublish( a, b, sortOrder ) );
}

function sortSitesByPlan< T extends SiteDetailsForSorting >(
sites: T[],
sortOrder: SitesSortOrder
): T[] {
return [ ...sites ].sort( ( a, b ) => sortByPlan( a, b, sortOrder ) );
}

function sortSitesByStatus< T extends SiteDetailsForSorting >(
sites: T[],
sortOrder: SitesSortOrder
): T[] {
return [ ...sites ].sort( ( a, b ) => sortByStatus( a, b, sortOrder ) );
}

type SitesSortingProps = {
sitesSorting: SitesSortOptions;
sites: SiteDetailsForSorting[];
Expand Down

0 comments on commit 3a76c46

Please sign in to comment.