Peach: Local-first video metadata scraper and media manager.
fix(index): 旧分类链接用 replace 改写地址,换分类时按钮行不塌缩
longmeidao204ef1236c37
diff --git a/frontend/e2e/index-people.test.ts b/frontend/e2e/index-people.test.tsindex a4e000c31..c53669feb 100644--- a/frontend/e2e/index-people.test.ts+++ b/frontend/e2e/index-people.test.ts@@ -84,4 +84,30 @@ describe('本地与在线艺人共用名册布局', () => {} finally { await opened.close() }});}+ it('卖家动画作者的旧链接就地落到艺人分类,地址、标题和历史只留一条', { timeout: 60_000 }, async () => {+ const opened = await visit(browser, '/performers', VIEWPORTS[0]);+ try {+ const page = opened.page;+ await page.locator('#index [data-index-page]').waitFor({ timeout: 15_000 });+ const before = await page.evaluate(() => history.length);+ const requests: string[] = [];+ page.on('request', request => {+ const url = new URL(request.url());+ if (url.pathname === '/api/index') requests.push(url.search);+ });+ await page.goto(requiredEnv('PEACH_E2E_ORIGIN') + '/creators?category=animation', { waitUntil: 'load' });+ await page.waitForURL(url => url.pathname === '/performers', { timeout: 15_000 });+ await page.locator('#index [data-index-page]').waitFor({ timeout: 15_000 });+ await settle(page);+ assert.equal(new URL(page.url()).searchParams.get('category'), 'animation');+ assert.equal(await page.evaluate(() => history.length), before + 1, '规范化不该多压一条历史');+ assert.equal(await page.evaluate(() => document.body.dataset.surface), '/performers');+ assert.match(await page.title(), /^艺人/);+ assert.deepEqual(await page.locator('#index [role="tab"][aria-selected="true"]').allTextContents(), ['艺人']);+ assert.equal(requests.length, 1, `名册只取一次:${requests.join(' | ')}`);+ assert.match(requests[0], /kind=performers/);+ assert.match(requests[0], /category=animation/);+ assert.deepEqual(opened.problems, []);+ } finally { await opened.close() }+ });});diff --git a/frontend/src/react/index/index-data.ts b/frontend/src/react/index/index-data.tsindex bf2bcc267..940209dcd 100644--- a/frontend/src/react/index/index-data.ts+++ b/frontend/src/react/index/index-data.ts@@ -44,10 +44,10 @@ export const ONLINE_TAG_CATEGORIES: readonly (readonly [string, string])[] = [['all', '全部'], ['general', '通用'], ['artist', '创作者'], ['character', '角色'],['copyright', '作品'], ['metadata', '元数据'],];-export const IDENTITY_CATEGORIES: Record<'performers' | 'creators', readonly (readonly [string, string])[]> = {- performers: [['all', '全部'], ['japanese_av', '女优'], ['amateur', '素人'], ['western', '西方'], ['blogger', '网黄博主'], ['animation', '动画作者']],- creators: [['all', '全部']],-};+/** 本地艺人名册的身份分类;卖家名册不分类。 */+export const IDENTITY_CATEGORIES: readonly (readonly [string, string])[] = [+ ['all', '全部'], ['japanese_av', '女优'], ['amateur', '素人'], ['western', '西方'], ['blogger', '网黄博主'], ['animation', '动画作者'],+];/** 每页条数:一屏头像 120 格,标签 180 枚。 */export const PAGE_SIZE = { people: 120, tags: 180 } as const;@@ -133,17 +133,17 @@ export interface IndexProps extends IndexRoute {}/** 地址栏上的类型可能是手敲或拼错的:认不出的回到「全部」,不拿它去问后端。- * 只有艺人和标签两页分本地与在线。 */+ * 只有艺人和标签两页分本地与在线。动画作者归艺人名册的一个分类,+ * 卖家名册上的 `category=animation` 链接转到那里。 */export function indexRoute(route: IndexRoute): IndexRoute {if (route.kind === 'creators' && route.category === 'animation') {return indexRoute({ ...route, kind: 'performers', scope: 'local' });}const scope = route.kind === 'tags' || route.kind === 'performers' ? route.scope : 'local';const categories = route.kind === 'tags' ? (scope === 'online' ? ONLINE_TAG_CATEGORIES : TAG_CATEGORIES)- : route.kind === 'performers' || route.kind === 'creators' ? IDENTITY_CATEGORIES[route.kind] : [];- const classified = scope === 'local' && (route.kind === 'creators' || route.kind === 'performers');+ : route.kind === 'performers' && scope === 'local' ? IDENTITY_CATEGORIES : [];const known = categories.some(([key]) => key === route.category);- return { ...route, scope, category: (route.kind === 'tags' || classified) && known ? route.category : 'all' };+ return { ...route, scope, category: known ? route.category : 'all' };}/** 一种状态下该读哪一份:本地名册、本地词表、在线创作者或在线标签。 */diff --git a/frontend/src/react/index/index-page.tsx b/frontend/src/react/index/index-page.tsxindex acdea331f..da3116410 100644--- a/frontend/src/react/index/index-page.tsx+++ b/frontend/src/react/index/index-page.tsx@@ -122,6 +122,14 @@ export function IndexPage(props: IndexProps) {const onlineTags = kind === 'tags' && scope === 'online';const onlineAuthors = kind === 'performers' && scope === 'online';+ /* 地址给的那一份被规范过(旧链接换了名册、认不出的分类回到全部):用 replace 改写地址,+ 标题、侧栏和历史记录都跟页面上真正显示的那一份走,后退也不会再落回旧地址。 */+ useEffect(() => {+ if (route.kind !== props.kind || route.scope !== props.scope || route.category !== props.category) {+ props.route(route, { replace: true });+ }+ }, []);+/* 选择键归壳:关掉时所选跟着清空,下次打开从零开始,同目录页的多选。 */useEffect(() => { if (!props.selectMode) setPicked(new Set()) }, [props.selectMode]);@@ -146,6 +154,14 @@ export function IndexPage(props: IndexProps) {const items = flatItems(result.data);const more = !!result.hasNextPage && !result.isPlaceholderData;const readout = countText(items.length, !!result.hasNextPage);+ /* 分类计数只随名册、词表和过滤词变,与选中哪一类无关。换分类时新一页还没到,沿用同一份+ 名册上一次拿到的计数:分类按钮行不先塌成「全部」加当前项、等数据回来再弹开。 */+ const countsKey = `${kind}:${scope}:${q}`;+ const lastCounts = useRef<{ key: string; counts: Record<string, number> } | null>(null);+ const pageCounts = result.data?.pages[0]?.categories;+ if (pageCounts && !result.isPlaceholderData) lastCounts.current = { key: countsKey, counts: pageCounts };+ const categoryCounts = pageCounts ?? (lastCounts.current?.key === countsKey ? lastCounts.current.counts : undefined);+ const hasEntries = ([key]: readonly [string, string]) => key === 'all' || Number(categoryCounts?.[key] || 0) > 0;/* 框换了大小,「这张图要不要补底」和人脸放大都得重算:图早加载完了,不会再自己发一次 load。赶在绘制之前,否则换版式那一帧是按旧框算的几何。 */@@ -179,8 +195,7 @@ export function IndexPage(props: IndexProps) {});};- const categories = (onlineTags ? ONLINE_TAG_CATEGORIES : TAG_CATEGORIES).filter(([key]) =>- key === 'all' || Number(result.data?.pages[0]?.categories?.[key] || 0) > 0);+ const categories = (onlineTags ? ONLINE_TAG_CATEGORIES : TAG_CATEGORIES).filter(hasEntries);const body = () => {if (result.isPending) {@@ -229,8 +244,7 @@ export function IndexPage(props: IndexProps) {{tabs}{kind === 'performers' && scope === 'local' ? (<div aria-label="身份分类" className="mb-4 flex flex-wrap gap-2">- {IDENTITY_CATEGORIES[kind].filter(([key]) => key === 'all' || key === category- || Number(result.data?.pages[0]?.categories?.[key] || 0) > 0).map(([key, label]) => (+ {IDENTITY_CATEGORIES.filter((entry) => entry[0] === category || hasEntries(entry)).map(([key, label]) => (<Button key={key} variant={category === key ? 'primary' : 'secondary'} size="small"aria-pressed={category === key} onClick={() => go({ category: key })}>{label}</Button>))}diff --git a/frontend/test/react/index-page.test.tsx b/frontend/test/react/index-page.test.tsxindex 2659f0a05..9640e0314 100644--- a/frontend/test/react/index-page.test.tsx+++ b/frontend/test/react/index-page.test.tsx@@ -105,6 +105,36 @@ describe('名册', () => {expect(host.querySelector('[aria-label="身份分类"]')?.textContent).toBe('全部女优西方');expect(indexRoute({ kind:'creators', q:'', scope:'local', view:'alphabet', category:'japanese_av' }).category).toBe('all');});+ it('换分类等新一页时,分类按钮行沿用上一份计数,不塌缩', async () => {+ seed(indexKey('performers', ''), [person('女优', { identity_labels:['女优'] })], false, { japanese_av:1, amateur:2, western:1 });+ const answer = pending<unknown>();+ serve(() => answer.answer);+ const host = await open(props());+ const row = () => host.querySelector('[aria-label="身份分类"]')?.textContent;+ expect(row()).toBe('全部女优素人西方');+ await click(buttonNamed('素人', host));+ await settle();+ expect(row()).toBe('全部女优素人西方');+ expect(buttonNamed('素人', host)?.getAttribute('aria-pressed')).toBe('true');+ await answer.release({ items: [person('素人')], categories: { japanese_av:1, amateur:2 }, has_more: false });+ await settle();+ expect(row()).toBe('全部女优素人');+ });+ it('旧的卖家动画作者链接规范成艺人分类后,用 replace 改写地址', async () => {+ seed(indexKey('performers', '', 'animation'), [person('动画作者')], false, { animation:1 });+ const given = props({ kind: 'creators', category: 'animation' });+ const host = await open(given);+ expect(host.querySelector('[data-index-title]')?.textContent).toBe('艺人');+ expect(given.route).toHaveBeenCalledTimes(1);+ expect(given.route).toHaveBeenCalledWith(+ expect.objectContaining({ kind: 'performers', scope: 'local', category: 'animation' }), { replace: true });+ });+ it('地址本来就规范时不改写', async () => {+ seed(indexKey('performers', ''), [person('女优')]);+ const given = props();+ await open(given);+ expect(given.route).not.toHaveBeenCalled();+ });it('艺人、卖家、在线共用一排导航,卖家没有重复身份筛选', async () => {seed(indexKey('performers',''),[person('真人账号')]);seed(indexKey('creators',''),[person('卖家账号')]);