File size: 15,926 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
import { isEnabled } from '@automattic/calypso-config';
import pagejs from '@automattic/calypso-router';
import {
type SiteExcerptData,
SitesSortKey,
useFilterDeletedSites,
useSitesListFiltering,
useSitesListGrouping,
useSitesListSorting,
} from '@automattic/sites';
import { GroupableSiteLaunchStatuses } from '@automattic/sites/src/use-sites-list-grouping';
import { DESKTOP_BREAKPOINT, WIDE_BREAKPOINT } from '@automattic/viewport';
import { useBreakpoint } from '@automattic/viewport-react';
import clsx from 'clsx';
import { translate } from 'i18n-calypso';
import React, { useEffect, useMemo, useState } from 'react';
import AsyncLoad from 'calypso/components/async-load';
import DocumentHead from 'calypso/components/data/document-head';
import GuidedTour from 'calypso/components/guided-tour';
import { GuidedTourContextProvider } from 'calypso/components/guided-tour/data/guided-tour-context';
import { useCurrentRoute } from 'calypso/components/route';
import { useSiteExcerptsQuery } from 'calypso/data/sites/use-site-excerpts-query';
import Layout from 'calypso/layout/hosting-dashboard';
import LayoutColumn from 'calypso/layout/hosting-dashboard/column';
import LayoutHeader, {
LayoutHeaderActions as Actions,
LayoutHeaderTitle as Title,
} from 'calypso/layout/hosting-dashboard/header';
import LayoutTop from 'calypso/layout/hosting-dashboard/top';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { isP2Theme } from 'calypso/lib/site/utils';
import {
SitesDashboardQueryParams,
handleQueryParamChange,
} from 'calypso/sites-dashboard/components/sites-content-controls';
import { useSelector } from 'calypso/state';
import { shouldShowSiteDashboard } from 'calypso/state/global-sidebar/selectors';
import { useSitesSorting } from 'calypso/state/sites/hooks/use-sites-sorting';
import { getSelectedSite } from 'calypso/state/ui/selectors';
import { useInitializeDataViewsPage } from '../hooks/use-initialize-dataviews-page';
import { useShowSiteCreationNotice } from '../hooks/use-show-site-creation-notice';
import { useShowSiteTransferredNotice } from '../hooks/use-show-site-transferred-notice';
import { useTracksEventOnFilterChange } from '../hooks/use-tracks-event-on-filter-change';
import {
CALYPSO_ONBOARDING_TOURS_PREFERENCE_NAME,
CALYPSO_ONBOARDING_TOURS_EVENT_NAMES,
useOnboardingTours,
} from '../onboarding-tours';
import { OVERVIEW, FEATURE_TO_ROUTE_MAP } from './site-preview-pane/constants';
import DotcomPreviewPane from './site-preview-pane/dotcom-preview-pane';
import { useRestoreSitesBanner } from './sites-dashboard-banners/use-restore-sites-reminder-banner';
import SitesDashboardBannersManager from './sites-dashboard-banners-manager';
import SitesDashboardHeader from './sites-dashboard-header';
import DotcomSitesDataViews, { useSiteStatusGroups } from './sites-dataviews';
import { getSitesPagination } from './sites-dataviews/utils';
import type { View } from '@wordpress/dataviews';
// todo: we are using A4A styles until we extract them as common styles in the ItemsDashboard component
import './style.scss';
// Add Dotcom specific styles
import './dotcom-style.scss';
import './guided-tours.scss';
interface SitesDashboardProps {
queryParams: SitesDashboardQueryParams;
initialSiteFeature?: string;
selectedSiteFeaturePreview?: React.ReactNode;
sectionName?: string;
isOnlyLayoutView?: boolean;
}
const siteSortingKeys = [
{ dataView: 'site-title', 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;
const DEFAULT_SITE_TYPE = 'non-p2';
const desktopFields = [ 'plan', 'status', 'last-publish', 'stats' ];
const mobileFields: string[] = [];
const listViewFields: string[] = [];
const getFieldsByBreakpoint = ( selectedSite: boolean, isDesktop: boolean ) => {
if ( selectedSite ) {
return listViewFields;
}
return isDesktop ? desktopFields : mobileFields;
};
export function showSitesPage( route: string, openInNewTab = false ) {
const currentParams = new URL( window.location.href ).searchParams;
const newUrl = new URL( route, window.location.origin );
const supportedParams = [ 'page', 'per-page', 'search', 'status', 'siteType' ];
supportedParams.forEach( ( param ) => {
if ( currentParams.has( param ) ) {
const value = currentParams.get( param );
if ( value ) {
newUrl.searchParams.set( param, value );
}
}
} );
if ( openInNewTab ) {
const newWindow = window.open(
newUrl.toString().replace( window.location.origin, '' ),
'_blank'
);
if ( newWindow ) {
newWindow.opener = null;
}
} else {
pagejs.show( newUrl.toString().replace( window.location.origin, '' ) );
}
}
const SitesDashboard = ( {
// Note - control params (eg. search, page, perPage, status...) are currently meant for
// initializing the dataViewsState. Further calculations should reference the dataViewsState.
queryParams: {
page = 1,
perPage = DEFAULT_PER_PAGE,
search,
newSiteID,
status,
siteType = DEFAULT_SITE_TYPE,
},
initialSiteFeature = OVERVIEW,
selectedSiteFeaturePreview = undefined,
isOnlyLayoutView = undefined,
}: SitesDashboardProps ) => {
const [ initialSortApplied, setInitialSortApplied ] = useState( false );
const isWide = useBreakpoint( WIDE_BREAKPOINT );
const isDesktop = useBreakpoint( DESKTOP_BREAKPOINT );
const { hasSitesSortingPreferenceLoaded, sitesSorting, onSitesSortingChange } = useSitesSorting();
const selectedSite = useSelector( getSelectedSite );
const { shouldShow: isRestoringAccount } = useRestoreSitesBanner();
const sitesFilterCallback = ( site: SiteExcerptData ) => {
const { options } = site || {};
// Early return if the site is domain-only
if ( options?.is_domain_only ) {
return false;
}
// siteType is 'all' - filter out sites that are P2 sites
if ( siteType === DEFAULT_SITE_TYPE ) {
return (
! options?.is_wpforteams_site &&
( ! options?.theme_slug || ! isP2Theme( options.theme_slug ) )
);
}
// siteType is 'p2' - filter out sites that are not P2
return (
!! options?.is_wpforteams_site ||
!! ( options?.theme_slug && isP2Theme( options.theme_slug ) )
);
};
const { data: allSites = [], isLoading } = useSiteExcerptsQuery(
[],
sitesFilterCallback,
'all',
[ 'is_a4a_dev_site', 'site_migration' ],
[ 'theme_slug' ],
// Don't fetch sites on narrow screens since it's not visible.
! selectedSite || isWide
);
useShowSiteCreationNotice( allSites, newSiteID );
useShowSiteTransferredNotice();
const siteStatusGroups = useSiteStatusGroups();
// Create the DataViews state based on initial values
const defaultDataViewsState: View = {
sort: {
field: '',
direction: 'asc',
},
page,
perPage,
search: search ?? '',
fields: getFieldsByBreakpoint( !! selectedSite, isDesktop ),
...( status
? {
filters: [
{
field: 'status',
operator: 'is',
value: siteStatusGroups.find( ( item ) => item.slug === status )?.value || 1,
},
],
}
: {} ),
...( selectedSite
? {
type: 'list',
titleField: 'site-title',
showTitle: true,
mediaField: 'icon',
showMedia: true,
}
: {
type: 'table',
titleField: 'site-title',
showTitle: true,
mediaField: 'icon',
showMedia: true,
layout: {
styles: {
site: {
width: '40%',
},
plan: {
width: '126px',
},
status: {
width: '142px',
},
'last-publish': {
width: '146px',
},
stats: {
width: '106px',
},
},
},
} ),
};
const [ dataViewsState, setDataViewsState ] = useState< View >( defaultDataViewsState );
useEffect( () => {
const fields = getFieldsByBreakpoint( !! selectedSite, isDesktop );
const fieldsForBreakpoint = [ ...fields ].sort().toString();
const existingFields = [ ...( dataViewsState?.fields ?? [] ) ].sort().toString();
// Compare the content of the arrays, not its referrences that will always be different.
// sort() sorts the array in place, so we need to clone them first.
if ( existingFields !== fieldsForBreakpoint ) {
setDataViewsState( ( prevState ) => ( { ...prevState, fields } ) );
}
}, [ isDesktop, isWide, dataViewsState, selectedSite ] );
// Ensure site sort preference is applied when it loads in. This isn't always available on
// initial mount.
useEffect( () => {
// Ensure we set and check initialSortApplied to prevent infinite loops when changing sort
// values after initial sort.
if ( hasSitesSortingPreferenceLoaded && ! initialSortApplied ) {
const newSortField =
siteSortingKeys.find( ( key ) => key.sortKey === sitesSorting.sortKey )?.dataView || '';
const newSortDirection = sitesSorting.sortOrder;
setDataViewsState( ( prevState ) => ( {
...prevState,
sort: {
field: newSortField,
direction: newSortDirection,
},
} ) );
setInitialSortApplied( true );
}
}, [
hasSitesSortingPreferenceLoaded,
sitesSorting,
dataViewsState.sort,
initialSortApplied,
siteType,
] );
// Get the status group slug.
const statusSlug = useMemo( () => {
const statusFilter = dataViewsState.filters?.find( ( filter ) => filter.field === 'status' );
const statusNumber = statusFilter?.value;
return siteStatusGroups.find( ( status ) => status.value === statusNumber )
?.slug as GroupableSiteLaunchStatuses;
}, [ dataViewsState.filters, siteStatusGroups ] );
// Filter sites list by status group.
const { currentStatusGroup, statuses } = useSitesListGrouping( allSites, {
status: statusSlug || 'all',
showHidden: true,
} );
// Remove deleted sites from default view
const filteredStatusGroup = useFilterDeletedSites( currentStatusGroup, {
shouldApplyFilter:
! search && ( ! statusSlug || statusSlug === 'all' ) && ! isRestoringAccount(),
} );
// Perform sorting actions
const sortedSites = useSitesListSorting( filteredStatusGroup, {
sortKey: siteSortingKeys.find( ( key ) => key.dataView === dataViewsState.sort?.field )
?.sortKey as SitesSortKey,
sortOrder: dataViewsState.sort?.direction || undefined,
} );
const hasA8CSitesFilter =
dataViewsState.filters?.some(
( { field, operator, value } ) => field === 'is_a8c' && operator === 'is' && value === true
) ?? false;
const includeA8CSites = siteType === 'p2' || hasA8CSitesFilter;
// Filter sites list by search query.
const filteredSites = useSitesListFiltering( sortedSites, {
search: dataViewsState.search,
includeA8CSites,
} );
const paginatedSites =
dataViewsState.page && dataViewsState.perPage
? filteredSites.slice(
( dataViewsState.page - 1 ) * dataViewsState.perPage,
dataViewsState.page * dataViewsState.perPage
)
: filteredSites;
const onboardingTours = useOnboardingTours();
useInitializeDataViewsPage( dataViewsState, setDataViewsState );
// Update URL with view control params on change.
useEffect( () => {
const queryParams = {
search: dataViewsState.search?.trim(),
status: statusSlug,
page: dataViewsState.page && dataViewsState.page > 1 ? dataViewsState.page : undefined,
'per-page': dataViewsState.perPage === DEFAULT_PER_PAGE ? undefined : dataViewsState.perPage,
};
window.setTimeout( () => handleQueryParamChange( queryParams ) );
}, [ dataViewsState.search, dataViewsState.page, dataViewsState.perPage, statusSlug ] );
// Update site sorting preference on change
useEffect( () => {
if ( dataViewsState.sort?.field ) {
onSitesSortingChange( {
sortKey: siteSortingKeys.find( ( key ) => key.dataView === dataViewsState.sort?.field )
?.sortKey as SitesSortKey,
sortOrder: dataViewsState.sort.direction || 'asc',
} );
}
}, [ dataViewsState.sort, onSitesSortingChange ] );
useTracksEventOnFilterChange( dataViewsState.filters ?? [] );
// Manage the closing of the preview pane
const closeSitePreviewPane = () => {
if ( selectedSite ) {
showSitesPage( '/sites' );
}
};
const sitePreviewPane = {
getUrl: ( site: SiteExcerptData ) => {
return `/${ FEATURE_TO_ROUTE_MAP[ initialSiteFeature ].replace( ':site', site.slug ) }`;
},
open: (
site: SiteExcerptData,
source: 'site_field' | 'action' | 'list_row_click' | 'environment_switcher',
openInNewTab?: boolean
) => {
recordTracksEvent( 'calypso_sites_dashboard_open_site_preview_pane', {
site_id: site.ID,
source,
} );
showSitesPage( sitePreviewPane.getUrl( site ), openInNewTab );
},
};
const changeSitePreviewPane = ( siteId: number ) => {
const targetSite = allSites.find( ( site ) => site.ID === siteId );
if ( targetSite ) {
sitePreviewPane.open( targetSite, 'environment_switcher' );
}
};
const { currentSection, currentRoute } = useCurrentRoute() as {
currentSection: false | { group?: string; name?: string };
currentRoute: string;
};
const showSiteDashboard = useSelector( ( state ) =>
shouldShowSiteDashboard( {
state,
siteId: selectedSite?.ID ?? null,
section: currentSection as { group?: string },
route: currentRoute,
} )
);
if ( !! selectedSite && ! showSiteDashboard ) {
return null;
}
// Hide the listing on narrow screens since it's not visible.
const hideListing = selectedSite && ! isWide;
// todo: temporary mock data
const isNarrowView = false;
const dashboardTitle = siteType === 'p2' ? translate( 'P2s' ) : translate( 'Sites' );
return (
<Layout
className={ clsx(
'sites-dashboard',
'sites-dashboard__layout',
! selectedSite && 'preview-hidden',
isOnlyLayoutView && 'domains-overview'
) }
wide
title={ selectedSite ? null : dashboardTitle }
>
<DocumentHead title={ dashboardTitle } />
{ ! hideListing && (
<LayoutColumn className="sites-overview" wide>
<LayoutTop withNavigation={ false }>
<LayoutHeader>
{ ! isNarrowView && <Title>{ dashboardTitle }</Title> }
<Actions>
<SitesDashboardHeader isPreviewPaneOpen={ !! selectedSite } />
</Actions>
</LayoutHeader>
</LayoutTop>
<DocumentHead title={ dashboardTitle } />
<SitesDashboardBannersManager
sitesStatuses={ statuses }
sitesCount={ paginatedSites.length }
/>
{ ! selectedSite &&
siteType === DEFAULT_SITE_TYPE &&
isEnabled( 'dashboard/v2/backport/sites-list' ) ? (
<AsyncLoad require="../v2/sites-list" placeholder={ null } />
) : (
<DotcomSitesDataViews
sites={ paginatedSites }
siteType={ siteType }
isLoading={ isLoading || ! initialSortApplied }
paginationInfo={ getSitesPagination( filteredSites, perPage ) }
dataViewsState={ dataViewsState }
setDataViewsState={ setDataViewsState }
selectedItem={ selectedSite }
sitePreviewPane={ sitePreviewPane }
/>
) }
</LayoutColumn>
) }
{ selectedSite && (
<GuidedTourContextProvider
guidedTours={ onboardingTours }
preferenceNames={ CALYPSO_ONBOARDING_TOURS_PREFERENCE_NAME }
eventNames={ CALYPSO_ONBOARDING_TOURS_EVENT_NAMES }
>
<LayoutColumn
className={ clsx(
'site-preview-pane',
isOnlyLayoutView && 'domains-overview__details'
) }
wide
>
{ isOnlyLayoutView ? (
selectedSiteFeaturePreview
) : (
<DotcomPreviewPane
site={ selectedSite }
selectedSiteFeature={ initialSiteFeature }
selectedSiteFeaturePreview={ selectedSiteFeaturePreview }
closeSitePreviewPane={ closeSitePreviewPane }
changeSitePreviewPane={ changeSitePreviewPane }
/>
) }
</LayoutColumn>
<GuidedTour defaultTourId="siteManagementTour" />
</GuidedTourContextProvider>
) }
</Layout>
);
};
export default SitesDashboard;
|