From 91f8deb3f3d76320c81d9f6c3fad07b1dac54a97 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sat, 14 Nov 2026 11:34:28 +0100 Subject: [PATCH] refactor(mobile): keep the settings views under the size and complexity limits The user sheet's actions on another account (passkeys, two-factor, SSO link, delete) are MUserDangerActions with their own confirms, the place names picker is MPlaceLanguage, and useApiKeys takes its copied flag from a small useCopiedFlag. --- client/src/components/Settings/useApiKeys.ts | 41 ++-- .../src/mobile/screens/admin/MAdminSheets.tsx | 232 ++++++++++-------- .../screens/settings/MSettingsGeneral.tsx | 61 +++-- 3 files changed, 183 insertions(+), 151 deletions(-) diff --git a/client/src/components/Settings/useApiKeys.ts b/client/src/components/Settings/useApiKeys.ts index 717a38f9e..32927ee2a 100644 --- a/client/src/components/Settings/useApiKeys.ts +++ b/client/src/components/Settings/useApiKeys.ts @@ -48,6 +48,29 @@ export function formatApiKeyStamp(ts: string, locale: string, dateFormat: string return Number.isNaN(d.getTime()) ? ts : formatDisplayDate(d, locale, {}, dateFormat); } +/** + * What was just copied (the key or the endpoint), for two seconds, so its + * button can say so. A clipboard the origin lacks (plain http, where a server + * at home tends to live) calls `onFail`. + */ +function useCopiedFlag(onFail: () => void) { + const [copied, setCopied] = useState<'key' | 'endpoint' | null>(null); + const timer = useRef | undefined>(undefined); + useEffect(() => () => clearTimeout(timer.current), []); + const copy = async (text: string, what: 'key' | 'endpoint') => { + try { + if (!navigator.clipboard) throw new Error('clipboard unavailable'); + await navigator.clipboard.writeText(text); + setCopied(what); + clearTimeout(timer.current); + timer.current = setTimeout(() => setCopied(null), 2000); + } catch { + onFail(); + } + }; + return { copied, setCopied, copy }; +} + export function useApiKeys() { const { t } = useTranslation(); const toast = useToast(); @@ -60,8 +83,7 @@ export function useApiKeys() { /** The raw key, held only until the dialog closes: the server keeps a hash. */ const [created, setCreated] = useState(null); const [deleteId, setDeleteId] = useState(null); - const [copied, setCopied] = useState<'key' | 'endpoint' | null>(null); - const copiedTimer = useRef | undefined>(undefined); + const { copied, setCopied, copy: handleCopy } = useCopiedFlag(() => toast.error(t('settings.apiKeys.copyFailed'))); /** * What the new key may read. Starts as everything, because that is what a key * did before this existed and what most integrations want: narrowing is a @@ -88,7 +110,6 @@ export function useApiKeys() { }); return () => { cancelled = true; - clearTimeout(copiedTimer.current); }; }, []); @@ -136,20 +157,6 @@ export function useApiKeys() { } }; - const handleCopy = async (text: string, what: 'key' | 'endpoint') => { - try { - // Absent on a plain-http origin, which is exactly where a self-run - // instance on the LAN tends to live. - if (!navigator.clipboard) throw new Error('clipboard unavailable'); - await navigator.clipboard.writeText(text); - setCopied(what); - clearTimeout(copiedTimer.current); - copiedTimer.current = setTimeout(() => setCopied(null), 2000); - } catch { - toast.error(t('settings.apiKeys.copyFailed')); - } - }; - const resetForm = () => { setCreated(null); setCopied(null); diff --git a/client/src/mobile/screens/admin/MAdminSheets.tsx b/client/src/mobile/screens/admin/MAdminSheets.tsx index 2ebdf00f0..60042040d 100644 --- a/client/src/mobile/screens/admin/MAdminSheets.tsx +++ b/client/src/mobile/screens/admin/MAdminSheets.tsx @@ -28,13 +28,11 @@ docker run -d --name trek \\ // and delete), the "how to update" instructions and the rotate-JWT confirm. export default function MAdminSheets({ admin, t }: MAdminSheetsProps) { const { - currentUser, editingUser, setEditingUser, editForm, setEditForm, showCreateUser, setShowCreateUser, createForm, setCreateForm, updateInfo, showUpdateModal, setShowUpdateModal, showRotateJwtModal, setShowRotateJwtModal, rotatingJwt, - handleCreateUser, handleSaveUser, handleDeleteUser, - oidcConfigured, + handleCreateUser, handleSaveUser, } = admin const roleOptions = [ @@ -42,24 +40,10 @@ export default function MAdminSheets({ admin, t }: MAdminSheetsProps) { { value: 'admin', label: t('settings.roleAdmin') }, ] - const [showResetPasskeys, setShowResetPasskeys] = useState(false) - const [resettingPk, setResettingPk] = useState(false) - const [showResetMfa, setShowResetMfa] = useState(false) - const [showUnlinkSso, setShowUnlinkSso] = useState(false) const userActions = useAdminUserActions(admin, t) const { rotateJwt } = userActions - // The sheet stays open and busy while the reset runs and closes once it worked. - const resetPasskeys = async () => { - if (!editingUser) return - setResettingPk(true) - try { - if (await userActions.resetPasskeys(editingUser)) setShowResetPasskeys(false) - } finally { - setResettingPk(false) - } - } return ( <> @@ -164,70 +148,12 @@ export default function MAdminSheets({ admin, t }: MAdminSheetsProps) { onChange={(role) => setEditForm((f) => ({ ...f, role }))} /> -
-

- {t('admin.passkey.resetHint')} -

-
- setShowResetPasskeys(true)}> - - {t('admin.passkey.reset')} - - {editingUser.id !== currentUser?.id && ( - setShowResetMfa(true)}> - - {t('admin.mfa.reset')} - - )} - {/* Unlinking single sign-on, as on the desktop: only once SSO is set up, and never for yourself. */} - {oidcConfigured && editingUser.id !== currentUser?.id && ( - setShowUnlinkSso(true)}> - - {t('admin.sso.unlink')} - - )} - { - const user = editingUser - setEditingUser(null) - void handleDeleteUser(user) - }} - > - - {t('admin.deleteUserTitle')} - -
-
+ )} - {/* Unlink single sign-on confirm */} - setShowUnlinkSso(false)} - title={t('admin.sso.unlink')} - message={`${t('admin.sso.unlinkHint')} ${t('admin.sso.unlinkConfirm', { name: editingUser?.username ?? '' })}`} - confirmLabel={t('admin.sso.unlink')} - cancelLabel={t('common.cancel')} - danger - onConfirm={() => { - setShowUnlinkSso(false) - if (editingUser) void userActions.unlinkSso(editingUser) - }} - /> - - {/* Reset two-factor confirm */} - setShowResetMfa(false)} - user={editingUser} - onReset={userActions.resetMfa} - t={t} - /> {/* How to update */} setShowUpdateModal(false)} ariaLabel={t('admin.update.howTo')}> @@ -281,38 +207,6 @@ export default function MAdminSheets({ admin, t }: MAdminSheetsProps) { - {/* Reset passkeys confirm */} - setShowResetPasskeys(false)} ariaLabel={t('admin.passkey.reset')}> - } - title={t('admin.passkey.reset')} - onClose={() => setShowResetPasskeys(false)} - footer={ - <> - setShowResetPasskeys(false)}> - {t('common.cancel')} - - - {t('admin.passkey.reset')} - - - } - > -
- - - -
-

- {t('admin.passkey.resetConfirm', { name: editingUser?.username ?? '' })} -

-

- {t('admin.passkey.resetHint')} -

-
-
-
-
{/* Rotate JWT confirm */} setShowRotateJwtModal(false)} ariaLabel={t('admin.rotateJwt.title')}> @@ -349,3 +243,125 @@ export default function MAdminSheets({ admin, t }: MAdminSheetsProps) { ) } + +/** + * The ways an admin acts on another account from its edit sheet, each behind + * its own confirm: passkeys, two-factor, the SSO link, and deleting the user. + */ +function MUserDangerActions({ admin, t, user }: { admin: MAdminSheetsProps['admin']; t: TranslationFn; user: NonNullable }) { + const { currentUser, setEditingUser, handleDeleteUser, oidcConfigured } = admin + const editingUser = user + const [showResetPasskeys, setShowResetPasskeys] = useState(false) + const [resettingPk, setResettingPk] = useState(false) + const [showResetMfa, setShowResetMfa] = useState(false) + const [showUnlinkSso, setShowUnlinkSso] = useState(false) + const userActions = useAdminUserActions(admin, t) + + // The sheet stays open and busy while the reset runs and closes once it worked. + const resetPasskeys = async () => { + if (!editingUser) return + setResettingPk(true) + try { + if (await userActions.resetPasskeys(editingUser)) setShowResetPasskeys(false) + } finally { + setResettingPk(false) + } + } + + return ( + <> +
+

+ {t('admin.passkey.resetHint')} +

+
+ setShowResetPasskeys(true)}> + + {t('admin.passkey.reset')} + + {editingUser.id !== currentUser?.id && ( + setShowResetMfa(true)}> + + {t('admin.mfa.reset')} + + )} + {/* Unlinking single sign-on, as on the desktop: only once SSO is set up, and never for yourself. */} + {oidcConfigured && editingUser.id !== currentUser?.id && ( + setShowUnlinkSso(true)}> + + {t('admin.sso.unlink')} + + )} + { + setEditingUser(null) + void handleDeleteUser(editingUser) + }} + > + + {t('admin.deleteUserTitle')} + +
+
+ + {/* Unlink single sign-on confirm */} + setShowUnlinkSso(false)} + title={t('admin.sso.unlink')} + message={`${t('admin.sso.unlinkHint')} ${t('admin.sso.unlinkConfirm', { name: editingUser?.username ?? '' })}`} + confirmLabel={t('admin.sso.unlink')} + cancelLabel={t('common.cancel')} + danger + onConfirm={() => { + setShowUnlinkSso(false) + if (editingUser) void userActions.unlinkSso(editingUser) + }} + /> + + {/* Reset two-factor confirm */} + setShowResetMfa(false)} + user={editingUser} + onReset={userActions.resetMfa} + t={t} + /> + + {/* Reset passkeys confirm */} + setShowResetPasskeys(false)} ariaLabel={t('admin.passkey.reset')}> + } + title={t('admin.passkey.reset')} + onClose={() => setShowResetPasskeys(false)} + footer={ + <> + setShowResetPasskeys(false)}> + {t('common.cancel')} + + + {t('admin.passkey.reset')} + + + } + > +
+ + + +
+

+ {t('admin.passkey.resetConfirm', { name: editingUser?.username ?? '' })} +

+

+ {t('admin.passkey.resetHint')} +

+
+
+
+
+ + ) +} diff --git a/client/src/mobile/screens/settings/MSettingsGeneral.tsx b/client/src/mobile/screens/settings/MSettingsGeneral.tsx index e5fd44ef6..92acd9386 100644 --- a/client/src/mobile/screens/settings/MSettingsGeneral.tsx +++ b/client/src/mobile/screens/settings/MSettingsGeneral.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react' +import { useState, type ReactNode } from 'react' import { ChevronDown, Languages, Map, Rocket } from 'lucide-react' import { useTranslation, SUPPORTED_LANGUAGES } from '../../../i18n' import { useSettingSaver } from '../../../components/Settings/useSettingSaver' @@ -27,7 +27,6 @@ export default function MSettingsGeneral() { const demoLocked = useDemoAccountLock() const [currencyOpen, setCurrencyOpen] = useState(false) const [langOpen, setLangOpen] = useState(false) - const [placeLangOpen, setPlaceLangOpen] = useState(false) const [startTabOpen, setStartTabOpen] = useState(false) const [navAppOpen, setNavAppOpen] = useState(false) const [dateFormatOpen, setDateFormatOpen] = useState(false) @@ -135,19 +134,7 @@ export default function MSettingsGeneral() { setLangOpen(true)} /> - {/* Place names (#1799): searches can answer in another language than the app. */} -
- -
- l.value === settings.place_language)?.label ?? t('settings.placeLanguageApp')} - trailing={chevron} - onClick={() => setPlaceLangOpen(true)} - /> + save('place_language', v)} trailing={chevron} /> {t('settings.temperature')} - setPlaceLangOpen(false)} - title={t('settings.placeLanguage')} - value={settings.place_language || ''} - onSelect={(v) => save('place_language', v)} - options={[ - { value: '', label: t('settings.placeLanguageApp') }, - ...SUPPORTED_LANGUAGES.map((l) => ({ value: l.value, label: l.label })), - ]} - /> setLangOpen(false)} @@ -286,3 +262,36 @@ export default function MSettingsGeneral() { ) } + +/** Place names (#1799): searches can answer in another language than the app. */ +function MPlaceLanguage({ value, onSave, trailing }: { value: string; onSave: (value: string) => void; trailing: ReactNode }) { + const { t } = useTranslation() + const [open, setOpen] = useState(false) + return ( + <> +
+ +
+ l.value === value)?.label ?? t('settings.placeLanguageApp')} + trailing={trailing} + onClick={() => setOpen(true)} + /> + setOpen(false)} + title={t('settings.placeLanguage')} + value={value} + onSelect={onSave} + options={[ + { value: '', label: t('settings.placeLanguageApp') }, + ...SUPPORTED_LANGUAGES.map((l) => ({ value: l.value, label: l.label })), + ]} + /> + + ) +}