Skip to content

Commit

Permalink
Global styles: leave screen if current shadow entry gets deleted (#65935
Browse files Browse the repository at this point in the history
)

* Global styles: leave screen if current shadow entry gets deleted

* Refine approach

* Apply feedback

---

Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: nith53 <nithins53@git.wordpress.org>
Co-authored-by: benniledl <benniledl@git.wordpress.org>
  • Loading branch information
6 people authored Oct 8, 2024
1 parent 4dc8a02 commit d716a25
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
reset,
moreVertical,
} from '@wordpress/icons';
import { useState, useMemo } from '@wordpress/element';
import { useState, useMemo, useEffect } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -73,12 +73,30 @@ const presetShadowMenuItems = [

export default function ShadowsEditPanel() {
const {
goBack,
params: { category, slug },
goTo,
} = useNavigator();
const [ shadows, setShadows ] = useGlobalSetting(
`shadow.presets.${ category }`
);

useEffect( () => {
const hasCurrentShadow = shadows?.some(
( shadow ) => shadow.slug === slug
);
// If the shadow being edited doesn't exist anymore in the global styles setting, navigate back
// to prevent the user from editing a non-existent shadow entry.
// This can happen, for example:
// - when the user deletes the shadow
// - when the user resets the styles while editing a custom shadow
//
// The check on the slug is necessary to prevent a double back navigation when the user triggers
// a backward navigation by interacting with the screen's UI.
if ( !! slug && ! hasCurrentShadow ) {
goBack();
}
}, [ shadows, slug, goBack ] );

const [ baseShadows ] = useGlobalSetting(
`shadow.presets.${ category }`,
undefined,
Expand Down Expand Up @@ -123,9 +141,7 @@ export default function ShadowsEditPanel() {
};

const handleShadowDelete = () => {
const updatedShadows = shadows.filter( ( s ) => s.slug !== slug );
setShadows( updatedShadows );
goTo( `/shadows` );
setShadows( shadows.filter( ( s ) => s.slug !== slug ) );
};

const handleShadowRename = ( newName ) => {
Expand Down

1 comment on commit d716a25

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in d716a25.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11235470265
📝 Reported issues:

Please sign in to comment.