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:
parent
5daff15196
commit
f111ee033a
|
|
@ -28,18 +28,20 @@ export function PredictionList({ layout }: PredictionListProps) {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FlexBox direction="flex-col" gap="gap-4" aria-busy={model.isFiltering}>
|
<FlexBox direction="flex-col" gap="gap-4" aria-busy={model.isFiltering}>
|
||||||
<FilterChips
|
{model.counts.open !== null && (
|
||||||
label={t('list.filter')}
|
<FilterChips
|
||||||
scroll
|
label={t('list.filter')}
|
||||||
options={FILTERS.map((status) => ({
|
scroll
|
||||||
value: status,
|
options={FILTERS.map((status) => ({
|
||||||
label: t(`list.filters.${status}`, {
|
value: status,
|
||||||
n: number(model.counts[status]),
|
label: t(`list.filters.${status}`, {
|
||||||
}),
|
n: number(model.counts[status] ?? 0),
|
||||||
}))}
|
}),
|
||||||
value={model.status}
|
}))}
|
||||||
onChange={model.setStatus}
|
value={model.status}
|
||||||
/>
|
onChange={model.setStatus}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
{model.items.length === 0 ? (
|
{model.items.length === 0 ? (
|
||||||
model.counts.all === 0 ? (
|
model.counts.all === 0 ? (
|
||||||
<EmptyState
|
<EmptyState
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,15 @@ import type { PostEntity } from '../../../post';
|
||||||
/** Which of the creator's published predictions a list holds. */
|
/** Which of the creator's published predictions a list holds. */
|
||||||
export type OwnPredictionsStatus = 'open' | 'closed' | 'all';
|
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
|
* `GET /posts/me/predictions`: one page of the creator's own published predictions, as owner
|
||||||
|
|
|
||||||
|
|
@ -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));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -6,6 +6,8 @@ import type { OwnPredictionSection } from './types';
|
||||||
/**
|
/**
|
||||||
* A section's total for its title, «سیگنالهای باز (۳)», from the API's counts; `undefined`
|
* 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).
|
* 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(
|
export function useOwnPredictionCount(
|
||||||
section: OwnPredictionSection,
|
section: OwnPredictionSection,
|
||||||
|
|
@ -14,5 +16,6 @@ export function useOwnPredictionCount(
|
||||||
...ownSectionQueryOptions(section),
|
...ownSectionQueryOptions(section),
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
});
|
});
|
||||||
return data?.counts[section];
|
if (!data) return undefined;
|
||||||
|
return data.counts[section] ?? (section === 'open' ? data.counts.all : 0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue