-
Notifications
You must be signed in to change notification settings - Fork 94
/
index.ts
158 lines (133 loc) · 3.85 KB
/
index.ts
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
import Cookies from 'js-cookie';
import {type Dispatch, useCallback, useEffect, useState} from 'react';
import {useFirstMountState} from '../useFirstMountState/index.js';
import {useMountEffect} from '../useMountEffect/index.js';
import {useSyncedRef} from '../useSyncedRef/index.js';
import {isBrowser} from '../util/const.js';
const cookiesSetters = new Map<string, Set<Dispatch<string | null>>>();
const registerSetter = (key: string, setter: Dispatch<string | null>) => {
let setters = cookiesSetters.get(key);
if (!setters) {
setters = new Set();
cookiesSetters.set(key, setters);
}
setters.add(setter);
};
const unregisterSetter = (key: string, setter: Dispatch<string | null>): void => {
const setters = cookiesSetters.get(key);
if (!setters) {
return;
}
setters.delete(setter);
if (setters.size === 0) {
cookiesSetters.delete(key);
}
};
const invokeRegisteredSetters = (
key: string,
value: string | null,
skipSetter?: Dispatch<string | null>,
) => {
const setters = cookiesSetters.get(key);
if (!setters) {
return;
}
for (const s of setters) {
if (s !== skipSetter) {
s(value);
}
}
};
export type UseCookieValueOptions<
InitializeWithValue extends boolean | undefined = boolean | undefined,
> = Cookies.CookieAttributes &
(InitializeWithValue extends undefined
? {
/**
* Whether to initialize state with the cookie value or `undefined`.
*
* _We suggest setting this to `false` during SSR._
*
* @default true
*/
initializeWithValue?: InitializeWithValue;
}
: {
initializeWithValue: InitializeWithValue;
});
export type UseCookieValueReturn<V extends undefined | null | string = undefined | null | string> =
[value: V, set: (value: string) => void, remove: () => void, fetch: () => void];
export function useCookieValue(
key: string,
options: UseCookieValueOptions<false>
): UseCookieValueReturn;
export function useCookieValue(key: string, options?: UseCookieValueOptions): UseCookieValueReturn;
/**
* Manages a single cookie.
*
* @param key Name of the cookie to manage.
* @param options Cookie options that will be used during setting and deleting the cookie.
*/
export function useCookieValue(
key: string,
options: UseCookieValueOptions = {},
): UseCookieValueReturn {
if (process.env.NODE_ENV === 'development' && Cookies === undefined) {
throw new ReferenceError(
'Dependency `js-cookies` is not installed, it is required for `useCookieValue` work.',
);
}
let {initializeWithValue = true, ...cookiesOptions} = options;
if (!isBrowser) {
initializeWithValue = false;
}
const methods = useSyncedRef({
set(value: string) {
setState(value);
Cookies.set(key, value, cookiesOptions);
// Update all other hooks with the same key
invokeRegisteredSetters(key, value, setState);
},
remove() {
setState(null);
Cookies.remove(key, cookiesOptions);
invokeRegisteredSetters(key, null, setState);
},
fetchVal: () => Cookies.get(key) ?? null,
fetch() {
const value = methods.current.fetchVal();
setState(value);
invokeRegisteredSetters(key, value, setState);
},
});
const isFirstMount = useFirstMountState();
const [state, setState] = useState<string | null | undefined>(
isFirstMount && initializeWithValue ? methods.current.fetchVal() : undefined,
);
useMountEffect(() => {
if (!initializeWithValue) {
methods.current.fetch();
}
});
useEffect(() => {
registerSetter(key, setState);
return () => {
unregisterSetter(key, setState);
};
}, [key]);
return [
state,
useCallback((value: string) => {
methods.current.set(value);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []),
useCallback(() => {
methods.current.remove();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []),
useCallback(() => {
methods.current.fetch();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []),
];
}