@elementor/editor-site-navigation 4.5.0-1220 → 4.5.0-1222
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 +492 -344
- package/dist/index.mjs +438 -290
- package/package.json +11 -11
- package/src/api/post.ts +6 -2
- package/src/components/panel/content-panel-active-filters.tsx +16 -3
- package/src/components/panel/content-panel-sort-list.tsx +39 -0
- package/src/components/panel/content-panel-sort.tsx +72 -0
- package/src/components/panel/content-panel-toolbar.tsx +2 -0
- package/src/components/panel/posts-list/posts-collapsible-list.tsx +9 -5
- package/src/contexts/content-panel-coordinator.tsx +27 -0
- package/src/hooks/use-posts.ts +22 -10
- package/src/site-content-sort.ts +39 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/editor-site-navigation",
|
|
3
|
-
"version": "4.5.0-
|
|
3
|
+
"version": "4.5.0-1222",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "Elementor Team",
|
|
6
6
|
"homepage": "https://elementor.com/",
|
|
@@ -39,17 +39,17 @@
|
|
|
39
39
|
"dev": "tsup --config=../../tsup.dev.ts"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@elementor/editor-app-bar": "4.5.0-
|
|
43
|
-
"@elementor/editor-documents": "4.5.0-
|
|
44
|
-
"@elementor/editor-panels": "4.5.0-
|
|
45
|
-
"@elementor/editor-sidebar": "4.5.0-
|
|
46
|
-
"@elementor/editor-ui": "4.5.0-
|
|
47
|
-
"@elementor/editor-v1-adapters": "4.5.0-
|
|
48
|
-
"@elementor/env": "4.5.0-
|
|
49
|
-
"@elementor/events": "4.5.0-
|
|
42
|
+
"@elementor/editor-app-bar": "4.5.0-1222",
|
|
43
|
+
"@elementor/editor-documents": "4.5.0-1222",
|
|
44
|
+
"@elementor/editor-panels": "4.5.0-1222",
|
|
45
|
+
"@elementor/editor-sidebar": "4.5.0-1222",
|
|
46
|
+
"@elementor/editor-ui": "4.5.0-1222",
|
|
47
|
+
"@elementor/editor-v1-adapters": "4.5.0-1222",
|
|
48
|
+
"@elementor/env": "4.5.0-1222",
|
|
49
|
+
"@elementor/events": "4.5.0-1222",
|
|
50
50
|
"@elementor/icons": "~1.78.1",
|
|
51
|
-
"@elementor/locations": "4.5.0-
|
|
52
|
-
"@elementor/query": "4.5.0-
|
|
51
|
+
"@elementor/locations": "4.5.0-1222",
|
|
52
|
+
"@elementor/query": "4.5.0-1222",
|
|
53
53
|
"@elementor/ui": "1.41.4",
|
|
54
54
|
"@wordpress/api-fetch": "^6.42.0",
|
|
55
55
|
"@wordpress/i18n": "^5.13.0"
|
package/src/api/post.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import apiFetch from '@wordpress/api-fetch';
|
|
2
2
|
import { __ } from '@wordpress/i18n';
|
|
3
3
|
|
|
4
|
+
import { DEFAULT_SITE_CONTENT_SORT, getRestOrderForSiteContentSort } from '../site-content-sort';
|
|
4
5
|
import { type Post } from '../types';
|
|
5
6
|
|
|
6
7
|
export type NewPost = {
|
|
@@ -80,15 +81,18 @@ export const getRequest = async (
|
|
|
80
81
|
page: number,
|
|
81
82
|
search = '',
|
|
82
83
|
status: string = 'any',
|
|
83
|
-
perPage = POST_PER_PAGE
|
|
84
|
+
perPage = POST_PER_PAGE,
|
|
85
|
+
sort = DEFAULT_SITE_CONTENT_SORT
|
|
84
86
|
): Promise< PostsResponse > => {
|
|
85
87
|
const baseUri = `/wp/v2/${ postTypesMap[ postTypeSlug ].rest_base }`;
|
|
86
88
|
|
|
87
89
|
const keys: Array< keyof Post > = [ 'id', 'type', 'title', 'link', 'status', 'user_can' ];
|
|
90
|
+
const { order, orderby } = getRestOrderForSiteContentSort( sort );
|
|
88
91
|
|
|
89
92
|
const queryParams = new URLSearchParams( {
|
|
90
93
|
status,
|
|
91
|
-
order
|
|
94
|
+
order,
|
|
95
|
+
orderby,
|
|
92
96
|
page: page.toString(),
|
|
93
97
|
per_page: perPage.toString(),
|
|
94
98
|
_fields: keys.join( ',' ),
|
|
@@ -3,6 +3,7 @@ import { Chip, Stack } from '@elementor/ui';
|
|
|
3
3
|
import { __ } from '@wordpress/i18n';
|
|
4
4
|
|
|
5
5
|
import { useContentPanelCoordinator } from '../../contexts/content-panel-coordinator';
|
|
6
|
+
import { DEFAULT_SITE_CONTENT_SORT, getSiteContentSortLabel, hasActiveSiteContentSort } from '../../site-content-sort';
|
|
6
7
|
import {
|
|
7
8
|
getActiveSiteContentStatusFilterKeys,
|
|
8
9
|
getSiteContentStatusFilterLabel,
|
|
@@ -21,9 +22,13 @@ const contentPanelFilterChipSx = {
|
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
export default function ContentPanelActiveFilters() {
|
|
24
|
-
const {
|
|
25
|
+
const { clearActiveToolbarFilters, setSiteContentSort, siteContentSort, statusFilters, toggleStatusFilter } =
|
|
26
|
+
useContentPanelCoordinator();
|
|
25
27
|
|
|
26
|
-
|
|
28
|
+
const hasStatusFilters = hasActiveSiteContentStatusFilters( statusFilters );
|
|
29
|
+
const hasSort = hasActiveSiteContentSort( siteContentSort );
|
|
30
|
+
|
|
31
|
+
if ( ! hasStatusFilters && ! hasSort ) {
|
|
27
32
|
return null;
|
|
28
33
|
}
|
|
29
34
|
|
|
@@ -32,6 +37,14 @@ export default function ContentPanelActiveFilters() {
|
|
|
32
37
|
return (
|
|
33
38
|
<Stack alignItems="center" direction="row" justifyContent="space-between">
|
|
34
39
|
<Stack alignItems="center" direction="row" flexWrap="wrap" gap={ 0.5 }>
|
|
40
|
+
{ hasSort && (
|
|
41
|
+
<Chip
|
|
42
|
+
label={ getSiteContentSortLabel( siteContentSort ) }
|
|
43
|
+
onDelete={ () => setSiteContentSort( DEFAULT_SITE_CONTENT_SORT ) }
|
|
44
|
+
size="tiny"
|
|
45
|
+
sx={ contentPanelFilterChipSx }
|
|
46
|
+
/>
|
|
47
|
+
) }
|
|
35
48
|
{ activeKeys.map( ( key ) => (
|
|
36
49
|
<Chip
|
|
37
50
|
key={ key }
|
|
@@ -44,7 +57,7 @@ export default function ContentPanelActiveFilters() {
|
|
|
44
57
|
</Stack>
|
|
45
58
|
<ContentPanelClearFiltersButton
|
|
46
59
|
ariaLabel={ __( 'Clear Filters', 'elementor' ) }
|
|
47
|
-
onClear={
|
|
60
|
+
onClear={ clearActiveToolbarFilters }
|
|
48
61
|
sx={ { margin: '0 0 auto auto' } }
|
|
49
62
|
tooltipText={ __( 'Clear Filters', 'elementor' ) }
|
|
50
63
|
/>
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { CheckIcon } from '@elementor/icons';
|
|
3
|
+
import { MenuItem, MenuList, Stack, Typography } from '@elementor/ui';
|
|
4
|
+
|
|
5
|
+
import { useContentPanelCoordinator } from '../../contexts/content-panel-coordinator';
|
|
6
|
+
import { getSiteContentSortLabel, SITE_CONTENT_SORT_OPTIONS } from '../../site-content-sort';
|
|
7
|
+
|
|
8
|
+
const renderSortMenuItemContent = ( label: string, selected: boolean ) => (
|
|
9
|
+
<Stack alignItems="center" direction="row" gap={ 1 } width="100%">
|
|
10
|
+
<Typography sx={ { color: 'text.secondary', flex: 1 } } variant="caption">
|
|
11
|
+
{ label }
|
|
12
|
+
</Typography>
|
|
13
|
+
{ selected ? <CheckIcon aria-hidden fontSize="tiny" /> : null }
|
|
14
|
+
</Stack>
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
export default function ContentPanelSortList() {
|
|
18
|
+
const { setSiteContentSort, siteContentSort } = useContentPanelCoordinator();
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<MenuList dense>
|
|
22
|
+
{ SITE_CONTENT_SORT_OPTIONS.map( ( option ) => {
|
|
23
|
+
const isSelected = siteContentSort === option;
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<MenuItem
|
|
27
|
+
key={ option }
|
|
28
|
+
aria-checked={ isSelected }
|
|
29
|
+
onClick={ () => setSiteContentSort( option ) }
|
|
30
|
+
role="menuitemradio"
|
|
31
|
+
selected={ isSelected }
|
|
32
|
+
>
|
|
33
|
+
{ renderSortMenuItemContent( getSiteContentSortLabel( option ), isSelected ) }
|
|
34
|
+
</MenuItem>
|
|
35
|
+
);
|
|
36
|
+
} ) }
|
|
37
|
+
</MenuList>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { PopoverBody, PopoverHeader } from '@elementor/editor-ui';
|
|
3
|
+
import { ArrowsSortIcon } from '@elementor/icons';
|
|
4
|
+
import { bindPopover, bindToggle, Divider, Popover, ToggleButton, Tooltip, usePopupState } from '@elementor/ui';
|
|
5
|
+
import { __ } from '@wordpress/i18n';
|
|
6
|
+
|
|
7
|
+
import { useContentPanelCoordinator } from '../../contexts/content-panel-coordinator';
|
|
8
|
+
import { hasActiveSiteContentSort, SITE_CONTENT_SORT_POPOVER_WIDTH_PX } from '../../site-content-sort';
|
|
9
|
+
import ContentPanelClearFiltersButton from './content-panel-clear-filters-button';
|
|
10
|
+
import ContentPanelSortList from './content-panel-sort-list';
|
|
11
|
+
|
|
12
|
+
export default function ContentPanelSort() {
|
|
13
|
+
const { clearSiteContentSort, siteContentSort } = useContentPanelCoordinator();
|
|
14
|
+
const popupState = usePopupState( {
|
|
15
|
+
variant: 'popover',
|
|
16
|
+
disableAutoFocus: true,
|
|
17
|
+
} );
|
|
18
|
+
|
|
19
|
+
const hasActiveSort = hasActiveSiteContentSort( siteContentSort );
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<>
|
|
23
|
+
<Tooltip placement="top" title={ __( 'Sort', 'elementor' ) }>
|
|
24
|
+
<ToggleButton
|
|
25
|
+
aria-label={ __( 'Sort', 'elementor' ) }
|
|
26
|
+
selected={ popupState.isOpen || hasActiveSort }
|
|
27
|
+
size="tiny"
|
|
28
|
+
value="sort"
|
|
29
|
+
{ ...bindToggle( popupState ) }
|
|
30
|
+
>
|
|
31
|
+
<ArrowsSortIcon fontSize="tiny" />
|
|
32
|
+
</ToggleButton>
|
|
33
|
+
</Tooltip>
|
|
34
|
+
<Popover
|
|
35
|
+
anchorOrigin={ {
|
|
36
|
+
horizontal: 'right',
|
|
37
|
+
vertical: 'top',
|
|
38
|
+
} }
|
|
39
|
+
sx={ {
|
|
40
|
+
maxWidth: `${ SITE_CONTENT_SORT_POPOVER_WIDTH_PX }px`,
|
|
41
|
+
} }
|
|
42
|
+
transformOrigin={ {
|
|
43
|
+
horizontal: -21,
|
|
44
|
+
vertical: 'top',
|
|
45
|
+
} }
|
|
46
|
+
{ ...bindPopover( popupState ) }
|
|
47
|
+
>
|
|
48
|
+
<PopoverHeader
|
|
49
|
+
actions={
|
|
50
|
+
hasActiveSort
|
|
51
|
+
? [
|
|
52
|
+
<ContentPanelClearFiltersButton
|
|
53
|
+
ariaLabel={ __( 'Clear sort', 'elementor' ) }
|
|
54
|
+
key="clear-sort"
|
|
55
|
+
onClear={ clearSiteContentSort }
|
|
56
|
+
tooltipText={ __( 'Clear sort', 'elementor' ) }
|
|
57
|
+
/>,
|
|
58
|
+
]
|
|
59
|
+
: []
|
|
60
|
+
}
|
|
61
|
+
icon={ <ArrowsSortIcon fontSize="tiny" /> }
|
|
62
|
+
onClose={ popupState.close }
|
|
63
|
+
title={ __( 'Sort', 'elementor' ) }
|
|
64
|
+
/>
|
|
65
|
+
<Divider sx={ { borderWidth: '1px 0 0 0' } } />
|
|
66
|
+
<PopoverBody height="auto" width={ SITE_CONTENT_SORT_POPOVER_WIDTH_PX }>
|
|
67
|
+
<ContentPanelSortList />
|
|
68
|
+
</PopoverBody>
|
|
69
|
+
</Popover>
|
|
70
|
+
</>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
@@ -4,6 +4,7 @@ import { Box, Stack } from '@elementor/ui';
|
|
|
4
4
|
import ContentPanelActiveFilters from './content-panel-active-filters';
|
|
5
5
|
import { contentPanelSearchBoxSx } from './content-panel-chrome';
|
|
6
6
|
import ContentPanelSearch from './content-panel-search';
|
|
7
|
+
import ContentPanelSort from './content-panel-sort';
|
|
7
8
|
import ContentPanelStatusFilter from './content-panel-status-filter';
|
|
8
9
|
|
|
9
10
|
export default function ContentPanelToolbar() {
|
|
@@ -15,6 +16,7 @@ export default function ContentPanelToolbar() {
|
|
|
15
16
|
<ContentPanelSearch />
|
|
16
17
|
</Box>
|
|
17
18
|
<ContentPanelStatusFilter />
|
|
19
|
+
<ContentPanelSort />
|
|
18
20
|
</Stack>
|
|
19
21
|
<ContentPanelActiveFilters />
|
|
20
22
|
</Stack>
|
|
@@ -10,6 +10,7 @@ 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
12
|
import { usePostsBaselineTotal } from '../../../hooks/use-posts-baseline-total';
|
|
13
|
+
import { shouldPinHomepageForSiteContentSort } from '../../../site-content-sort';
|
|
13
14
|
import {
|
|
14
15
|
hasActiveSiteContentStatusFilters,
|
|
15
16
|
isSiteContentListNarrowed,
|
|
@@ -30,7 +31,8 @@ type Props = {
|
|
|
30
31
|
|
|
31
32
|
export default function PostsCollapsibleList( { isOpenByDefault = false }: Props ) {
|
|
32
33
|
const { type, editMode, resetEditMode, setEditMode } = usePostListContext();
|
|
33
|
-
const { registerSection, searchQuery, statusFilters, unregisterSection } =
|
|
34
|
+
const { registerSection, searchQuery, siteContentSort, statusFilters, unregisterSection } =
|
|
35
|
+
useContentPanelCoordinator();
|
|
34
36
|
const [ isOpen, setIsOpen ] = useState( isOpenByDefault );
|
|
35
37
|
const trimmedSearch = searchQuery.trim();
|
|
36
38
|
const [ debouncedSearch, setDebouncedSearch ] = useState( trimmedSearch );
|
|
@@ -52,7 +54,7 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
|
|
|
52
54
|
fetchNextPage,
|
|
53
55
|
hasNextPage,
|
|
54
56
|
isFetchingNextPage,
|
|
55
|
-
} = usePosts( type, debouncedSearch, statusFilters );
|
|
57
|
+
} = usePosts( type, debouncedSearch, statusFilters, siteContentSort );
|
|
56
58
|
const isNarrowed = isSiteContentListNarrowed( statusFilters, debouncedSearch );
|
|
57
59
|
const isTrashFilter = isSiteContentTrashFilterActive( statusFilters );
|
|
58
60
|
const baselineStatus = isTrashFilter ? 'trash' : 'any';
|
|
@@ -85,7 +87,9 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
|
|
|
85
87
|
? posts.map( ( post ) => ( post.id === homepageId ? { ...post, isHome: true } : post ) )
|
|
86
88
|
: posts;
|
|
87
89
|
|
|
88
|
-
const
|
|
90
|
+
const shouldPinHomepage = isPageType && shouldPinHomepageForSiteContentSort( siteContentSort );
|
|
91
|
+
|
|
92
|
+
const sorted = shouldPinHomepage
|
|
89
93
|
? [ ...mappedPosts ].sort( ( a, b ) => {
|
|
90
94
|
if ( a.id === homepageId ) {
|
|
91
95
|
return -1;
|
|
@@ -100,14 +104,14 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
|
|
|
100
104
|
: mappedPosts;
|
|
101
105
|
|
|
102
106
|
return sorted;
|
|
103
|
-
}, [
|
|
107
|
+
}, [ homepageId, isPageType, posts, siteContentSort ] );
|
|
104
108
|
|
|
105
109
|
const pagination = usePostListPagination( {
|
|
106
110
|
fetchNextPage,
|
|
107
111
|
hasNextPage,
|
|
108
112
|
isFetchingNextPage,
|
|
109
113
|
loadedCount: filteredPosts.length,
|
|
110
|
-
resetKey: `${ type }:${ debouncedSearch }:${ statusFiltersKey }`,
|
|
114
|
+
resetKey: `${ type }:${ debouncedSearch }:${ statusFiltersKey }:${ siteContentSort }`,
|
|
111
115
|
totalCount: total,
|
|
112
116
|
} );
|
|
113
117
|
|
|
@@ -2,6 +2,7 @@ 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 { DEFAULT_SITE_CONTENT_SORT, type SiteContentSortOption } from '../site-content-sort';
|
|
5
6
|
import {
|
|
6
7
|
EMPTY_SITE_CONTENT_STATUS_FILTERS,
|
|
7
8
|
type SiteContentSelectableStatus,
|
|
@@ -15,11 +16,15 @@ type SectionHandlers = {
|
|
|
15
16
|
};
|
|
16
17
|
|
|
17
18
|
type ContentPanelCoordinatorValue = {
|
|
19
|
+
clearActiveToolbarFilters: () => void;
|
|
20
|
+
clearSiteContentSort: () => void;
|
|
18
21
|
clearStatusFilters: () => void;
|
|
19
22
|
registerSection: ( type: Slug, handlers: SectionHandlers ) => void;
|
|
20
23
|
requestCreate: ( type: Slug ) => void;
|
|
21
24
|
searchQuery: string;
|
|
22
25
|
setSearchQuery: ( query: string ) => void;
|
|
26
|
+
setSiteContentSort: ( sort: SiteContentSortOption ) => void;
|
|
27
|
+
siteContentSort: SiteContentSortOption;
|
|
23
28
|
statusFilters: SiteContentStatusFilters;
|
|
24
29
|
toggleStatusFilter: ( filter: SiteContentSelectableStatus ) => void;
|
|
25
30
|
unregisterSection: ( type: Slug ) => void;
|
|
@@ -30,6 +35,7 @@ const ContentPanelCoordinatorContext = createContext< ContentPanelCoordinatorVal
|
|
|
30
35
|
export function ContentPanelCoordinatorProvider( { children }: { children: ReactNode } ) {
|
|
31
36
|
const sectionsRef = useRef( new Map< Slug, SectionHandlers >() );
|
|
32
37
|
const [ searchQuery, setSearchQuery ] = useState( '' );
|
|
38
|
+
const [ siteContentSort, setSiteContentSortState ] = useState< SiteContentSortOption >( DEFAULT_SITE_CONTENT_SORT );
|
|
33
39
|
const [ statusFilters, setStatusFilters ] = useState< SiteContentStatusFilters >(
|
|
34
40
|
EMPTY_SITE_CONTENT_STATUS_FILTERS
|
|
35
41
|
);
|
|
@@ -53,30 +59,51 @@ export function ContentPanelCoordinatorProvider( { children }: { children: React
|
|
|
53
59
|
handlers.startCreate();
|
|
54
60
|
}, [] );
|
|
55
61
|
|
|
62
|
+
const clearActiveToolbarFilters = useCallback( () => {
|
|
63
|
+
setSiteContentSortState( DEFAULT_SITE_CONTENT_SORT );
|
|
64
|
+
setStatusFilters( EMPTY_SITE_CONTENT_STATUS_FILTERS );
|
|
65
|
+
}, [] );
|
|
66
|
+
|
|
67
|
+
const clearSiteContentSort = useCallback( () => {
|
|
68
|
+
setSiteContentSortState( DEFAULT_SITE_CONTENT_SORT );
|
|
69
|
+
}, [] );
|
|
70
|
+
|
|
56
71
|
const clearStatusFilters = useCallback( () => {
|
|
57
72
|
setStatusFilters( EMPTY_SITE_CONTENT_STATUS_FILTERS );
|
|
58
73
|
}, [] );
|
|
59
74
|
|
|
75
|
+
const setSiteContentSort = useCallback( ( sort: SiteContentSortOption ) => {
|
|
76
|
+
setSiteContentSortState( sort );
|
|
77
|
+
}, [] );
|
|
78
|
+
|
|
60
79
|
const toggleStatusFilter = useCallback( ( filter: SiteContentSelectableStatus ) => {
|
|
61
80
|
setStatusFilters( ( current ) => toggleSiteContentStatusFilter( current, filter ) );
|
|
62
81
|
}, [] );
|
|
63
82
|
|
|
64
83
|
const value = useMemo(
|
|
65
84
|
() => ( {
|
|
85
|
+
clearActiveToolbarFilters,
|
|
86
|
+
clearSiteContentSort,
|
|
66
87
|
clearStatusFilters,
|
|
67
88
|
registerSection,
|
|
68
89
|
requestCreate,
|
|
69
90
|
searchQuery,
|
|
70
91
|
setSearchQuery,
|
|
92
|
+
setSiteContentSort,
|
|
93
|
+
siteContentSort,
|
|
71
94
|
statusFilters,
|
|
72
95
|
toggleStatusFilter,
|
|
73
96
|
unregisterSection,
|
|
74
97
|
} ),
|
|
75
98
|
[
|
|
99
|
+
clearActiveToolbarFilters,
|
|
100
|
+
clearSiteContentSort,
|
|
76
101
|
clearStatusFilters,
|
|
77
102
|
registerSection,
|
|
78
103
|
requestCreate,
|
|
79
104
|
searchQuery,
|
|
105
|
+
siteContentSort,
|
|
106
|
+
setSiteContentSort,
|
|
80
107
|
statusFilters,
|
|
81
108
|
toggleStatusFilter,
|
|
82
109
|
unregisterSection,
|
package/src/hooks/use-posts.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { keepPreviousData, useInfiniteQuery } from '@elementor/query';
|
|
2
2
|
|
|
3
|
-
import { getRequest, type PostsResponse, type Slug } from '../api/post';
|
|
3
|
+
import { getRequest, POST_PER_PAGE, type PostsResponse, type Slug } from '../api/post';
|
|
4
|
+
import { DEFAULT_SITE_CONTENT_SORT, type SiteContentSortOption } from '../site-content-sort';
|
|
4
5
|
import {
|
|
5
6
|
EMPTY_SITE_CONTENT_STATUS_FILTERS,
|
|
6
7
|
getRestStatusForSiteContentFilters,
|
|
@@ -10,11 +11,12 @@ import { type Post } from '../types';
|
|
|
10
11
|
|
|
11
12
|
export const postsQueryKeyBase = ( postTypeSlug: string ) => [ 'site-navigation', 'posts', postTypeSlug ];
|
|
12
13
|
|
|
13
|
-
export const postsQueryKey = (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
export const postsQueryKey = (
|
|
15
|
+
postTypeSlug: string,
|
|
16
|
+
trimmedSearch = '',
|
|
17
|
+
restStatus = 'any',
|
|
18
|
+
sort: SiteContentSortOption = DEFAULT_SITE_CONTENT_SORT
|
|
19
|
+
) => [ ...postsQueryKeyBase( postTypeSlug ), restStatus, trimmedSearch, sort ];
|
|
18
20
|
|
|
19
21
|
const flattenData = ( data?: { pages: PostsResponse[] } ) => {
|
|
20
22
|
if ( ! data ) {
|
|
@@ -22,9 +24,17 @@ const flattenData = ( data?: { pages: PostsResponse[] } ) => {
|
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
const flattened: Post[] = [];
|
|
27
|
+
const seenIds = new Set< number >();
|
|
25
28
|
|
|
26
29
|
data.pages.forEach( ( page ) => {
|
|
27
|
-
|
|
30
|
+
page.data.forEach( ( post ) => {
|
|
31
|
+
if ( seenIds.has( post.id ) ) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
seenIds.add( post.id );
|
|
36
|
+
flattened.push( post );
|
|
37
|
+
} );
|
|
28
38
|
} );
|
|
29
39
|
|
|
30
40
|
return flattened;
|
|
@@ -33,14 +43,16 @@ const flattenData = ( data?: { pages: PostsResponse[] } ) => {
|
|
|
33
43
|
export function usePosts(
|
|
34
44
|
postTypeSlug: Slug,
|
|
35
45
|
search = '',
|
|
36
|
-
statusFilters: SiteContentStatusFilters = EMPTY_SITE_CONTENT_STATUS_FILTERS
|
|
46
|
+
statusFilters: SiteContentStatusFilters = EMPTY_SITE_CONTENT_STATUS_FILTERS,
|
|
47
|
+
sort: SiteContentSortOption = DEFAULT_SITE_CONTENT_SORT
|
|
37
48
|
) {
|
|
38
49
|
const trimmedSearch = search.trim();
|
|
39
50
|
const restStatus = getRestStatusForSiteContentFilters( statusFilters );
|
|
40
51
|
|
|
41
52
|
const query = useInfiniteQuery( {
|
|
42
|
-
queryKey: postsQueryKey( postTypeSlug, trimmedSearch, restStatus ),
|
|
43
|
-
queryFn: ( { pageParam = 1 } ) =>
|
|
53
|
+
queryKey: postsQueryKey( postTypeSlug, trimmedSearch, restStatus, sort ),
|
|
54
|
+
queryFn: ( { pageParam = 1 } ) =>
|
|
55
|
+
getRequest( postTypeSlug, pageParam as number, trimmedSearch, restStatus, POST_PER_PAGE, sort ),
|
|
44
56
|
initialPageParam: 1,
|
|
45
57
|
getNextPageParam: ( lastPage: PostsResponse ) => {
|
|
46
58
|
return lastPage.currentPage < lastPage.totalPages ? lastPage.currentPage + 1 : undefined;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { __ } from '@wordpress/i18n';
|
|
2
|
+
|
|
3
|
+
export type SiteContentSortOption = 'newest' | 'oldest';
|
|
4
|
+
|
|
5
|
+
export const SITE_CONTENT_SORT_OPTIONS: SiteContentSortOption[] = [ 'oldest', 'newest' ];
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_SITE_CONTENT_SORT: SiteContentSortOption = 'oldest';
|
|
8
|
+
|
|
9
|
+
export const SITE_CONTENT_SORT_POPOVER_WIDTH_PX = 280;
|
|
10
|
+
|
|
11
|
+
export type SiteContentRestListOrder = {
|
|
12
|
+
order: 'asc' | 'desc';
|
|
13
|
+
orderby: 'date';
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const sortLabels: Record< SiteContentSortOption, string > = {
|
|
17
|
+
oldest: __( 'Oldest', 'elementor' ),
|
|
18
|
+
newest: __( 'Newest', 'elementor' ),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export function getSiteContentSortLabel( sort: SiteContentSortOption ): string {
|
|
22
|
+
return sortLabels[ sort ];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function hasActiveSiteContentSort( sort: SiteContentSortOption ): boolean {
|
|
26
|
+
return sort !== DEFAULT_SITE_CONTENT_SORT;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getRestOrderForSiteContentSort( sort: SiteContentSortOption ): SiteContentRestListOrder {
|
|
30
|
+
if ( sort === 'newest' ) {
|
|
31
|
+
return { order: 'desc', orderby: 'date' };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return { order: 'asc', orderby: 'date' };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function shouldPinHomepageForSiteContentSort( sort: SiteContentSortOption ): boolean {
|
|
38
|
+
return sort === DEFAULT_SITE_CONTENT_SORT;
|
|
39
|
+
}
|