@jsb188/react 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -52,6 +52,20 @@ export const ArrowLeft = <svg xmlns="http://www.w3.org/2000/svg" className="icon
52
52
  <path d="M5 12l6 -6" stroke="currentColor" />
53
53
  </svg>;
54
54
 
55
+ export const ArrowUp = <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" className="icon icon-tabler icons-tabler-outline icon-tabler-arrow-up">
56
+ <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
57
+ <path d="M12 5l0 14" />
58
+ <path d="M18 11l-6 -6" />
59
+ <path d="M6 11l6 -6" />
60
+ </svg>;
61
+
62
+ export const ArrowUpFilled = <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.75" strokeLinecap="round" strokeLinejoin="round" className="icon icon-tabler icons-tabler-outline icon-tabler-arrow-up">
63
+ <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
64
+ <path d="M12 5l0 14" />
65
+ <path d="M18 11l-6 -6" />
66
+ <path d="M6 11l6 -6" />
67
+ </svg>;
68
+
55
69
  export const AsteriskSimple = <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="icon icon-tabler icons-tabler-outline icon-tabler-asterisk-simple">
56
70
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
57
71
  <path d="M12 12v-9" stroke="currentColor" />
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@jsb188/react",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Contains react (and DOM) related utils and hooks.",
5
5
  "license": "ISC",
6
6
  "author": "jsb188",
7
7
  "type": "module",
8
8
  "exports": {
9
+ "./states": "./states/index.ts",
9
10
  "./dom": "./utils/dom.ts",
10
11
  "./hooks": "./utils/hooks.ts",
11
12
  "./icons": "./icons/Icon.tsx",
@@ -13,9 +14,14 @@
13
14
  "./types/dom": "./types/dom.d.ts"
14
15
  },
15
16
  "dependencies": {
17
+ "jotai": "^2.12.1"
18
+ },
19
+ "peerDependencies": {
20
+ "@jsb188/app": "^1.0.3",
16
21
  "react": "^18.2.0"
17
22
  },
18
23
  "devDependencies": {
24
+ "react": "^18.2.0",
19
25
  "@types/react": "^18.2.20"
20
26
  },
21
27
  "scripts": {
@@ -0,0 +1,63 @@
1
+ import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai'
2
+
3
+ /**
4
+ * connectedToState; Track network connection state;
5
+ * Developer should use either WebSockets, or SSE to track this,
6
+ * or just keep the value set to {true} on app mount.
7
+ */
8
+
9
+ const connectedToServerState = atom(false);
10
+
11
+ /**
12
+ * screenIsFocused; whether or not app is open & focused
13
+ */
14
+
15
+ const screenIsFocusedState = atom(true);
16
+
17
+ /**
18
+ * Export; set connectedToServerState
19
+ */
20
+
21
+ export function useSetConnectedToServer() {
22
+ return useSetAtom(connectedToServerState);
23
+ }
24
+
25
+ /**
26
+ * Export; get connectedToServerState value
27
+ */
28
+
29
+ export function useConnectedToServerValue() {
30
+ return useAtomValue(connectedToServerState);
31
+ }
32
+
33
+ /**
34
+ * Export; get & set connectedToServerState
35
+ */
36
+
37
+ export function useConnectedToServer() {
38
+ return useAtom(connectedToServerState);
39
+ }
40
+
41
+ /**
42
+ * Export; set screenIsFocusedState
43
+ */
44
+
45
+ export function useSetScreenIsFocused() {
46
+ return useSetAtom(screenIsFocusedState);
47
+ }
48
+
49
+ /**
50
+ * Export; get screenIsFocusedState value
51
+ */
52
+
53
+ export function useScreenIsFocusedValue() {
54
+ return useAtomValue(screenIsFocusedState);
55
+ }
56
+
57
+ /**
58
+ * Export; get & set screenIsFocusedState
59
+ */
60
+
61
+ export function useScreenIsFocused() {
62
+ return useAtom(screenIsFocusedState);
63
+ }
@@ -0,0 +1,7 @@
1
+ import { Provider } from 'jotai';
2
+
3
+ export * from './connected';
4
+ export * from './modal';
5
+ export * from './popOver';
6
+
7
+ export default Provider;
@@ -0,0 +1,340 @@
1
+ import i18n from '@jsb188/app/i18n';
2
+ import type { ServerErrorObj } from '@jsb188/app/types/app';
3
+ import { atom, useAtom, useSetAtom } from 'jotai';
4
+ import { useCallback } from 'react';
5
+
6
+ /**
7
+ * Types
8
+ */
9
+
10
+ interface ModalRequestParams {
11
+ name: string;
12
+ preset?: string;
13
+ props?: Record<string, any>;
14
+ }
15
+
16
+ export interface ModalProps {
17
+ name: string;
18
+ preset: string | null;
19
+ [key: string]: any;
20
+ }
21
+
22
+ export type OpenModalScreenFn = (data: ModalRequestParams) => void;
23
+
24
+ export type OpenModalPopUpFn = (data: ModalRequestParams | null, err?: ServerErrorObj) => void;
25
+
26
+ export interface ModalScreenProps {
27
+ screen: ModalProps | null;
28
+ openModalScreen: OpenModalScreenFn;
29
+ closeModalScreen: () => void;
30
+ }
31
+
32
+ export interface ModalPopUpProps {
33
+ popUp: ModalProps | null;
34
+ openModalPopUp: OpenModalPopUpFn;
35
+ closeModalPopUp: () => void;
36
+ }
37
+
38
+ export interface ModalHandlerProps {
39
+ openModalScreen: OpenModalScreenFn;
40
+ openModalPopUp: OpenModalPopUpFn;
41
+ closeModalScreen: () => void;
42
+ closeModalPopUp: () => void;
43
+ }
44
+
45
+ export interface ModalHookProps extends ModalScreenProps, ModalPopUpProps {}
46
+
47
+ /**
48
+ * Default values
49
+ * NOTE: i18n() isn't ready yet, so I have to provide the key here and use i18n.t(..) in the logic function
50
+ */
51
+
52
+ export const POPUP_DEFAULT_VALUES = {
53
+ app_error: {
54
+ iconName: 'file-broken',
55
+ title: 'error.app_error_title',
56
+ message: 'error.app_error',
57
+ },
58
+ chat_not_live_feature: {
59
+ iconName: 'message-cancel',
60
+ title: 'chat.is_offline',
61
+ message: 'chat.must_be_live_feature',
62
+ },
63
+ not_verified_email: {
64
+ iconName: 'mailbox',
65
+ title: 'auth.check_email',
66
+ message: 'auth.not_verified_yet',
67
+ },
68
+ not_allowed_permission: {
69
+ iconName: 'lock',
70
+ title: 'error.not_allowed',
71
+ message: 'error.not_allowed_perms',
72
+ },
73
+ network_error: {
74
+ iconName: 'wifi-off',
75
+ title: 'error.network_error_title',
76
+ message: 'error.network_error',
77
+ },
78
+ external_link: {
79
+ iconName: 'outbound',
80
+ title: 'app.leaving_app',
81
+ message: 'app.leaving_app_desc',
82
+ confirmText: 'app.link_yes',
83
+ },
84
+ '20020': {
85
+ // Not allowed in chat
86
+ iconName: 'lock-square-rounded-filled',
87
+ },
88
+ '20021': {
89
+ iconName: 'mood-sad-dizzy',
90
+ },
91
+ '10000': {
92
+ preset: 'force_reload',
93
+ iconName: 'confetti',
94
+ },
95
+ '10012': {
96
+ preset: 'force_reload',
97
+ title: 'error.login_changed',
98
+ iconName: 'user-question',
99
+ },
100
+ } as Record<string, Record<string, string>>;
101
+
102
+ /**
103
+ * Modal screen class
104
+ */
105
+
106
+ class ModalScreen {
107
+ readonly name: string;
108
+ readonly state = atom<ModalProps | null>(null);
109
+ readonly defaultValues?: Record<string, any>;
110
+
111
+ constructor(name: string, defaultValues?: Record<string, any>) {
112
+ this.name = `modal_${name}_state`;
113
+ if (defaultValues) {
114
+ this.defaultValues = defaultValues;
115
+ }
116
+ }
117
+
118
+ open(data: ModalRequestParams): ModalProps | null {
119
+ if (data) {
120
+ const { name, props } = data;
121
+ const values = this.defaultValues?.[name ?? ''];
122
+
123
+ return {
124
+ name,
125
+ ...values,
126
+ ...props,
127
+ };
128
+ }
129
+
130
+ return null;
131
+ }
132
+
133
+ close() {
134
+ return null;
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Modal popup class
140
+ */
141
+
142
+ class ModalPopUp extends ModalScreen {
143
+ readonly name: string = 'modal_popup_state';
144
+
145
+ open(data: ModalRequestParams | null, error?: ServerErrorObj): ModalProps | null {
146
+
147
+ if (data) {
148
+ const { preset, name, props } = data;
149
+ const values = this.defaultValues?.[name ?? ''];
150
+
151
+ let updatedAlertValue: ModalProps = {
152
+ // Template values
153
+ name,
154
+ preset: preset || 'normal',
155
+ // Alert props
156
+ id: `alert-${name || 'normal'}`,
157
+ iconName: null,
158
+ title: '',
159
+ message: '',
160
+ };
161
+
162
+ if (values?.iconName) {
163
+ updatedAlertValue.iconName = values.iconName;
164
+ }
165
+
166
+ // NOTE: i18n() isn't ready yet, so I have to use i18n.t(..) in the logic
167
+ ['title', 'message', 'confirmText', 'cancelText'].forEach((key) => {
168
+ if (values?.[key]) {
169
+ updatedAlertValue[key] = i18n.t(values[key]);
170
+ } else if (key === 'message' && i18n.has(`error.${name}`)) {
171
+ updatedAlertValue[key] = i18n.t(`error.${name}`);
172
+ } else if (i18n.has(`error.${name}_${key}`)) {
173
+ updatedAlertValue[key] = i18n.t(`error.${name}_${key}`);
174
+ }
175
+ });
176
+
177
+ if (values?.preset) {
178
+ updatedAlertValue.preset = values.preset;
179
+ }
180
+
181
+ if (props) {
182
+ updatedAlertValue = {
183
+ ...updatedAlertValue,
184
+ ...props,
185
+ };
186
+ }
187
+
188
+ if (!updatedAlertValue.title && !isNaN(Number(name))) {
189
+ updatedAlertValue.title = i18n.t('error.error');
190
+ }
191
+
192
+ return updatedAlertValue;
193
+
194
+ } else if (error) {
195
+ const { errorCode } = error;
196
+ const { iconName: defIconName, title: defTitle, message: defMessage, ...values } = this.defaultValues?.[errorCode ?? ''] || {};
197
+
198
+ return {
199
+ // Template values
200
+ name: 'ERROR',
201
+ preset: 'normal',
202
+ // Alert props
203
+ id: `gql-${errorCode}`,
204
+ iconName: error.iconName || defIconName,
205
+ title: error.title || defTitle || i18n.t('error.error'),
206
+ message: error.message || defMessage || i18n.t('error.unknown_error'),
207
+ ...values,
208
+ };
209
+ }
210
+
211
+ return null;
212
+ }
213
+
214
+ close() {
215
+ return null;
216
+ }
217
+ }
218
+
219
+ /**
220
+ * Create states
221
+ */
222
+
223
+ const screenClass = new ModalScreen('screen');
224
+ const popUpClass = new ModalPopUp('popup', POPUP_DEFAULT_VALUES);
225
+
226
+ /**
227
+ * Create handler functions
228
+ */
229
+
230
+ function composeOpenModalScreenFn(setScreen: (value: ModalProps | null) => void) {
231
+ return (data: ModalRequestParams) => {
232
+ const nextScreenState = screenClass.open(data);
233
+ if (nextScreenState) {
234
+ setScreen(nextScreenState);
235
+ } else {
236
+ console.warn('Invalid {screen} data:', data);
237
+ }
238
+ };
239
+ }
240
+
241
+ function composeCloseModalScreenFn(setScreen: (value: ModalProps | null) => void) {
242
+ return () => {
243
+ setScreen(screenClass.close());
244
+ };
245
+ }
246
+
247
+ function composeOpenModalPopUpFn(setPopUp: (value: ModalProps | null) => void) {
248
+ return (data: ModalRequestParams | null, err?: ServerErrorObj) => {
249
+ const nextPopUpState = popUpClass.open(data, err);
250
+ if (nextPopUpState) {
251
+ setPopUp(nextPopUpState);
252
+ } else {
253
+ console.warn('Invalid {popUp} data:', data, err);
254
+ }
255
+ };
256
+ }
257
+
258
+ function composeCloseModalPopUpFn(setPopUp: (value: ModalProps | null) => void) {
259
+ return () => {
260
+ setPopUp(popUpClass.close());
261
+ };
262
+ }
263
+
264
+ /**
265
+ * Use modal screen hook
266
+ */
267
+
268
+ export function useModalScreen(): ModalScreenProps {
269
+ const [screen, setScreen] = useAtom(screenClass.state);
270
+ const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen), []);
271
+ const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
272
+
273
+ return {
274
+ screen,
275
+ openModalScreen,
276
+ closeModalScreen,
277
+ };
278
+ }
279
+
280
+ /**
281
+ * Use modal popup hook
282
+ */
283
+
284
+ export function useModalPopUp(): ModalPopUpProps {
285
+ const [popUp, setPopUp] = useAtom(popUpClass.state);
286
+ const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
287
+ const closeModalPopUp = useCallback( composeCloseModalPopUpFn(setPopUp), []);
288
+
289
+ return {
290
+ popUp,
291
+ openModalPopUp,
292
+ closeModalPopUp,
293
+ };
294
+ }
295
+
296
+ /**
297
+ * Use modal hook handlers
298
+ */
299
+
300
+ export function useModalHandlers(): ModalHandlerProps {
301
+ const setScreen = useSetAtom(screenClass.state);
302
+ const setPopUp = useSetAtom(popUpClass.state);
303
+
304
+ const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen), []);
305
+ const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
306
+ const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
307
+ const closeModalPopUp = useCallback( composeCloseModalPopUpFn(setPopUp), []);
308
+
309
+ return {
310
+ openModalScreen,
311
+ closeModalScreen,
312
+ openModalPopUp,
313
+ closeModalPopUp,
314
+ };
315
+ }
316
+
317
+ /**
318
+ * Use popup open function
319
+ */
320
+
321
+ export function useOpenModalPopUp(): OpenModalPopUpFn {
322
+ const setPopUp = useSetAtom(popUpClass.state);
323
+ const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
324
+
325
+ return openModalPopUp;
326
+ }
327
+
328
+ /**
329
+ * Use full modal hook
330
+ */
331
+
332
+ export function useModal(): ModalHookProps {
333
+ const screenProps = useModalScreen();
334
+ const popUpProps = useModalPopUp();
335
+
336
+ return {
337
+ ...screenProps,
338
+ ...popUpProps,
339
+ };
340
+ }
@@ -0,0 +1,299 @@
1
+ import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai';
2
+ import { useCallback } from 'react';
3
+
4
+ /**
5
+ * Type; Pop overs
6
+ */
7
+
8
+ export enum PopOverNames {
9
+ popover = 'popover',
10
+ options = 'options',
11
+ chat_options = 'chat_options',
12
+ mini_profile = 'mini_profile',
13
+ notification = 'notification',
14
+ persona_picker = 'persona_picker',
15
+ image_popover = 'image_popover'
16
+ }
17
+
18
+ export type POPosition = 'top' | 'left' | 'right' | 'bottom';
19
+
20
+ export interface ClientRectValues {
21
+ width: number;
22
+ height: number;
23
+ left: number;
24
+ right: number;
25
+ top: number;
26
+ bottom: number;
27
+ x: number;
28
+ y: number;
29
+ }
30
+
31
+ interface FixedElementProps {
32
+ id: string; // This ensures that the popover doesn't reset from within the same mouse pointer element
33
+ className: string;
34
+ position: POPosition;
35
+ variables: Record<string, any>;
36
+ popOverWidth: number;
37
+ popOverHeight: number;
38
+ rect?: ClientRectValues;
39
+ offsetX: number;
40
+ offsetY: number;
41
+ leftEdgeThreshold: number;
42
+ rightEdgeThreshold: number;
43
+ leftEdgePosition: number;
44
+ rightEdgePosition: number;
45
+ hover: boolean;
46
+ }
47
+
48
+ interface UpdateStateParams {
49
+ name: PopOverNames;
50
+ variables: Record<string, any>;
51
+ }
52
+
53
+ export interface PopOverProps extends Partial<FixedElementProps> {
54
+ name: PopOverNames; // Used to be "__type"
55
+ doNotTrackHover?: boolean;
56
+ children?: any; // ????????
57
+ }
58
+
59
+ export interface TooltipProps extends Partial<FixedElementProps> {
60
+ title?: string;
61
+ message?: string;
62
+ }
63
+
64
+ export type OpenPopOverFn = (data: PopOverProps) => void;
65
+ export type ClosePopOverFn = (dismissIfName?: PopOverNames) => void;
66
+ export type UpdatePopOverFn = (data: UpdateStateParams) => void;
67
+
68
+ export interface PopOverHandlerProps {
69
+ openPopOver: OpenPopOverFn;
70
+ closePopOver: ClosePopOverFn;
71
+ updatePopOver: UpdatePopOverFn;
72
+ }
73
+
74
+ export interface PopOverHookProps extends PopOverHandlerProps {
75
+ popOver: PopOverProps | null;
76
+ }
77
+
78
+ export type OpenTooltipFn = (data: TooltipProps) => void;
79
+ export type CloseTooltipFn = (removeId: string) => void;
80
+ export type UpdateTooltipFn = (data: TooltipProps) => void;
81
+
82
+ export interface TooltipHandlerProps {
83
+ openTooltip: OpenTooltipFn;
84
+ closeTooltip: CloseTooltipFn;
85
+ updateTooltip: UpdateTooltipFn;
86
+ }
87
+
88
+ export interface TooltipHookProps extends TooltipHandlerProps {
89
+ tooltip: TooltipProps | null;
90
+ }
91
+
92
+ /**
93
+ * Pop over class
94
+ */
95
+
96
+ class PopOver {
97
+ readonly name: string = 'pop_over_state';
98
+ readonly state = atom<PopOverProps | null>(null);
99
+
100
+ open(data: PopOverProps): PopOverProps | null {
101
+ if (!data?.name) {
102
+ return null;
103
+ }
104
+ return data;
105
+ }
106
+
107
+ update(data: UpdateStateParams, prev: PopOverProps | null): PopOverProps | null {
108
+ const { name, ...variables } = data;
109
+ const nextName = name || prev?.name
110
+ if (!nextName) {
111
+ return null;
112
+ }
113
+ return {
114
+ ...prev,
115
+ name: nextName,
116
+ variables,
117
+ };
118
+ }
119
+
120
+ close(): PopOverProps | null {
121
+ return null;
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Tooltip class
127
+ */
128
+
129
+ class Tooltip {
130
+ readonly name: string = 'tooltip_state';
131
+ readonly state = atom<TooltipProps | null>(null);
132
+
133
+ open(data: TooltipProps): TooltipProps | null {
134
+ if (!data?.id) {
135
+ return null;
136
+ }
137
+ return data;
138
+ }
139
+
140
+ update(data: TooltipProps, prev: TooltipProps | null): TooltipProps | null {
141
+ if (!data || !prev) {
142
+ return null;
143
+ }
144
+ return {
145
+ ...prev,
146
+ ...data,
147
+ };
148
+ }
149
+
150
+ close(): TooltipProps | null {
151
+ return null;
152
+ }
153
+ }
154
+
155
+ const popOverClass = new PopOver();
156
+ const tooltipClass = new Tooltip();
157
+
158
+ /**
159
+ * Hover state for pop overs
160
+ */
161
+
162
+ const popOverIsHoverState = atom(false);
163
+
164
+ export function usePopOverIsHover() {
165
+ return useAtomValue(popOverIsHoverState);
166
+ }
167
+
168
+ export function useSetPopOverIsHover() {
169
+ return useSetAtom(popOverIsHoverState);
170
+ }
171
+
172
+ /**
173
+ * Create handlers functions for pop overs
174
+ */
175
+
176
+ function composeOpenPopOverFn( setPopOver: (value: PopOverProps | null) => void): OpenPopOverFn {
177
+ return (data: PopOverProps) => {
178
+ const nextPopOverState = popOverClass.open(data);
179
+ if (nextPopOverState) {
180
+ setPopOver(nextPopOverState);
181
+ } else {
182
+ console.warn('Invalid {popOver} data for open():', data);
183
+ }
184
+ };
185
+ }
186
+
187
+ function composeClosePopOverFn(
188
+ setPopOver: (value: PopOverProps | null) => void,
189
+ setPopOverIsHover: (update: boolean | ((prev: boolean) => boolean)) => void,
190
+ popOver: PopOverProps | null
191
+ ): ClosePopOverFn {
192
+ return (dismissIfName?: PopOverNames) => {
193
+ if (!popOver || (typeof dismissIfName === 'string' && popOver.name !== dismissIfName)) {
194
+ return;
195
+ }
196
+ setPopOverIsHover(false);
197
+ setPopOver(popOverClass.close());
198
+ };
199
+ }
200
+
201
+ function composeUpdatePopOverFn( setPopOver: (value: PopOverProps | null) => void): UpdatePopOverFn {
202
+ return (data: UpdateStateParams) => {
203
+ // @ts-expect-error - I couldn't get rid of this type error
204
+ setPopOver((prev: PopOverProps | null) => {
205
+ const nextPopOverState = popOverClass.update(data, prev);
206
+ if (nextPopOverState) {
207
+ return nextPopOverState;
208
+ }
209
+ console.warn('Invalid {popOver} data for update():', data);
210
+ return prev;
211
+ });
212
+ };
213
+ }
214
+
215
+ /**
216
+ * Use pop over
217
+ */
218
+
219
+ export function usePopOver(): PopOverHookProps {
220
+ const [popOver, setPopOver] = useAtom<PopOverProps | null>(popOverClass.state);
221
+ const setPopOverIsHover = useSetAtom(popOverIsHoverState);
222
+
223
+ const openPopOver = useCallback( composeOpenPopOverFn(setPopOver), []);
224
+ const closePopOver = useCallback( composeClosePopOverFn(setPopOver, setPopOverIsHover, popOver), [popOver]);
225
+
226
+ // Test to make sure updatePopOver updates previous state even with [] deps
227
+ const updatePopOver = useCallback( composeUpdatePopOverFn(setPopOver), []);
228
+
229
+ return {
230
+ popOver,
231
+ openPopOver,
232
+ closePopOver,
233
+ updatePopOver,
234
+ };
235
+ }
236
+
237
+ /**
238
+ * Create handlers functions for tooltips
239
+ */
240
+
241
+ function composeOpenTooltipFn( setTooltip: (value: TooltipProps | null) => void): OpenTooltipFn {
242
+ return (data: TooltipProps) => {
243
+ const nextTooltipState = tooltipClass.open(data);
244
+ if (nextTooltipState) {
245
+ setTooltip(nextTooltipState);
246
+ } else {
247
+ console.warn('Invalid {tooltip} data for open():', data);
248
+ }
249
+ };
250
+ }
251
+
252
+ function composeCloseTooltipFn( setTooltip: (value: TooltipProps | null) => void): CloseTooltipFn {
253
+ return (removeId?: string) => {
254
+ if (!removeId) {
255
+ return;
256
+ }
257
+
258
+ // @ts-expect-error - I couldn't get rid of this type error
259
+ setTooltip((prev: TooltipProps | null) => {
260
+ if (prev?.id !== removeId) {
261
+ return prev;
262
+ }
263
+ return popOverClass.close();
264
+ });
265
+ };
266
+ }
267
+
268
+ function composeUpdateTooltipFn( setTooltip: (value: TooltipProps | null) => void): UpdateTooltipFn {
269
+ return (data: TooltipProps) => {
270
+ // @ts-expect-error - I couldn't get rid of this type error
271
+ setTooltip((prev: TooltipProps | null) => {
272
+ const nextTooltipState = tooltipClass.update(data, prev);
273
+ if (nextTooltipState) {
274
+ return nextTooltipState;
275
+ }
276
+ console.warn('Invalid {tooltip} data for update():', data);
277
+ return prev;
278
+ });
279
+ };
280
+ }
281
+
282
+ /**
283
+ * Use tooltip
284
+ */
285
+
286
+ export function useTooltip(): TooltipHookProps {
287
+ const [tooltip, setTooltip] = useAtom<TooltipProps | null>(tooltipClass.state);
288
+
289
+ const openTooltip = useCallback( composeOpenTooltipFn(setTooltip), []);
290
+ const closeTooltip = useCallback( composeCloseTooltipFn(setTooltip), []);
291
+ const updateTooltip = useCallback( composeUpdateTooltipFn(setTooltip), []);
292
+
293
+ return {
294
+ tooltip,
295
+ openTooltip,
296
+ closeTooltip,
297
+ updateTooltip,
298
+ };
299
+ }