Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { RangeControl, Spinner } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
Expand Down Expand Up @@ -39,6 +38,8 @@ function RevisionsSlider() {
const query = {
per_page: -1,
context: 'edit',
orderby: 'date',
order: 'asc',
_fields: [
...new Set( [
'id',
Expand Down Expand Up @@ -72,28 +73,12 @@ function RevisionsSlider() {

const { setCurrentRevisionId } = unlock( useDispatch( editorStore ) );

// Template revisions use the template REST API format, which exposes
// 'modified' instead of 'date'.
const revisionDateField = revisionKey === 'wp_id' ? 'modified' : 'date';

const sortedRevisions = useMemo( () => {
return (
revisions
?.slice()
.sort(
( a, b ) =>
new Date( a[ revisionDateField ] ) -
new Date( b[ revisionDateField ] )
) ?? []
);
}, [ revisions, revisionDateField ] );

const selectedIndex = sortedRevisions.findIndex(
const selectedIndex = revisions?.findIndex(
( r ) => r[ revisionKey ] === currentRevisionId
);

const handleSliderChange = ( index ) => {
const revision = sortedRevisions[ index ];
const revision = revisions?.[ index ];
if ( revision ) {
setCurrentRevisionId( revision[ revisionKey ] );
}
Expand All @@ -102,29 +87,26 @@ function RevisionsSlider() {
// Format date for tooltip.
const dateSettings = getDateSettings();
const renderTooltipContent = ( index ) => {
const revision = sortedRevisions[ index ];
const revision = revisions?.[ index ];
if ( ! revision ) {
return index;
}
return dateI18n(
dateSettings.formats.datetime,
revision[ revisionDateField ]
);
return dateI18n( dateSettings.formats.datetime, revision.date );
};

if ( isLoading ) {
return <Spinner />;
}

if ( ! sortedRevisions.length ) {
if ( ! revisions?.length ) {
return (
<span className="editor-revisions-header__no-revisions">
{ __( 'No revisions found.' ) }
</span>
);
}

if ( sortedRevisions.length === 1 ) {
if ( revisions?.length === 1 ) {
return (
<span className="editor-revisions-header__no-revisions">
{ __( 'Only one revision found.' ) }
Expand All @@ -138,7 +120,7 @@ function RevisionsSlider() {
className="editor-revisions-header__slider"
hideLabelFromVision
label={ __( 'Revision' ) }
max={ sortedRevisions.length - 1 }
max={ revisions?.length - 1 }
min={ 0 }
marks
onChange={ handleSliderChange }
Expand Down
7 changes: 1 addition & 6 deletions packages/editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,7 @@ export const restoreRevision =
sprintf(
/* translators: %s: Date and time of the revision. */
__( 'Restored to revision from %s.' ),
dateI18n(
getDateSettings().formats.datetime,
// Template revisions use the template REST API format, which
// exposes 'modified' instead of 'date'.
revisionKey === 'wp_id' ? revision.modified : revision.date
)
dateI18n( getDateSettings().formats.datetime, revision.date )
),
{
type: 'snackbar',
Expand Down
17 changes: 4 additions & 13 deletions packages/editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ export const getPreviousRevision = createRegistrySelector(
{
per_page: -1,
context: 'edit',
orderby: 'date',
order: 'asc',
_fields: [
...new Set( [
'id',
Expand All @@ -501,25 +503,14 @@ export const getPreviousRevision = createRegistrySelector(
return null;
}

// Template revisions use the template REST API format, which exposes
// 'modified' instead of 'date'. Regular post revisions use 'date'.
const revisionDateField = revisionKey === 'wp_id' ? 'modified' : 'date';

// Sort by date ascending (oldest first).
const sortedRevisions = [ ...revisions ].sort(
( a, b ) =>
new Date( a[ revisionDateField ] ) -
new Date( b[ revisionDateField ] )
);

// Find current revision index.
const currentIndex = sortedRevisions.findIndex(
const currentIndex = revisions.findIndex(
( r ) => r[ revisionKey ] === currentRevisionId
);

// Return the previous revision (older one) if it exists.
if ( currentIndex > 0 ) {
return sortedRevisions[ currentIndex - 1 ];
return revisions[ currentIndex - 1 ];
}

return null;
Expand Down
Loading