Files
readest/apps/readest-app/src/components/settings/IntegrationsPanel.tsx
T
Huang Xin 772bb73b46 ui/ux: codify design system and migrate settings to shared primitives (#4116)
* ui/ux: codify design system and migrate settings to shared primitives

Document Readest's design language in DESIGN.md (Adwaita-aligned, e-ink-first,
RTL-correct) and migrate every settings panel onto a small set of primitives
(BoxedList, SettingsRow, SettingsSwitchRow, SettingsSelect, SettingsInput,
NavigationRow, Tips, SubPageHeader). AGENTS.md links to DESIGN.md so contributors
land there before inventing new chassis classes.

Replace the standalone KOReader/Readwise/Hardcover Config dialogs with a single
Integrations panel (Reading Sync + Content Sources sub-pages). The reader's
BookMenu now hides each provider until it's configured, and Hardcover's per-book
"Enable for This Book" toggle is dropped — there's no auto-sync to gate, so the
flag was just extra clicks.

Refresh highlight colors (two-trigger swatch + label, translatable default
names), background texture / theme color selectors (border-current keeps
selection legible on any backdrop), CustomFonts/CustomDictionaries (quiet
list-extension style + shared Tips primitive), the OPDS catalog manager
(debounced auto-download, right-aligned Browse), Set PIN, and the KOSync
conflict resolver. Translate the ~30 new strings across all 33 locales.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* ui/ux: responsive typography, OPDS card polish, deep-link return paths

Restore the .settings-content responsive cascade (14px desktop / 16px
mobile) the legacy panels relied on by dropping hardcoded `text-sm`/`text-xs`
from the new primitives. Secondary text moves to em-relative `text-[0.85em]`
so it scales with the parent. Form controls (`<input>`, `<select>`) re-apply
the cascade explicitly via the `settings-content` class since browsers don't
inherit font-size onto form elements.

Extract `<SectionTitle>` primitive (caseless-language aware via
`isCaselessUILang`/`isCaselessLang`) and route every uppercase tag-style
header through it: BoxedList groups, Reading Sync, Content Sources, Theme
Color, Background Image, integration form labels, KOSyncResolver device
labels, and the OPDS My Catalogs / Popular Catalogs sections. CJK / Arabic
/ Hebrew / Indic / Thai / Tibetan locales bump to `1em` since `uppercase`
is a no-op on those scripts.

Redesign the OPDS My Catalogs cards: whole card becomes the browse trigger
(role='button'), edit/delete collapse into a 3-dot dropdown menu, and the
sync-status moves to a sub-line under Auto-download so the card height stays
constant whether the toggle is on/off or sync data has arrived.

Plumb a `from=settings-integrations` URL marker through the OPDS browser so
both manual close and auto-close-on-failure (preserved as `router.back()`
for transient failures, paired with a new `stashOPDSReturnTarget` helper)
return the user to Settings -> Integrations -> OPDS Catalogs sub-page
rather than the dialog's top level. Backed by new `requestedSubPage`
deep-link store field.

Skip the OPDS catalog passphrase prompt when credentials sync is disabled
-- `replicaPublish` already drops encrypted fields at the wire, so prompting
was both pointless and confusing.

Fix `SettingsDialog` calling `setRequestedPanel(null)` inside a `useState`
lazy initializer (zustand setter during render -> React warning); move the
clear into a one-shot `useEffect`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* ui/ux: opt Settings into OverlayScrollbars + caseless typography polish

Add an opt-in `useOverlayScroll` prop to `<Dialog>` that swaps the body's
native `overflow-y-auto` for `<OverlayScrollbarsComponent>` (autohide,
click-scroll, no native overlaid bars). SettingsDialog flips it on so the
long Layout / Color panels keep a visible, theme-aware scroll track on
Android / iOS webviews where native scrollbars auto-hide entirely. Other
short-modal callers stay on the native scrollbar.

Drop the `uppercase tracking-wider` SectionTitle styling for caseless
scripts and pair it with body-weight `font-medium` instead — those
typographic effects are no-ops on Han / Hangul / Devanagari / Thai etc.,
so a plain medium-weight body-size title reads more correctly than a
shrunken pseudo-uppercase one. SettingsRow / NavigationRow primary labels
follow the same rule (drop `font-medium` in caseless locales since the
inherited body weight already carries; CJK fonts bold poorly at body
size).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* ui/ux: SettingLabel primitive + KOSyncForm select polish + Tips alignment

Add `<SettingLabel>` primitive — caseless-aware row/field label that pairs
with `<SectionTitle>` (groups) for per-item labels. Cased scripts get
`font-medium`; caseless scripts (CJK / Arabic / Hebrew / Indic / Thai /
Tibetan) drop the weight since Han / Hangul / Devanagari etc. bold poorly
at body size. No font-size class so it inherits the `.settings-content`
14/16 cascade. Routed through `SettingsRow`, `NavigationRow`, and the
~12 ad-hoc inline `text-sm font-medium` callsites in AIPanel / FontPanel
/ ColorPanel / IntegrationsPanel / KOSync / Readwise / Hardcover forms.

Refactor KOSyncForm's Sync Strategy + Checksum Method rows onto the
shared `<SettingsSelect>` primitive — the inline 17-line div/select/
MdArrowDropDown chassis becomes a single SettingsSelect call with an
options array. Drops the unused MdArrowDropDown import and ~25 lines.

Fix Tips list-item alignment: callers traditionally pass `<li>` elements
(semantic) but the primitive was double-wrapping into `<li><span><li>...</li></span></li>` — invalid HTML, and the inner `<li>`'s
`display: list-item` broke line-wrap alignment on multi-line items.
Unwrap caller `<li>` to its content; add `flex-1` on the text span so
wrapped lines align under the first line instead of falling back to the
bullet column. Bullet container switches to `h-[1.4em]` so it tracks the
text line-height and pins to the first line's optical center via
`items-center` regardless of how much the content wraps.

DESIGN.md §5 typography updated to point primary-label callers at
`<SettingLabel>`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 17:46:25 +02:00

190 lines
6.8 KiB
TypeScript

import clsx from 'clsx';
import React, { useEffect, useState } from 'react';
import { MdChevronRight } from 'react-icons/md';
import { RiBookOpenLine, RiRssLine, RiBookReadLine, RiBook3Line } from 'react-icons/ri';
import { useTranslation } from '@/hooks/useTranslation';
import { useSettingsStore } from '@/store/settingsStore';
import { useCustomOPDSStore } from '@/store/customOPDSStore';
import { CatalogManager } from '@/app/opds/components/CatalogManager';
import KOSyncForm from './integrations/KOSyncForm';
import ReadwiseForm from './integrations/ReadwiseForm';
import HardcoverForm from './integrations/HardcoverForm';
import SubPageHeader from './SubPageHeader';
import { SectionTitle, SettingLabel } from './primitives';
type SubPage = 'kosync' | 'readwise' | 'hardcover' | 'opds' | null;
/**
* Integrations panel — single point of discovery for external service config:
* KOReader Sync, Readwise, Hardcover, and OPDS Catalogs.
*
* Pattern: boxed list of NavigationRows. Each row pushes the panel into an
* inline sub-page (with breadcrumb back-navigation matching the Dictionaries
* pattern) — no nested modals.
*
* TODO(design-system): Once we extract BoxedList / NavigationRow primitives,
* this panel and CustomDictionaries should both consume them instead of
* inlining the chassis.
*/
const IntegrationsPanel: React.FC = () => {
const _ = useTranslation();
const { settings, requestedSubPage, setRequestedSubPage } = useSettingsStore();
const opdsCatalogs = useCustomOPDSStore((s) => s.catalogs);
const opdsCount = opdsCatalogs.filter((c) => !c.deletedAt).length;
const [subPage, setSubPage] = useState<SubPage>(null);
// Deep-link consumption: when a caller (e.g. OPDS browser close handler)
// sets `requestedSubPage` in the store before opening the dialog, drill
// straight into that sub-page on mount and clear the request so it doesn't
// stick to the next open. Recognised values match the SubPage union.
useEffect(() => {
if (!requestedSubPage) return;
if (
requestedSubPage === 'kosync' ||
requestedSubPage === 'readwise' ||
requestedSubPage === 'hardcover' ||
requestedSubPage === 'opds'
) {
setSubPage(requestedSubPage);
}
setRequestedSubPage(null);
}, [requestedSubPage, setRequestedSubPage]);
// Sub-page wrapper matches the list-view's `my-4 w-full` so the
// SubPageHeader's "Integrations" label lands at the exact same Y position
// as the list-view's h2 — clicking a row reads as a navigation morph
// rather than a layout shift.
if (subPage === 'kosync')
return (
<div className='my-4 w-full'>
<KOSyncForm onBack={() => setSubPage(null)} />
</div>
);
if (subPage === 'readwise')
return (
<div className='my-4 w-full'>
<ReadwiseForm onBack={() => setSubPage(null)} />
</div>
);
if (subPage === 'hardcover')
return (
<div className='my-4 w-full'>
<HardcoverForm onBack={() => setSubPage(null)} />
</div>
);
if (subPage === 'opds')
return (
<div className='my-4 w-full'>
<SubPageHeader
parentLabel={_('Integrations')}
currentLabel={_('OPDS Catalogs')}
description={_('Browse and download books from online catalogs.')}
onBack={() => setSubPage(null)}
/>
<CatalogManager inSubPage />
</div>
);
const koSyncStatus = settings.kosync?.enabled
? settings.kosync.username
? _('Connected as {{user}}', { user: settings.kosync.username })
: _('Connected')
: _('Not connected');
const readwiseStatus = settings.readwise?.enabled ? _('Connected') : _('Not connected');
const hardcoverStatus = settings.hardcover?.enabled ? _('Connected') : _('Not connected');
const opdsStatus =
opdsCount > 0 ? _('{{count}} catalog', { count: opdsCount }) : _('No catalogs');
return (
<div className='my-4 w-full space-y-6'>
<div className='w-full px-4'>
<h2 className='mb-1.5 text-lg font-semibold tracking-tight'>{_('Integrations')}</h2>
<p className='text-base-content/70 text-sm leading-relaxed'>
{_('Connect Readest to external services for sync, highlights, and catalogs.')}
</p>
</div>
<div className='w-full' data-setting-id='settings.integrations.sync'>
<SectionTitle className='mb-2'>{_('Reading Sync')}</SectionTitle>
<div className='card eink-bordered border-base-200 bg-base-100 overflow-hidden border'>
<div className='divide-base-200 divide-y'>
<IntegrationRow
icon={RiBookOpenLine}
title={_('KOReader Sync')}
status={koSyncStatus}
onClick={() => setSubPage('kosync')}
/>
<IntegrationRow
icon={RiBookReadLine}
title={_('Readwise')}
status={readwiseStatus}
onClick={() => setSubPage('readwise')}
/>
<IntegrationRow
icon={RiBook3Line}
title={_('Hardcover')}
status={hardcoverStatus}
onClick={() => setSubPage('hardcover')}
/>
</div>
</div>
</div>
<div className='w-full' data-setting-id='settings.integrations.catalogs'>
<SectionTitle className='mb-2'>{_('Content Sources')}</SectionTitle>
<div className='card eink-bordered border-base-200 bg-base-100 overflow-hidden border'>
<div className='divide-base-200 divide-y'>
<IntegrationRow
icon={RiRssLine}
title={_('OPDS Catalogs')}
status={opdsStatus}
onClick={() => setSubPage('opds')}
/>
</div>
</div>
</div>
</div>
);
};
interface IntegrationRowProps {
icon: React.ElementType;
title: string;
status: string;
onClick: () => void;
}
const IntegrationRow: React.FC<IntegrationRowProps> = ({ icon: Icon, title, status, onClick }) => {
return (
<button
type='button'
onClick={onClick}
className={clsx(
'group flex w-full items-center gap-3 px-4 py-3 text-left',
'transition-colors duration-150',
'focus-visible:ring-base-content/15 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset',
)}
>
<span
className={clsx(
'flex h-9 w-9 flex-shrink-0 items-center justify-center rounded-full',
'bg-base-200 text-base-content/70',
'transition-colors duration-150',
'group-hover:bg-base-300/70',
)}
>
<Icon className='h-5 w-5' />
</span>
<div className='flex min-w-0 flex-1 flex-col gap-0.5'>
<SettingLabel>{title}</SettingLabel>
<span className='text-base-content/65 truncate text-[0.85em]'>{status}</span>
</div>
<MdChevronRight className='text-base-content/50 h-5 w-5 flex-shrink-0' />
</button>
);
};
export default IntegrationsPanel;