Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Site editor: conditionally render global styles revisions footer in sidebar #53204

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
Expand Up @@ -108,30 +108,10 @@ function SidebarNavigationScreenGlobalStylesContent() {
);
}

function SidebarNavigationScreenGlobalStylesFooter( { onClickRevisions } ) {
const { revisions, isLoading } = useGlobalStylesRevisions();
const { revisionsCount } = useSelect( ( select ) => {
const { getEntityRecord, __experimentalGetCurrentGlobalStylesId } =
select( coreStore );

const globalStylesId = __experimentalGetCurrentGlobalStylesId();
const globalStyles = globalStylesId
? getEntityRecord( 'root', 'globalStyles', globalStylesId )
: undefined;

return {
revisionsCount:
globalStyles?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0,
};
}, [] );

const hasRevisions = revisionsCount >= 2;
const modified = revisions?.[ 0 ]?.modified;

if ( ! hasRevisions || isLoading || ! modified ) {
return null;
}

function SidebarNavigationScreenGlobalStylesFooter( {
modifiedDateTime,
onClickRevisions,
} ) {
return (
<VStack className="edit-site-sidebar-navigation-screen-global-styles__footer">
<SidebarNavigationItem
Expand All @@ -150,8 +130,8 @@ function SidebarNavigationScreenGlobalStylesFooter( { onClickRevisions } ) {
{ __( 'Last modified' ) }
</span>
<span>
<time dateTime={ modified }>
{ humanTimeDiff( modified ) }
<time dateTime={ modifiedDateTime }>
{ humanTimeDiff( modifiedDateTime ) }
</time>
</span>
<Icon icon={ backup } style={ { fill: 'currentcolor' } } />
Expand All @@ -162,6 +142,8 @@ function SidebarNavigationScreenGlobalStylesFooter( { onClickRevisions } ) {
}

export default function SidebarNavigationScreenGlobalStyles() {
const { revisions, isLoading: isLoadingRevisions } =
useGlobalStylesRevisions();
const { openGeneralSidebar, setIsListViewOpened } =
useDispatch( editSiteStore );
const isMobileViewport = useViewportMatch( 'medium', '<' );
Expand All @@ -171,15 +153,28 @@ export default function SidebarNavigationScreenGlobalStyles() {
const { createNotice } = useDispatch( noticesStore );
const { set: setPreference } = useDispatch( preferencesStore );
const { get: getPreference } = useSelect( preferencesStore );
const { isViewMode, isStyleBookOpened } = useSelect( ( select ) => {
const { getCanvasMode, getEditorCanvasContainerView } = unlock(
select( editSiteStore )
);
return {
isViewMode: 'view' === getCanvasMode(),
isStyleBookOpened: 'style-book' === getEditorCanvasContainerView(),
};
}, [] );
const { isViewMode, isStyleBookOpened, revisionsCount } = useSelect(
( select ) => {
const { getCanvasMode, getEditorCanvasContainerView } = unlock(
select( editSiteStore )
);
const { getEntityRecord, __experimentalGetCurrentGlobalStylesId } =
select( coreStore );
const globalStylesId = __experimentalGetCurrentGlobalStylesId();
const globalStyles = globalStylesId
? getEntityRecord( 'root', 'globalStyles', globalStylesId )
: undefined;
return {
isViewMode: 'view' === getCanvasMode(),
isStyleBookOpened:
'style-book' === getEditorCanvasContainerView(),
revisionsCount:
globalStyles?._links?.[ 'version-history' ]?.[ 0 ]?.count ??
0,
};
},
[]
);

const turnOffDistractionFreeMode = useCallback( () => {
const isDistractionFree = getPreference(
Expand Down Expand Up @@ -226,6 +221,12 @@ export default function SidebarNavigationScreenGlobalStyles() {
setEditorCanvasContainerView( 'global-styles-revisions' );
}, [ openGlobalStyles, setEditorCanvasContainerView ] );

// If there are no revisions, do not render a footer.
const hasRevisions = revisionsCount >= 2;
const modifiedDateTime = revisions?.[ 0 ]?.modified;
const shouldShowGlobalStylesFooter =
hasRevisions && ! isLoadingRevisions && modifiedDateTime;

return (
<>
<SidebarNavigationScreen
Expand All @@ -235,9 +236,12 @@ export default function SidebarNavigationScreenGlobalStyles() {
) }
content={ <SidebarNavigationScreenGlobalStylesContent /> }
footer={
<SidebarNavigationScreenGlobalStylesFooter
onClickRevisions={ openRevisions }
/>
shouldShowGlobalStylesFooter && (
<SidebarNavigationScreenGlobalStylesFooter
modifiedDateTime={ modifiedDateTime }
onClickRevisions={ openRevisions }
/>
)
}
actions={
<>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -51,7 +56,12 @@ export default function SidebarNavigationScreen( {
return (
<>
<VStack
className="edit-site-sidebar-navigation-screen__main"
className={ classnames(
'edit-site-sidebar-navigation-screen__main',
{
'has-footer': !! footer,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit brute force, but it's all I could think of to ensure the gap is consistent.

The footer needs a top margin to allow the top grey border some room to display clearly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it is a little brute force, I like the clarity of has-footer as a classname, and it's clear what's going on here. Looks good to me for now!

}
) }
spacing={ 0 }
justify="flex-start"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
// Ensure the sidebar is always at least as tall as the viewport.
// This allows the footer section to be sticky at the bottom of the viewport.
flex-grow: 1;
margin-bottom: $grid-unit-20;
&.has-footer {
margin-bottom: 0;
}
}

.edit-site-sidebar-navigation-screen__content {
Expand Down