Peach: Local-first video metadata scraper and media manager.
fix(search): 聚焦时的数据晚于敲字回来,不再按同一个词补问一次
longmeidao85fea48f6d5b
diff --git a/frontend/src/react/search/search-page.tsx b/frontend/src/react/search/search-page.tsxindex 2d4bcaf07..e841f0b59 100644--- a/frontend/src/react/search/search-page.tsx+++ b/frontend/src/react/search/search-page.tsx@@ -30,6 +30,8 @@ interface Store {groups: SuggestGroup[];tabs: SuggestTab[];suggestRequest: number;+ /** 最近一次不分类补全问的词:聚焦时的数据晚到,框里还是这个词就不再问一遍。 */+ asked: string;timer: number;kind: string;active: number;@@ -41,7 +43,7 @@ interface Store {const emptyStore = (): Store => ({history: [], pool: [], poolRequest: 0, query: '', picks: [], suggestFor: '', groups: [], tabs: [],- suggestRequest: 0, timer: 0, kind: '', active: -1, reveal: false, rewind: false,+ suggestRequest: 0, asked: '', timer: 0, kind: '', active: -1, reveal: false, rewind: false,});const modelOf = (s: Store): MenuModel => menuModel({@@ -170,6 +172,7 @@ function createOps(props: () => SearchProps, s: Store, menu: () => HTMLElement |先发的那次完全可能后回来。 */const loadSuggestions = async (query: string, kind = '') => {const request = ++s.suggestRequest;+ if (!kind) s.asked = query;try {const data = await apiGet<SuggestResponse>(`/api/suggest?q=${encodeURIComponent(query)}`+ (kind ? `&kind=${kind}&limit=${SUGGEST_ONE_KIND}` : `&limit=${SUGGEST_EACH}`));@@ -306,9 +309,10 @@ function createOps(props: () => SearchProps, s: Store, menu: () => HTMLElement |const focus = () => {void Promise.all([loadHistory(), loadPool()]).then(() => {if (!focused()) return;- // 带着 `?q=` 进来再点回输入框时,框里已经有词,补全该跟着这个词给。+ // 带着 `?q=` 进来再点回输入框时,框里已经有词,补全该跟着这个词给。这两份数据回来之前+ // 人可能已经敲完字、那个词的补全也问过了,同一个词不再问第二遍。paint();- refresh();+ if (input().value.trim() !== s.asked) refresh();});};return { input: onInput, key, focus, close, pick, peek, remove, pickKind };diff --git a/frontend/test/react/search.test.tsx b/frontend/test/react/search.test.tsxindex 6d55930e8..bf8b1eb08 100644--- a/frontend/test/react/search.test.tsx+++ b/frontend/test/react/search.test.tsx@@ -62,8 +62,8 @@ interface Stage {}/** 壳那一侧:一个静态输入框、一块带 `data-search-menu` 的面,岛挂在面里。 */-async function stage({ pool = ['剧情'], suggest = async (q: string): Promise<SuggestResponse> => ({ q, groups: [] }) } = {}):- Promise<Stage> {+async function stage({ pool = ['剧情'], loadPool = async () => pool,+ suggest = async (q: string): Promise<SuggestResponse> => ({ q, groups: [] }) } = {}): Promise<Stage> {const posts: unknown[] = [];vi.stubGlobal('fetch', vi.fn(async (url: string, init?: RequestInit) => {const path = new URL(url, 'http://peach.test');@@ -94,7 +94,7 @@ async function stage({ pool = ['剧情'], suggest = async (q: string): Promise<S}),};const helpers: SearchProps['helpers'] = {- pool: async () => pool, coverHtml: () => '',+ pool: loadPool, coverHtml: () => '',present: (el) => { el.hidden = false }, dismiss: (el) => { el.hidden = true },wireScroller: () => {}, typed: () => {}, composing: () => {}, clearField: (el) => { el.value = '' },};@@ -154,6 +154,24 @@ describe('输入框上的时序', () => {expect(values).toEqual(['七海ひな', '七海の夏']);});+ it('聚焦时的推荐池晚于敲字回来,不再按同一个词补问一次', async () => {+ vi.useFakeTimers();+ const asked: string[] = [];+ const slowPool = pending<string[]>();+ const s = await stage({ loadPool: () => slowPool.answer,+ suggest: async (q) => { asked.push(q); return { q, groups: [performer] } } });+ await act(async () => s.input.focus());+ await s.typeInto('七海');+ await debounce();+ await settle();+ expect(asked).toEqual(['七海']);+ await slowPool.release(['剧情']);+ await debounce();+ await settle();+ expect(asked).toEqual(['七海']);+ expect(s.menu.hidden).toBe(false);+ });+it('组字期间的输入不问补全,组完才问:拿半截拼音去查,查的是一个不存在的词', async () => {vi.useFakeTimers();const asked: string[] = [];diff --git a/web/dist/peach-react.js b/web/dist/peach-react.jsindex 9974b38e2..3ce2a6796 100644--- a/web/dist/peach-react.js+++ b/web/dist/peach-react.js@@ -49617,6 +49617,7 @@ var uye = () => ({groups: [],tabs: [],suggestRequest: 0,+ asked: "",timer: 0,kind: "",active: -1,@@ -49724,6 +49725,7 @@ function pye(e, t, n, r) {} catch {}}, d = async (e, n = "") => {let r = ++t.suggestRequest;+ n || (t.asked = e);try {let i = await G(`/api/suggest?q=${encodeURIComponent(e)}` + (n ? `&kind=${n}&limit=20` : "&limit=5"));if (r !== t.suggestRequest) return;@@ -49807,7 +49809,7 @@ function pye(e, t, n, r) {},focus: () => {Promise.all([c(), u()]).then(() => {- a() && (m(), h());+ a() && (m(), i().value.trim() !== t.asked && h());});},close: p,