|
1 | | -import { Id, OptionalId } from '@audius/sdk' |
| 1 | +import { Id, OptionalHashId, OptionalId } from '@audius/sdk' |
2 | 2 | import { create, windowScheduler } from '@yornaath/batshit' |
3 | 3 | import { memoize } from 'lodash' |
4 | 4 |
|
5 | 5 | import { eventMetadataListFromSDK } from '~/adapters/event' |
6 | 6 | import { ID, Event } from '~/models' |
7 | 7 |
|
| 8 | +import { getRemixesCountQueryKey } from '../remixes/useRemixes' |
| 9 | + |
8 | 10 | import { contextCacheResolver } from './contextCacheResolver' |
9 | 11 | import { BatchContext } from './types' |
10 | 12 |
|
11 | 13 | export const getEventsByEntityIdBatcher = memoize( |
12 | 14 | (context: BatchContext) => |
13 | 15 | create({ |
14 | 16 | fetcher: async (entityIds: ID[]): Promise<Event[]> => { |
15 | | - const { sdk, currentUserId } = context |
| 17 | + const { sdk, currentUserId, queryClient } = context |
16 | 18 | if (!entityIds.length) return [] |
17 | | - const { data } = await sdk.events.getEntityEvents({ |
| 19 | + const { data, related } = await sdk.events.getEntityEvents({ |
18 | 20 | entityId: entityIds.map((entityId) => Id.parse(entityId)), |
19 | 21 | userId: OptionalId.parse(currentUserId) |
20 | 22 | }) |
21 | 23 |
|
| 24 | + // Prime the dedicated `useRemixesCount` cache from the entry counts |
| 25 | + // delivered alongside the event list (keyed by the contest's parent |
| 26 | + // track hashid). This turns ContestCard's entry-count badge into a |
| 27 | + // cache hit so surfaces that resolve contests via this endpoint (the |
| 28 | + // track-page contest section, cold contest pages, web Explore's |
| 29 | + // featured contests) don't fire a count-only |
| 30 | + // `/tracks/{id}/remixes?limit=0` per card. Same pattern as |
| 31 | + // useAllRemixContests / useUserRemixContests. |
| 32 | + const entryCounts = related?.entryCounts ?? {} |
| 33 | + for (const [hashedTrackId, count] of Object.entries(entryCounts)) { |
| 34 | + const trackId = OptionalHashId.parse(hashedTrackId) |
| 35 | + if (!trackId) continue |
| 36 | + queryClient.setQueryData( |
| 37 | + getRemixesCountQueryKey({ trackId, isContestEntry: true }), |
| 38 | + count |
| 39 | + ) |
| 40 | + } |
| 41 | + |
22 | 42 | return eventMetadataListFromSDK(data) |
23 | 43 | }, |
24 | 44 | resolver: (events: Event[], entityId: ID) => |
|
0 commit comments