Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix: 保留访问密码输入并按关闭选项禁用

longmeidaobde692114e6c
diff --git a/frontend/src/islands/access-settings.tsx b/frontend/src/islands/access-settings.tsx
index 74a591316..afac77a08 100644
--- a/frontend/src/islands/access-settings.tsx
+++ b/frontend/src/islands/access-settings.tsx
@@ -7,13 +7,13 @@ import { ApiError, apiSend, errorMessage } from '../api';
export interface AccessState { mode: 'open' | 'password' | 'legacy' | 'locked'; revision: string }
type FieldErrors = Partial<Record<'current_password' | 'password' | 'confirmation', string>>;
-function PasswordField({ id, label, value, onInput, error, help, current = false }: {
- id: string; label: string; value: string; onInput(value: string): void; error?: string | undefined; help?: string; current?: boolean;
+function PasswordField({ id, label, value, onInput, error, help, current = false, disabled = false }: {
+ id: string; label: string; value: string; onInput(value: string): void; error?: string | undefined; help?: string; current?: boolean; disabled?: boolean;
}) {
return <div class="configfield">
<label for={id}>{label}</label>
<input id={id} class="geist-input" type="password" autoComplete={current ? 'current-password' : 'new-password'}
- maxLength={256} value={value} onInput={(event) => onInput(event.currentTarget.value)} required
+ maxLength={256} value={value} onInput={(event) => onInput(event.currentTarget.value)} required={!disabled} disabled={disabled}
aria-invalid={Boolean(error)} aria-describedby={error || help ? `${id}-hint` : undefined} />
{error || help ? <p id={`${id}-hint`} class={error ? 'configbad' : 'confighelp'} role={error ? 'alert' : undefined}>{error || help}</p> : null}
</div>;
@@ -66,12 +66,12 @@ export function AccessSettings({ initial, receipt }: { initial: AccessState; rec
<div dangerouslySetInnerHTML={{ __html: fieldsetTitle('accessTitle', '访问密码') }} />
<p class="confighelp">{state.mode === 'open' ? '未设置密码,能连接到 Peach 的设备可直接访问。' : state.mode === 'legacy' ? '当前使用系统生成的访问口令。你可以设置自己的密码,或关闭登录要求。' : state.mode === 'locked' ? '访问设置无法读取,请在本机检查配置文件。' : '已设置密码。新设备需要登录,保持登录时间在登录页选择。'}</p>
</div>
+ {state.mode === 'password' || state.mode === 'legacy' ? <label class="configcheck"><span class="pcheck"><input type="checkbox" checked={disable} onChange={(event) => setDisable(event.currentTarget.checked)} /><span aria-hidden="true"><svg viewBox="0 0 24 24"><use href="#i-check" /></svg></span></span><span>关闭访问密码,允许能连接到 Peach 的设备直接访问</span></label> : null}
{state.mode === 'password' ? <PasswordField id="access-current" label="当前访问密码" value={current} onInput={setCurrent} error={fields.current_password} current /> : null}
- {!disable && state.mode !== 'locked' ? <>
- <PasswordField id="access-password" label={state.mode === 'password' ? '新访问密码' : '设置访问密码'} value={password} onInput={setPassword} error={fields.password} help="至少 8 个字符。保存后其他设备需要重新登录。" />
- <PasswordField id="access-confirm" label="确认访问密码" value={confirmation} onInput={setConfirmation} error={fields.confirmation} />
+ {state.mode !== 'locked' ? <>
+ <PasswordField id="access-password" label={state.mode === 'password' ? '新访问密码' : '设置访问密码'} value={password} onInput={setPassword} disabled={disable} error={disable ? undefined : fields.password} help={disable ? '关闭访问密码时无需填写。' : '至少 8 个字符。保存后其他设备需要重新登录。'} />
+ <PasswordField id="access-confirm" label="确认访问密码" value={confirmation} onInput={setConfirmation} disabled={disable} error={disable ? undefined : fields.confirmation} />
</> : null}
- {state.mode === 'password' || state.mode === 'legacy' ? <label class="configcheck"><span class="pcheck"><input type="checkbox" checked={disable} onChange={(event) => setDisable(event.currentTarget.checked)} /><span aria-hidden="true"><svg viewBox="0 0 24 24"><use href="#i-check" /></svg></span></span><span>关闭访问密码,允许能连接到 Peach 的设备直接访问</span></label> : null}
{disable && <div dangerouslySetInnerHTML={{__html:noteHtml('保存后,能连接到 Peach 的设备将直接访问馆藏。',{variant:'warning',label:'访问范围'})}} />}
{error ? <p class="configbad" role="alert">{error}</p> : null}
</div>
diff --git a/frontend/test/access-settings.test.tsx b/frontend/test/access-settings.test.tsx
index 87eea34e7..ab2884f52 100644
--- a/frontend/test/access-settings.test.tsx
+++ b/frontend/test/access-settings.test.tsx
@@ -15,7 +15,9 @@ it('系统口令可直接转为可选密码且关闭需要明确提交', async (
expect(host.querySelector('.geist-fieldset-footer .primary')?.textContent).toBe('保存配置');
const toggle = host.querySelector<HTMLInputElement>('[type="checkbox"]')!;
toggle.checked = true; toggle.dispatchEvent(new Event('change', { bubbles: true })); await settle();
- expect(host.querySelector('[autocomplete="new-password"]')).toBeNull();
+ const passwordFields = [...host.querySelectorAll<HTMLInputElement>('[autocomplete="new-password"]')];
+ expect(passwordFields).toHaveLength(2);
+ expect(passwordFields.every((input) => input.disabled && !input.required)).toBe(true);
expect(host.querySelector('form')?.getAttribute('data-fieldset-type')).toBe('warning');
expect(host.querySelector('.geist-note-warning')?.textContent).toContain('直接访问馆藏');
expect(fetcher).not.toHaveBeenCalled();
@@ -25,6 +27,37 @@ it('系统口令可直接转为可选密码且关闭需要明确提交', async (
expect(host.textContent).toContain('未设置密码');
});
+it('关闭选项位于输入上方且保留草稿并要求当前密码验证', async () => {
+ const host = document.createElement('div'); document.body.append(host);
+ const fetcher = vi.fn(async () => ({ ok: true, json: async () => ({ mode: 'open', revision: 'next' }) }));
+ vi.stubGlobal('fetch', fetcher);
+ render(<AccessSettings initial={{ mode: 'password', revision: 'one' }} receipt={vi.fn()} />, host);
+ const toggle = host.querySelector<HTMLInputElement>('[type="checkbox"]')!;
+ const current = host.querySelector<HTMLInputElement>('#access-current')!;
+ const fields = [...host.querySelectorAll<HTMLInputElement>('[autocomplete="new-password"]')];
+ expect(toggle.compareDocumentPosition(current) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
+ for (const input of fields) {
+ input.value = 'draft-password'; input.dispatchEvent(new Event('input', { bubbles: true }));
+ }
+ await settle();
+ toggle.checked = true; toggle.dispatchEvent(new Event('change', { bubbles: true })); await settle();
+ expect(fields.every((input) => input.disabled && input.value === 'draft-password')).toBe(true);
+ expect(current.disabled).toBe(false);
+ expect(current.required).toBe(true);
+ toggle.checked = false; toggle.dispatchEvent(new Event('change', { bubbles: true })); await settle();
+ expect(fields.every((input) => !input.disabled && input.required && input.value === 'draft-password')).toBe(true);
+ toggle.checked = true; toggle.dispatchEvent(new Event('change', { bubbles: true })); await settle();
+ expect(fetcher).not.toHaveBeenCalled();
+ host.querySelector('form')!.dispatchEvent(new Event('submit', { cancelable: true })); await settle();
+ expect(current.getAttribute('aria-invalid')).toBe('true');
+ expect(fetcher).not.toHaveBeenCalled();
+ current.value = 'current-password'; current.dispatchEvent(new Event('input', { bubbles: true })); await settle();
+ host.querySelector('form')!.dispatchEvent(new Event('submit', { cancelable: true })); await settle();
+ expect(JSON.parse((fetcher.mock.calls[0] as unknown as [string, RequestInit])[1].body as string)).toMatchObject({
+ action: 'disable', confirm_disable: true, current_password: 'current-password', password: '', confirmation: '',
+ });
+});
+
it('已设置的密码需要当前密码且失败原因留在表单中', async () => {
const host = document.createElement('div'); document.body.append(host);
vi.stubGlobal('fetch', vi.fn(async () => ({ ok: false, status: 400, json: async () => ({ message: '当前访问密码不正确', errors: { current_password: '当前访问密码不正确' } }) })));
diff --git a/web/css/23-configuration.css b/web/css/23-configuration.css
index 2751d0ca7..a656909a6 100644
--- a/web/css/23-configuration.css
+++ b/web/css/23-configuration.css
@@ -33,6 +33,8 @@
border-top:1px solid var(--line);background:var(--surface)}
.configfieldset>.geist-fieldset-footer>p{min-width:0;margin:0 auto 0 0;color:var(--muted);font-size:var(--fs-sm)}
.configfield>label,.configfield>.configlabel{display:block;margin:0 0 8px;font-weight:500;color:var(--ink)}
+.configfield:has(>.geist-input:disabled)>label{color:var(--muted)}
+.configpage .geist-input:disabled{background:var(--surface);border-color:var(--line);color:var(--muted);cursor:not-allowed}
.configpage .geist-input[aria-invalid="true"]{border-color:var(--drop)}
.configpage .geist-input[aria-invalid="true"]:focus{box-shadow:0 0 0 4px color-mix(in srgb,var(--drop) 24%,transparent)}
.confighelp{margin:8px 0 0;color:var(--muted);font-size:var(--fs-sm);line-height:1.5}
diff --git a/web/dist/peach-ui.js b/web/dist/peach-ui.js
index 9bf729d5a..cf26ee3e1 100644
--- a/web/dist/peach-ui.js
+++ b/web/dist/peach-ui.js
@@ -472,7 +472,7 @@ function K(e, t, n, r, i, a) {
}
//#endregion
//#region src/islands/access-settings.tsx
-function Qe({ id: e, label: t, value: n, onInput: r, error: i, help: a, current: o = !1 }) {
+function Qe({ id: e, label: t, value: n, onInput: r, error: i, help: a, current: o = !1, disabled: s = !1 }) {
return /* @__PURE__ */ K("div", {
class: "configfield",
children: [
@@ -488,7 +488,8 @@ function Qe({ id: e, label: t, value: n, onInput: r, error: i, help: a, current:
maxLength: 256,
value: n,
onInput: (e) => r(e.currentTarget.value),
- required: !0,
+ required: !s,
+ disabled: s,
"aria-invalid": !!i,
"aria-describedby": i || a ? `${e}-hint` : void 0
}),
@@ -545,28 +546,6 @@ function $e({ initial: e, receipt: t }) {
children: n.mode === "open" ? "未设置密码,能连接到 Peach 的设备可直接访问。" : n.mode === "legacy" ? "当前使用系统生成的访问口令。你可以设置自己的密码,或关闭登录要求。" : n.mode === "locked" ? "访问设置无法读取,请在本机检查配置文件。" : "已设置密码。新设备需要登录,保持登录时间在登录页选择。"
})]
}),
- n.mode === "password" ? /* @__PURE__ */ K(Qe, {
- id: "access-current",
- label: "当前访问密码",
- value: i,
- onInput: o,
- error: _.current_password,
- current: !0
- }) : null,
- !p && n.mode !== "locked" ? /* @__PURE__ */ K(P, { children: [/* @__PURE__ */ K(Qe, {
- id: "access-password",
- label: n.mode === "password" ? "新访问密码" : "设置访问密码",
- value: c,
- onInput: l,
- error: _.password,
- help: "至少 8 个字符。保存后其他设备需要重新登录。"
- }), /* @__PURE__ */ K(Qe, {
- id: "access-confirm",
- label: "确认访问密码",
- value: u,
- onInput: f,
- error: _.confirmation
- })] }) : null,
n.mode === "password" || n.mode === "legacy" ? /* @__PURE__ */ K("label", {
class: "configcheck",
children: [/* @__PURE__ */ K("span", {
@@ -584,6 +563,30 @@ function $e({ initial: e, receipt: t }) {
})]
}), /* @__PURE__ */ K("span", { children: "关闭访问密码,允许能连接到 Peach 的设备直接访问" })]
}) : null,
+ n.mode === "password" ? /* @__PURE__ */ K(Qe, {
+ id: "access-current",
+ label: "当前访问密码",
+ value: i,
+ onInput: o,
+ error: _.current_password,
+ current: !0
+ }) : null,
+ n.mode === "locked" ? null : /* @__PURE__ */ K(P, { children: [/* @__PURE__ */ K(Qe, {
+ id: "access-password",
+ label: n.mode === "password" ? "新访问密码" : "设置访问密码",
+ value: c,
+ onInput: l,
+ disabled: p,
+ error: p ? void 0 : _.password,
+ help: p ? "关闭访问密码时无需填写。" : "至少 8 个字符。保存后其他设备需要重新登录。"
+ }), /* @__PURE__ */ K(Qe, {
+ id: "access-confirm",
+ label: "确认访问密码",
+ value: u,
+ onInput: f,
+ disabled: p,
+ error: p ? void 0 : _.confirmation
+ })] }),
p && /* @__PURE__ */ K("div", { dangerouslySetInnerHTML: { __html: s("保存后,能连接到 Peach 的设备将直接访问馆藏。", {
variant: "warning",
label: "访问范围"