-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathmessage-overlay-store.ts
More file actions
352 lines (284 loc) · 10.6 KB
/
message-overlay-store.ts
File metadata and controls
352 lines (284 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import { useCallback, useEffect, useRef } from 'react';
import { makeMutable, type SharedValue } from 'react-native-reanimated';
import { StateStore } from 'stream-chat';
import { useSyncExternalStore } from 'use-sync-external-store/shim';
import { useStateStore } from '../hooks';
type OverlayState = {
closingPortalHostBlacklist: string[];
id: string | undefined;
messageId: string | undefined;
closing: boolean;
};
type OpenOverlayParams = {
id: string;
messageId?: string;
};
export type Rect = { x: number; y: number; w: number; h: number } | undefined;
export type ClosingPortalLayoutEntry = {
id: string;
layout: SharedValue<Rect>;
};
type ClosingPortalLayoutsState = {
layouts: Record<string, ClosingPortalLayoutEntry[]>;
};
const DefaultState = {
closingPortalHostBlacklist: [],
closing: false,
id: undefined,
messageId: undefined,
};
const DefaultClosingPortalLayoutsState: ClosingPortalLayoutsState = {
layouts: {},
};
let closingPortalLayoutRegistrationCounter = 0;
let closingPortalHostBlacklistRegistrationCounter = 0;
let closingPortalHostBlacklistStack: Array<{ hostNames: string[]; id: string }> = [];
type OverlaySharedValueController = {
incrementCloseCorrectionY: (deltaY: number) => void;
resetCloseCorrectionY: () => void;
reset: () => void;
setBottomH: (rect: Rect) => void;
setMessageH: (rect: Rect) => void;
setTopH: (rect: Rect) => void;
};
let sharedValueController: OverlaySharedValueController | undefined;
export const registerOverlaySharedValueController = (controller: OverlaySharedValueController) => {
sharedValueController = controller;
return () => {
if (sharedValueController === controller) {
sharedValueController = undefined;
}
};
};
export const setOverlayMessageH = (messageH: Rect) => {
sharedValueController?.setMessageH(messageH);
};
export const setOverlayTopH = (topH: Rect) => {
sharedValueController?.setTopH(topH);
};
export const setOverlayBottomH = (bottomH: Rect) => {
sharedValueController?.setBottomH(bottomH);
};
export const bumpOverlayLayoutRevision = (closeCorrectionDeltaY = 0) => {
sharedValueController?.incrementCloseCorrectionY(closeCorrectionDeltaY);
};
export const openOverlay = (params: OpenOverlayParams | string) => {
const overlayPayload = typeof params === 'string' ? { id: params, messageId: undefined } : params;
sharedValueController?.resetCloseCorrectionY();
overlayStore.partialNext({
closing: false,
closingPortalHostBlacklist: getCurrentClosingPortalHostBlacklist(),
...overlayPayload,
});
};
export const closeOverlay = () => {
if (!overlayStore.getLatestValue().id) {
return;
}
requestAnimationFrame(() => {
if (!overlayStore.getLatestValue().id) {
return;
}
overlayStore.partialNext({ closing: true });
});
};
let actionQueue: Array<() => void | Promise<void>> = [];
export const scheduleActionOnClose = (action: () => void | Promise<void>) => {
const { id } = overlayStore.getLatestValue();
if (id) {
actionQueue.push(action);
return;
}
action();
};
export const finalizeCloseOverlay = () => {
overlayStore.next({
...DefaultState,
closingPortalHostBlacklist: getCurrentClosingPortalHostBlacklist(),
});
sharedValueController?.reset();
};
export const overlayStore = new StateStore<OverlayState>(DefaultState);
const closingPortalLayoutsStore = new StateStore<ClosingPortalLayoutsState>(
DefaultClosingPortalLayoutsState,
);
export const createClosingPortalLayoutRegistrationId = () =>
`closing-portal-layout-${closingPortalLayoutRegistrationCounter++}`;
const getCurrentClosingPortalHostBlacklist = () =>
closingPortalHostBlacklistStack[closingPortalHostBlacklistStack.length - 1]?.hostNames ?? [];
const syncClosingPortalHostBlacklist = () => {
overlayStore.partialNext({
closingPortalHostBlacklist: getCurrentClosingPortalHostBlacklist(),
});
};
const createClosingPortalHostBlacklistRegistrationId = () =>
`closing-portal-host-blacklist-${closingPortalHostBlacklistRegistrationCounter++}`;
const setClosingPortalHostBlacklist = (id: string, hostNames: string[]) => {
const existingEntryIndex = closingPortalHostBlacklistStack.findIndex((entry) => entry.id === id);
if (existingEntryIndex === -1) {
closingPortalHostBlacklistStack = [...closingPortalHostBlacklistStack, { hostNames, id }];
} else {
closingPortalHostBlacklistStack = closingPortalHostBlacklistStack.map((entry, index) =>
index === existingEntryIndex ? { ...entry, hostNames } : entry,
);
}
syncClosingPortalHostBlacklist();
};
const clearClosingPortalHostBlacklist = (id: string) => {
const nextBlacklistStack = closingPortalHostBlacklistStack.filter((entry) => entry.id !== id);
if (nextBlacklistStack.length === closingPortalHostBlacklistStack.length) {
return;
}
closingPortalHostBlacklistStack = nextBlacklistStack;
syncClosingPortalHostBlacklist();
};
export const setClosingPortalLayout = (hostName: string, id: string, layout: Rect) => {
const { layouts } = closingPortalLayoutsStore.getLatestValue();
const hostEntries = layouts[hostName] ?? [];
const existingEntry = hostEntries.find((entry) => entry.id === id);
if (existingEntry) {
existingEntry.layout.value = layout;
return;
}
if (!layout) return;
closingPortalLayoutsStore.next({
layouts: {
...layouts,
[hostName]: [...hostEntries, { id, layout: makeMutable<Rect>(layout) }],
},
});
};
export const clearClosingPortalLayout = (hostName: string, id: string) => {
const { layouts } = closingPortalLayoutsStore.getLatestValue();
const hostEntries = layouts[hostName];
if (!hostEntries?.length) {
return;
}
const nextHostEntries = hostEntries.filter((entry) => entry.id !== id);
const nextLayouts = { ...layouts };
if (nextHostEntries.length === 0) {
delete nextLayouts[hostName];
} else {
nextLayouts[hostName] = nextHostEntries;
}
closingPortalLayoutsStore.next({ layouts: nextLayouts });
};
const actionQueueSelector = (nextState: OverlayState) => ({ active: !!nextState.id });
// TODO: V9: Consider having a store per `MessageOverlayHostLayer` to prevent multi-instance
// integrations causing UI issues.
overlayStore.subscribeWithSelector(actionQueueSelector, async ({ active }) => {
if (!active) {
// flush the queue
for (const action of actionQueue) {
await action();
}
actionQueue = [];
}
});
const selector = (nextState: OverlayState) => ({
closing: nextState.closing,
id: nextState.id,
messageId: nextState.messageId,
});
export const useOverlayController = () => {
return useStateStore(overlayStore, selector);
};
const overlayClosingSelector = (nextState: OverlayState) => ({
closing: nextState.closing,
});
const closingPortalHostBlacklistSelector = (nextState: OverlayState) => ({
closingPortalHostBlacklist: nextState.closingPortalHostBlacklist,
});
export const useIsOverlayClosing = () => {
return useStateStore(overlayStore, overlayClosingSelector).closing;
};
export const useClosingPortalHostBlacklistState = () => {
return useStateStore(overlayStore, closingPortalHostBlacklistSelector).closingPortalHostBlacklist;
};
export const useShouldTeleportToClosingPortal = (hostName: string, id: string) => {
const closing = useIsOverlayClosing();
const closingPortalHostBlacklist = useClosingPortalHostBlacklistState();
const closingPortalLayouts = useClosingPortalLayouts();
const hostEntries = closingPortalLayouts[hostName];
return (
!!overlayStore.getLatestValue().id &&
closing &&
!closingPortalHostBlacklist.includes(hostName) &&
hostEntries?.[hostEntries.length - 1]?.id === id
);
};
/**
* Registers a screen-level blacklist of closing portal hosts that should not render while this hook is active.
*
* The blacklist uses stack semantics:
* - mounting/enabling a new instance makes its blacklist active
* - unmounting/disabling restores the previous active blacklist automatically
*
* This keeps stacked screens predictable without requiring previous screens to rerun effects when the top screen
* disappears.
*/
export const useClosingPortalHostBlacklist = (hostNames: string[], enabled = true) => {
const registrationIdRef = useRef<string | null>(null);
if (!registrationIdRef.current) {
registrationIdRef.current = createClosingPortalHostBlacklistRegistrationId();
}
const registrationId = registrationIdRef.current;
const serializedNormalizedHostNames = JSON.stringify([...new Set(hostNames)]);
useEffect(() => {
if (!enabled) {
clearClosingPortalHostBlacklist(registrationId);
return;
}
setClosingPortalHostBlacklist(
registrationId,
JSON.parse(serializedNormalizedHostNames) as string[],
);
return () => {
clearClosingPortalHostBlacklist(registrationId);
};
}, [enabled, registrationId, serializedNormalizedHostNames]);
};
/**
* NOTE:
* Do not swap this back to `useStateStore(closingPortalLayoutsStore, selector)`.
*
* Why this is special:
* - `layouts` is a dynamic-key map (hosts are added/removed at runtime)
* - each host key maintains a stack of registrations
* - We only need React updates when the key set changes (add/remove/reset)
* - Per-layout movement is already on UI thread via the top `entry.layout.value`
*
* Why `useStateStore` is unsafe here:
* - Both `stream-chat`'s `subscribeWithSelector` and our `useStateStore` snapshot
* comparator use an asymmetric key comparison (they iterate previous keys only)
* - That means `{}` -> `{ newHost: entry }` can be treated as "no change"
* - When that happens, overlay slots never mount even though registration has run
*
* `useSyncExternalStore` with raw store subscription avoids that selector compare path,
* so add/remove of hosts is always treated as observable.
*/
export const useClosingPortalLayouts = () => {
const subscribe = useCallback(
(listener: () => void) =>
closingPortalLayoutsStore.subscribe(() => {
listener();
}),
[],
);
const getSnapshot = useCallback(() => closingPortalLayoutsStore.getLatestValue().layouts, []);
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
};
const noOpObject = { active: false, closing: false };
export const useIsOverlayActive = (id: string) => {
const messageOverlaySelector = useCallback(
(nextState: OverlayState) =>
nextState.id === id ? { active: true, closing: nextState.closing } : noOpObject,
[id],
);
return useStateStore(overlayStore, messageOverlaySelector);
};
export const activeIdSelector = (nextState: OverlayState) => ({ id: nextState.id });
export const useHasActiveId = () => {
const { id } = useStateStore(overlayStore, activeIdSelector);
return !!id;
};