From 9475e4df6dc3775ff346409c78b25d580cdbb7f0 Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Fri, 16 Oct 2026 12:12:10 -0700 Subject: [PATCH 1/5] Protect Dashboard: Redesign the Login protection card --- .../update-protect-dashboard-login-card | 4 + .../login-protection-card.tsx | 206 ++++++++---------- .../login-protection-settings-card.tsx | 73 +------ .../sections/login-protection/sso-toggle.tsx | 90 ++++++++ .../sections/login-protection/style.scss | 29 +++ .../test/login-protection-card.test.tsx | 65 +++--- .../update-protect-dashboard-login-card | 5 + 7 files changed, 261 insertions(+), 211 deletions(-) create mode 100644 projects/packages/protect/changelog/update-protect-dashboard-login-card create mode 100644 projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx create mode 100644 projects/packages/protect/routes/dashboard/sections/login-protection/style.scss create mode 100644 projects/plugins/jetpack/changelog/update-protect-dashboard-login-card diff --git a/projects/packages/protect/changelog/update-protect-dashboard-login-card b/projects/packages/protect/changelog/update-protect-dashboard-login-card new file mode 100644 index 000000000000..61da80e93c3b --- /dev/null +++ b/projects/packages/protect/changelog/update-protect-dashboard-login-card @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Dashboard: Redesign the Login protection card with toggles for account protection and Secure Sign On. diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx index 2987b2fa0d73..c48a501801e1 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx @@ -1,76 +1,22 @@ -import { __, sprintf } from '@wordpress/i18n'; -import { lock } from '@wordpress/icons'; -import { Badge, Stack, Text } from '@wordpress/ui'; +import { useMemo } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { key } from '@wordpress/icons'; +import { Notice, Stack } from '@wordpress/ui'; import { CardRow, ProtectCard, Stat } from '../../components/card'; +import SettingToggle from '../../components/settings/setting-toggle'; import TabLink from '../../components/tab-link'; import isModuleActive from '../../data/is-module-active'; -import type { LoginProtectionContext, ModuleState } from './types'; -import type { CardStatus } from '../../components/card'; - -/** Opens the Settings tab at this section's card. */ -const SETTINGS_PARAMS = { section: 'loginProtection' }; - -/** - * One way of protecting logins, with whether it's on. - * - * @param props - Component props. - * @param props.name - The method's name. - * @param props.description - What it does. - * @param props.state - Its module's live state. - * @return The row. - */ -function Method( { - name, - description, - state, -}: { - name: string; - description: string; - state: ModuleState; -} ) { - let badge = { __( 'Off', 'jetpack-protect-pkg' ) }; - if ( ! state.available ) { - badge = { __( 'Unavailable', 'jetpack-protect-pkg' ) }; - } else if ( state.active ) { - badge = { __( 'On', 'jetpack-protect-pkg' ) }; - } - - return ( - - - - { name } - - { description } - - - { badge } - - - ); -} +import SsoToggle from './sso-toggle'; +import type { LoginProtectionContext } from './types'; +import './style.scss'; /** - * The card badge's intent: neutral when no method is available. - * - * @param on - How many methods are on. - * @param available - How many methods are available. - * @return The intent. - */ -function getStatusIntent( on: number, available: number ): CardStatus[ 'intent' ] { - if ( available === 0 ) { - return 'none'; - } - return on === available ? 'stable' : 'medium'; -} - -/** - * The Login protection card: brute force protection, account protection and WordPress.com login. + * The Login protection card: blocked logins, and toggles for account protection and WordPress.com login. * * @param props - The dashboard context. * @param props.state - This section's state from PHP. * @param props.settings - Jetpack settings, for live module state. - * @param props.openTab - Switches dashboard tabs, for the link to Settings. + * @param props.openTab - Switches dashboard tabs, for the links to Settings. * @return The card, or nothing without the section's state. */ export default function LoginProtectionCard( { @@ -78,73 +24,95 @@ export default function LoginProtectionCard( { settings, openTab, }: LoginProtectionContext ) { - if ( ! login ) { + // Settings load on the Settings tab; until then the toggles show the page-load state. + const data = useMemo( + () => + login && { + ...settings, + settings: { + protect: login.bruteForce.active, + 'account-protection': login.accountProtection.active, + sso: login.sso.active, + ...settings.settings, + }, + }, + [ settings, login ] + ); + + if ( ! login || ! data ) { return null; } - const live = ( module: string, initial: ModuleState ): ModuleState => ( { - available: initial.available, - active: isModuleActive( settings.settings, module, initial.active ), - } ); - const bruteForce = live( 'protect', login.bruteForce ); - const accountProtection = live( 'account-protection', login.accountProtection ); - // The module can stay active after the owner disconnects, but WordPress.com login doesn't run then. - const sso = live( 'sso', { ...login.sso, available: login.sso.available && login.ssoUsable } ); - - const methods = [ bruteForce, accountProtection, sso ]; - const on = methods.filter( method => method.available && method.active ).length; - const available = methods.filter( method => method.available ).length; + const bruteForceOn = isModuleActive( settings.settings, 'protect', login.bruteForce.active ); + const ssoOn = Boolean( data.settings.sso ) && login.sso.available && login.ssoUsable; return ( - { bruteForce.active && ( + { settings.error && ( - + + { settings.error } + + ) } - - - - - { __( 'Configure login protection', 'jetpack-protect-pkg' ) } - + { bruteForceOn ? ( + + + + { __( 'Configure Brute Force Protection', 'jetpack-protect-pkg' ) } + + + ) : ( + + ) } + + +
+ +
+ + + { ssoOn && ( + + { __( 'Configure', 'jetpack-protect-pkg' ) } + + ) } +
); diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx index c253b01a2f8b..38a830c20320 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx @@ -1,11 +1,11 @@ import { ToggleControl } from '@wordpress/components'; -import { useCallback, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; -import { lock } from '@wordpress/icons'; -import { AlertDialog, Stack, Text } from '@wordpress/ui'; +import { key } from '@wordpress/icons'; +import { Stack, Text } from '@wordpress/ui'; import { CardRow, ProtectCard } from '../../components/card'; import IpListField from '../../components/settings/ip-list-field'; import SettingToggle from '../../components/settings/setting-toggle'; +import SsoToggle from './sso-toggle'; import type { LoginProtectionContext, LoginProtectionState } from './types'; import type { ProtectSettingsData } from '../../data/use-protect-settings'; @@ -58,31 +58,6 @@ function SsoOptionToggle( { ); } -/** - * What the WordPress.com login toggle says under it: what it does, or why it can't be changed. - * - * @param login - This section's state from PHP. - * @return The help text. - */ -function getSsoHelp( login: LoginProtectionState | undefined ): string { - if ( login && ! login.sso.available ) { - return __( 'WordPress.com login isn’t available on this site.', 'jetpack-protect-pkg' ); - } - if ( login?.isOffline ) { - return __( 'WordPress.com login isn’t available in offline mode.', 'jetpack-protect-pkg' ); - } - if ( ! login?.ssoUsable ) { - return __( - 'Needs the site owner’s WordPress.com account to be connected.', - 'jetpack-protect-pkg' - ); - } - return __( - 'If you have several sites with this on, you can log in to all of them with the same credentials.', - 'jetpack-protect-pkg' - ); -} - /** * The Login protection settings: brute force protection, account protection and WordPress.com login. * @@ -98,22 +73,10 @@ export default function LoginProtectionSettingsCard( { const settings = data.settings ?? {}; const ssoLocks = login?.ssoLocks ?? NO_LOCKS; const ssoEffective = login?.ssoEffective ?? NO_LOCKS; - const ssoUsable = login?.ssoUsable ?? false; const ssoOn = Boolean( settings.sso ); - const { save } = data; - - const [ confirmingSsoOff, setConfirmingSsoOff ] = useState( false ); - const onSsoChange = useCallback( - ( value: boolean ) => ( value ? save( { sso: true } ) : setConfirmingSsoOff( true ) ), - [ save ] - ); - // `save()` resolves to whether it worked; the dialog only needs to know it's done, then closes. - const disableSso = useCallback( async () => { - await save( { sso: false } ); - }, [ save ] ); return ( - + } className="jp-protect-card__subheading"> @@ -172,17 +135,17 @@ export default function LoginProtectionSettingsCard( { } className="jp-protect-card__subheading"> { __( 'WordPress.com login', 'jetpack-protect-pkg' ) } - - - - ); } diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx new file mode 100644 index 000000000000..1ebaf7f7ba12 --- /dev/null +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx @@ -0,0 +1,90 @@ +import { ToggleControl } from '@wordpress/components'; +import { useCallback, useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { AlertDialog } from '@wordpress/ui'; +import type { LoginProtectionState } from './types'; +import type { ProtectSettingsData } from '../../data/use-protect-settings'; + +/** + * Why WordPress.com login can't be turned on, if it can't. + * + * @param login - This section's state from PHP. + * @return The reason, or null when it can be used. + */ +function getSsoBlocker( login: LoginProtectionState | undefined ): string | null { + if ( login && ! login.sso.available ) { + return __( 'WordPress.com login isn’t available on this site.', 'jetpack-protect-pkg' ); + } + if ( login?.isOffline ) { + return __( 'WordPress.com login isn’t available in offline mode.', 'jetpack-protect-pkg' ); + } + if ( ! login?.ssoUsable ) { + return __( + 'Needs the site owner’s WordPress.com account to be connected.', + 'jetpack-protect-pkg' + ); + } + return null; +} + +/** + * The WordPress.com login toggle, which asks before turning it off. + * + * @param props - Component props. + * @param props.data - The settings and their save function. + * @param props.login - This section's state from PHP. + * @param props.label - The toggle's label. + * @param props.help - What it does, shown unless it can't be used. + * @return The toggle. + */ +export default function SsoToggle( { + data, + login, + label, + help, +}: { + data: ProtectSettingsData; + login: LoginProtectionState | undefined; + label: string; + help: string; +} ) { + const { save } = data; + const blocker = getSsoBlocker( login ); + const [ confirmingOff, setConfirmingOff ] = useState( false ); + const onChange = useCallback( + ( value: boolean ) => ( value ? save( { sso: true } ) : setConfirmingOff( true ) ), + [ save ] + ); + // `save()` resolves to whether it worked; the dialog only needs to know it's done, then closes. + const turnOff = useCallback( async () => { + await save( { sso: false } ); + }, [ save ] ); + + return ( + <> + + + + + + ); +} diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/style.scss b/projects/packages/protect/routes/dashboard/sections/login-protection/style.scss new file mode 100644 index 000000000000..201b3351b477 --- /dev/null +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/style.scss @@ -0,0 +1,29 @@ +// Two cells split by a rule; each cell takes the row's padding, +// so the rule runs edge to edge. +.jp-protect-card__row.jp-protect-login-protection__split { + display: grid; + grid-template-columns: 1fr 1fr; + padding: 0; + + > * { + box-sizing: border-box; + padding: 16px var(--wp-ui-card-padding); + } + + > * + * { + border-inline-start: + var(--wpds-border-width-xs) solid + var(--wpds-color-stroke-surface-neutral); + } + + @media (max-width: 600px) { + grid-template-columns: 1fr; + + > * + * { + border-inline-start: 0; + border-block-start: + var(--wpds-border-width-xs) solid + var(--wpds-color-stroke-surface-neutral); + } + } +} diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx index a8e9f52bfc6b..1f6accc16cae 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx @@ -6,7 +6,6 @@ import type { LoginProtectionState } from '../types'; const on = { available: true, active: true }; const off = { available: true, active: false }; -const unavailable = { available: false, active: false }; const renderCard = ( state: Partial< LoginProtectionState > = {}, @@ -25,45 +24,51 @@ const renderCard = ( ...state, } as LoginProtectionState } - settings={ { settings } as ProtectSettingsData } + settings={ + { + settings, + error: null, + isSaving: () => false, + save: jest.fn(), + } as unknown as ProtectSettingsData + } openTab={ openTab } /> ); describe( 'LoginProtectionCard', () => { it.each( [ - [ 'every method is on', {}, null, '3 of 3 on', true ], - [ 'one method is off', { sso: off }, null, '2 of 3 on', true ], - [ 'a method is unavailable', { sso: unavailable }, null, '2 of 2 on', true ], - [ 'brute force protection is off', { bruteForce: off }, null, '2 of 3 on', false ], - [ 'a method was turned off since page load', {}, { protect: false }, '2 of 3 on', false ], - [ 'a method was turned on since page load', { sso: off }, { sso: true }, '3 of 3 on', true ], - [ 'WordPress.com login is set but can’t run', { ssoUsable: false }, null, '2 of 2 on', true ], - [ - 'an unavailable method is still set', - { sso: unavailable }, - { sso: true }, - '2 of 2 on', - true, - ], - ] )( 'counts the methods that are on when %s', ( _name, state, settings, badge, hasCount ) => { - renderCard( state, settings ); + [ 'on', {}, null, true ], + [ 'off', { bruteForce: off }, null, false ], + [ 'turned off since page load', {}, { protect: false }, false ], + [ 'turned on since page load', { bruteForce: off }, { protect: true }, true ], + ] )( + 'shows blocked logins only while brute force protection is %s', + ( _name, state, settings, isOn ) => { + renderCard( state, settings ); + + expect( screen.queryByText( '1,234' ) !== null ).toBe( isOn ); + expect( + screen.queryByRole( 'checkbox', { name: 'Enable Brute Force Protection' } ) === null + ).toBe( isOn ); + } + ); + + it( 'shows the page-load state before the settings load', () => { + renderCard( { accountProtection: on, sso: off } ); - expect( screen.getByText( badge ) ).toBeInTheDocument(); - expect( screen.queryByText( '1,234' ) !== null ).toBe( hasCount ); + expect( screen.getByRole( 'checkbox', { name: 'Enable Account protection' } ) ).toBeChecked(); + expect( screen.getByRole( 'checkbox', { name: 'Enable Secure Sign On' } ) ).not.toBeChecked(); } ); it.each( [ - [ 'on', { sso: on }, { On: 1, Off: 2, Unavailable: 0 } ], - [ 'off', { sso: off }, { On: 0, Off: 3, Unavailable: 0 } ], - [ 'unavailable', { sso: unavailable }, { On: 0, Off: 2, Unavailable: 1 } ], - [ 'on but unable to run', { sso: on, ssoUsable: false }, { On: 0, Off: 2, Unavailable: 1 } ], - ] )( 'badges a method that is %s', ( _name, state, badges ) => { - renderCard( { bruteForce: off, accountProtection: off, ...state } ); + [ 'on', {}, true ], + [ 'off', { sso: off }, false ], + [ 'on but unable to run', { ssoUsable: false }, false ], + ] )( 'links to WordPress.com login settings only while it is %s', ( _name, state, hasLink ) => { + renderCard( state ); - Object.entries( badges ).forEach( ( [ text, count ] ) => - expect( screen.queryAllByText( text ) ).toHaveLength( count ) - ); + expect( screen.queryByRole( 'link', { name: 'Configure' } ) !== null ).toBe( hasLink ); } ); it( 'renders nothing when PHP registered no section', () => { @@ -84,7 +89,7 @@ describe( 'LoginProtectionCard', () => { await userEvent .setup() - .click( screen.getByRole( 'link', { name: 'Configure login protection' } ) ); + .click( screen.getByRole( 'link', { name: 'Configure Brute Force Protection' } ) ); expect( openTab ).toHaveBeenCalledWith( 'settings', { section: 'loginProtection' } ); } ); diff --git a/projects/plugins/jetpack/changelog/update-protect-dashboard-login-card b/projects/plugins/jetpack/changelog/update-protect-dashboard-login-card new file mode 100644 index 000000000000..354ff4c05616 --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-protect-dashboard-login-card @@ -0,0 +1,5 @@ +Significance: patch +Type: other +Comment: Updates the Login protection card added in an unreleased change, which already has an entry. + + From 1ddbb9f0ad120ceac9cb590cf77aaea3e49a62fc Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Fri, 16 Oct 2026 12:45:36 -0700 Subject: [PATCH 2/5] Protect Dashboard: Share the divided card row and settings error row --- .../protect/routes/dashboard/route.scss | 32 ++++++++++++ .../login-protection-card.tsx | 51 +++++++------------ .../sections/login-protection/sso-toggle.tsx | 2 +- .../sections/login-protection/style.scss | 29 ----------- .../dashboard/sections/scan/scan-card.tsx | 2 +- .../routes/dashboard/sections/scan/style.scss | 33 ++---------- 6 files changed, 57 insertions(+), 92 deletions(-) delete mode 100644 projects/packages/protect/routes/dashboard/sections/login-protection/style.scss diff --git a/projects/packages/protect/routes/dashboard/route.scss b/projects/packages/protect/routes/dashboard/route.scss index 2281b2fcebee..65d78b7001f9 100644 --- a/projects/packages/protect/routes/dashboard/route.scss +++ b/projects/packages/protect/routes/dashboard/route.scss @@ -42,6 +42,38 @@ body.toplevel_page_jetpack-protect { border-block-start: var(--wpds-border-width-xs) solid var(--wpds-color-stroke-surface-neutral); } + // Cells split by rules; each cell takes the row's padding, + // so the rules run edge to edge. + &__row.jp-protect-card__cells { + --jp-protect-cells: 2; + + display: grid; + grid-template-columns: repeat(var(--jp-protect-cells), minmax(0, 1fr)); + padding: 0; + + > * { + box-sizing: border-box; + padding: 16px var(--wp-ui-card-padding); + } + + > * + * { + border-inline-start: + var(--wpds-border-width-xs) solid + var(--wpds-color-stroke-surface-neutral); + } + + @media (max-width: 600px) { + grid-template-columns: minmax(0, 1fr); + + > * + * { + border-inline-start: 0; + border-block-start: + var(--wpds-border-width-xs) solid + var(--wpds-color-stroke-surface-neutral); + } + } + } + &__muted { color: var(--wpds-color-foreground-content-neutral-weak); } diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx index c48a501801e1..9a5f73fe5282 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx @@ -1,14 +1,11 @@ -import { useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { key } from '@wordpress/icons'; -import { Notice, Stack } from '@wordpress/ui'; -import { CardRow, ProtectCard, Stat } from '../../components/card'; +import { Stack } from '@wordpress/ui'; +import { CardRow, ProtectCard, SettingsErrorRow, Stat } from '../../components/card'; import SettingToggle from '../../components/settings/setting-toggle'; import TabLink from '../../components/tab-link'; -import isModuleActive from '../../data/is-module-active'; -import SsoToggle from './sso-toggle'; +import SsoToggle, { getSsoBlocker } from './sso-toggle'; import type { LoginProtectionContext } from './types'; -import './style.scss'; /** * The Login protection card: blocked logins, and toggles for account protection and WordPress.com login. @@ -24,27 +21,22 @@ export default function LoginProtectionCard( { settings, openTab, }: LoginProtectionContext ) { - // Settings load on the Settings tab; until then the toggles show the page-load state. - const data = useMemo( - () => - login && { - ...settings, - settings: { - protect: login.bruteForce.active, - 'account-protection': login.accountProtection.active, - sso: login.sso.active, - ...settings.settings, - }, - }, - [ settings, login ] - ); - - if ( ! login || ! data ) { + if ( ! login ) { return null; } - const bruteForceOn = isModuleActive( settings.settings, 'protect', login.bruteForce.active ); - const ssoOn = Boolean( data.settings.sso ) && login.sso.available && login.ssoUsable; + // Settings load on the Settings tab; until then the toggles show the page-load state. + const data = { + ...settings, + settings: { + protect: login.bruteForce.active, + 'account-protection': login.accountProtection.active, + sso: login.sso.active, + ...settings.settings, + }, + }; + const bruteForceOn = Boolean( data.settings.protect ); + const ssoOn = Boolean( data.settings.sso ) && getSsoBlocker( login ) === null; return ( - { settings.error && ( - - - { settings.error } - - - - ) } + { bruteForceOn ? ( @@ -84,7 +69,7 @@ export default function LoginProtectionCard( { /> ) } - +
* { - box-sizing: border-box; - padding: 16px var(--wp-ui-card-padding); - } - - > * + * { - border-inline-start: - var(--wpds-border-width-xs) solid - var(--wpds-color-stroke-surface-neutral); - } - - @media (max-width: 600px) { - grid-template-columns: 1fr; - - > * + * { - border-inline-start: 0; - border-block-start: - var(--wpds-border-width-xs) solid - var(--wpds-color-stroke-surface-neutral); - } - } -} diff --git a/projects/packages/protect/routes/dashboard/sections/scan/scan-card.tsx b/projects/packages/protect/routes/dashboard/sections/scan/scan-card.tsx index 4f4f1962eb57..3a4ebacad08a 100644 --- a/projects/packages/protect/routes/dashboard/sections/scan/scan-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/scan/scan-card.tsx @@ -181,7 +181,7 @@ export default function ScanCard( { openTab }: Pick< DashboardContext, 'openTab' body = ( <> { hasThreats && ( - + * { - padding: 20px var(--wp-ui-card-padding); - } - - > * + * { - border-inline-start: - var(--wpds-border-width-xs) solid - var(--wpds-color-stroke-surface-neutral); - } - - @media (max-width: 600px) { - grid-template-columns: minmax(0, 1fr); - - > * + * { - border-inline-start: 0; - border-block-start: - var(--wpds-border-width-xs) solid - var(--wpds-color-stroke-surface-neutral); - } + padding-block: 20px; } } + // Full-bleed, so the threat list sets its own padding. &__row.jp-protect-card__threats { - padding-block-end: 8px; + padding: 0 0 8px; } } From 3fe7ea34624a7587617fc5744ff5a90e3c155c84 Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Fri, 16 Oct 2026 15:06:51 -0700 Subject: [PATCH 3/5] Protect Dashboard: Scope settings errors to each card and explain unavailable login toggles A failed save now records its keys, so the Login and Firewall cards only show their own errors. Unavailable brute force and account protection toggles say why, the Secure Sign On link names its feature, and focus stays in the brute force row when its toggle swaps out. Co-Authored-By: Claude Opus 5.5 --- .../routes/dashboard/components/card.tsx | 15 ++- .../data/test/use-protect-settings.test.ts | 2 + .../dashboard/data/use-protect-settings.ts | 43 +++++-- .../sections/firewall/overview-card.tsx | 5 +- .../login-protection-card.tsx | 115 +++++++++++++----- .../test/login-protection-card.test.tsx | 111 ++++++++++++----- .../sections/monitor/monitor-card.tsx | 5 +- 7 files changed, 219 insertions(+), 77 deletions(-) diff --git a/projects/packages/protect/routes/dashboard/components/card.tsx b/projects/packages/protect/routes/dashboard/components/card.tsx index ab63b71438b4..a877f5ea170c 100644 --- a/projects/packages/protect/routes/dashboard/components/card.tsx +++ b/projects/packages/protect/routes/dashboard/components/card.tsx @@ -133,14 +133,21 @@ export function UnavailableCard( { icon, title }: { icon: ReactElement; title: s } /** - * A row showing the last settings save error, if any. + * A row showing why the last save of one of the card's settings failed, until dismissed. * * @param props - Component props. * @param props.settings - Jetpack settings, with their error. - * @return The row, or nothing. + * @param props.keys - The settings this card saves. + * @return The row, or nothing without an error from one of `keys`. */ -export function SettingsErrorRow( { settings }: { settings: ProtectSettingsData } ) { - if ( ! settings.error ) { +export function SettingsErrorRow( { + settings, + keys, +}: { + settings: ProtectSettingsData; + keys: string[]; +} ) { + if ( ! settings.error || ! settings.errorKeys.some( key => keys.includes( key ) ) ) { return null; } return ( diff --git a/projects/packages/protect/routes/dashboard/data/test/use-protect-settings.test.ts b/projects/packages/protect/routes/dashboard/data/test/use-protect-settings.test.ts index 8679f1e62cd6..a2d05459c4ed 100644 --- a/projects/packages/protect/routes/dashboard/data/test/use-protect-settings.test.ts +++ b/projects/packages/protect/routes/dashboard/data/test/use-protect-settings.test.ts @@ -85,6 +85,7 @@ describe( 'useProtectSettings', () => { expect( result.current.settings ).toEqual( expected ); expect( result.current.error ).toBe( 'Nope' ); + expect( result.current.errorKeys ).toEqual( [ 'list' ] ); expect( result.current.isSaving( 'list' ) ).toBe( false ); } ); @@ -149,6 +150,7 @@ describe( 'useProtectSettings', () => { const { result } = renderHook( () => useProtectSettings() ); await act( async () => result.current.load() ); expect( result.current.error ).toEqual( expect.any( String ) ); + expect( result.current.errorKeys ).toEqual( [] ); expect( result.current.isLoaded ).toBe( false ); const retry = defer(); diff --git a/projects/packages/protect/routes/dashboard/data/use-protect-settings.ts b/projects/packages/protect/routes/dashboard/data/use-protect-settings.ts index 18b6ec9cd8d3..d86f3fe0b44d 100644 --- a/projects/packages/protect/routes/dashboard/data/use-protect-settings.ts +++ b/projects/packages/protect/routes/dashboard/data/use-protect-settings.ts @@ -7,6 +7,9 @@ import { store as noticesStore } from '@wordpress/notices'; // One id, so a burst of saves shows one snackbar. const SAVED_NOTICE_ID = 'jetpack-protect-settings-saved'; +type Failure = { message: string; keys: string[] }; +const NO_KEYS: string[] = []; + /** Flat Jetpack settings, keyed by option or module slug, as `jetpack/v4/settings` returns them. */ export type ProtectSettings = Record< string, unknown >; @@ -22,6 +25,8 @@ export type ProtectSettingsData = { isLoaded: boolean; waf: WafConfig | null; error: string | null; + /** The keys whose save failed, so a card shows only its own errors; empty when loading failed. */ + errorKeys: string[]; dismissError: () => void; isSaving: ( key: string ) => boolean; /** Fetches the settings once; later calls do nothing unless the fetch failed. */ @@ -43,7 +48,7 @@ export default function useProtectSettings(): ProtectSettingsData { const [ settings, setSettingsState ] = useState< ProtectSettings | null >( null ); const [ waf, setWaf ] = useState< WafConfig | null >( null ); const [ isLoaded, setIsLoaded ] = useState( false ); - const [ error, setError ] = useState< string | null >( null ); + const [ failure, setFailure ] = useState< Failure | null >( null ); const [ saving, setSaving ] = useState< string[] >( [] ); const settingsRef = useRef< ProtectSettings | null >( null ); const loadStarted = useRef( false ); @@ -60,7 +65,7 @@ export default function useProtectSettings(): ProtectSettingsData { return; } loadStarted.current = true; - setError( null ); + setFailure( null ); Promise.all( [ apiFetch< ProtectSettings >( { path: '/jetpack/v4/settings' } ), @@ -77,7 +82,10 @@ export default function useProtectSettings(): ProtectSettingsData { } ) .catch( () => { loadStarted.current = false; - setError( __( 'Your settings couldn’t be loaded.', 'jetpack-protect-pkg' ) ); + setFailure( { + message: __( 'Your settings couldn’t be loaded.', 'jetpack-protect-pkg' ), + keys: NO_KEYS, + } ); } ); }, [ setSettings ] ); @@ -92,7 +100,7 @@ export default function useProtectSettings(): ProtectSettingsData { keys.forEach( key => savedKeys.current.add( key ) ); setSettings( { ...before, ...patch } ); setSaving( current => [ ...current, ...keys ] ); - setError( null ); + setFailure( null ); let saved = true; try { @@ -116,10 +124,12 @@ export default function useProtectSettings(): ProtectSettingsData { } } ); setSettings( rolledBack ); - setError( - ( e as { message?: string } )?.message || - __( 'Your change couldn’t be saved. Try again.', 'jetpack-protect-pkg' ) - ); + setFailure( { + message: + ( e as { message?: string } )?.message || + __( 'Your change couldn’t be saved. Try again.', 'jetpack-protect-pkg' ), + keys, + } ); saved = false; } finally { // One entry per save in flight, so an overlapping save of the same key keeps it busy. @@ -148,10 +158,21 @@ export default function useProtectSettings(): ProtectSettingsData { ); const isSaving = useCallback( ( key: string ) => saving.includes( key ), [ saving ] ); - const dismissError = useCallback( () => setError( null ), [] ); + const dismissError = useCallback( () => setFailure( null ), [] ); return useMemo( - () => ( { settings, isLoaded, waf, error, dismissError, isSaving, load, save, refresh } ), - [ settings, isLoaded, waf, error, dismissError, isSaving, load, save, refresh ] + () => ( { + settings, + isLoaded, + waf, + error: failure?.message ?? null, + errorKeys: failure?.keys ?? NO_KEYS, + dismissError, + isSaving, + load, + save, + refresh, + } ), + [ settings, isLoaded, waf, failure, dismissError, isSaving, load, save, refresh ] ); } diff --git a/projects/packages/protect/routes/dashboard/sections/firewall/overview-card.tsx b/projects/packages/protect/routes/dashboard/sections/firewall/overview-card.tsx index a68a4c123637..ca9f357a25e9 100644 --- a/projects/packages/protect/routes/dashboard/sections/firewall/overview-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/firewall/overview-card.tsx @@ -41,6 +41,9 @@ const UNAVAILABLE: FirewallState = { ipRules: {}, }; +// The only setting this card saves. +const FIREWALL_KEYS = [ 'waf' ]; + // One id for the test's snackbars, so the result replaces "Testing the firewall…". const NOTICE_ID = 'jetpack-protect-firewall-test'; @@ -277,7 +280,7 @@ export default function FirewallOverviewCard( { state, settings, openTab }: Fire onOpenChange={ setDetailsOpen } /> ) } - + { ! active && ( diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx index 9a5f73fe5282..1d6d5aeff671 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx @@ -1,3 +1,4 @@ +import { useEffect, useRef } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { key } from '@wordpress/icons'; import { Stack } from '@wordpress/ui'; @@ -5,7 +6,80 @@ import { CardRow, ProtectCard, SettingsErrorRow, Stat } from '../../components/c import SettingToggle from '../../components/settings/setting-toggle'; import TabLink from '../../components/tab-link'; import SsoToggle, { getSsoBlocker } from './sso-toggle'; -import type { LoginProtectionContext } from './types'; +import type { LoginProtectionContext, LoginProtectionState } from './types'; +import type { ProtectSettingsData } from '../../data/use-protect-settings'; + +// The settings this card saves. +const LOGIN_KEYS = [ 'protect', 'account-protection', 'sso' ]; + +/** + * Blocked logins while brute force protection is on, else a toggle to turn it on. + * + * @param props - Component props. + * @param props.data - The settings, with page-load state filled in. + * @param props.login - This section's state from PHP. + * @param props.openTab - Switches dashboard tabs, for the link to Settings. + * @return The row. + */ +function BruteForceRow( { + data, + login, + openTab, +}: { + data: ProtectSettingsData; + login: LoginProtectionState; + openTab: LoginProtectionContext[ 'openTab' ]; +} ) { + const isOn = Boolean( data.settings?.protect ); + const rowRef = useRef< HTMLDivElement >( null ); + const wasOn = useRef( isOn ); + + useEffect( () => { + const row = rowRef.current; + // Switching swaps out the focused control, so keep keyboard focus in the row. + if ( + row && + isOn !== wasOn.current && + row.ownerDocument.activeElement === row.ownerDocument.body + ) { + row.querySelector< HTMLElement >( 'a, input' )?.focus(); + } + wasOn.current = isOn; + }, [ isOn ] ); + + return ( + +
+ { isOn ? ( + + + + { __( 'Configure Brute Force Protection', 'jetpack-protect-pkg' ) } + + + ) : ( + + ) } +
+
+ ); +} /** * The Login protection card: blocked logins, and toggles for account protection and WordPress.com login. @@ -35,7 +109,6 @@ export default function LoginProtectionCard( { ...settings.settings, }, }; - const bruteForceOn = Boolean( data.settings.protect ); const ssoOn = Boolean( data.settings.sso ) && getSsoBlocker( login ) === null; return ( @@ -47,28 +120,8 @@ export default function LoginProtectionCard( { 'jetpack-protect-pkg' ) } > - - - { bruteForceOn ? ( - - - - { __( 'Configure Brute Force Protection', 'jetpack-protect-pkg' ) } - - - ) : ( - - ) } - + +
@@ -94,7 +151,7 @@ export default function LoginProtectionCard( { /> { ssoOn && ( - { __( 'Configure', 'jetpack-protect-pkg' ) } + { __( 'Configure Secure Sign On', 'jetpack-protect-pkg' ) } ) } diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx index 1f6accc16cae..10f6e171f481 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx @@ -6,35 +6,37 @@ import type { LoginProtectionState } from '../types'; const on = { available: true, active: true }; const off = { available: true, active: false }; +const unavailable = { available: false, active: false }; -const renderCard = ( +const card = ( state: Partial< LoginProtectionState > = {}, - settings: ProtectSettingsData[ 'settings' ] = null, + settings: Partial< ProtectSettingsData > = {}, openTab = jest.fn() -) => - render( - false, - save: jest.fn(), - } as unknown as ProtectSettingsData - } - openTab={ openTab } - /> - ); +) => ( + false, + save: jest.fn(), + ...settings, + } as unknown as ProtectSettingsData + } + openTab={ openTab } + /> +); describe( 'LoginProtectionCard', () => { it.each( [ @@ -45,7 +47,7 @@ describe( 'LoginProtectionCard', () => { ] )( 'shows blocked logins only while brute force protection is %s', ( _name, state, settings, isOn ) => { - renderCard( state, settings ); + render( card( state, { settings } ) ); expect( screen.queryByText( '1,234' ) !== null ).toBe( isOn ); expect( @@ -55,20 +57,67 @@ describe( 'LoginProtectionCard', () => { ); it( 'shows the page-load state before the settings load', () => { - renderCard( { accountProtection: on, sso: off } ); + render( card( { accountProtection: on, sso: off } ) ); expect( screen.getByRole( 'checkbox', { name: 'Enable Account protection' } ) ).toBeChecked(); expect( screen.getByRole( 'checkbox', { name: 'Enable Secure Sign On' } ) ).not.toBeChecked(); } ); + it.each( [ + [ 'Enable Brute Force Protection', { bruteForce: off }, { protect: true } ], + [ 'Enable Account protection', { accountProtection: off }, { 'account-protection': true } ], + ] )( 'saves “%s” when it is switched on', async ( name, state, patch ) => { + const save = jest.fn(); + render( card( state, { save } ) ); + + await userEvent.setup().click( screen.getByRole( 'checkbox', { name } ) ); + + expect( save ).toHaveBeenCalledWith( patch ); + } ); + + it.each( [ + [ 'Enable Brute Force Protection', { bruteForce: unavailable }, 'Brute force protection' ], + [ 'Enable Account protection', { accountProtection: unavailable }, 'Account protection' ], + ] )( 'says why “%s” is disabled when unavailable', ( name, state, feature ) => { + render( card( state ) ); + + expect( screen.getByRole( 'checkbox', { name } ) ).toBeDisabled(); + expect( screen.getByText( `${ feature } isn’t available on this site.` ) ).toBeInTheDocument(); + } ); + + it( 'keeps keyboard focus in the row when brute force protection turns on', async () => { + const { rerender } = render( card( { bruteForce: off } ) ); + await userEvent + .setup() + .click( screen.getByRole( 'checkbox', { name: 'Enable Brute Force Protection' } ) ); + + rerender( card( { bruteForce: off }, { settings: { protect: true } } ) ); + + expect( + screen.getByRole( 'link', { name: 'Configure Brute Force Protection' } ) + ).toHaveFocus(); + } ); + + it.each( [ + [ 'one of its settings', [ 'sso' ], true ], + [ 'another card’s setting', [ 'waf' ], false ], + [ 'loading the settings', [], false ], + ] )( 'shows a failure from %s only when it is the card’s own', ( _name, errorKeys, isShown ) => { + render( card( {}, { error: 'Nope', errorKeys } ) ); + + expect( screen.queryByText( 'Nope' ) !== null ).toBe( isShown ); + } ); + it.each( [ [ 'on', {}, true ], [ 'off', { sso: off }, false ], [ 'on but unable to run', { ssoUsable: false }, false ], ] )( 'links to WordPress.com login settings only while it is %s', ( _name, state, hasLink ) => { - renderCard( state ); + render( card( state ) ); - expect( screen.queryByRole( 'link', { name: 'Configure' } ) !== null ).toBe( hasLink ); + expect( screen.queryByRole( 'link', { name: 'Configure Secure Sign On' } ) !== null ).toBe( + hasLink + ); } ); it( 'renders nothing when PHP registered no section', () => { @@ -85,7 +134,7 @@ describe( 'LoginProtectionCard', () => { it( 'opens the Settings tab from its link', async () => { const openTab = jest.fn(); - renderCard( {}, null, openTab ); + render( card( {}, {}, openTab ) ); await userEvent .setup() diff --git a/projects/packages/protect/routes/dashboard/sections/monitor/monitor-card.tsx b/projects/packages/protect/routes/dashboard/sections/monitor/monitor-card.tsx index 05aed29a0968..6c20e09af94f 100644 --- a/projects/packages/protect/routes/dashboard/sections/monitor/monitor-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/monitor/monitor-card.tsx @@ -21,6 +21,9 @@ const PERCENT = { style: 'percent', maximumFractionDigits: 3 } as const; /** Opens the Settings tab at this section's card. */ const SETTINGS_PARAMS = { section: 'monitor' }; +// The only setting this card saves. +const MONITOR_KEYS = [ 'monitor' ]; + /** * A day's date, in the site's date format. * @@ -176,7 +179,7 @@ export default function MonitorCard( { state: monitor, settings, openTab }: Moni 'jetpack-protect-pkg' ) } > - + { ! active && ( From 625fae55d219333c0721696a0563d730cefbb5f5 Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Fri, 16 Oct 2026 15:38:40 -0700 Subject: [PATCH 4/5] Protect Dashboard: Call SSO "WordPress.com login (SSO)" and sentence-case the login toggles Matches the name Jetpack Settings and the connectors card use, so the Overview toggle, its link, the Settings heading and the turn-off dialog all name the same feature. Co-Authored-By: Claude Opus 5.5 --- .../login-protection-card.tsx | 10 +++---- .../login-protection-settings-card.tsx | 2 +- .../sections/login-protection/sso-toggle.tsx | 2 +- .../test/login-protection-card.test.tsx | 28 ++++++++++--------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx index 1d6d5aeff671..69c8f171ca0e 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx @@ -57,7 +57,7 @@ function BruteForceRow( { value={ login.blockedCount } /> - { __( 'Configure Brute Force Protection', 'jetpack-protect-pkg' ) } + { __( 'Configure brute force protection', 'jetpack-protect-pkg' ) } ) : ( @@ -65,7 +65,7 @@ function BruteForceRow( { data={ data } name="protect" disabled={ ! login.bruteForce.available } - label={ __( 'Enable Brute Force Protection', 'jetpack-protect-pkg' ) } + label={ __( 'Enable brute force protection', 'jetpack-protect-pkg' ) } help={ login.bruteForce.available ? __( 'Block IP addresses that keep failing to log in.', 'jetpack-protect-pkg' ) @@ -128,7 +128,7 @@ export default function LoginProtectionCard( { data={ data } name="account-protection" disabled={ ! login.accountProtection.available } - label={ __( 'Enable Account protection', 'jetpack-protect-pkg' ) } + label={ __( 'Enable account protection', 'jetpack-protect-pkg' ) } help={ login.accountProtection.available ? __( @@ -143,7 +143,7 @@ export default function LoginProtectionCard( { { ssoOn && ( - { __( 'Configure Secure Sign On', 'jetpack-protect-pkg' ) } + { __( 'Configure WordPress.com login (SSO)', 'jetpack-protect-pkg' ) } ) } diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx index 38a830c20320..2869e7408d2e 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx @@ -133,7 +133,7 @@ export default function LoginProtectionSettingsCard( { } className="jp-protect-card__subheading"> - { __( 'WordPress.com login', 'jetpack-protect-pkg' ) } + { __( 'WordPress.com login (SSO)', 'jetpack-protect-pkg' ) } { expect( screen.queryByText( '1,234' ) !== null ).toBe( isOn ); expect( - screen.queryByRole( 'checkbox', { name: 'Enable Brute Force Protection' } ) === null + screen.queryByRole( 'checkbox', { name: 'Enable brute force protection' } ) === null ).toBe( isOn ); } ); @@ -59,13 +59,15 @@ describe( 'LoginProtectionCard', () => { it( 'shows the page-load state before the settings load', () => { render( card( { accountProtection: on, sso: off } ) ); - expect( screen.getByRole( 'checkbox', { name: 'Enable Account protection' } ) ).toBeChecked(); - expect( screen.getByRole( 'checkbox', { name: 'Enable Secure Sign On' } ) ).not.toBeChecked(); + expect( screen.getByRole( 'checkbox', { name: 'Enable account protection' } ) ).toBeChecked(); + expect( + screen.getByRole( 'checkbox', { name: 'Enable WordPress.com login (SSO)' } ) + ).not.toBeChecked(); } ); it.each( [ - [ 'Enable Brute Force Protection', { bruteForce: off }, { protect: true } ], - [ 'Enable Account protection', { accountProtection: off }, { 'account-protection': true } ], + [ 'Enable brute force protection', { bruteForce: off }, { protect: true } ], + [ 'Enable account protection', { accountProtection: off }, { 'account-protection': true } ], ] )( 'saves “%s” when it is switched on', async ( name, state, patch ) => { const save = jest.fn(); render( card( state, { save } ) ); @@ -76,8 +78,8 @@ describe( 'LoginProtectionCard', () => { } ); it.each( [ - [ 'Enable Brute Force Protection', { bruteForce: unavailable }, 'Brute force protection' ], - [ 'Enable Account protection', { accountProtection: unavailable }, 'Account protection' ], + [ 'Enable brute force protection', { bruteForce: unavailable }, 'Brute force protection' ], + [ 'Enable account protection', { accountProtection: unavailable }, 'Account protection' ], ] )( 'says why “%s” is disabled when unavailable', ( name, state, feature ) => { render( card( state ) ); @@ -89,12 +91,12 @@ describe( 'LoginProtectionCard', () => { const { rerender } = render( card( { bruteForce: off } ) ); await userEvent .setup() - .click( screen.getByRole( 'checkbox', { name: 'Enable Brute Force Protection' } ) ); + .click( screen.getByRole( 'checkbox', { name: 'Enable brute force protection' } ) ); rerender( card( { bruteForce: off }, { settings: { protect: true } } ) ); expect( - screen.getByRole( 'link', { name: 'Configure Brute Force Protection' } ) + screen.getByRole( 'link', { name: 'Configure brute force protection' } ) ).toHaveFocus(); } ); @@ -115,9 +117,9 @@ describe( 'LoginProtectionCard', () => { ] )( 'links to WordPress.com login settings only while it is %s', ( _name, state, hasLink ) => { render( card( state ) ); - expect( screen.queryByRole( 'link', { name: 'Configure Secure Sign On' } ) !== null ).toBe( - hasLink - ); + expect( + screen.queryByRole( 'link', { name: 'Configure WordPress.com login (SSO)' } ) !== null + ).toBe( hasLink ); } ); it( 'renders nothing when PHP registered no section', () => { @@ -138,7 +140,7 @@ describe( 'LoginProtectionCard', () => { await userEvent .setup() - .click( screen.getByRole( 'link', { name: 'Configure Brute Force Protection' } ) ); + .click( screen.getByRole( 'link', { name: 'Configure brute force protection' } ) ); expect( openTab ).toHaveBeenCalledWith( 'settings', { section: 'loginProtection' } ); } ); From 796af658455f69f6590028769dcf43b8be2113c1 Mon Sep 17 00:00:00 2001 From: Enej Bajgoric Date: Mon, 19 Oct 2026 14:15:50 -0700 Subject: [PATCH 5/5] Protect Dashboard: Use SwitchControl and wpds padding tokens, link Login settings to its card Moves SettingToggle and the WordPress.com login toggles to SwitchControl from @wordpress/ui and drops them from the ESLint exclude list. Card row paddings use wpds dimension tokens instead of fixed pixels and the Card's internal padding variable. Both Login protection links open the Settings tab at the Login protection card, as the other Overview cards' links now do. Co-Authored-By: Claude Opus 5.5 --- .../components/settings/setting-toggle.tsx | 12 ++++------ .../protect/routes/dashboard/route.scss | 4 ++-- .../firewall/test/overview-card.test.tsx | 2 +- .../login-protection-card.tsx | 9 +++++--- .../login-protection-settings-card.tsx | 13 ++--------- .../sections/login-protection/sso-toggle.tsx | 10 ++++---- .../test/login-protection-card.test.tsx | 12 +++++----- .../login-protection-settings-card.test.tsx | 23 +++++++++++++------ .../monitor/test/monitor-card.test.tsx | 2 +- .../monitor/test/settings-card.test.tsx | 21 ++++++++++++----- .../routes/dashboard/sections/scan/style.scss | 4 ++-- tools/eslint-excludelist.json | 2 -- 12 files changed, 60 insertions(+), 54 deletions(-) diff --git a/projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx b/projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx index 5c406e8bf201..85c3fed3a77a 100644 --- a/projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx +++ b/projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx @@ -1,13 +1,12 @@ -import { ToggleControl } from '@wordpress/components'; import { useCallback } from '@wordpress/element'; +import { SwitchControl } from '@wordpress/ui'; import type { ProtectSettingsData } from '../../data/use-protect-settings'; -import type { ReactNode } from 'react'; type Props = { data: ProtectSettingsData; name: string; label: string; - help?: ReactNode; + help?: string; disabled?: boolean; }; @@ -27,13 +26,12 @@ export default function SettingToggle( { data, name, label, help, disabled = fal const onChange = useCallback( ( value: boolean ) => save( { [ name ]: value } ), [ save, name ] ); return ( - ); } diff --git a/projects/packages/protect/routes/dashboard/route.scss b/projects/packages/protect/routes/dashboard/route.scss index 65d78b7001f9..0568b734dc1b 100644 --- a/projects/packages/protect/routes/dashboard/route.scss +++ b/projects/packages/protect/routes/dashboard/route.scss @@ -38,7 +38,7 @@ body.toplevel_page_jetpack-protect { // The row is full-bleed for its divider; the padding re-aligns its contents. &__row { box-sizing: border-box; - padding: 16px var(--wp-ui-card-padding); + padding: var(--wpds-dimension-padding-lg) var(--wpds-dimension-padding-2xl); border-block-start: var(--wpds-border-width-xs) solid var(--wpds-color-stroke-surface-neutral); } @@ -53,7 +53,7 @@ body.toplevel_page_jetpack-protect { > * { box-sizing: border-box; - padding: 16px var(--wp-ui-card-padding); + padding: var(--wpds-dimension-padding-lg) var(--wpds-dimension-padding-2xl); } > * + * { diff --git a/projects/packages/protect/routes/dashboard/sections/firewall/test/overview-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/firewall/test/overview-card.test.tsx index b1d9c1dc7c72..cf93e207580e 100644 --- a/projects/packages/protect/routes/dashboard/sections/firewall/test/overview-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/firewall/test/overview-card.test.tsx @@ -118,7 +118,7 @@ describe( 'FirewallOverviewCard', () => { it( 'turns the firewall on from the card while it is off', async () => { render( ); - await userEvent.click( screen.getByRole( 'checkbox', { name: 'Enable firewall' } ) ); + await userEvent.click( screen.getByRole( 'switch', { name: 'Enable firewall' } ) ); expect( props.settings.save ).toHaveBeenCalledWith( { waf: true } ); } ); diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx index 69c8f171ca0e..fd71d4fdf56f 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx @@ -9,6 +9,9 @@ import SsoToggle, { getSsoBlocker } from './sso-toggle'; import type { LoginProtectionContext, LoginProtectionState } from './types'; import type { ProtectSettingsData } from '../../data/use-protect-settings'; +/** Opens the Settings tab at this section's card. */ +const SETTINGS_PARAMS = { section: 'loginProtection' }; + // The settings this card saves. const LOGIN_KEYS = [ 'protect', 'account-protection', 'sso' ]; @@ -42,7 +45,7 @@ function BruteForceRow( { isOn !== wasOn.current && row.ownerDocument.activeElement === row.ownerDocument.body ) { - row.querySelector< HTMLElement >( 'a, input' )?.focus(); + row.querySelector< HTMLElement >( 'a, [role="switch"]' )?.focus(); } wasOn.current = isOn; }, [ isOn ] ); @@ -56,7 +59,7 @@ function BruteForceRow( { label={ __( 'All-time blocked login attempts', 'jetpack-protect-pkg' ) } value={ login.blockedCount } /> - + { __( 'Configure brute force protection', 'jetpack-protect-pkg' ) } @@ -150,7 +153,7 @@ export default function LoginProtectionCard( { ) } /> { ssoOn && ( - + { __( 'Configure WordPress.com login (SSO)', 'jetpack-protect-pkg' ) } ) } diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx index 2869e7408d2e..38f6cdebe5e0 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx @@ -1,7 +1,6 @@ -import { ToggleControl } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { key } from '@wordpress/icons'; -import { Stack, Text } from '@wordpress/ui'; +import { Stack, SwitchControl, Text } from '@wordpress/ui'; import { CardRow, ProtectCard } from '../../components/card'; import IpListField from '../../components/settings/ip-list-field'; import SettingToggle from '../../components/settings/setting-toggle'; @@ -9,7 +8,6 @@ import SsoToggle from './sso-toggle'; import type { LoginProtectionContext, LoginProtectionState } from './types'; import type { ProtectSettingsData } from '../../data/use-protect-settings'; -const noop = () => {}; const NO_LOCKS: LoginProtectionState[ 'ssoLocks' ] = { matchByEmail: false, twoStep: false }; /** @@ -47,14 +45,7 @@ function SsoOptionToggle( { } return ( - + ); } diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx index 55b68540550c..cee84f6cb3ee 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx @@ -1,7 +1,6 @@ -import { ToggleControl } from '@wordpress/components'; import { useCallback, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; -import { AlertDialog } from '@wordpress/ui'; +import { AlertDialog, SwitchControl } from '@wordpress/ui'; import type { LoginProtectionState } from './types'; import type { ProtectSettingsData } from '../../data/use-protect-settings'; @@ -62,14 +61,13 @@ export default function SsoToggle( { return ( <> - { expect( screen.queryByText( '1,234' ) !== null ).toBe( isOn ); expect( - screen.queryByRole( 'checkbox', { name: 'Enable brute force protection' } ) === null + screen.queryByRole( 'switch', { name: 'Enable brute force protection' } ) === null ).toBe( isOn ); } ); @@ -59,9 +59,9 @@ describe( 'LoginProtectionCard', () => { it( 'shows the page-load state before the settings load', () => { render( card( { accountProtection: on, sso: off } ) ); - expect( screen.getByRole( 'checkbox', { name: 'Enable account protection' } ) ).toBeChecked(); + expect( screen.getByRole( 'switch', { name: 'Enable account protection' } ) ).toBeChecked(); expect( - screen.getByRole( 'checkbox', { name: 'Enable WordPress.com login (SSO)' } ) + screen.getByRole( 'switch', { name: 'Enable WordPress.com login (SSO)' } ) ).not.toBeChecked(); } ); @@ -72,7 +72,7 @@ describe( 'LoginProtectionCard', () => { const save = jest.fn(); render( card( state, { save } ) ); - await userEvent.setup().click( screen.getByRole( 'checkbox', { name } ) ); + await userEvent.setup().click( screen.getByRole( 'switch', { name } ) ); expect( save ).toHaveBeenCalledWith( patch ); } ); @@ -83,7 +83,7 @@ describe( 'LoginProtectionCard', () => { ] )( 'says why “%s” is disabled when unavailable', ( name, state, feature ) => { render( card( state ) ); - expect( screen.getByRole( 'checkbox', { name } ) ).toBeDisabled(); + expect( screen.getByRole( 'switch', { name } ) ).toHaveAttribute( 'aria-disabled', 'true' ); expect( screen.getByText( `${ feature } isn’t available on this site.` ) ).toBeInTheDocument(); } ); @@ -91,7 +91,7 @@ describe( 'LoginProtectionCard', () => { const { rerender } = render( card( { bruteForce: off } ) ); await userEvent .setup() - .click( screen.getByRole( 'checkbox', { name: 'Enable brute force protection' } ) ); + .click( screen.getByRole( 'switch', { name: 'Enable brute force protection' } ) ); rerender( card( { bruteForce: off }, { settings: { protect: true } } ) ); diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-settings-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-settings-card.test.tsx index 3454341024ab..92303253edf6 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-settings-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-settings-card.test.tsx @@ -52,7 +52,7 @@ describe( 'LoginProtectionSettingsCard', () => { const user = userEvent.setup(); renderCard( { save } ); - await user.click( screen.getByLabelText( SSO ) ); + await user.click( screen.getByRole( 'switch', { name: SSO } ) ); expect( save ).not.toHaveBeenCalled(); @@ -65,7 +65,7 @@ describe( 'LoginProtectionSettingsCard', () => { const save = jest.fn(); renderCard( { save, settings: { sso: false } } ); - await userEvent.setup().click( screen.getByLabelText( SSO ) ); + await userEvent.setup().click( screen.getByRole( 'switch', { name: SSO } ) ); expect( save ).toHaveBeenCalledWith( { sso: true } ); } ); @@ -81,7 +81,7 @@ describe( 'LoginProtectionSettingsCard', () => { renderCard( { state, settings } ); const enabled = ( label: string ) => - ! screen.getByLabelText( label ).hasAttribute( 'disabled' ); + screen.getByRole( 'switch', { name: label } ).getAttribute( 'aria-disabled' ) !== 'true'; expect( enabled( SSO ) ).toBe( canToggle ); expect( enabled( MATCH_BY_EMAIL ) ).toBe( canSetOptions ); expect( enabled( TWO_STEP ) ).toBe( canSetOptions ); @@ -116,7 +116,10 @@ describe( 'LoginProtectionSettingsCard', () => { ] )( 'locks "%s" when its module is unavailable', ( label, key ) => { renderCard( { state: { [ key ]: { available: false, active: false } } } ); - expect( screen.getByLabelText( label ) ).toBeDisabled(); + expect( screen.getByRole( 'switch', { name: label } ) ).toHaveAttribute( + 'aria-disabled', + 'true' + ); } ); it( 'locks a forced option to the value WordPress.com login applies', () => { @@ -128,12 +131,18 @@ describe( 'LoginProtectionSettingsCard', () => { settings: { jetpack_sso_require_two_step: false }, } ); - expect( screen.getByLabelText( TWO_STEP ) ).toBeDisabled(); - expect( screen.getByLabelText( TWO_STEP ) ).toBeChecked(); + expect( screen.getByRole( 'switch', { name: TWO_STEP } ) ).toHaveAttribute( + 'aria-disabled', + 'true' + ); + expect( screen.getByRole( 'switch', { name: TWO_STEP } ) ).toBeChecked(); expect( screen.getByText( 'Two-Step Authentication is enforced by your site’s configuration.' ) ).toBeInTheDocument(); - expect( screen.getByLabelText( MATCH_BY_EMAIL ) ).toBeEnabled(); + expect( screen.getByRole( 'switch', { name: MATCH_BY_EMAIL } ) ).not.toHaveAttribute( + 'aria-disabled', + 'true' + ); } ); it.each( [ diff --git a/projects/packages/protect/routes/dashboard/sections/monitor/test/monitor-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/monitor/test/monitor-card.test.tsx index 296ea52c9259..3cd4240c1b77 100644 --- a/projects/packages/protect/routes/dashboard/sections/monitor/test/monitor-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/monitor/test/monitor-card.test.tsx @@ -131,7 +131,7 @@ describe( 'MonitorCard', () => { render( ); await expect( - screen.findByLabelText( 'Enable downtime monitoring' ) + screen.findByRole( 'switch', { name: 'Enable downtime monitoring' } ) ).resolves.not.toBeChecked(); expect( screen.queryByText( 'Configure downtime monitoring' ) ).not.toBeInTheDocument(); expect( diff --git a/projects/packages/protect/routes/dashboard/sections/monitor/test/settings-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/monitor/test/settings-card.test.tsx index 95946062164c..789247586ebd 100644 --- a/projects/packages/protect/routes/dashboard/sections/monitor/test/settings-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/monitor/test/settings-card.test.tsx @@ -41,7 +41,7 @@ describe( 'MonitorSettingsCard', () => { renderCard( options ); const enabled = ( label: string ) => - ! screen.getByLabelText( label ).hasAttribute( 'disabled' ); + screen.getByRole( 'switch', { name: label } ).getAttribute( 'aria-disabled' ) !== 'true'; expect( enabled( MONITOR ) ).toBe( canToggle ); expect( enabled( EMAIL ) ).toBe( canEmail ); } ); @@ -49,7 +49,10 @@ describe( 'MonitorSettingsCard', () => { it( 'keeps the email toggle locked while a Monitor save is in flight', () => { renderCard( { saving: true } ); - expect( screen.getByLabelText( EMAIL ) ).toBeDisabled(); + expect( screen.getByRole( 'switch', { name: EMAIL } ) ).toHaveAttribute( + 'aria-disabled', + 'true' + ); } ); it( 'keeps the email toggle locked until the re-read after turning Monitor on settles', async () => { @@ -63,13 +66,19 @@ describe( 'MonitorSettingsCard', () => { const save = jest.fn().mockResolvedValue( undefined ); const { rerender } = renderCard( { monitor: false, save, refresh } ); - await userEvent.setup().click( screen.getByLabelText( MONITOR ) ); + await userEvent.setup().click( screen.getByRole( 'switch', { name: MONITOR } ) ); // The Monitor save has settled and the setting now reads on; only the re-read is pending. rerender( card( { save, refresh } ) ); - expect( screen.getByLabelText( EMAIL ) ).toBeDisabled(); + expect( screen.getByRole( 'switch', { name: EMAIL } ) ).toHaveAttribute( + 'aria-disabled', + 'true' + ); await act( async () => finishRefresh() ); - expect( screen.getByLabelText( EMAIL ) ).toBeEnabled(); + expect( screen.getByRole( 'switch', { name: EMAIL } ) ).not.toHaveAttribute( + 'aria-disabled', + 'true' + ); } ); it.each( [ @@ -82,7 +91,7 @@ describe( 'MonitorSettingsCard', () => { const refresh = jest.fn().mockResolvedValue( undefined ); renderCard( { monitor, save, refresh } ); - await userEvent.setup().click( screen.getByLabelText( MONITOR ) ); + await userEvent.setup().click( screen.getByRole( 'switch', { name: MONITOR } ) ); expect( save ).toHaveBeenCalledWith( { monitor: ! monitor }, undefined ); expect( refresh.mock.calls ).toEqual( calls ); diff --git a/projects/packages/protect/routes/dashboard/sections/scan/style.scss b/projects/packages/protect/routes/dashboard/sections/scan/style.scss index 3a28b2ccff9e..f89d7139fc51 100644 --- a/projects/packages/protect/routes/dashboard/sections/scan/style.scss +++ b/projects/packages/protect/routes/dashboard/sections/scan/style.scss @@ -4,13 +4,13 @@ --jp-protect-cells: 3; > * { - padding-block: 20px; + padding-block: var(--wpds-dimension-padding-xl); } } // Full-bleed, so the threat list sets its own padding. &__row.jp-protect-card__threats { - padding: 0 0 8px; + padding: 0 0 var(--wpds-dimension-padding-sm); } } diff --git a/tools/eslint-excludelist.json b/tools/eslint-excludelist.json index e1adbfb1ed6d..adc9643d41c7 100644 --- a/tools/eslint-excludelist.json +++ b/tools/eslint-excludelist.json @@ -156,10 +156,8 @@ "projects/packages/premium-analytics/packages/widgets-toolkit/src/components/legend/legend.tsx", "projects/packages/premium-analytics/packages/widgets-toolkit/src/components/page-options-menu/feedback-fields.tsx", "projects/packages/premium-analytics/packages/widgets-toolkit/src/components/wordads-earnings-history/fields.tsx", - "projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx", "projects/packages/protect/routes/dashboard/sections/firewall/ip-rule.tsx", "projects/packages/protect/routes/dashboard/sections/firewall/settings-card.tsx", - "projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx", "projects/packages/publicize/_inc/components/connection-management/mark-as-shared.tsx", "projects/packages/publicize/_inc/components/connections-toggle-list/index.tsx", "projects/packages/publicize/_inc/components/customize-and-preview/connection-toggle/index.tsx",