fix(web): смена ключа фильтра пар сбрасывает страницу в состоянии (M-13)

Возврат A→B→A даёт первую страницу, а не прежнюю.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Andrey Epifancev 2026-10-27 22:26:05 +04:00
parent fb13389f75
commit d230fb13ee
3 changed files with 19 additions and 13 deletions

View File

@ -8,7 +8,7 @@ import { getConsolidatedOsv } from '../../services/api/osv';
import type { ConsolidatedRowOut, CounterPairOut, OsvFilters } from '../../types/api';
import { CounterpartyMapModal } from './CounterpartyMapModal';
import { group, money } from './OsvTable';
import { clampPage, currentPage, missingSideEmptyText, pairLabel } from './counterPairs';
import { clampPage, missingSideEmptyText, pairLabel, syncPageState } from './counterPairs';
import { mapEntryId } from './counterpartyMapping';
import { OSV_PAGE_SIZE, osvErrorText } from './useConsolidatedOsv';
@ -38,8 +38,11 @@ function usePagedRows(versionId: string, filters: OsvFilters | null, reloadKey:
const requestRef = useRef(0);
const key = JSON.stringify(filters);
// ключ и страница — одно состояние: смена фильтра даёт первую страницу и один запрос, а не два
const [pageState, setPageState] = useState({ key, page: 1 });
const page = currentPage(pageState, key);
const [rawPageState, setPageState] = useState({ key, page: 1 });
const pageState = syncPageState(rawPageState, key);
// ключ сменился — состояние сразу пересоздаётся (возврат к прежнему ключу не вернёт старую страницу)
if (pageState !== rawPageState) setPageState(pageState);
const page = pageState.page;
const setPage = useCallback((p: number) => setPageState({ key, page: p }), [key]);
// biome-ignore lint/correctness/useExhaustiveDependencies: key — сериализованный filters; reloadKey — перечитать после сопоставления

View File

@ -2,13 +2,13 @@ import { describe, expect, it } from 'vitest';
import type { CounterPairOut } from '../../types/api';
import {
clampPage,
currentPage,
missingSideEmptyText,
pairLabel,
pairsViewSearch,
readPairsView,
selectPair,
switchPairsView,
syncPageState,
} from './counterPairs';
const pair = (holder: string, missing: string): CounterPairOut => ({
@ -96,14 +96,16 @@ describe('clampPage (T5.6)', () => {
});
});
describe('currentPage (M-8)', () => {
it('ключ тот же — сохранённая страница', () => {
expect(currentPage({ key: 'a', page: 3 }, 'a')).toBe(3);
describe('syncPageState (M-8, M-13)', () => {
it('ключ тот же — состояние то же', () => {
const s = { key: 'a', page: 3 };
expect(syncPageState(s, 'a')).toBe(s);
});
it('ключ сменился — первая страница без отдельного обновления состояния', () => {
expect(currentPage({ key: 'a', page: 3 }, 'b')).toBe(1);
it('ключ сменился — {новый ключ, 1}', () => {
expect(syncPageState({ key: 'a', page: 3 }, 'b')).toEqual({ key: 'b', page: 1 });
});
it('вернулись к прежнему ключу после смены страницы под новым — страница нового', () => {
expect(currentPage({ key: 'b', page: 2 }, 'b')).toBe(2);
it('A (стр. 3) → B → A: снова первая страница, а не прежняя', () => {
const onB = syncPageState({ key: 'a', page: 3 }, 'b');
expect(syncPageState(onB, 'a')).toEqual({ key: 'a', page: 1 });
});
});

View File

@ -41,5 +41,6 @@ export const missingSideEmptyText = (p: CounterPairOut): string | null => {
export const clampPage = (page: number, total: number, pageSize: number): number =>
Math.min(page, Math.max(1, Math.ceil(total / pageSize)));
/** Страница, привязанная к ключу фильтра: другой ключ — первая страница сразу, без отдельного обновления состояния (один запрос). */
export const currentPage = (state: { key: string; page: number }, key: string): number => (state.key === key ? state.page : 1);
/** Состояние страницы под ключом фильтра: другой ключ — `{ключ, 1}`; прежнее состояние не хранится (A→B→A даёт первую страницу). */
export const syncPageState = (state: { key: string; page: number }, key: string): { key: string; page: number } =>
state.key === key ? state : { key, page: 1 };