From d4a6fd344b70fe972466b6381bbfecc47ce24486 Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Tue, 24 Nov 2026 16:09:42 +0800 Subject: [PATCH] fix(settings): keep the card readable across breakpoints and exits --- web/components/settings/ModulesPanel.tsx | 44 +++++++++------ web/components/settings/SettingsMain.tsx | 44 +++++++++++++-- web/components/settings/shared.tsx | 4 +- web/tests/settings-escape-close.spec.tsx | 71 ++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 23 deletions(-) create mode 100644 web/tests/settings-escape-close.spec.tsx diff --git a/web/components/settings/ModulesPanel.tsx b/web/components/settings/ModulesPanel.tsx index 6b61fb919..ada1717b0 100644 --- a/web/components/settings/ModulesPanel.tsx +++ b/web/components/settings/ModulesPanel.tsx @@ -3,30 +3,42 @@ import { useTranslation } from "react-i18next"; import { useModules } from "@/hooks/useModules"; +// Same card language as SettingSection: a quiet bordered surface with a +// 13.5px header. It used to be its own `rounded-xl border p-5` island with an +// 18px heading, which read as a different product next to the other cards. export default function ModulesPanel() { const { t } = useTranslation(); const { modules, error, reload } = useModules(); return ( -
-

{t("Modules")}

-

- {t("Stop DeepTutor before changing modules. Removing a module keeps your data.")} -

+
+
+

+ {t("Modules")} +

+

+ {t("Stop DeepTutor before changing modules. Removing a module keeps your data.")} +

+
{error && } {!modules && !error &&

{t("Loading...")}

} -
- - - - - - {modules?.map(module => -
{t("Module")}{t("Installed")}{t("Enabled")}{t("Ready")}{t("Command")}
{module.id} - {module.issues.map(issue =>

{issue}

)} +
+ + + + + + + + + + + {modules?.map(module => + {[module.installed, module.enabled, module.ready].map((value, index) => - )} - )} + )}
{t("Module")}{t("Installed")}{t("Enabled")}{t("Ready")}{t("Command")}
{module.id} + {module.issues.map(issue =>

{issue}

)}
{value ? t("Yes") : t("No")}{module.id === "core" ? "—" : + {value ? t("Yes") : t("No")}{module.id === "core" ? "—" : `deeptutor module ${!module.installed ? "install" : !module.enabled ? "enable" : "disable"} ${module.id}`}
diff --git a/web/components/settings/SettingsMain.tsx b/web/components/settings/SettingsMain.tsx index 3d3a3e6c0..609e3b19a 100644 --- a/web/components/settings/SettingsMain.tsx +++ b/web/components/settings/SettingsMain.tsx @@ -44,6 +44,30 @@ export default function SettingsMain({ children }: { children: React.ReactNode } } router.push(destination) } + // One gate for every way out of the shell (back button, Escape, scrim click), + // so no exit can drop an unsaved draft silently. + const requestClose = () => { + if (saving || applying || leaving) return + if (hasUnsavedChanges) setLeaving(true) + else returnToApp() + } + + useEffect(() => { + const onKeyDown = (event: KeyboardEvent) => { + if (event.key !== 'Escape' || event.defaultPrevented) return + if (mobileOpen) { + event.preventDefault() + setMobileOpen(false) + return + } + if (leaving) return + event.preventDefault() + requestClose() + } + window.addEventListener('keydown', onKeyDown) + return () => window.removeEventListener('keydown', onKeyDown) + }) + const returnButton = ( diff --git a/web/components/settings/shared.tsx b/web/components/settings/shared.tsx index 6123ee39b..62e4d9232 100644 --- a/web/components/settings/shared.tsx +++ b/web/components/settings/shared.tsx @@ -159,7 +159,9 @@ export function SettingRow({

)}
-
{control}
+ {/* Below sm the row stacks, so the control takes the full column: a + half-width select under a full-width description reads as a mistake. */} +
{control}
); } diff --git a/web/tests/settings-escape-close.spec.tsx b/web/tests/settings-escape-close.spec.tsx new file mode 100644 index 000000000..b94666f3c --- /dev/null +++ b/web/tests/settings-escape-close.spec.tsx @@ -0,0 +1,71 @@ +import React from 'react' +import { fireEvent, render, screen } from '@testing-library/react' +import { afterAll, beforeAll, beforeEach, expect, it, vi } from 'vitest' +import SettingsMain from '@/components/settings/SettingsMain' + +const mocks = vi.hoisted(() => ({ push: vi.fn(), setActiveSection: vi.fn() })) +vi.mock('next/navigation', () => ({ + usePathname: () => '/settings/general', + useRouter: () => ({ push: mocks.push }), +})) +vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) })) +vi.mock('@/features/settings/store/SettingsStore', () => ({ + useSettings: () => ({ + hasUnsavedChanges: true, + saveDraft: vi.fn(), + saving: false, + applying: false, + toast: '', + setActiveSection: mocks.setActiveSection, + }), +})) +vi.mock('@/components/settings/SettingsNav', () => ({ default: () => null })) +vi.mock('@/components/settings/SettingsToolbar', () => ({ SettingsToolbar: () => null })) +vi.mock('@/components/settings/SettingsLoadStatusBanner', () => ({ + SettingsLoadStatusBanner: () => null, +})) +vi.mock('@/components/common/Modal', () => ({ + default: ({ + isOpen, + title, + children, + }: { + isOpen: boolean + title?: string + children: React.ReactNode + }) => (isOpen ?
{title}{children}
: null), +})) + +const priorScroll = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'scrollTo') +beforeAll(() => + Object.defineProperty(HTMLElement.prototype, 'scrollTo', { configurable: true, value: vi.fn() }) +) +afterAll(() => { + if (priorScroll) Object.defineProperty(HTMLElement.prototype, 'scrollTo', priorScroll) + else Reflect.deleteProperty(HTMLElement.prototype, 'scrollTo') +}) +beforeEach(() => mocks.push.mockReset()) + +// The floating card behaves like a dialog: Escape and a scrim click are exits, +// and neither may drop an unsaved draft without asking first. +it('Escape asks before leaving with unsaved changes', () => { + render( + +

General settings

+
+ ) + fireEvent.keyDown(window, { key: 'Escape' }) + expect(screen.getByRole('dialog')).toHaveTextContent('Keep your changes?') + expect(mocks.push).not.toHaveBeenCalled() +}) + +it('clicking the scrim asks before leaving with unsaved changes', () => { + const { container } = render( + +

General settings

+
+ ) + fireEvent.mouseDown(container.querySelector('[data-settings-shell]') as HTMLElement) + expect(screen.getByRole('dialog')).toHaveTextContent('Keep your changes?') + expect(mocks.push).not.toHaveBeenCalled() +})