-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathDialogManager.ts
More file actions
184 lines (162 loc) Β· 4.43 KB
/
DialogManager.ts
File metadata and controls
184 lines (162 loc) Β· 4.43 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
import { nanoid } from 'nanoid';
import { StateStore } from 'stream-chat';
export type GetOrCreateDialogParams = {
id: DialogId;
};
type DialogId = string;
export type Dialog = {
close: () => void;
id: DialogId;
isOpen: boolean | undefined;
open: (zIndex?: number) => void;
removalTimeout: NodeJS.Timeout | undefined;
remove: () => void;
toggle: (closeAll?: boolean) => void;
};
export type DialogManagerOptions = {
id?: string;
};
type Dialogs = Record<DialogId, Dialog>;
export type DialogManagerState = {
dialogsById: Dialogs;
};
/**
* Keeps a map of Dialog objects.
* Dialog can be controlled via `Dialog` object retrieved using `useDialog()` hook.
* The hook returns an object with the following API:
*
* - `dialog.open()` - opens the dialog
* - `dialog.close()` - closes the dialog
* - `dialog.toggle()` - toggles the dialog open state. Accepts boolean argument closeAll. If enabled closes any other dialog that would be open.
* - `dialog.remove()` - removes the dialog object reference from the state (primarily for cleanup purposes)
*/
export class DialogManager {
id: string;
state = new StateStore<DialogManagerState>({
dialogsById: {},
});
constructor({ id }: DialogManagerOptions = {}) {
this.id = id ?? nanoid();
}
get openDialogCount() {
return Object.values(this.state.getLatestValue().dialogsById).reduce(
(count, dialog) => {
if (dialog.isOpen) return count + 1;
return count;
},
0,
);
}
getOrCreate({ id }: GetOrCreateDialogParams) {
let dialog = this.state.getLatestValue().dialogsById[id];
if (!dialog) {
dialog = {
close: () => {
this.close(id);
},
id,
isOpen: false,
open: () => {
this.open({ id });
},
removalTimeout: undefined,
remove: () => {
this.remove(id);
},
toggle: (closeAll = false) => {
this.toggle({ id }, closeAll);
},
};
this.state.next((current) => ({
...current,
...{ dialogsById: { ...current.dialogsById, [id]: dialog } },
}));
}
if (dialog.removalTimeout) {
clearTimeout(dialog.removalTimeout);
this.state.next((current) => ({
...current,
...{
dialogsById: {
...current.dialogsById,
[id]: {
...dialog,
removalTimeout: undefined,
},
},
},
}));
}
return dialog;
}
open(params: GetOrCreateDialogParams, closeRest?: boolean) {
const dialog = this.getOrCreate(params);
if (dialog.isOpen) return;
if (closeRest) {
this.closeAll();
}
this.state.next((current) => ({
...current,
dialogsById: { ...current.dialogsById, [dialog.id]: { ...dialog, isOpen: true } },
}));
}
close(id: DialogId) {
const dialog = this.state.getLatestValue().dialogsById[id];
if (!dialog?.isOpen) return;
this.state.next((current) => ({
...current,
dialogsById: { ...current.dialogsById, [dialog.id]: { ...dialog, isOpen: false } },
}));
}
closeAll() {
Object.values(this.state.getLatestValue().dialogsById).forEach((dialog) =>
dialog.close(),
);
}
toggle(params: GetOrCreateDialogParams, closeAll = false) {
if (this.state.getLatestValue().dialogsById[params.id]?.isOpen) {
this.close(params.id);
} else {
this.open(params, closeAll);
}
}
remove(id: DialogId) {
const state = this.state.getLatestValue();
const dialog = state.dialogsById[id];
if (!dialog) return;
if (dialog.removalTimeout) {
clearTimeout(dialog.removalTimeout);
}
this.state.next((current) => {
const newDialogs = { ...current.dialogsById };
delete newDialogs[id];
return {
...current,
dialogsById: newDialogs,
};
});
}
/**
* Marks the dialog state as unused. If the dialog id is referenced again quickly,
* the state will not be removed. Otherwise, the state will be removed after
* a short timeout.
*/
markForRemoval(id: DialogId) {
const dialog = this.state.getLatestValue().dialogsById[id];
if (!dialog) {
return;
}
this.state.next((current) => ({
...current,
dialogsById: {
...current.dialogsById,
[id]: {
...dialog,
removalTimeout: setTimeout(() => {
this.remove(id);
}, 16),
},
},
}));
}
}