@elementor/editor-site-navigation 4.5.0-1216 → 4.5.0-1218

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/index.d.mts +24 -1
  2. package/dist/index.d.ts +24 -1
  3. package/dist/index.js +830 -344
  4. package/dist/index.mjs +782 -288
  5. package/package.json +11 -10
  6. package/src/api/post.ts +21 -5
  7. package/src/components/panel/actions-menu/action-menu-item.tsx +4 -3
  8. package/src/components/panel/actions-menu/actions/force-delete.tsx +99 -0
  9. package/src/components/panel/actions-menu/actions/restore.tsx +33 -0
  10. package/src/components/panel/content-panel-active-filters.tsx +53 -0
  11. package/src/components/panel/content-panel-chrome.ts +19 -0
  12. package/src/components/panel/content-panel-clear-filters-button.tsx +22 -0
  13. package/src/components/panel/content-panel-search.tsx +1 -2
  14. package/src/components/panel/content-panel-status-filter-list.tsx +70 -0
  15. package/src/components/panel/content-panel-status-filter.tsx +75 -0
  16. package/src/components/panel/content-panel-toolbar.tsx +23 -0
  17. package/src/components/panel/posts-list/collapsible-list.tsx +7 -4
  18. package/src/components/panel/posts-list/list-items/list-item-view.tsx +80 -47
  19. package/src/components/panel/posts-list/list-items/site-content-list-item-actions-location.tsx +14 -0
  20. package/src/components/panel/posts-list/posts-collapsible-list.tsx +30 -9
  21. package/src/components/panel/shell.tsx +6 -2
  22. package/src/components/shared/page-title-and-status.tsx +2 -0
  23. package/src/contexts/content-panel-coordinator.tsx +34 -3
  24. package/src/hooks/use-posts-actions.ts +20 -1
  25. package/src/hooks/use-posts-baseline-total.ts +22 -0
  26. package/src/hooks/use-posts.ts +19 -13
  27. package/src/hooks/use-site-content-status-filter-counts.ts +56 -0
  28. package/src/index.ts +5 -0
  29. package/src/site-content-restore.ts +1 -0
  30. package/src/site-content-status-filter.ts +111 -0
@@ -13,6 +13,7 @@ import {
13
13
  ListItemButton,
14
14
  ListItemText,
15
15
  Menu,
16
+ Stack,
16
17
  Tooltip,
17
18
  Typography,
18
19
  usePopupState,
@@ -25,10 +26,16 @@ import { type Post } from '../../../../types';
25
26
  import PageTitleAndStatus from '../../../shared/page-title-and-status';
26
27
  import Delete from '../../actions-menu/actions/delete';
27
28
  import Duplicate from '../../actions-menu/actions/duplicate';
29
+ import ForceDelete from '../../actions-menu/actions/force-delete';
28
30
  import Rename from '../../actions-menu/actions/rename';
31
+ import Restore from '../../actions-menu/actions/restore';
29
32
  import SetHome from '../../actions-menu/actions/set-home';
30
33
  import View from '../../actions-menu/actions/view';
31
- import { contentPanelListItemButtonSx, contentPanelListRowPaddingSx } from '../../content-panel-chrome';
34
+ import { contentPanelListItemButtonSx, getContentPanelListItemSx } from '../../content-panel-chrome';
35
+ import {
36
+ SiteContentListItemActionsSlot,
37
+ useSiteContentListItemActionsInjections,
38
+ } from './site-content-list-item-actions-location';
32
39
 
33
40
  const DisabledPostTooltip = ( {
34
41
  children,
@@ -73,62 +80,88 @@ export default function ListItemView( { post }: { post: Post } ) {
73
80
  const isActive = activeDocument?.id === post.id;
74
81
  const status = isActive ? activeDocument?.status.value : post.status;
75
82
  const title = isActive ? activeDocument?.title : post.title.rendered;
76
- const isDisabled = ! post.user_can.edit;
83
+ const isTrashed = 'trash' === post.status;
84
+ const isEditBlocked = ! post.user_can.edit && ! isTrashed;
85
+ const isRowDisabled = isEditBlocked || isTrashed;
77
86
  const menuTriggerProps = bindTrigger( popupState );
87
+ const hasListItemActions = useSiteContentListItemActionsInjections().length > 0;
88
+ const showListItemActions = isActive && post.user_can.edit && ! isTrashed && hasListItemActions;
78
89
 
79
- return (
80
- <>
81
- <DisabledPostTooltip isDisabled={ isDisabled } postTypeSlug={ type }>
82
- <ListItem
83
- disablePadding
84
- sx={ contentPanelListRowPaddingSx }
85
- secondaryAction={
86
- <IconButton
87
- aria-label={ __( 'More actions', 'elementor' ) }
88
- size="tiny"
89
- { ...menuTriggerProps }
90
- onClick={ ( event: React.MouseEvent ) => {
91
- event.stopPropagation();
92
- menuTriggerProps.onClick?.( event );
93
- } }
94
- >
95
- <DotsVerticalIcon fontSize="tiny" />
96
- </IconButton>
97
- }
98
- >
99
- <ListItemButton
100
- dense
101
- disableGutters
102
- disabled={ isDisabled }
103
- onClick={ () => {
104
- if ( ! isActive ) {
105
- navigateToDocument( post.id );
106
- }
90
+ const listRow = (
91
+ <ListItem
92
+ disablePadding
93
+ sx={ getContentPanelListItemSx( showListItemActions ) }
94
+ secondaryAction={
95
+ <Stack alignItems="center" direction="row" spacing={ 0 }>
96
+ { showListItemActions && <SiteContentListItemActionsSlot post={ post } title={ title } /> }
97
+ <IconButton
98
+ aria-label={ __( 'More actions', 'elementor' ) }
99
+ size="tiny"
100
+ { ...menuTriggerProps }
101
+ onClick={ ( event: React.MouseEvent ) => {
102
+ event.stopPropagation();
103
+ menuTriggerProps.onClick?.( event );
107
104
  } }
108
- selected={ isActive }
109
- shape="rounded"
110
- sx={ contentPanelListItemButtonSx }
111
105
  >
112
- <ListItemText disableTypography sx={ { flex: 1, minWidth: 0 } }>
113
- <PageTitleAndStatus title={ title } status={ status } />
114
- </ListItemText>
115
- { post.isHome && <HomeIcon titleAccess={ __( 'Homepage', 'elementor' ) } color="disabled" /> }
116
- </ListItemButton>
117
- </ListItem>
118
- </DisabledPostTooltip>
106
+ <DotsVerticalIcon fontSize="tiny" />
107
+ </IconButton>
108
+ </Stack>
109
+ }
110
+ >
111
+ <ListItemButton
112
+ dense
113
+ disableGutters
114
+ disabled={ isRowDisabled }
115
+ onClick={ () => {
116
+ if ( isTrashed || isActive ) {
117
+ return;
118
+ }
119
+
120
+ navigateToDocument( post.id );
121
+ } }
122
+ selected={ isActive }
123
+ shape="rounded"
124
+ sx={ contentPanelListItemButtonSx }
125
+ >
126
+ <ListItemText disableTypography sx={ { flex: 1, minWidth: 0 } }>
127
+ <PageTitleAndStatus title={ title } status={ status } />
128
+ </ListItemText>
129
+ { post.isHome && <HomeIcon titleAccess={ __( 'Homepage', 'elementor' ) } color="disabled" /> }
130
+ </ListItemButton>
131
+ </ListItem>
132
+ );
133
+
134
+ return (
135
+ <>
136
+ { isEditBlocked ? (
137
+ <DisabledPostTooltip isDisabled postTypeSlug={ type }>
138
+ { listRow }
139
+ </DisabledPostTooltip>
140
+ ) : (
141
+ listRow
142
+ ) }
119
143
  <Menu
120
144
  PaperProps={ { sx: { mt: 2, width: 200 } } }
121
145
  MenuListProps={ { dense: true } }
122
146
  { ...bindMenu( popupState ) }
123
147
  >
124
- <Rename post={ post } />
125
- <Duplicate post={ post } popupState={ popupState } />
126
- <Delete post={ post } />
127
- <View post={ post } />
128
- { type === 'page' && (
148
+ { isTrashed ? (
149
+ <>
150
+ <Restore post={ post } />
151
+ <ForceDelete post={ post } />
152
+ </>
153
+ ) : (
129
154
  <>
130
- <Divider />
131
- <SetHome post={ post } closeMenu={ () => popupState.close() } />
155
+ <Rename post={ post } />
156
+ <Duplicate post={ post } popupState={ popupState } />
157
+ <Delete post={ post } />
158
+ <View post={ post } />
159
+ { type === 'page' && (
160
+ <>
161
+ <Divider />
162
+ <SetHome post={ post } closeMenu={ () => popupState.close() } />
163
+ </>
164
+ ) }
132
165
  </>
133
166
  ) }
134
167
  </Menu>
@@ -0,0 +1,14 @@
1
+ import { createLocation } from '@elementor/locations';
2
+
3
+ import { type Post } from '../../../../types';
4
+
5
+ export type SiteContentListItemActionsProps = {
6
+ post: Post;
7
+ title: string;
8
+ };
9
+
10
+ export const {
11
+ Slot: SiteContentListItemActionsSlot,
12
+ inject: injectIntoSiteContentListItemActions,
13
+ useInjections: useSiteContentListItemActionsInjections,
14
+ } = createLocation< SiteContentListItemActionsProps >();
@@ -9,6 +9,13 @@ import { usePostListContext } from '../../../contexts/post-list-context';
9
9
  import { useHomepage } from '../../../hooks/use-homepage';
10
10
  import { usePostListPagination } from '../../../hooks/use-post-list-pagination';
11
11
  import { usePosts } from '../../../hooks/use-posts';
12
+ import { usePostsBaselineTotal } from '../../../hooks/use-posts-baseline-total';
13
+ import {
14
+ hasActiveSiteContentStatusFilters,
15
+ isSiteContentListNarrowed,
16
+ isSiteContentTrashFilterActive,
17
+ serializeSiteContentStatusFilters,
18
+ } from '../../../site-content-status-filter';
12
19
  import SectionAddButton from '../section-add-button';
13
20
  import CollapsibleList from './collapsible-list';
14
21
  import ErrorState from './error-state';
@@ -22,8 +29,8 @@ type Props = {
22
29
  };
23
30
 
24
31
  export default function PostsCollapsibleList( { isOpenByDefault = false }: Props ) {
25
- const { type, editMode, setEditMode } = usePostListContext();
26
- const { registerSection, unregisterSection, searchQuery } = useContentPanelCoordinator();
32
+ const { type, editMode, resetEditMode, setEditMode } = usePostListContext();
33
+ const { registerSection, searchQuery, statusFilters, unregisterSection } = useContentPanelCoordinator();
27
34
  const [ isOpen, setIsOpen ] = useState( isOpenByDefault );
28
35
  const trimmedSearch = searchQuery.trim();
29
36
  const [ debouncedSearch, setDebouncedSearch ] = useState( trimmedSearch );
@@ -45,9 +52,20 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
45
52
  fetchNextPage,
46
53
  hasNextPage,
47
54
  isFetchingNextPage,
48
- } = usePosts( type, debouncedSearch );
55
+ } = usePosts( type, debouncedSearch, statusFilters );
56
+ const isNarrowed = isSiteContentListNarrowed( statusFilters, debouncedSearch );
57
+ const isTrashFilter = isSiteContentTrashFilterActive( statusFilters );
58
+ const baselineStatus = isTrashFilter ? 'trash' : 'any';
59
+ const { data: baselineTotal } = usePostsBaselineTotal( type, isNarrowed, baselineStatus );
49
60
  const { data: homepageId } = useHomepage();
50
61
  const isPageType = type === 'page';
62
+ const statusFiltersKey = serializeSiteContentStatusFilters( statusFilters );
63
+
64
+ useEffect( () => {
65
+ if ( isTrashFilter ) {
66
+ resetEditMode();
67
+ }
68
+ }, [ isTrashFilter, resetEditMode ] );
51
69
 
52
70
  useEffect( () => {
53
71
  registerSection( type, {
@@ -89,7 +107,7 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
89
107
  hasNextPage,
90
108
  isFetchingNextPage,
91
109
  loadedCount: filteredPosts.length,
92
- resetKey: `${ type }:${ debouncedSearch }`,
110
+ resetKey: `${ type }:${ debouncedSearch }:${ statusFiltersKey }`,
93
111
  totalCount: total,
94
112
  } );
95
113
 
@@ -127,10 +145,10 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
127
145
  }, [ pagination.canLoadMore, pagination.visibleCount ] );
128
146
 
129
147
  useEffect( () => {
130
- if ( debouncedSearch && filteredPosts.length > 0 ) {
148
+ if ( ( debouncedSearch || hasActiveSiteContentStatusFilters( statusFilters ) ) && filteredPosts.length > 0 ) {
131
149
  setIsOpen( true );
132
150
  }
133
- }, [ debouncedSearch, filteredPosts.length ] );
151
+ }, [ debouncedSearch, filteredPosts.length, statusFilters ] );
134
152
 
135
153
  if ( postsError ) {
136
154
  return <ErrorState />;
@@ -151,16 +169,19 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
151
169
 
152
170
  const displayCount = total;
153
171
  const sectionTitle = postTypesMap[ type ].labels.plural_name;
154
- const showNoResults = debouncedSearch && filteredPosts.length === 0;
172
+ const showNoResults =
173
+ ( debouncedSearch || hasActiveSiteContentStatusFilters( statusFilters ) ) && filteredPosts.length === 0;
174
+ const countChipLabel = isNarrowed && baselineTotal !== undefined ? `${ total } / ${ baselineTotal }` : undefined;
155
175
 
156
176
  return (
157
177
  <CollapsibleList
158
178
  count={ displayCount }
179
+ countChipLabel={ countChipLabel }
159
180
  headerRef={ sectionHeaderRef }
160
181
  isOpen={ isOpen }
161
182
  onOpenChange={ setIsOpen }
162
183
  title={ sectionTitle }
163
- trailingActions={ <SectionAddButton onBeforeCreate={ () => setIsOpen( true ) } /> }
184
+ trailingActions={ isTrashFilter ? null : <SectionAddButton onBeforeCreate={ () => setIsOpen( true ) } /> }
164
185
  >
165
186
  { showNoResults && (
166
187
  <Box sx={ { py: 2, px: 2 } }>
@@ -173,7 +194,7 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
173
194
  { displayedPosts.map( ( post ) => {
174
195
  return <PostListItem key={ post.id } post={ post } />;
175
196
  } ) }
176
- { [ 'duplicate', 'create' ].includes( editMode.mode ) && <PostListItem /> }
197
+ { ! isTrashFilter && [ 'duplicate', 'create' ].includes( editMode.mode ) && <PostListItem /> }
177
198
  <PostListPaginationActions
178
199
  canLoadMore={ pagination.canLoadMore }
179
200
  canShowLess={ pagination.canShowLess }
@@ -1,6 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { useState } from 'react';
3
3
  import { Panel, PanelBody, PanelHeader, PanelHeaderTitle } from '@elementor/editor-panels';
4
+ import { isSidebarEnabled } from '@elementor/editor-sidebar';
4
5
  import { Divider, Stack } from '@elementor/ui';
5
6
  import { __ } from '@wordpress/i18n';
6
7
 
@@ -8,11 +9,12 @@ import { ContentPanelCoordinatorProvider } from '../../contexts/content-panel-co
8
9
  import useAccessiblePanelPostTypes from '../../hooks/use-accessible-panel-post-types';
9
10
  import { contentPanelHeaderStackSx } from './content-panel-chrome';
10
11
  import ContentPanelHeaderAdd from './content-panel-header-add';
11
- import ContentPanelSearch from './content-panel-search';
12
+ import ContentPanelToolbar from './content-panel-toolbar';
12
13
  import ErrorSnackbar from './error-snackbar';
13
14
  import ShellPanelBodyContent from './shell-panel-body-content';
14
15
 
15
16
  const Shell = () => {
17
+ const sidebarPanelChrome = isSidebarEnabled();
16
18
  const [ isErrorSnackbarOpen, setIsErrorSnackbarOpen ] = useState( false );
17
19
  const { accessiblePostTypes, isUserPending, showUserLoadError } = useAccessiblePanelPostTypes();
18
20
 
@@ -26,6 +28,7 @@ const Shell = () => {
26
28
  spacing={ 0.5 }
27
29
  sx={ {
28
30
  ...contentPanelHeaderStackSx,
31
+ ...( sidebarPanelChrome ? {} : { p: 1, pl: 2 } ),
29
32
  } }
30
33
  >
31
34
  <PanelHeaderTitle sx={ { flex: 1, minWidth: 0, textAlign: 'start' } }>
@@ -36,7 +39,8 @@ const Shell = () => {
36
39
  </PanelHeader>
37
40
  <Divider />
38
41
  <PanelBody>
39
- <ContentPanelSearch />
42
+ <ContentPanelToolbar />
43
+ <Divider />
40
44
  <ShellPanelBodyContent
41
45
  accessiblePostTypes={ accessiblePostTypes }
42
46
  isUserPending={ isUserPending }
@@ -92,7 +92,9 @@ const PageTitle = ( { title }: { title: string } ) => {
92
92
  noWrap
93
93
  sx={ {
94
94
  ...contentPanelListRowTitleSx,
95
+ flex: 1,
95
96
  flexBasis: 'auto',
97
+ minWidth: 0,
96
98
  } }
97
99
  >
98
100
  { modifiedTitle }
@@ -2,6 +2,12 @@ import * as React from 'react';
2
2
  import { createContext, type ReactNode, useCallback, useContext, useMemo, useRef, useState } from 'react';
3
3
 
4
4
  import { type Slug } from '../api/post';
5
+ import {
6
+ EMPTY_SITE_CONTENT_STATUS_FILTERS,
7
+ type SiteContentSelectableStatus,
8
+ type SiteContentStatusFilters,
9
+ toggleSiteContentStatusFilter,
10
+ } from '../site-content-status-filter';
5
11
 
6
12
  type SectionHandlers = {
7
13
  setExpanded: ( expanded: boolean ) => void;
@@ -9,11 +15,14 @@ type SectionHandlers = {
9
15
  };
10
16
 
11
17
  type ContentPanelCoordinatorValue = {
18
+ clearStatusFilters: () => void;
12
19
  registerSection: ( type: Slug, handlers: SectionHandlers ) => void;
13
- unregisterSection: ( type: Slug ) => void;
14
20
  requestCreate: ( type: Slug ) => void;
15
21
  searchQuery: string;
16
22
  setSearchQuery: ( query: string ) => void;
23
+ statusFilters: SiteContentStatusFilters;
24
+ toggleStatusFilter: ( filter: SiteContentSelectableStatus ) => void;
25
+ unregisterSection: ( type: Slug ) => void;
17
26
  };
18
27
 
19
28
  const ContentPanelCoordinatorContext = createContext< ContentPanelCoordinatorValue | null >( null );
@@ -21,6 +30,9 @@ const ContentPanelCoordinatorContext = createContext< ContentPanelCoordinatorVal
21
30
  export function ContentPanelCoordinatorProvider( { children }: { children: ReactNode } ) {
22
31
  const sectionsRef = useRef( new Map< Slug, SectionHandlers >() );
23
32
  const [ searchQuery, setSearchQuery ] = useState( '' );
33
+ const [ statusFilters, setStatusFilters ] = useState< SiteContentStatusFilters >(
34
+ EMPTY_SITE_CONTENT_STATUS_FILTERS
35
+ );
24
36
 
25
37
  const registerSection = useCallback( ( type: Slug, handlers: SectionHandlers ) => {
26
38
  sectionsRef.current.set( type, handlers );
@@ -41,15 +53,34 @@ export function ContentPanelCoordinatorProvider( { children }: { children: React
41
53
  handlers.startCreate();
42
54
  }, [] );
43
55
 
56
+ const clearStatusFilters = useCallback( () => {
57
+ setStatusFilters( EMPTY_SITE_CONTENT_STATUS_FILTERS );
58
+ }, [] );
59
+
60
+ const toggleStatusFilter = useCallback( ( filter: SiteContentSelectableStatus ) => {
61
+ setStatusFilters( ( current ) => toggleSiteContentStatusFilter( current, filter ) );
62
+ }, [] );
63
+
44
64
  const value = useMemo(
45
65
  () => ( {
66
+ clearStatusFilters,
46
67
  registerSection,
47
- unregisterSection,
48
68
  requestCreate,
49
69
  searchQuery,
50
70
  setSearchQuery,
71
+ statusFilters,
72
+ toggleStatusFilter,
73
+ unregisterSection,
51
74
  } ),
52
- [ registerSection, unregisterSection, requestCreate, searchQuery ]
75
+ [
76
+ clearStatusFilters,
77
+ registerSection,
78
+ requestCreate,
79
+ searchQuery,
80
+ statusFilters,
81
+ toggleStatusFilter,
82
+ unregisterSection,
83
+ ]
53
84
  );
54
85
 
55
86
  return (
@@ -9,8 +9,11 @@ import {
9
9
  type UpdatePost,
10
10
  updateRequest,
11
11
  } from '../api/post';
12
+ import { SITE_CONTENT_V1_RESTORE_STATUS } from '../site-content-restore';
12
13
  import { postsQueryKeyBase } from './use-posts';
14
+ import { postsBaselineTotalQueryKeyBase } from './use-posts-baseline-total';
13
15
  import { recentPostsQueryKey } from './use-recent-posts';
16
+ import { siteContentStatusFilterCountsQueryKeyBase } from './use-site-content-status-filter-counts';
14
17
 
15
18
  export function usePostActions( postTypeSlug: Slug ) {
16
19
  const invalidatePosts = useInvalidatePosts( postTypeSlug );
@@ -32,6 +35,17 @@ export function usePostActions( postTypeSlug: Slug ) {
32
35
  onSuccess,
33
36
  } );
34
37
 
38
+ const forceDeletePost = useMutation( {
39
+ mutationFn: ( postId: number ) => deleteRequest( postTypeSlug, postId, true ),
40
+ onSuccess,
41
+ } );
42
+
43
+ const restorePost = useMutation( {
44
+ mutationFn: ( postId: number ) =>
45
+ updateRequest( postTypeSlug, { id: postId, status: SITE_CONTENT_V1_RESTORE_STATUS } ),
46
+ onSuccess,
47
+ } );
48
+
35
49
  const duplicatePost = useMutation( {
36
50
  mutationFn: ( originalPost: UpdatePost ) => duplicateRequest( originalPost ),
37
51
  onSuccess,
@@ -39,9 +53,11 @@ export function usePostActions( postTypeSlug: Slug ) {
39
53
 
40
54
  return {
41
55
  createPost,
42
- updatePost,
43
56
  deletePost,
44
57
  duplicatePost,
58
+ forceDeletePost,
59
+ restorePost,
60
+ updatePost,
45
61
  };
46
62
  }
47
63
 
@@ -51,6 +67,9 @@ function useInvalidatePosts( postTypeSlug: string ) {
51
67
  return () => {
52
68
  const queryKey = postsQueryKeyBase( postTypeSlug );
53
69
  queryClient.invalidateQueries( { queryKey: recentPostsQueryKey } );
70
+ queryClient.invalidateQueries( { queryKey: postsBaselineTotalQueryKeyBase( postTypeSlug ) } );
71
+ queryClient.invalidateQueries( { queryKey: siteContentStatusFilterCountsQueryKeyBase } );
72
+
54
73
  return queryClient.invalidateQueries( { queryKey } );
55
74
  };
56
75
  }
@@ -0,0 +1,22 @@
1
+ import { useQuery } from '@elementor/query';
2
+
3
+ import { fetchPostsTotal, type Slug } from '../api/post';
4
+
5
+ export const postsBaselineTotalQueryKeyBase = ( postTypeSlug: string ) => [
6
+ 'site-navigation',
7
+ 'posts-baseline-total',
8
+ postTypeSlug,
9
+ ];
10
+
11
+ export const postsBaselineTotalQueryKey = ( postTypeSlug: string, status = 'any' ) => [
12
+ ...postsBaselineTotalQueryKeyBase( postTypeSlug ),
13
+ status,
14
+ ];
15
+
16
+ export function usePostsBaselineTotal( postTypeSlug: Slug, enabled: boolean, status = 'any' ) {
17
+ return useQuery( {
18
+ enabled,
19
+ queryFn: () => fetchPostsTotal( postTypeSlug, status ),
20
+ queryKey: postsBaselineTotalQueryKey( postTypeSlug, status ),
21
+ } );
22
+ }
@@ -1,19 +1,20 @@
1
1
  import { keepPreviousData, useInfiniteQuery } from '@elementor/query';
2
2
 
3
3
  import { getRequest, type PostsResponse, type Slug } from '../api/post';
4
+ import {
5
+ EMPTY_SITE_CONTENT_STATUS_FILTERS,
6
+ getRestStatusForSiteContentFilters,
7
+ type SiteContentStatusFilters,
8
+ } from '../site-content-status-filter';
4
9
  import { type Post } from '../types';
5
10
 
6
11
  export const postsQueryKeyBase = ( postTypeSlug: string ) => [ 'site-navigation', 'posts', postTypeSlug ];
7
12
 
8
- export const postsQueryKey = ( postTypeSlug: string, search = '' ) => {
9
- const trimmedSearch = search.trim();
10
-
11
- if ( ! trimmedSearch ) {
12
- return postsQueryKeyBase( postTypeSlug );
13
- }
14
-
15
- return [ ...postsQueryKeyBase( postTypeSlug ), trimmedSearch ];
16
- };
13
+ export const postsQueryKey = ( postTypeSlug: string, trimmedSearch = '', restStatus = 'any' ) => [
14
+ ...postsQueryKeyBase( postTypeSlug ),
15
+ restStatus,
16
+ trimmedSearch,
17
+ ];
17
18
 
18
19
  const flattenData = ( data?: { pages: PostsResponse[] } ) => {
19
20
  if ( ! data ) {
@@ -29,14 +30,19 @@ const flattenData = ( data?: { pages: PostsResponse[] } ) => {
29
30
  return flattened;
30
31
  };
31
32
 
32
- export function usePosts( postTypeSlug: Slug, search = '' ) {
33
+ export function usePosts(
34
+ postTypeSlug: Slug,
35
+ search = '',
36
+ statusFilters: SiteContentStatusFilters = EMPTY_SITE_CONTENT_STATUS_FILTERS
37
+ ) {
33
38
  const trimmedSearch = search.trim();
39
+ const restStatus = getRestStatusForSiteContentFilters( statusFilters );
34
40
 
35
41
  const query = useInfiniteQuery( {
36
- queryKey: postsQueryKey( postTypeSlug, trimmedSearch ),
37
- queryFn: ( { pageParam = 1 } ) => getRequest( postTypeSlug, pageParam, trimmedSearch ),
42
+ queryKey: postsQueryKey( postTypeSlug, trimmedSearch, restStatus ),
43
+ queryFn: ( { pageParam = 1 } ) => getRequest( postTypeSlug, pageParam as number, trimmedSearch, restStatus ),
38
44
  initialPageParam: 1,
39
- getNextPageParam: ( lastPage ) => {
45
+ getNextPageParam: ( lastPage: PostsResponse ) => {
40
46
  return lastPage.currentPage < lastPage.totalPages ? lastPage.currentPage + 1 : undefined;
41
47
  },
42
48
  placeholderData: keepPreviousData,
@@ -0,0 +1,56 @@
1
+ import { useQuery } from '@elementor/query';
2
+
3
+ import { fetchPostsTotal, type Slug } from '../api/post';
4
+ import {
5
+ SITE_CONTENT_SELECTABLE_STATUS_FILTERS,
6
+ type SiteContentSelectableStatus,
7
+ } from '../site-content-status-filter';
8
+
9
+ const STATUS_FILTER_COUNTS_STALE_TIME_MS = 30 * 1000;
10
+
11
+ export const siteContentStatusFilterCountsQueryKeyBase = [ 'site-navigation', 'status-filter-counts' ];
12
+
13
+ export const siteContentStatusFilterCountsQueryKey = ( postTypes: Slug[] ) => [
14
+ ...siteContentStatusFilterCountsQueryKeyBase,
15
+ postTypes.join( ',' ),
16
+ ];
17
+
18
+ const sumFulfilledTotals = ( results: PromiseSettledResult< number >[] ): number | undefined => {
19
+ const fulfilled = results.filter(
20
+ ( result ): result is PromiseFulfilledResult< number > => 'fulfilled' === result.status
21
+ );
22
+
23
+ if ( fulfilled.length !== results.length ) {
24
+ return undefined;
25
+ }
26
+
27
+ return fulfilled.reduce( ( sum, result ) => sum + result.value, 0 );
28
+ };
29
+
30
+ const fetchStatusFilterCounts = async (
31
+ postTypes: Slug[]
32
+ ): Promise< Partial< Record< SiteContentSelectableStatus, number > > > => {
33
+ const entries = await Promise.all(
34
+ SITE_CONTENT_SELECTABLE_STATUS_FILTERS.map( async ( status ) => {
35
+ const results = await Promise.allSettled( postTypes.map( ( slug ) => fetchPostsTotal( slug, status ) ) );
36
+ const total = sumFulfilledTotals( results );
37
+
38
+ if ( undefined === total ) {
39
+ return null;
40
+ }
41
+
42
+ return [ status, total ] as const;
43
+ } )
44
+ );
45
+
46
+ return Object.fromEntries( entries.filter( ( entry ) => null !== entry ) );
47
+ };
48
+
49
+ export function useSiteContentStatusFilterCounts( postTypes: Slug[], enabled: boolean ) {
50
+ return useQuery( {
51
+ enabled: enabled && postTypes.length > 0,
52
+ queryFn: () => fetchStatusFilterCounts( postTypes ),
53
+ queryKey: siteContentStatusFilterCountsQueryKey( postTypes ),
54
+ staleTime: STATUS_FILTER_COUNTS_STALE_TIME_MS,
55
+ } );
56
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,8 @@
1
1
  export { extendIconsMap } from './icons-map';
2
2
 
3
3
  export { init } from './init';
4
+ export {
5
+ injectIntoSiteContentListItemActions,
6
+ SiteContentListItemActionsSlot,
7
+ type SiteContentListItemActionsProps,
8
+ } from './components/panel/posts-list/list-items/site-content-list-item-actions-location';
@@ -0,0 +1 @@
1
+ export const SITE_CONTENT_V1_RESTORE_STATUS = 'draft' as const;