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 <noreply@anthropic.com>
This commit is contained in:
Amiram_se 2026-11-05 19:01:42 +04:00
parent 5daff15196
commit f111ee033a
4 changed files with 69 additions and 15 deletions

View File

@ -28,18 +28,20 @@ export function PredictionList({ layout }: PredictionListProps) {
return (
<FlexBox direction="flex-col" gap="gap-4" aria-busy={model.isFiltering}>
<FilterChips
label={t('list.filter')}
scroll
options={FILTERS.map((status) => ({
value: status,
label: t(`list.filters.${status}`, {
n: number(model.counts[status]),
}),
}))}
value={model.status}
onChange={model.setStatus}
/>
{model.counts.open !== null && (
<FilterChips
label={t('list.filter')}
scroll
options={FILTERS.map((status) => ({
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 ? (
<EmptyState

View File

@ -3,8 +3,15 @@ import type { PostEntity } from '../../../post';
/** Which of the creator's published predictions a list holds. */
export type OwnPredictionsStatus = 'open' | 'closed' | 'all';
/** Totals whatever the filter or page: the client never counts. */
export type OwnPredictionCountsEntity = Record<OwnPredictionsStatus, number>;
/**
* 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

View File

@ -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 }) => (
<QueryClientProvider client={client}>{children}</QueryClientProvider>
),
});
}
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));
});
});

View File

@ -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);
}