/** * External dependencies */ import type { ComponentType } from 'react'; /** * WordPress dependencies */ import { useContext } from '@wordpress/element'; import { Spinner } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ import DataViewsContext from '../dataviews-context'; import { VIEW_LAYOUTS } from '../dataviews-layouts'; import { useDelayedLoading } from '../../hooks/use-delayed-loading'; import type { ViewBaseProps } from '../../types'; type DataViewsLayoutProps = { className?: string; }; export default function DataViewsLayout( { className }: DataViewsLayoutProps ) { const { actions = [], data, fields, getItemId, getItemLevel, hasInitiallyLoaded, isLoading, view, onChangeView, selection, onChangeSelection, setOpenedFilter, onClickItem, isItemClickable, renderItemLink, defaultLayouts, containerRef, empty =
{ __( 'No results' ) }
, } = useContext( DataViewsContext ); const isDelayedInitialLoading = useDelayedLoading( ! hasInitiallyLoaded, { delay: 200, } ); // Until the initial data load completes, show a spinner (or nothing if fast). // After that, render the layout component which preserves previous data // while loading subsequent requests. if ( ! hasInitiallyLoaded ) { // If the initial data load is fast, don't show the loading state at all. if ( ! isDelayedInitialLoading ) { return null; } // If the initial data load takes more than 200ms, show the loading state. return (