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
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: deprecated

Deprecate GlobalNotices and useGlobalNotices. Use SnackbarNotices from @wordpress/notices instead.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
GlobalNotices
=========

> **Deprecated.** Use `SnackbarNotices` from `@wordpress/notices` instead, and
> dispatch to that store directly with `{ type: 'snackbar' }`. Pages rendered by
> `@wordpress/boot` already mount a `SnackbarNotices`, so mounting this one too
> shows every notice twice. Retained as a compatibility shim for downstream
> consumers and will be removed in the next major version.

Renders the snackbar notices held in the `@wordpress/notices` store.

## General Usage:

```js
import { GlobalNotices } from '@automattic/jetpack-components';
render() {
return (
<GlobalNotices />
);
}
```

## Props

- `maxVisibleNotices`: how many notices to show at once. Defaults to 3.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export type GlobalNoticesProps = {
/**
* Renders the global notices.
*
* @deprecated Use `SnackbarNotices` from `@wordpress/notices` instead. Pages
* rendered by `@wordpress/boot` already mount one, so mounting this as well
* shows every notice twice.
*
* @param {GlobalNoticesProps} props - Component props.
*
* @return {import('react').ReactNode} The rendered notices list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export type TGlobalNotices = ReturnType< NoticesStore[ 'getActions' ] > &
/**
* The global notices hook.
*
* @deprecated Dispatch to `@wordpress/notices` directly, passing
* `{ type: 'snackbar' }` — this hook only defaulted that option.
*
* @return {TGlobalNotices} The global notices selectors and actions.
*/
export function useGlobalNotices(): TGlobalNotices {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useGlobalNotices } from '@automattic/jetpack-components';
import { store as modulesStore } from '@automattic/jetpack-shared-stores';
import { FormToggle } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useCallback } from 'react';
import { requestModuleSwitch } from '../../data/module-switch';
import { moduleSwitchKey, useRequestedSwitch } from '../../data/requested-switch-state';
Expand Down Expand Up @@ -46,7 +46,7 @@ export function useModuleActivation(
{ reload = true }: { reload?: boolean } = {}
) {
const { updateJetpackModuleStatus: toggleModule } = useDispatch( modulesStore );
const { createSuccessNotice, createErrorNotice } = useGlobalNotices();
const { createSuccessNotice, createErrorNotice } = useDispatch( noticesStore );
const { trackProductAction } = useProductFiltersContext() || {};

const storeIsUpdating = useSelect(
Expand Down Expand Up @@ -81,7 +81,7 @@ export function useModuleActivation(
__( '%s has been deactivated.', 'jetpack-my-jetpack' ),
$module.name
);
createSuccessNotice( message );
createSuccessNotice( message, { type: 'snackbar' } );
} else {
const message =
action === 'activation'
Expand All @@ -96,7 +96,7 @@ export function useModuleActivation(
$module.name
);

createErrorNotice( message );
createErrorNotice( message, { type: 'snackbar' } );
}
},
[ $module.module, $module.name, createErrorNotice, createSuccessNotice ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ const mockCreateSuccessNotice = jest.fn();
const mockCreateErrorNotice = jest.fn();

jest.mock( '@automattic/jetpack-shared-stores', () => ( { store: {} } ) );
jest.mock( '@wordpress/notices', () => ( { store: 'core/notices' } ) );

jest.mock( '@wordpress/data', () => ( {
useDispatch: () => ( { updateJetpackModuleStatus: mockToggleModule } ),
useDispatch: () => ( {
updateJetpackModuleStatus: mockToggleModule,
createSuccessNotice: mockCreateSuccessNotice,
createErrorNotice: mockCreateErrorNotice,
} ),
useSelect: callback => callback( () => ( { isModuleUpdating: () => false } ) ),
} ) );

Expand Down Expand Up @@ -57,13 +62,6 @@ jest.mock( '@wordpress/ui', () => {
};
} );

jest.mock( '@automattic/jetpack-components', () => ( {
useGlobalNotices: () => ( {
createSuccessNotice: mockCreateSuccessNotice,
createErrorNotice: mockCreateErrorNotice,
} ),
} ) );

jest.mock( '../../my-jetpack-tab-panel/products/products-tracking-context', () => ( {
useProductFiltersContext: () => ( { trackProductAction: mockTrackProductAction } ),
} ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jest.mock( '@automattic/jetpack-components', () => ( {
} ) );

jest.mock( '@automattic/jetpack-shared-stores', () => ( { store: 'modules-store' } ) );
jest.mock( '@wordpress/notices', () => ( { store: 'core/notices' } ) );

jest.mock( '@wordpress/data', () => ( {
useDispatch: () => ( { fetchModules: mockFetchModules } ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jest.mock( '@automattic/jetpack-components', () => ( {
} ) );

jest.mock( '@automattic/jetpack-shared-stores', () => ( { store: 'modules-store' } ) );
jest.mock( '@wordpress/notices', () => ( { store: 'core/notices' } ) );

jest.mock( '@wordpress/data', () => ( {
useDispatch: () => ( { invalidateResolution: jest.fn() } ),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useGlobalNotices } from '@automattic/jetpack-components';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { useEffect } from 'react';

/**
Expand Down Expand Up @@ -46,15 +47,15 @@ export function consumePendingSuccessNotice(): string | null {
* On mount, replays any success notice persisted before a page reload.
*/
export function useReplayPendingNotice(): void {
const { createSuccessNotice } = useGlobalNotices();
const { createSuccessNotice } = useDispatch( noticesStore );

// Consume the stored notice exactly once on mount. It must not re-run on later
// re-renders (e.g. a product refetch), or it would consume the notice on the page
// that set it — before the reload — so it would never reach the freshly-loaded page.
useEffect( () => {
const message = consumePendingSuccessNotice();
if ( message ) {
createSuccessNotice( message );
createSuccessNotice( message, { type: 'snackbar' } );
}
// eslint-disable-next-line react-hooks/exhaustive-deps -- run once on mount.
}, [] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
} from '../pending-notice';

const mockCreateSuccessNotice = jest.fn();
jest.mock( '@automattic/jetpack-components', () => ( {
useGlobalNotices: () => ( { createSuccessNotice: mockCreateSuccessNotice } ),
jest.mock( '@wordpress/notices', () => ( { store: 'core/notices' } ) );
jest.mock( '@wordpress/data', () => ( {
useDispatch: () => ( { createSuccessNotice: mockCreateSuccessNotice } ),
} ) );

describe( 'pending-notice', () => {
Expand All @@ -33,7 +34,9 @@ describe( 'pending-notice', () => {

renderHook( () => useReplayPendingNotice() );

expect( mockCreateSuccessNotice ).toHaveBeenCalledWith( 'Forms activated successfully!' );
expect( mockCreateSuccessNotice ).toHaveBeenCalledWith( 'Forms activated successfully!', {
type: 'snackbar',
} );
} );

it( 'shows no notice when none is pending', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useGlobalNotices } from '@automattic/jetpack-components';
import { useDispatch } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import useAnalytics from '../../hooks/use-analytics';
import { REST_API_SITE_PRODUCTS_ENDPOINT, QUERY_ACTIVATE_PRODUCT_KEY } from '../constants';
import useSimpleMutation from '../use-simple-mutation';
Expand Down Expand Up @@ -37,7 +38,7 @@ const useActivatePlugins = ( productSlugs: string | string[] ) => {

const { products, refetch } = useProducts( productIds );
const { recordEvent } = useAnalytics();
const { createSuccessNotice } = useGlobalNotices();
const { createSuccessNotice } = useDispatch( noticesStore );

const {
mutate: activate,
Expand Down Expand Up @@ -70,7 +71,8 @@ const useActivatePlugins = ( productSlugs: string | string[] ) => {
/* translators: %s is either the product name, i.e.- "Jetpack Backup" or the word "Plugins". */
__( '%s activated successfully!', 'jetpack-my-jetpack' ),
products?.length === 1 ? products[ 0 ].title : __( 'Plugins', 'jetpack-my-jetpack' )
)
),
{ type: 'snackbar' }
);
} );
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useGlobalNotices } from '@automattic/jetpack-components';
import { useDispatch } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import useAnalytics from '../../hooks/use-analytics';
import { QUERY_ACTIVATE_PRODUCT_KEY, REST_API_SITE_PRODUCTS_ENDPOINT } from '../constants';
import useSimpleMutation from '../use-simple-mutation';
Expand Down Expand Up @@ -35,7 +36,7 @@ export const useDeactivatePlugins = ( productSlugs: string | string[] ) => {
const { products, refetch } = useProducts( productIds );
const { recordEvent } = useAnalytics();

const { createSuccessNotice } = useGlobalNotices();
const { createSuccessNotice } = useDispatch( noticesStore );

const {
mutate: deactivate,
Expand Down Expand Up @@ -68,7 +69,8 @@ export const useDeactivatePlugins = ( productSlugs: string | string[] ) => {
/* translators: %s is either the product name, i.e.- "Jetpack Backup" or the word "Plugins". */
__( '%s deactivated successfully!', 'jetpack-my-jetpack' ),
products?.length === 1 ? products[ 0 ].title : __( 'Plugins', 'jetpack-my-jetpack' )
)
),
{ type: 'snackbar' }
);
} );
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useGlobalNotices } from '@automattic/jetpack-components';
import { useDispatch } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { REST_API_SITE_PRODUCTS_ENDPOINT, QUERY_INSTALL_PRODUCT_KEY } from '../constants';
import useSimpleMutation from '../use-simple-mutation';
import useProducts from './use-products';
Expand All @@ -8,7 +9,7 @@ const useInstallPlugins = ( productSlugs: string | string[] ) => {
const productIds = Array.isArray( productSlugs ) ? productSlugs : [ productSlugs ];

const { products, refetch } = useProducts( productIds );
const { createSuccessNotice } = useGlobalNotices();
const { createSuccessNotice } = useDispatch( noticesStore );

const successMessageSingular = sprintf(
/* translators: %s is the name of a Jetpack plugin, i.e.- "VaultPress Backup" or "Boost" or "Social" or "Search" or "VideoPress", etc. */
Expand All @@ -28,7 +29,7 @@ const useInstallPlugins = ( productSlugs: string | string[] ) => {
options: {
onSuccess: () => {
refetch().then( () => {
createSuccessNotice( successMessage );
createSuccessNotice( successMessage, { type: 'snackbar' } );
} );
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Use core SnackbarNotices instead of Jetpack GlobalNotices.
1 change: 1 addition & 0 deletions projects/packages/my-jetpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@wordpress/element": "8.7.0",
"@wordpress/i18n": "6.28.0",
"@wordpress/icons": "16.0.0",
"@wordpress/notices": "5.55.0",
"@wordpress/route": "0.21.0",
"@wordpress/theme": "2.1.0",
"@wordpress/ui": "0.22.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Use core SnackbarNotices instead of Jetpack GlobalNotices.
7 changes: 4 additions & 3 deletions projects/packages/newsletter/src/settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* External dependencies
*/
import { AdminPage, Col, Container, GlobalNotices } from '@automattic/jetpack-components';
import { AdminPage, Col, Container } from '@automattic/jetpack-components';
import { useConnection, getUserConnectionUrl } from '@automattic/jetpack-connection';
import { getSiteData, isSimpleSite } from '@automattic/jetpack-script-data';
import { createRoot, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { SnackbarNotices } from '@wordpress/notices';
/**
* Internal dependencies
*/
Expand All @@ -15,7 +16,7 @@ import './style.scss';
/**
* Newsletter Settings App — legacy `wp-admin/admin.php?page=jetpack-newsletter`
* surface. The shared body lives in `./newsletter-settings`; this file owns
* the standalone chrome (Jetpack-styled `AdminPage`, container grid, global
* the standalone chrome (Jetpack-styled `AdminPage`, container grid, core
* snackbar surface) and the WP.com connection check.
*
* The modernized dashboard mounts `NewsletterSettingsBody` directly inside
Expand Down Expand Up @@ -52,7 +53,7 @@ export function NewsletterSettingsApp(): JSX.Element {
apiRoot={ siteData?.rest_root }
apiNonce={ siteData?.rest_nonce }
>
<GlobalNotices />
<SnackbarNotices className="jetpack-newsletter-snackbar-notices" />
<Container horizontalSpacing={ 0 }>
<Col>
<div id="jp-admin-notices" className="newsletter-jitm-card" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ export type NewsletterSettingsBodyProps = {
* byte-for-byte identical.
*
* Snackbar dispatch goes straight to `@wordpress/notices`'s
* `noticesStore` — the wp-build polyfills already render a `SnackbarList`
* for us, and the legacy app wraps the body with its own `<GlobalNotices />`
* `noticesStore` — the wp-build polyfills already render `SnackbarNotices`
* for us, and the legacy app mounts its own `<SnackbarNotices />`
* surface, so both paths see the same snackbars without a shared rendering
* primitive.
*
Expand Down Expand Up @@ -188,9 +188,8 @@ export function NewsletterSettingsBody( {
// Get newsletter script data.
const newsletterScriptData = useMemo( () => getNewsletterScriptData(), [] );

// Snackbar notices via core data — `type: 'snackbar'` matches what the
// legacy `useGlobalNotices` defaulted to, so both legacy and modernized
// surfaces render the same snackbars.
// Snackbar notices via core data — `type: 'snackbar'` is required so
// both legacy and modernized surfaces render them in SnackbarNotices.
const { createNotice } = useDispatch( noticesStore );
const createSuccessNotice = useCallback(
( content: string ) => createNotice( 'success', content, { type: 'snackbar' } ),
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/newsletter/src/settings/style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@use "@automattic/jetpack-base-styles/admin-page-layout" as *;
@use "@wordpress/base-styles/mixins" as gb;
@use "sass:meta";

// This stylesheet feeds two bundles: the wp-build dashboard
Expand Down Expand Up @@ -115,3 +116,9 @@ body.jetpack_page_jetpack-newsletter {
}
}
}

// Core snackbar placement (matches `@wordpress/boot`'s snackbar-container).
.jetpack-newsletter-snackbar-notices {

@include gb.snackbar-container();
}
6 changes: 1 addition & 5 deletions projects/packages/newsletter/tests/settings.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@ jest.mock( '@wordpress/components', () => ( {

jest.mock( '@wordpress/notices', () => ( {
store: {},
SnackbarNotices: () => null,
} ) );

jest.mock( '@automattic/jetpack-components', () => ( {
AdminPage: ( { children } ) => <div data-testid="admin-page">{ children }</div>,
Col: ( { children } ) => <div>{ children }</div>,
Container: ( { children } ) => <div>{ children }</div>,
GlobalNotices: () => null,
useGlobalNotices: () => ( {
createSuccessNotice: jest.fn(),
createErrorNotice: jest.fn(),
} ),
} ) );

// Capture the parent's wired props for `SubscribeModalSection` so the
Expand Down
Loading
Loading