-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathusePredictFeedConfig.ts
More file actions
370 lines (336 loc) · 12.1 KB
/
Copy pathusePredictFeedConfig.ts
File metadata and controls
370 lines (336 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import {
PredictFeedConfig,
PredictFeedId,
PredictFeedTabConfig,
resolvePredictFeedConfig,
} from '../constants/feedConfig';
import type {
PredictFilterOptionsParams,
PredictMarketListParams,
} from '../types';
import { usePredictFilterOptions } from './usePredictFilterOptions';
/** `'not-found'` when the route `feedId` is unknown (caller navigates home). */
export type PredictFeedConfigStatus = 'ready' | 'not-found';
/** Lifecycle of the active tab's dynamic (related-tags) filter chips. */
export type PredictDynamicFiltersStatus =
| 'idle'
| 'loading'
| 'ready'
| 'unavailable';
/**
* Unified render shape so the view treats static + dynamic filters identically.
* Static filters carry `titleKey` (i18n key); dynamic filters carry `label`
* (already-localized API text). Resolve a chip label as
* `titleKey ? strings(titleKey) : label`.
*/
export interface PredictFeedRenderFilter {
/** Stable id/slug, used as the dedupe + selection key. */
id: string;
titleKey?: string;
label?: string;
/** Ready-to-use list params; feed straight into `usePredictMarketList`. */
params: PredictMarketListParams;
isDynamic: boolean;
}
export interface PredictFeedTabSummary {
id: string;
titleKey: string;
}
export interface UsePredictFeedConfigOptions {
initialTabId?: string;
initialFilterId?: string;
}
export interface PredictFeedConfigResult {
status: PredictFeedConfigStatus;
feedId?: PredictFeedId;
titleKey?: string;
header?: { showBackButton: boolean; showSearchButton: boolean };
tabs: PredictFeedTabSummary[];
/** Hidden for single-tab feeds; filters still render for that one tab. */
showTabBar: boolean;
/** Hidden when the active feed has no user-selectable filters. */
showFilterBar: boolean;
activeTabId?: string;
setActiveTabId: (id: string) => void;
/** Static + deduped dynamic filters for the active tab. */
filters: PredictFeedRenderFilter[];
dynamicFilters: { status: PredictDynamicFiltersStatus };
activeFilterId?: string;
setActiveFilterId: (id: string) => void;
/** The resolved active filter; `.params` feeds `usePredictMarketList`. */
activeFilter?: PredictFeedRenderFilter;
}
/**
* Dynamic filters are best-effort and non-blocking. When a tab declares no
* `filters.dynamic`, we still call `usePredictFilterOptions` (rules of hooks)
* but disabled, with this throwaway params object so no fetch is issued.
*/
const DISABLED_FILTER_OPTIONS_PARAMS: PredictFilterOptionsParams = {
source: 'related-tags',
};
const findTab = (
config: PredictFeedConfig | undefined,
tabId: string | undefined,
): PredictFeedTabConfig | undefined =>
config?.tabs.find((tab) => tab.id === tabId) ?? config?.tabs[0];
const resolveInitialTabId = (
config: PredictFeedConfig | undefined,
initialTabId: string | undefined,
): string | undefined => {
const tabs = config?.tabs ?? [];
if (initialTabId && tabs.some((tab) => tab.id === initialTabId)) {
return initialTabId;
}
return tabs[0]?.id;
};
/**
* The filter we can select synchronously: only static filters (and the tab's
* default) are known before dynamic filters resolve. A dynamic `initialFilterId`
* is honored later, once it appears (see the late-resolution effect).
*/
const resolveInitialFilterId = (
tab: PredictFeedTabConfig | undefined,
initialFilterId: string | undefined,
): string | undefined => {
if (
initialFilterId &&
tab?.filters.static.some((filter) => filter.id === initialFilterId)
) {
return initialFilterId;
}
return tab?.defaultFilterId;
};
/** True when `initialFilterId` is set but not resolvable from static config. */
const isPendingDynamicFilter = (
tab: PredictFeedTabConfig | undefined,
initialFilterId: string | undefined,
): boolean =>
Boolean(
initialFilterId &&
!tab?.filters.static.some((filter) => filter.id === initialFilterId),
);
/**
* Resolves a route `feedId` into a render-ready feed config by combining the
* static `PREDICT_FEED_REGISTRY` shell with runtime dynamic (related-tags)
* filters. Owns the active tab/filter selection state so the consuming view
* (`PredictFeedView`) stays presentational.
*
* Unknown `feedId` resolves to `status: 'not-found'` so the view can navigate
* home. Static/default filters are usable immediately; dynamic filters hydrate
* asynchronously via `usePredictFilterOptions`, are deduped against static by
* id/slug, and on failure leave the static filters in place with
* `dynamicFilters.status: 'unavailable'`. A dynamic `initialFilterId` is
* selected once it appears; otherwise selection falls back to the tab default.
*/
export const usePredictFeedConfig = (
feedId?: string,
options: UsePredictFeedConfigOptions = {},
): PredictFeedConfigResult => {
const { initialTabId, initialFilterId } = options;
const config = useMemo(() => resolvePredictFeedConfig(feedId), [feedId]);
const [activeTabId, setActiveTabIdState] = useState<string | undefined>(() =>
resolveInitialTabId(config, initialTabId),
);
const [activeFilterId, setActiveFilterIdState] = useState<string | undefined>(
() =>
resolveInitialFilterId(
findTab(config, resolveInitialTabId(config, initialTabId)),
initialFilterId,
),
);
// The route's `initialFilterId` when it targets a (not-yet-loaded) dynamic
// filter; consumed once dynamic filters resolve, then cleared. Manual
// tab/filter changes cancel it. Seeded once at mount (the re-init effect
// below re-seeds it on route-param changes).
const pendingDynamicFilterIdRef = useRef<string | undefined>(undefined);
const didSeedPendingRef = useRef(false);
if (!didSeedPendingRef.current) {
didSeedPendingRef.current = true;
const initialTab = findTab(
config,
resolveInitialTabId(config, initialTabId),
);
pendingDynamicFilterIdRef.current = isPendingDynamicFilter(
initialTab,
initialFilterId,
)
? initialFilterId
: undefined;
}
const activeTab = useMemo(
() => findTab(config, activeTabId),
[config, activeTabId],
);
const staticFilters = useMemo<PredictFeedRenderFilter[]>(
() =>
(activeTab?.filters.static ?? []).map((filter) => ({
id: filter.id,
titleKey: filter.titleKey,
label: filter.label,
params: filter.params,
isDynamic: false,
})),
[activeTab],
);
const dynamicConfig = activeTab?.filters.dynamic;
const dynamicParams = useMemo<PredictFilterOptionsParams | undefined>(
() =>
dynamicConfig
? {
source: dynamicConfig.source,
baseTagSlug: dynamicConfig.baseTagSlug,
baseParams: dynamicConfig.baseParams,
limit: dynamicConfig.limit,
}
: undefined,
[dynamicConfig],
);
const {
filterOptions: dynamicOptions,
isLoading: isDynamicLoading,
error: dynamicError,
} = usePredictFilterOptions(dynamicParams ?? DISABLED_FILTER_OPTIONS_PARAMS, {
enabled: Boolean(dynamicParams),
});
const staticFilterIds = useMemo(
() => new Set(staticFilters.map((filter) => filter.id)),
[staticFilters],
);
const dynamicFilters = useMemo<PredictFeedRenderFilter[]>(() => {
if (!dynamicParams) {
return [];
}
return dynamicOptions
.filter((option) => !staticFilterIds.has(option.id))
.map((option) => ({
id: option.id,
label: option.label,
params: option.params,
isDynamic: true,
}));
}, [dynamicParams, dynamicOptions, staticFilterIds]);
const filters = useMemo<PredictFeedRenderFilter[]>(
() => [...staticFilters, ...dynamicFilters],
[staticFilters, dynamicFilters],
);
const dynamicStatus = useMemo<PredictDynamicFiltersStatus>(() => {
if (!dynamicParams) {
return 'idle';
}
if (isDynamicLoading) {
return 'loading';
}
// A fetch error and a successful-but-empty related-tags list are
// intentionally collapsed: both simply hide the dynamic chips.
if (dynamicError || dynamicOptions.length === 0) {
return 'unavailable';
}
return 'ready';
}, [dynamicParams, isDynamicLoading, dynamicError, dynamicOptions.length]);
// Re-initialize selection when any route input changes (the feed itself, or
// the initial tab/filter on the same feed) so a reused screen instance honors
// updated deeplink/navigation params. Skipped on mount (state is already
// seeded by the lazy initializers above). These are primitive route inputs,
// not user gestures, so re-seeding on a value change is intentional.
const seedKey = [feedId, initialTabId, initialFilterId].join('\u0000');
const previousSeedKeyRef = useRef(seedKey);
useEffect(() => {
if (previousSeedKeyRef.current === seedKey) {
return;
}
previousSeedKeyRef.current = seedKey;
const nextConfig = resolvePredictFeedConfig(feedId);
const nextTabId = resolveInitialTabId(nextConfig, initialTabId);
const nextTab = findTab(nextConfig, nextTabId);
setActiveTabIdState(nextTabId);
setActiveFilterIdState(resolveInitialFilterId(nextTab, initialFilterId));
pendingDynamicFilterIdRef.current = isPendingDynamicFilter(
nextTab,
initialFilterId,
)
? initialFilterId
: undefined;
}, [seedKey, feedId, initialTabId, initialFilterId]);
// Honor a dynamic `initialFilterId` once dynamic filters settle. If the
// target never appears (or the load fails), keep the current default.
useEffect(() => {
const pending = pendingDynamicFilterIdRef.current;
if (!pending || dynamicStatus === 'loading') {
return;
}
if (filters.some((filter) => filter.id === pending)) {
setActiveFilterIdState(pending);
}
pendingDynamicFilterIdRef.current = undefined;
}, [dynamicStatus, filters]);
const setActiveTabId = useCallback(
(id: string) => {
// Resolve before storing so `activeTabId` always matches `activeTab`
// (which falls back to the first tab); ignore unknown ids.
const nextTab = findTab(config, id);
if (!nextTab) {
return;
}
pendingDynamicFilterIdRef.current = undefined;
setActiveTabIdState(nextTab.id);
setActiveFilterIdState(nextTab.defaultFilterId);
},
[config],
);
const setActiveFilterId = useCallback(
(id: string) => {
// Ignore ids absent from the active tab's filters so the highlighted chip
// can't diverge from the rendered `activeFilter`/list.
if (!filters.some((filter) => filter.id === id)) {
return;
}
pendingDynamicFilterIdRef.current = undefined;
setActiveFilterIdState(id);
},
[filters],
);
const activeFilter = useMemo<PredictFeedRenderFilter | undefined>(
() =>
filters.find((filter) => filter.id === activeFilterId) ??
filters.find((filter) => filter.id === activeTab?.defaultFilterId) ??
filters[0],
[filters, activeFilterId, activeTab],
);
const tabs = useMemo<PredictFeedTabSummary[]>(
() =>
(config?.tabs ?? []).map((tab) => ({
id: tab.id,
titleKey: tab.titleKey,
})),
[config],
);
const dynamicFiltersInfo = useMemo(
() => ({ status: dynamicStatus }),
[dynamicStatus],
);
// Expose the *resolved* selection ids so the public values always agree with
// the rendered content. `activeTab`/`activeFilter` reconcile via `findTab`/
// fallback every render, whereas the underlying state is only re-seeded by the
// effect a tick later — so on a `feedId` change the raw state can briefly hold
// the previous feed's (now-invalid) ids. Deriving from the resolved values
// removes that transient disagreement.
const resolvedActiveTabId = activeTab?.id ?? activeTabId;
const resolvedActiveFilterId = activeFilter?.id ?? activeFilterId;
return {
status: config ? 'ready' : 'not-found',
feedId: config?.id,
titleKey: config?.titleKey,
header: config?.header,
tabs,
showTabBar: tabs.length > 1,
showFilterBar: config?.showFilterBar ?? true,
activeTabId: resolvedActiveTabId,
setActiveTabId,
filters,
dynamicFilters: dynamicFiltersInfo,
activeFilterId: resolvedActiveFilterId,
setActiveFilterId,
activeFilter,
};
};