From d4595269b52a1ead55af0374660b889da3df0f5b Mon Sep 17 00:00:00 2001 From: Maurice Date: Fri, 27 Nov 2026 20:51:44 +0100 Subject: [PATCH] refactor(login): both sign-in screens take their headings and labels from loginModel LoginPage grew past its size baseline with the app's branch. Its heading, subtitle and submit label chains now come from loginModel, which the app's screen already used, so the file shrinks by 21 lines and the baseline follows. The model translates the keys itself, written out, so the i18n key check keeps seeing them. --- client/scripts/size-baseline.json | 2 +- client/src/pages/LoginPage.tsx | 42 +++++------------------ client/src/pages/login/AppLogin.tsx | 10 +++--- client/src/pages/login/loginModel.test.ts | 27 ++++++++------- client/src/pages/login/loginModel.ts | 36 ++++++++++++------- 5 files changed, 52 insertions(+), 65 deletions(-) diff --git a/client/scripts/size-baseline.json b/client/scripts/size-baseline.json index 087a460a3..9364b4d39 100644 --- a/client/scripts/size-baseline.json +++ b/client/scripts/size-baseline.json @@ -18,7 +18,7 @@ "src/index.css": 1636, "src/mobile/screens/admin/MAdminPluginsPanel.tsx": 1503, "src/pages/JourneyDetailPage.test.tsx": 3886, - "src/pages/LoginPage.tsx": 1294, + "src/pages/LoginPage.tsx": 1273, "src/pages/TripPlannerPage.tsx": 1127, "src/pages/atlas/useAtlas.ts": 1207, "src/pages/tripPlanner/useTripPlanner.roadtrip.test.tsx": 3518, diff --git a/client/src/pages/LoginPage.tsx b/client/src/pages/LoginPage.tsx index eddf02e46..6b60238b9 100644 --- a/client/src/pages/LoginPage.tsx +++ b/client/src/pages/LoginPage.tsx @@ -17,6 +17,7 @@ import { SUPPORTED_LANGUAGES, useTranslation } from '../i18n'; import { IDP_LOGIN_URL, useLogin } from './login/useLogin'; import LoginWorld from './login/LoginWorld'; import AppLogin from './login/AppLogin'; +import { heading as headingOf, submitLabel } from './login/loginModel'; import MfaCodeStep from './login/MfaCodeStep'; import { LANGUAGE_CORNER_CLASS } from './login/languageCorner'; import PasswordChecklist from '../components/shared/PasswordChecklist'; @@ -91,6 +92,8 @@ export default function LoginPage(): React.ReactElement { handleDemoLogin, handleSubmit, handlePasskeyLogin, handleSsoClick, } = login; + const step = { mode, mfaStep, passwordChangeStep, hasUsers: !!appConfig?.has_users }; + const heading = headingOf(step, t); const oidcButtonShown = !!(appConfig?.oidc_configured && appConfig?.oidc_login && !oidcOnly); const passkeyAvailable = !!( appConfig?.passkey_login && @@ -229,8 +232,7 @@ export default function LoginPage(): React.ReactElement { ); } - // Inside the iOS and Android app: the screen that continues the app's own - // address screen, on the same hook. The browser keeps the page below. + // In the app: the screen continuing its address screen (login/AppLogin.tsx). if (nativeApp) return ; return ( @@ -582,26 +584,10 @@ export default function LoginPage(): React.ReactElement { color: '#111827', }} > - {passwordChangeStep - ? t('login.setNewPassword') - : mode === 'login' && mfaStep - ? t('login.mfaTitle') - : mode === 'register' - ? !appConfig?.has_users - ? t('login.createAdmin') - : t('login.createAccount') - : t('login.title')} + {heading.title}

- {passwordChangeStep - ? t('login.setNewPasswordHint') - : mode === 'login' && mfaStep - ? t('login.mfaSubtitle') - : mode === 'register' - ? !appConfig?.has_users - ? t('login.createAdminHint') - : t('login.createAccountHint') - : t('login.subtitle')} + {heading.subtitle}

@@ -1020,24 +1006,12 @@ export default function LoginPage(): React.ReactElement { animation: 'spin 0.7s linear infinite', }} /> - {passwordChangeStep - ? t('settings.updatePassword') - : mode === 'register' - ? t('login.creating') - : mode === 'login' && mfaStep - ? t('login.mfaVerify') - : t('login.signingIn')} + {submitLabel(step, true, t)} ) : ( <> - {passwordChangeStep - ? t('settings.updatePassword') - : mode === 'register' - ? t('login.createAccount') - : mode === 'login' && mfaStep - ? t('login.mfaVerify') - : t('login.signIn')} + {submitLabel(step, false, t)} )} diff --git a/client/src/pages/login/AppLogin.tsx b/client/src/pages/login/AppLogin.tsx index a66dd1d5d..41a093a5d 100644 --- a/client/src/pages/login/AppLogin.tsx +++ b/client/src/pages/login/AppLogin.tsx @@ -6,7 +6,7 @@ import PasswordChecklist from '../../components/shared/PasswordChecklist'; import { useTranslation } from '../../i18n'; import { lightStatusBarIcons } from '../../native/appBoot'; import './appLogin.css'; -import { headingKeys, oidcLoginHref, oidcOnlyHref, submitKey, type LoginStep } from './loginModel'; +import { heading as headingOf, oidcLoginHref, oidcOnlyHref, submitLabel, type LoginStep } from './loginModel'; import { mfaCodeInputProps } from './MfaCodeStep'; import { IDP_LOGIN_URL, type useLogin } from './useLogin'; @@ -163,22 +163,22 @@ function stepOf(login: Login): LoginStep { function Credentials({ login }: { login: Login }) { const { t } = useTranslation(); const step = stepOf(login); - const heading = headingKeys(step); + const heading = headingOf(step, t); const onMfa = login.mode === 'login' && login.mfaStep && !login.passwordChangeStep; const onAccount = !(login.mode === 'login' && login.mfaStep) && !login.passwordChangeStep; return ( <> -

{t(heading.title)}

+

{heading.title}

{/* The plain sign-in needs no line under its heading; the other steps say what they ask for. */} - {heading.subtitle !== 'login.subtitle' &&

{t(heading.subtitle)}

} + {!heading.plain &&

{heading.subtitle}

} {login.passwordChangeStep && } {onMfa && } {onAccount && } diff --git a/client/src/pages/login/loginModel.test.ts b/client/src/pages/login/loginModel.test.ts index 6f671da87..54c27fe7c 100644 --- a/client/src/pages/login/loginModel.test.ts +++ b/client/src/pages/login/loginModel.test.ts @@ -1,6 +1,9 @@ // FE-LOGIN-MODEL-001 to FE-LOGIN-MODEL-004 import { describe, expect, it } from 'vitest'; -import { headingKeys, oidcLoginHref, oidcOnlyHref, submitKey, type LoginStep } from './loginModel'; +import { heading, oidcLoginHref, oidcOnlyHref, submitLabel, type LoginStep } from './loginModel'; + +/** Returns the key itself, so the assertions read the keys. */ +const t = (key: string) => key; const step = (overrides: Partial = {}): LoginStep => ({ mode: 'login', @@ -12,20 +15,20 @@ const step = (overrides: Partial = {}): LoginStep => ({ describe('loginModel', () => { it('FE-LOGIN-MODEL-001: names each step of the sign-in', () => { - expect(headingKeys(step())).toEqual({ title: 'login.title', subtitle: 'login.subtitle' }); - expect(headingKeys(step({ mfaStep: true })).title).toBe('login.mfaTitle'); - expect(headingKeys(step({ passwordChangeStep: true, mfaStep: true })).title).toBe('login.setNewPassword'); - expect(headingKeys(step({ mode: 'register' })).title).toBe('login.createAccount'); - expect(headingKeys(step({ mode: 'register', hasUsers: false })).title).toBe('login.createAdmin'); + expect(heading(step(), t)).toEqual({ title: 'login.title', subtitle: 'login.subtitle', plain: true }); + expect(heading(step({ mfaStep: true }), t).title).toBe('login.mfaTitle'); + expect(heading(step({ passwordChangeStep: true, mfaStep: true }), t).title).toBe('login.setNewPassword'); + expect(heading(step({ mode: 'register' }), t).title).toBe('login.createAccount'); + expect(heading(step({ mode: 'register', hasUsers: false }), t).title).toBe('login.createAdmin'); }); it('FE-LOGIN-MODEL-002: labels the submit button for the step and while it runs', () => { - expect(submitKey(step(), false)).toBe('login.signIn'); - expect(submitKey(step(), true)).toBe('login.signingIn'); - expect(submitKey(step({ mfaStep: true }), true)).toBe('login.mfaVerify'); - expect(submitKey(step({ mode: 'register' }), false)).toBe('login.createAccount'); - expect(submitKey(step({ mode: 'register' }), true)).toBe('login.creating'); - expect(submitKey(step({ passwordChangeStep: true }), true)).toBe('settings.updatePassword'); + expect(submitLabel(step(), false, t)).toBe('login.signIn'); + expect(submitLabel(step(), true, t)).toBe('login.signingIn'); + expect(submitLabel(step({ mfaStep: true }), true, t)).toBe('login.mfaVerify'); + expect(submitLabel(step({ mode: 'register' }), false, t)).toBe('login.createAccount'); + expect(submitLabel(step({ mode: 'register' }), true, t)).toBe('login.creating'); + expect(submitLabel(step({ passwordChangeStep: true }), true, t)).toBe('settings.updatePassword'); }); it('FE-LOGIN-MODEL-003: carries the invite and the remember-me choice to SSO, only when signing in', () => { diff --git a/client/src/pages/login/loginModel.ts b/client/src/pages/login/loginModel.ts index 9ebfdd501..806744ef1 100644 --- a/client/src/pages/login/loginModel.ts +++ b/client/src/pages/login/loginModel.ts @@ -11,24 +11,34 @@ export interface LoginStep { hasUsers: boolean; } -/** The heading and the line under it, as translation keys. */ -export function headingKeys(step: LoginStep): { title: string; subtitle: string } { - if (step.passwordChangeStep) return { title: 'login.setNewPassword', subtitle: 'login.setNewPasswordHint' }; - if (step.mode === 'login' && step.mfaStep) return { title: 'login.mfaTitle', subtitle: 'login.mfaSubtitle' }; +type Translate = (key: string) => string; + +/** + * The heading and the line under it. `plain` is the plain sign-in, whose line + * the app's screen leaves out. Translated here, with the keys written out, so + * the i18n key check sees every one of them. + */ +export function heading(step: LoginStep, t: Translate): { title: string; subtitle: string; plain: boolean } { + if (step.passwordChangeStep) { + return { title: t('login.setNewPassword'), subtitle: t('login.setNewPasswordHint'), plain: false }; + } + if (step.mode === 'login' && step.mfaStep) { + return { title: t('login.mfaTitle'), subtitle: t('login.mfaSubtitle'), plain: false }; + } if (step.mode === 'register') { return step.hasUsers - ? { title: 'login.createAccount', subtitle: 'login.createAccountHint' } - : { title: 'login.createAdmin', subtitle: 'login.createAdminHint' }; + ? { title: t('login.createAccount'), subtitle: t('login.createAccountHint'), plain: false } + : { title: t('login.createAdmin'), subtitle: t('login.createAdminHint'), plain: false }; } - return { title: 'login.title', subtitle: 'login.subtitle' }; + return { title: t('login.title'), subtitle: t('login.subtitle'), plain: true }; } -/** The submit button's label, as a translation key; another while the request runs. */ -export function submitKey(step: LoginStep, loading: boolean): string { - if (step.passwordChangeStep) return 'settings.updatePassword'; - if (step.mode === 'register') return loading ? 'login.creating' : 'login.createAccount'; - if (step.mfaStep) return 'login.mfaVerify'; - return loading ? 'login.signingIn' : 'login.signIn'; +/** The submit button's label; another while the request runs. */ +export function submitLabel(step: LoginStep, loading: boolean, t: Translate): string { + if (step.passwordChangeStep) return t('settings.updatePassword'); + if (step.mode === 'register') return loading ? t('login.creating') : t('login.createAccount'); + if (step.mfaStep) return t('login.mfaVerify'); + return loading ? t('login.signingIn') : t('login.signIn'); } /**