From f111ee033adc7a9bc517d843a15658a158e2d9a8 Mon Sep 17 00:00:00 2001 From: Amiram_se Date: Thu, 5 Nov 2026 19:01:42 +0400 Subject: [PATCH] fix(signals): list each signal once before results are released Until results reach the analyst the API answers one list for every status and no per-status totals, so the signals tab drew each signal under both sections and the full list drew empty filter counts. Co-Authored-By: Claude Opus 5.5 --- .../prediction-list/PredictionList.tsx | 26 ++++++------ .../entities/own-predictions/types.ts | 11 ++++- .../use-own-prediction-count.test.tsx | 42 +++++++++++++++++++ .../use-own-prediction-count.ts | 5 ++- 4 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 platforms/market/src/features/prediction/models/own-predictions/use-own-prediction-count.test.tsx diff --git a/platforms/market/src/app/screens/(main)/dashboard/predictions/all/organisms/prediction-list/PredictionList.tsx b/platforms/market/src/app/screens/(main)/dashboard/predictions/all/organisms/prediction-list/PredictionList.tsx index b75fdef..3708555 100644 --- a/platforms/market/src/app/screens/(main)/dashboard/predictions/all/organisms/prediction-list/PredictionList.tsx +++ b/platforms/market/src/app/screens/(main)/dashboard/predictions/all/organisms/prediction-list/PredictionList.tsx @@ -28,18 +28,20 @@ export function PredictionList({ layout }: PredictionListProps) { return ( - ({ - value: status, - label: t(`list.filters.${status}`, { - n: number(model.counts[status]), - }), - }))} - value={model.status} - onChange={model.setStatus} - /> + {model.counts.open !== null && ( + ({ + value: status, + label: t(`list.filters.${status}`, { + n: number(model.counts[status] ?? 0), + }), + }))} + value={model.status} + onChange={model.setStatus} + /> + )} {model.items.length === 0 ? ( model.counts.all === 0 ? ( ; +/** + * Totals whatever the filter or page: the client never counts. `open` and `closed` are null until + * results reach the creator, when both filters answer the same list. + */ +export type OwnPredictionCountsEntity = { + all: number; + open: number | null; + closed: number | null; +}; /** * `GET /posts/me/predictions`: one page of the creator's own published predictions, as owner diff --git a/platforms/market/src/features/prediction/models/own-predictions/use-own-prediction-count.test.tsx b/platforms/market/src/features/prediction/models/own-predictions/use-own-prediction-count.test.tsx new file mode 100644 index 0000000..7253525 --- /dev/null +++ b/platforms/market/src/features/prediction/models/own-predictions/use-own-prediction-count.test.tsx @@ -0,0 +1,42 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { renderHook, waitFor } from '@testing-library/react'; +import type { ReactNode } from 'react'; +import { describe, expect, it, vi } from 'vitest'; + +import { ownSectionQueryOptions } from './queries'; +import type { OwnPredictionSection } from './types'; +import { useOwnPredictionCount } from './use-own-prediction-count'; + +vi.mock('./queries', () => ({ ownSectionQueryOptions: vi.fn() })); + +function countOf( + section: OwnPredictionSection, + counts: { all: number; open: number | null; closed: number | null }, +) { + vi.mocked(ownSectionQueryOptions).mockReturnValue({ + queryKey: ['own', section], + queryFn: async () => ({ items: [], counts }), + } as never); + const client = new QueryClient(); + return renderHook(() => useOwnPredictionCount(section), { + wrapper: ({ children }: { children: ReactNode }) => ( + {children} + ), + }); +} + +describe('useOwnPredictionCount', () => { + it('holds every signal under «باز» before results are released', async () => { + const counts = { all: 2, open: null, closed: null }; + const open = countOf('open', counts); + await waitFor(() => expect(open.result.current).toBe(2)); + const closed = countOf('closed', counts); + await waitFor(() => expect(closed.result.current).toBe(0)); + }); + + it('answers each section its own total once released', async () => { + const counts = { all: 5, open: 3, closed: 2 }; + const closed = countOf('closed', counts); + await waitFor(() => expect(closed.result.current).toBe(2)); + }); +}); diff --git a/platforms/market/src/features/prediction/models/own-predictions/use-own-prediction-count.ts b/platforms/market/src/features/prediction/models/own-predictions/use-own-prediction-count.ts index 3683991..8ce6081 100644 --- a/platforms/market/src/features/prediction/models/own-predictions/use-own-prediction-count.ts +++ b/platforms/market/src/features/prediction/models/own-predictions/use-own-prediction-count.ts @@ -6,6 +6,8 @@ import type { OwnPredictionSection } from './types'; /** * A section's total for its title, «سیگنال‌های باز (۳)», from the API's counts; `undefined` * while loading or failed, so the title stands without it over the section's error (J10-3e). + * Before results are released the API answers no per-status totals and the same list for both + * sections, since whether a signal closed is itself a result: it all sits under «باز». */ export function useOwnPredictionCount( section: OwnPredictionSection, @@ -14,5 +16,6 @@ export function useOwnPredictionCount( ...ownSectionQueryOptions(section), throwOnError: false, }); - return data?.counts[section]; + if (!data) return undefined; + return data.counts[section] ?? (section === 'open' ? data.counts.all : 0); }