-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathhandlePredictUrl.ts
More file actions
337 lines (313 loc) · 10.6 KB
/
Copy pathhandlePredictUrl.ts
File metadata and controls
337 lines (313 loc) · 10.6 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
import NavigationService from '../../../NavigationService';
import Routes from '../../../../constants/navigation/Routes';
import DevLogger from '../../../SDKConnect/utils/DevLogger';
import ReduxService from '../../../redux';
import {
isPredictTabKey,
type PredictTabKey,
} from '../../../../components/UI/Predict/constants/feedTabs';
import {
PREDICT_WORLD_CUP_FEED_PARAM,
resolvePredictWorldCupInitialTab,
type PredictWorldCupTabKey,
} from '../../../../components/UI/Predict/constants/worldCupTabs';
import { DEFAULT_PREDICT_WORLD_CUP_FLAG } from '../../../../components/UI/Predict/constants/flags';
import {
selectPredictHomeRedesignEnabledFlag,
selectPredictWorldCupConfig,
} from '../../../../components/UI/Predict/selectors/featureFlags';
import type { PredictWorldCupConfig } from '../../../../components/UI/Predict/types/flags';
import type { DeeplinkIntent } from '../../types/DeeplinkIntent';
import { executeDeeplinkIntent } from '../../utils/executeDeeplinkIntent';
import {
isPredictFeedId,
type PredictFeedId,
} from '../../../../components/UI/Predict/constants/feedConfig';
interface HandlePredictUrlParams {
predictPath: string;
origin?: string;
}
/**
* Interface for parsed predict navigation parameters
*/
interface PredictNavigationParams {
market?: string; // Market ID
utmSource?: string; // UTM source for analytics tracking
tab?: PredictTabKey; // Feed tab (when no market param)
// TODO: `worldCupTab` holds the raw (unvalidated) tab value and is also reused
// by the generic feed. Remove/rename to a neutral field once the World Cup
// feature is sunset.
worldCupTab?: PredictWorldCupTabKey; // World Cup feed initial tab (raw tab value)
feed?: string; // Dedicated feed key
filter?: string; // Generic feed filter chip id (parsed separately from tab)
query?: string; // Search query (when no market param)
}
/**
* Parse URL parameters into typed navigation parameters
* @param predictPath The predict URL path with query parameters
* @returns Typed navigation parameters
*/
const parsePredictNavigationParams = (
predictPath: string,
): PredictNavigationParams => {
const urlParams = new URLSearchParams(
predictPath.includes('?') ? predictPath.split('?')[1] : '',
);
const marketId = urlParams.get('market') || urlParams.get('marketId');
const utmSource = urlParams.get('utm_source');
const tabParam = urlParams.get('tab')?.toLowerCase();
const tab = isPredictTabKey(tabParam) ? tabParam : undefined;
const feed = urlParams.get('feed')?.toLowerCase();
const filter = urlParams.get('filter')?.toLowerCase();
const query = urlParams.get('query') || urlParams.get('q') || undefined;
return {
market: marketId || undefined,
utmSource: utmSource || undefined,
tab,
worldCupTab: tabParam,
feed: feed || undefined,
filter: filter || undefined,
query,
};
};
const getPredictWorldCupConfig = (): PredictWorldCupConfig => {
try {
return selectPredictWorldCupConfig(ReduxService.store.getState());
} catch (error) {
DevLogger.log(
'[handlePredictUrl] Unable to read World Cup config, using default:',
error,
);
return DEFAULT_PREDICT_WORLD_CUP_FLAG;
}
};
const getPredictHomeRedesignEnabled = (): boolean => {
try {
return selectPredictHomeRedesignEnabledFlag(ReduxService.store.getState());
} catch (error) {
DevLogger.log(
'[handlePredictUrl] Unable to read home redesign flag, defaulting to disabled:',
error,
);
return false;
}
};
const getMarketListParams = ({
entryPoint,
tab,
query,
}: {
entryPoint: string;
tab?: PredictTabKey;
query?: string;
}) => ({
entryPoint,
...(tab && { tab }),
...(query && { query }),
});
/**
* Build a `main-stack` target into the Predict stack for a specific screen.
*/
const predictTarget = (
screen: string,
params: object,
): DeeplinkIntent['target'] => ({
type: 'main-stack',
routeName: Routes.PREDICT.ROOT,
params: { screen, params },
});
const marketListTarget = ({
entryPoint,
tab,
query,
}: {
entryPoint: string;
tab?: PredictTabKey;
query?: string;
}): DeeplinkIntent['target'] =>
predictTarget(
Routes.PREDICT.MARKET_LIST,
getMarketListParams({ entryPoint, tab, query }),
);
const worldCupTarget = ({
requestedTab,
entryPoint,
}: {
requestedTab?: PredictWorldCupTabKey;
entryPoint: string;
}): DeeplinkIntent['target'] => {
const config = getPredictWorldCupConfig();
if (config.enabled && config.showWorldCupScreen) {
return predictTarget(Routes.PREDICT.WORLD_CUP, {
entryPoint,
initialTab: resolvePredictWorldCupInitialTab(requestedTab, config),
});
}
DevLogger.log(
'[handlePredictUrl] World Cup screen disabled, fallback to market list',
);
return marketListTarget({ entryPoint });
};
/**
* Build the target for a generic, config-driven Predict feed (`PredictFeedView`).
* @param params Resolved generic feed params
*/
const genericFeedTarget = ({
feedId,
initialTabId,
initialFilterId,
query,
entryPoint,
}: {
feedId: PredictFeedId;
initialTabId?: string;
initialFilterId?: string;
query?: string;
entryPoint: string;
}): DeeplinkIntent['target'] => {
DevLogger.log('[handlePredictUrl] Navigating to generic feed:', feedId);
return predictTarget(Routes.PREDICT.FEED, {
feedId,
...(initialTabId && { initialTabId }),
...(initialFilterId && { initialFilterId }),
...(query && { query }),
entryPoint,
});
};
/**
* Resolve the target for market-specific navigation
* @param marketId The market ID to navigate to
* @param entryPoint The entry point for analytics tracking
*/
const marketTarget = (
marketId: string,
entryPoint: string,
): DeeplinkIntent['target'] => {
DevLogger.log(
'[handlePredictUrl] Navigating to market details for market:',
marketId,
);
if (!marketId) {
DevLogger.log(
'[handlePredictUrl] No market ID provided, fallback to market list',
);
return marketListTarget({ entryPoint });
}
return predictTarget(Routes.PREDICT.MARKET_DETAILS, {
marketId,
entryPoint,
});
};
/**
* Predict deeplink handler
*
* @param params Object containing the predict path and origin
*
* Supported URL formats:
* - https://metamask.app.link/predict
* - https://metamask.app.link/predict?market=23246
* - https://metamask.app.link/predict?marketId=23246
* - https://metamask.app.link/predict?market=23246&utm_source=test
* - https://link.metamask.io/predict?market=23246
* - https://link.metamask.io/predict?marketId=23246
* - https://link.metamask.io/predict?tab=crypto
* - https://link.metamask.io/predict?q=bitcoin
* - https://link.metamask.io/predict?query=bitcoin
* - https://link.metamask.io/predict?feed=world-cup
* - https://link.metamask.io/predict?feed=world-cup&tab=live
* - https://link.metamask.io/predict?feed=sports
* - https://link.metamask.io/predict?feed=sports&tab=all&filter=live
* - https://link.metamask.io/predict?feed=popular-today&filter=elections (redirects to Trending)
* - https://link.metamask.io/predict?feed=trending&q=bitcoin
*
* Origin/EntryPoint handling:
* - Base entryPoint is origin if provided, otherwise 'deeplink'
* - If utm_source is present, always appends '_' + utm_source to the base
* - Examples: 'deeplink', 'deeplink_test', 'carousel_twitter', 'notification_campaign'
*
* Navigation behavior:
* - No market param: Navigate to market list
* - market=X or marketId=X: Navigate directly to market details for market X
* - feed=world-cup: Navigate to the dedicated World Cup feed when enabled
* - feed=<known generic id> (sports/politics/crypto/live/trending): Navigate to the generic PredictFeedView when the predictHomeRedesign flag is enabled (tab -> initialTabId, filter -> initialFilterId)
* - feed=popular-today: Redirect to the Trending feed while preserving its filter
* - Unknown feed (or flag disabled): Fall back to the Predict market list
* - Optional tab param when no market: Open feed on a specific tab
* - query=X or q=X: Open feed with search overlay showing results for X
*/
const resolvePredictTarget = ({
predictPath,
origin,
}: HandlePredictUrlParams): DeeplinkIntent['target'] => {
// Parse navigation parameters from URL
const navParams = parsePredictNavigationParams(predictPath);
DevLogger.log('[handlePredictUrl] Parsed navigation parameters:', navParams);
const genericFeed =
navParams.feed === 'popular-today' ? 'trending' : navParams.feed;
// Determine entry point:
// - Base is origin if provided, otherwise 'deeplink'
// - If utm_source is present and different from base, append '_' + utm_source
// - If utm_source equals base, don't append (avoid 'deeplink_deeplink')
// - Examples: 'deeplink_test', 'carousel_twitter', 'notification_campaign'
const baseEntryPoint = origin || 'deeplink';
const shouldAppendUtmSource =
navParams.utmSource && navParams.utmSource !== baseEntryPoint;
const entryPoint = shouldAppendUtmSource
? `${baseEntryPoint}_${navParams.utmSource}`
: baseEntryPoint;
DevLogger.log('[handlePredictUrl] Entry point:', entryPoint);
if (navParams.market) {
return marketTarget(navParams.market, entryPoint);
} else if (navParams.feed === PREDICT_WORLD_CUP_FEED_PARAM) {
return worldCupTarget({
requestedTab: navParams.worldCupTab,
entryPoint,
});
} else if (isPredictFeedId(genericFeed) && getPredictHomeRedesignEnabled()) {
return genericFeedTarget({
feedId: genericFeed,
// worldCupTab holds the raw (unvalidated) tab value; the generic feed's
// sub-tab ids (e.g. basketball/all/live) are resolved by the view.
initialTabId: navParams.worldCupTab,
initialFilterId: navParams.filter,
query: navParams.query,
entryPoint,
});
}
DevLogger.log('[handlePredictUrl] No market parameter, showing list');
return marketListTarget({
entryPoint,
tab: navParams.tab,
query: navParams.query,
});
};
export const createPredictDeeplinkIntent = ({
predictPath,
origin,
}: HandlePredictUrlParams): DeeplinkIntent => ({
target: resolvePredictTarget({ predictPath, origin }),
});
export const handlePredictUrl = async ({
predictPath,
origin,
}: HandlePredictUrlParams) => {
DevLogger.log(
'[handlePredictUrl] Starting predict deeplink handling with path:',
predictPath,
'origin:',
origin,
);
try {
await executeDeeplinkIntent(
createPredictDeeplinkIntent({ predictPath, origin }),
);
} catch (error) {
DevLogger.log('Failed to handle predict deeplink:', error);
// Fallback to market list on error
// Default to 'deeplink' entry point for error fallback
NavigationService.navigation.navigate(Routes.PREDICT.ROOT, {
screen: Routes.PREDICT.MARKET_LIST,
params: { entryPoint: 'deeplink' },
});
}
};