diff --git a/packages/media-editor/src/components/media-editor-panel/index.tsx b/packages/media-editor/src/components/media-editor-panel/index.tsx index 489bd0c54504c7..afe052f948ae1e 100644 --- a/packages/media-editor/src/components/media-editor-panel/index.tsx +++ b/packages/media-editor/src/components/media-editor-panel/index.tsx @@ -36,8 +36,6 @@ export interface MediaEditorPanelProps extends MediaEditorPanelRegistration { interface MediaEditorPanelsContextValue { register: ( panel: MediaEditorPanelRegistration, instance: object ) => void; unregister: ( name: string, instance: object ) => void; - /** Panel name to the mounted panel that has it. */ - owners: Record< string, object >; } const MediaEditorPanelsContext = @@ -62,10 +60,7 @@ export function getPanelSlotName( name: string ) { */ export function useMediaEditorPanels() { const [ panels, setPanels ] = useState< - Record< - string, - { registration: MediaEditorPanelRegistration; owner: object } - > + Record< string, MediaEditorPanelRegistration > >( {} ); // The first panel to announce a name keeps it. A ref, so panels // announcing in the same commit see each other. @@ -74,26 +69,18 @@ export function useMediaEditorPanels() { ( panel: MediaEditorPanelRegistration, instance: object ) => { const owner = ownersRef.current.get( panel.name ); if ( owner && owner !== instance ) { - warning( - `A media editor panel named "${ panel.name }" is already registered, so this one is not shown. Give each panel a unique name.` + throw new Error( + `A media editor panel named "${ panel.name }" is already registered. Give each panel a unique name.` ); - return; } ownersRef.current.set( panel.name, instance ); setPanels( ( current ) => { // Skip unchanged details: an inline `mediaTypes` array would // otherwise re-render the editor and the panel in a loop. - const entry = current[ panel.name ]; - if ( - entry?.owner === instance && - fastDeepEqual( entry.registration, panel ) - ) { + if ( fastDeepEqual( current[ panel.name ], panel ) ) { return current; } - return { - ...current, - [ panel.name ]: { registration: panel, owner: instance }, - }; + return { ...current, [ panel.name ]: panel }; } ); }, [] @@ -112,25 +99,11 @@ export function useMediaEditorPanels() { return next; } ); }, [] ); - const owners = useMemo( - () => - Object.fromEntries( - Object.entries( panels ).map( ( [ name, { owner } ] ) => [ - name, - owner, - ] ) - ), - [ panels ] - ); const contextValue = useMemo( - () => ( { register, unregister, owners } ), - [ register, unregister, owners ] - ); - const registrations = useMemo( - () => - Object.values( panels ).map( ( { registration } ) => registration ), - [ panels ] + () => ( { register, unregister } ), + [ register, unregister ] ); + const registrations = useMemo( () => Object.values( panels ), [ panels ] ); return { panels: registrations, contextValue }; } @@ -147,10 +120,9 @@ export default function MediaEditorPanel( { 'MediaEditorPanel is rendered outside the media editor.' ); } - const { register, unregister, owners } = context; + const { register, unregister } = context; // Identifies this mounted panel. const [ instance ] = useState( () => ( {} ) ); - const owner = owners[ name ]; // Layout effects, so the tab exists before the sidebar paints. Two of // them: details may change on any render, but the tab only goes when // the panel unmounts or changes its name. @@ -168,18 +140,11 @@ export default function MediaEditorPanel( { } } ); register( { name, title, mediaTypes }, instance ); - // When two panels share a name, the second waits for the first to - // unmount. `owner` changes at that point, which re-runs this effect - // and gives the second panel the tab. - }, [ name, title, mediaTypes, register, owner, instance ] ); + }, [ name, title, mediaTypes, register, instance ] ); useLayoutEffect( () => { return () => unregister( name, instance ); }, [ name, unregister, instance ] ); - if ( owner !== instance ) { - return null; - } - return ( { ( fillProps ) => ( diff --git a/packages/media-editor/src/components/media-editor-panel/test/index.jsdom.test.tsx b/packages/media-editor/src/components/media-editor-panel/test/index.jsdom.test.tsx index add5c47ef3b2c7..8d4c8864e9be67 100644 --- a/packages/media-editor/src/components/media-editor-panel/test/index.jsdom.test.tsx +++ b/packages/media-editor/src/components/media-editor-panel/test/index.jsdom.test.tsx @@ -287,40 +287,46 @@ describe( 'MediaEditorPanel', () => { ).toThrow( 'MediaEditorPanel is rendered outside the media editor.' ); } ); - it( 'keeps the first of two panels that share a name, and warns', () => { - function Toggle() { - const [ shown, setShown ] = useState( true ); + it( 'throws when another mounted panel has its name', () => { + expect( () => + render( + + + first + + + second + + + ) + ).toThrow( + 'A media editor panel named "plugin/same" is already registered.' + ); + } ); + + it( 'frees a name when its panel unmounts, for a panel that mounts in the same render', () => { + function Swap() { + const [ swapped, setSwapped ] = useState( false ); return ( <> - - { shown && ( - - first - - ) } + + + { swapped ? 'second' : 'first' } + ); } render( - - - second - + ); - expect( mockedWarning ).toHaveBeenCalledWith( - expect.stringContaining( '"plugin/same"' ) - ); - expect( screen.getByTestId( 'tabs' ) ).toHaveTextContent( - /^plugin\/same$/ - ); - expect( - screen.getByRole( 'region', { name: 'First' } ) - ).toHaveTextContent( /^first$/ ); - - fireEvent.click( screen.getByRole( 'button', { name: 'hide' } ) ); + fireEvent.click( screen.getByRole( 'button', { name: 'swap' } ) ); expect( screen.getByRole( 'region', { name: 'Second' } ) diff --git a/packages/media-editor/src/components/media-editor/index.tsx b/packages/media-editor/src/components/media-editor/index.tsx index 987c84d54ab003..00e14c86573d3d 100644 --- a/packages/media-editor/src/components/media-editor/index.tsx +++ b/packages/media-editor/src/components/media-editor/index.tsx @@ -1262,6 +1262,9 @@ function MediaEditorContent( { > + { /* After Core's panels: when a plugin reuses one of + their names, the plugin's panel is the one that + throws, and `PluginArea` catches it. */ } { unregisterPlugin( 'test-media-panels' ); } } ); + + it( 'drops a plugin whose panel takes a name already in use, and says so', async () => { + registerPlugin( 'test-duplicate-panel', { + scope: MEDIA_EDITOR_PLUGIN_SCOPE, + render: () => ( + + Never shown + + ), + } ); + try { + await setup(); + + expect( await screen.findByTestId( 'snackbar' ) ).toHaveTextContent( + 'The "test-duplicate-panel" plugin has encountered an error and cannot be rendered.' + ); + // React reports the error it handed to the plugin's boundary. + expect( console ).toHaveErrored(); + expect( + screen.getByRole( 'tab', { name: 'Details' } ) + ).toBeInTheDocument(); + expect( + screen.queryByRole( 'tab', { name: 'Duplicate' } ) + ).not.toBeInTheDocument(); + } finally { + unregisterPlugin( 'test-duplicate-panel' ); + } + } ); } );