fix(settings): keep the card readable across breakpoints and exits

This commit is contained in:
Bingxi Zhao (Frank)
2026-11-24 16:09:42 +08:00
committed by qiuqianyi66
parent a75e804e75
commit d4a6fd344b
4 changed files with 140 additions and 23 deletions

View File

@@ -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 (
<section className="my-6 rounded-xl border p-5">
<h2 className="text-lg font-semibold">{t("Modules")}</h2>
<p className="my-2 text-sm text-[var(--muted-foreground)]">
{t("Stop DeepTutor before changing modules. Removing a module keeps your data.")}
</p>
<section className="mb-4 rounded-xl border border-[color-mix(in_srgb,var(--border)_60%,transparent)] bg-[color-mix(in_srgb,var(--muted)_10%,var(--background))] px-4 md:px-5">
<header className="pb-1 pt-3.5">
<h2 className="text-[13.5px] font-semibold tracking-tight text-[var(--foreground)]">
{t("Modules")}
</h2>
<p className="mt-1 text-[12.5px] leading-relaxed text-[var(--muted-foreground)]">
{t("Stop DeepTutor before changing modules. Removing a module keeps your data.")}
</p>
</header>
{error && <button onClick={reload}>{t("Retry")}</button>}
{!modules && !error && <p aria-busy="true">{t("Loading...")}</p>}
<div className="overflow-x-auto">
<table className="w-full text-left text-sm">
<thead><tr>
<th className="py-2">{t("Module")}</th><th>{t("Installed")}</th>
<th>{t("Enabled")}</th><th>{t("Ready")}</th><th>{t("Command")}</th>
</tr></thead>
<tbody>{modules?.map(module => <tr key={module.id} className="border-t align-top">
<td className="py-2 pr-3"><code>{module.id}</code>
{module.issues.map(issue => <p key={issue} className="max-w-md text-xs text-[var(--muted-foreground)]">{issue}</p>)}
<div className="overflow-x-auto pb-2">
<table className="w-full text-left text-[13px]">
<thead>
<tr className="text-[12.5px] text-[var(--muted-foreground)]">
<th className="w-[34%] py-2 pr-3 text-left font-medium">{t("Module")}</th>
<th className="whitespace-nowrap px-2 py-2 text-left font-medium">{t("Installed")}</th>
<th className="whitespace-nowrap px-2 py-2 text-left font-medium">{t("Enabled")}</th>
<th className="whitespace-nowrap px-2 py-2 text-left font-medium">{t("Ready")}</th>
<th className="min-w-[220px] py-2 pl-2 text-left font-medium">{t("Command")}</th>
</tr>
</thead>
<tbody>{modules?.map(module => <tr key={module.id} className="border-t border-[color-mix(in_srgb,var(--border)_50%,transparent)] align-top">
<td className="py-2.5 pr-3"><code>{module.id}</code>
{module.issues.map(issue => <p key={issue} className="mt-1 max-w-md text-[11.5px] leading-relaxed text-[var(--muted-foreground)]">{issue}</p>)}
</td>
{[module.installed, module.enabled, module.ready].map((value, index) =>
<td className="py-2 pr-3" key={index}>{value ? t("Yes") : t("No")}</td>)}
<td className="py-2"><code>{module.id === "core" ? "—" :
<td className="px-2 py-2.5" key={index}>{value ? t("Yes") : t("No")}</td>)}
<td className="py-2.5 pl-2"><code className="break-words">{module.id === "core" ? "—" :
`deeptutor module ${!module.installed ? "install" : !module.enabled ? "enable" : "disable"} ${module.id}`}</code></td>
</tr>)}</tbody>
</table>

View File

@@ -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 = (
<button
type="button"
@@ -64,14 +88,19 @@ export default function SettingsMain({ children }: { children: React.ReactNode }
<div
className="fixed inset-0 flex items-center justify-center overflow-hidden bg-[color-mix(in_srgb,var(--foreground)_18%,transparent)] p-0 md:p-6"
data-settings-shell
onMouseDown={event => {
// Clicking the scrim behaves like Escape: leave settings, but never
// discard a draft without asking.
if (event.target === event.currentTarget) requestClose()
}}
>
<div
data-settings-card
className="flex h-dvh w-full min-w-0 flex-col overflow-hidden bg-[var(--background)] md:h-[min(820px,88dvh)] md:w-[min(1080px,94vw)] md:flex-row md:rounded-2xl md:border md:border-[color-mix(in_srgb,var(--border)_70%,transparent)] md:shadow-[0_28px_70px_-32px_rgba(0,0,0,0.45)]"
className="flex h-dvh w-full min-w-0 flex-col overflow-hidden bg-[var(--background)] md:h-[min(820px,88dvh)] md:w-[min(1080px,94vw)] md:rounded-2xl md:border md:border-[color-mix(in_srgb,var(--border)_70%,transparent)] md:shadow-[0_28px_70px_-32px_rgba(0,0,0,0.45)] lg:flex-row"
>
<aside className="hidden w-[224px] shrink-0 flex-col border-r border-[color-mix(in_srgb,var(--border)_55%,transparent)] bg-[color-mix(in_srgb,var(--muted)_18%,var(--background))] md:flex">
<div className="flex items-center justify-between gap-1 px-3 pb-1 pt-3">
<p className="truncate text-[13px] font-medium text-[var(--foreground)]">
<aside className="hidden w-[224px] shrink-0 flex-col border-r border-[color-mix(in_srgb,var(--border)_55%,transparent)] bg-[color-mix(in_srgb,var(--muted)_18%,var(--background))] lg:flex">
<div className="flex items-center justify-between gap-1 px-4 pb-2 pt-4">
<p className="truncate text-[14px] font-semibold text-[var(--foreground)]">
{t('Settings')}
</p>
{returnButton}
@@ -79,14 +108,17 @@ export default function SettingsMain({ children }: { children: React.ReactNode }
<SettingsNav />
</aside>
<main className="flex min-w-0 flex-1 flex-col overflow-hidden">
<div className="flex h-14 shrink-0 items-center justify-between border-b border-[color-mix(in_srgb,var(--border)_50%,transparent)] px-3 md:hidden">
<div className="grid h-14 shrink-0 grid-cols-[1fr_auto_1fr] items-center border-b border-[color-mix(in_srgb,var(--border)_50%,transparent)] px-2 lg:hidden">
{returnButton}
<p className="truncate text-center text-[15px] font-semibold text-[var(--foreground)]">
{t('Settings')}
</p>
<button
type="button"
aria-label={t('Settings sections')}
aria-expanded={mobileOpen}
onClick={() => setMobileOpen(true)}
className="rounded-md p-2 hover:bg-accent"
className="justify-self-end rounded-md p-2 hover:bg-accent"
>
<Menu size={20} />
</button>

View File

@@ -159,7 +159,9 @@ export function SettingRow({
</p>
)}
</div>
<div className="max-w-full shrink-0">{control}</div>
{/* 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. */}
<div className="w-full max-w-full shrink-0 sm:w-auto">{control}</div>
</div>
);
}

View File

@@ -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 ? <div role="dialog">{title}{children}</div> : 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(
<SettingsMain>
<p>General settings</p>
</SettingsMain>
)
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(
<SettingsMain>
<p>General settings</p>
</SettingsMain>
)
fireEvent.mouseDown(container.querySelector('[data-settings-shell]') as HTMLElement)
expect(screen.getByRole('dialog')).toHaveTextContent('Keep your changes?')
expect(mocks.push).not.toHaveBeenCalled()
})