@elementor/editor-site-navigation 4.5.0-1216 → 4.5.0-1217
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.
- package/dist/index.js +787 -336
- package/dist/index.mjs +741 -281
- package/package.json +10 -10
- package/src/api/post.ts +21 -5
- package/src/components/panel/actions-menu/action-menu-item.tsx +4 -3
- package/src/components/panel/actions-menu/actions/force-delete.tsx +99 -0
- package/src/components/panel/actions-menu/actions/restore.tsx +33 -0
- package/src/components/panel/content-panel-active-filters.tsx +53 -0
- package/src/components/panel/content-panel-clear-filters-button.tsx +22 -0
- package/src/components/panel/content-panel-search.tsx +1 -2
- package/src/components/panel/content-panel-status-filter-list.tsx +70 -0
- package/src/components/panel/content-panel-status-filter.tsx +75 -0
- package/src/components/panel/content-panel-toolbar.tsx +23 -0
- package/src/components/panel/posts-list/collapsible-list.tsx +7 -4
- package/src/components/panel/posts-list/list-items/list-item-view.tsx +69 -46
- package/src/components/panel/posts-list/posts-collapsible-list.tsx +30 -9
- package/src/components/panel/shell.tsx +3 -2
- package/src/contexts/content-panel-coordinator.tsx +34 -3
- package/src/hooks/use-posts-actions.ts +20 -1
- package/src/hooks/use-posts-baseline-total.ts +22 -0
- package/src/hooks/use-posts.ts +19 -13
- package/src/hooks/use-site-content-status-filter-counts.ts +56 -0
- package/src/site-content-restore.ts +1 -0
- package/src/site-content-status-filter.ts +111 -0
|
@@ -25,7 +25,9 @@ import { type Post } from '../../../../types';
|
|
|
25
25
|
import PageTitleAndStatus from '../../../shared/page-title-and-status';
|
|
26
26
|
import Delete from '../../actions-menu/actions/delete';
|
|
27
27
|
import Duplicate from '../../actions-menu/actions/duplicate';
|
|
28
|
+
import ForceDelete from '../../actions-menu/actions/force-delete';
|
|
28
29
|
import Rename from '../../actions-menu/actions/rename';
|
|
30
|
+
import Restore from '../../actions-menu/actions/restore';
|
|
29
31
|
import SetHome from '../../actions-menu/actions/set-home';
|
|
30
32
|
import View from '../../actions-menu/actions/view';
|
|
31
33
|
import { contentPanelListItemButtonSx, contentPanelListRowPaddingSx } from '../../content-panel-chrome';
|
|
@@ -73,62 +75,83 @@ export default function ListItemView( { post }: { post: Post } ) {
|
|
|
73
75
|
const isActive = activeDocument?.id === post.id;
|
|
74
76
|
const status = isActive ? activeDocument?.status.value : post.status;
|
|
75
77
|
const title = isActive ? activeDocument?.title : post.title.rendered;
|
|
76
|
-
const
|
|
78
|
+
const isTrashed = 'trash' === post.status;
|
|
79
|
+
const isEditBlocked = ! post.user_can.edit && ! isTrashed;
|
|
80
|
+
const isRowDisabled = isEditBlocked || isTrashed;
|
|
77
81
|
const menuTriggerProps = bindTrigger( popupState );
|
|
78
82
|
|
|
83
|
+
const listRow = (
|
|
84
|
+
<ListItem
|
|
85
|
+
disablePadding
|
|
86
|
+
sx={ contentPanelListRowPaddingSx }
|
|
87
|
+
secondaryAction={
|
|
88
|
+
<IconButton
|
|
89
|
+
aria-label={ __( 'More actions', 'elementor' ) }
|
|
90
|
+
size="tiny"
|
|
91
|
+
{ ...menuTriggerProps }
|
|
92
|
+
onClick={ ( event: React.MouseEvent ) => {
|
|
93
|
+
event.stopPropagation();
|
|
94
|
+
menuTriggerProps.onClick?.( event );
|
|
95
|
+
} }
|
|
96
|
+
>
|
|
97
|
+
<DotsVerticalIcon fontSize="tiny" />
|
|
98
|
+
</IconButton>
|
|
99
|
+
}
|
|
100
|
+
>
|
|
101
|
+
<ListItemButton
|
|
102
|
+
dense
|
|
103
|
+
disableGutters
|
|
104
|
+
disabled={ isRowDisabled }
|
|
105
|
+
onClick={ () => {
|
|
106
|
+
if ( isTrashed || isActive ) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
navigateToDocument( post.id );
|
|
111
|
+
} }
|
|
112
|
+
selected={ isActive }
|
|
113
|
+
shape="rounded"
|
|
114
|
+
sx={ contentPanelListItemButtonSx }
|
|
115
|
+
>
|
|
116
|
+
<ListItemText disableTypography sx={ { flex: 1, minWidth: 0 } }>
|
|
117
|
+
<PageTitleAndStatus title={ title } status={ status } />
|
|
118
|
+
</ListItemText>
|
|
119
|
+
{ post.isHome && <HomeIcon titleAccess={ __( 'Homepage', 'elementor' ) } color="disabled" /> }
|
|
120
|
+
</ListItemButton>
|
|
121
|
+
</ListItem>
|
|
122
|
+
);
|
|
123
|
+
|
|
79
124
|
return (
|
|
80
125
|
<>
|
|
81
|
-
|
|
82
|
-
<
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
}
|
|
107
|
-
} }
|
|
108
|
-
selected={ isActive }
|
|
109
|
-
shape="rounded"
|
|
110
|
-
sx={ contentPanelListItemButtonSx }
|
|
111
|
-
>
|
|
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>
|
|
126
|
+
{ isEditBlocked ? (
|
|
127
|
+
<DisabledPostTooltip isDisabled postTypeSlug={ type }>
|
|
128
|
+
{ listRow }
|
|
129
|
+
</DisabledPostTooltip>
|
|
130
|
+
) : (
|
|
131
|
+
listRow
|
|
132
|
+
) }
|
|
119
133
|
<Menu
|
|
120
134
|
PaperProps={ { sx: { mt: 2, width: 200 } } }
|
|
121
135
|
MenuListProps={ { dense: true } }
|
|
122
136
|
{ ...bindMenu( popupState ) }
|
|
123
137
|
>
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
138
|
+
{ isTrashed ? (
|
|
139
|
+
<>
|
|
140
|
+
<Restore post={ post } />
|
|
141
|
+
<ForceDelete post={ post } />
|
|
142
|
+
</>
|
|
143
|
+
) : (
|
|
129
144
|
<>
|
|
130
|
-
<
|
|
131
|
-
<
|
|
145
|
+
<Rename post={ post } />
|
|
146
|
+
<Duplicate post={ post } popupState={ popupState } />
|
|
147
|
+
<Delete post={ post } />
|
|
148
|
+
<View post={ post } />
|
|
149
|
+
{ type === 'page' && (
|
|
150
|
+
<>
|
|
151
|
+
<Divider />
|
|
152
|
+
<SetHome post={ post } closeMenu={ () => popupState.close() } />
|
|
153
|
+
</>
|
|
154
|
+
) }
|
|
132
155
|
</>
|
|
133
156
|
) }
|
|
134
157
|
</Menu>
|
|
@@ -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,
|
|
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 =
|
|
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 }
|
|
@@ -8,7 +8,7 @@ import { ContentPanelCoordinatorProvider } from '../../contexts/content-panel-co
|
|
|
8
8
|
import useAccessiblePanelPostTypes from '../../hooks/use-accessible-panel-post-types';
|
|
9
9
|
import { contentPanelHeaderStackSx } from './content-panel-chrome';
|
|
10
10
|
import ContentPanelHeaderAdd from './content-panel-header-add';
|
|
11
|
-
import
|
|
11
|
+
import ContentPanelToolbar from './content-panel-toolbar';
|
|
12
12
|
import ErrorSnackbar from './error-snackbar';
|
|
13
13
|
import ShellPanelBodyContent from './shell-panel-body-content';
|
|
14
14
|
|
|
@@ -36,7 +36,8 @@ const Shell = () => {
|
|
|
36
36
|
</PanelHeader>
|
|
37
37
|
<Divider />
|
|
38
38
|
<PanelBody>
|
|
39
|
-
<
|
|
39
|
+
<ContentPanelToolbar />
|
|
40
|
+
<Divider />
|
|
40
41
|
<ShellPanelBodyContent
|
|
41
42
|
accessiblePostTypes={ accessiblePostTypes }
|
|
42
43
|
isUserPending={ isUserPending }
|
|
@@ -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
|
-
[
|
|
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
|
+
}
|
package/src/hooks/use-posts.ts
CHANGED
|
@@ -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,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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(
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SITE_CONTENT_V1_RESTORE_STATUS = 'draft' as const;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { __ } from '@wordpress/i18n';
|
|
2
|
+
|
|
3
|
+
export type SiteContentSelectableStatus = 'draft' | 'future' | 'pending' | 'private' | 'publish' | 'trash';
|
|
4
|
+
|
|
5
|
+
export type SiteContentStatusFilters = Record< SiteContentSelectableStatus, boolean >;
|
|
6
|
+
|
|
7
|
+
export const SITE_CONTENT_SELECTABLE_STATUS_FILTERS: SiteContentSelectableStatus[] = [
|
|
8
|
+
'publish',
|
|
9
|
+
'draft',
|
|
10
|
+
'pending',
|
|
11
|
+
'private',
|
|
12
|
+
'future',
|
|
13
|
+
'trash',
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
export const EMPTY_SITE_CONTENT_STATUS_FILTERS: SiteContentStatusFilters = {
|
|
17
|
+
draft: false,
|
|
18
|
+
future: false,
|
|
19
|
+
pending: false,
|
|
20
|
+
private: false,
|
|
21
|
+
publish: false,
|
|
22
|
+
trash: false,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const filterLabels: Record< SiteContentSelectableStatus, string > = {
|
|
26
|
+
publish: __( 'Published', 'elementor' ),
|
|
27
|
+
draft: __( 'Drafts', 'elementor' ),
|
|
28
|
+
pending: __( 'Pending', 'elementor' ),
|
|
29
|
+
private: __( 'Private', 'elementor' ),
|
|
30
|
+
future: __( 'Scheduled', 'elementor' ),
|
|
31
|
+
trash: __( 'Trash', 'elementor' ),
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const SITE_CONTENT_ALL_FILTER_LABEL = __( 'All', 'elementor' );
|
|
35
|
+
|
|
36
|
+
export const SITE_CONTENT_FILTER_POPOVER_WIDTH_PX = 280;
|
|
37
|
+
|
|
38
|
+
export function getSiteContentStatusFilterLabel( filter: SiteContentSelectableStatus ): string {
|
|
39
|
+
return filterLabels[ filter ];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function hasActiveSiteContentStatusFilters( filters: SiteContentStatusFilters ): boolean {
|
|
43
|
+
return SITE_CONTENT_SELECTABLE_STATUS_FILTERS.some( ( key ) => filters[ key ] );
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function getActiveSiteContentStatusFilterKeys(
|
|
47
|
+
filters: SiteContentStatusFilters
|
|
48
|
+
): SiteContentSelectableStatus[] {
|
|
49
|
+
return SITE_CONTENT_SELECTABLE_STATUS_FILTERS.filter( ( key ) => filters[ key ] );
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const serializeSiteContentStatusFilters = ( statusFilters: SiteContentStatusFilters ): string =>
|
|
53
|
+
SITE_CONTENT_SELECTABLE_STATUS_FILTERS.map( ( key ) => ( statusFilters[ key ] ? key : '' ) ).join( '|' );
|
|
54
|
+
|
|
55
|
+
export function getRestStatusForSiteContentFilters( filters: SiteContentStatusFilters ): string {
|
|
56
|
+
if ( filters.trash ) {
|
|
57
|
+
return 'trash';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const active = getActiveSiteContentStatusFilterKeys( filters );
|
|
61
|
+
|
|
62
|
+
if ( 0 === active.length ) {
|
|
63
|
+
return 'any';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return active.join( ',' );
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function isSiteContentTrashFilterActive( filters: SiteContentStatusFilters ): boolean {
|
|
70
|
+
return filters.trash;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function isSiteContentListNarrowed( filters: SiteContentStatusFilters, search: string ): boolean {
|
|
74
|
+
return hasActiveSiteContentStatusFilters( filters ) || search.trim().length > 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function toggleSiteContentStatusFilter(
|
|
78
|
+
filters: SiteContentStatusFilters,
|
|
79
|
+
filter: SiteContentSelectableStatus
|
|
80
|
+
): SiteContentStatusFilters {
|
|
81
|
+
if ( 'trash' === filter ) {
|
|
82
|
+
const isTrashOn = ! filters.trash;
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
...EMPTY_SITE_CONTENT_STATUS_FILTERS,
|
|
86
|
+
trash: isTrashOn,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
...filters,
|
|
92
|
+
trash: false,
|
|
93
|
+
[ filter ]: ! filters[ filter ],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function shouldShowSiteContentStatusFilterRow(
|
|
98
|
+
count: number | undefined,
|
|
99
|
+
isLoadingCounts: boolean,
|
|
100
|
+
isSelected: boolean
|
|
101
|
+
): boolean {
|
|
102
|
+
if ( isSelected ) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if ( isLoadingCounts || undefined === count ) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return count > 0;
|
|
111
|
+
}
|