forked from facebook/docusaurus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabGroupChoice.tsx
197 lines (180 loc) · 5.79 KB
/
tabGroupChoice.tsx
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, {
useState,
useCallback,
useEffect,
useMemo,
useContext,
type ReactNode,
} from 'react';
import {useHistory, useLocation} from '@docusaurus/router';
import {createStorageSlot, listStorageKeys} from '../utils/storageUtils';
import {ReactContextError} from '../utils/reactUtils';
const TAB_CHOICE_PREFIX = 'docusaurus.tab.';
type ContextValue = {
/** A map from `groupId` to the `value` of the saved choice in storage. */
readonly tabGroupChoicesInStorage: {readonly [groupId: string]: string};
/** A map from `searchKey` to the `value` of the choice in query parameter. */
readonly tabGroupChoicesInQueryParams: {readonly [searchKey: string]: string};
/** Set the new choice value of a group. */
readonly setTabGroupChoice: (
groupId: string | undefined,
queryString: string | boolean | undefined,
newChoice: string,
) => void;
};
const Context = React.createContext<ContextValue | undefined>(undefined);
function getSearchKey(
groupId: string | undefined,
queryString: string | boolean | undefined,
) {
if (typeof queryString === 'string') {
return queryString;
}
if (queryString === false) {
return undefined;
}
if (queryString === true && !groupId) {
throw new Error(
`Docusaurus error: The <Tabs> component groupId prop is required if queryString=true, because this value is used as the search param name. You can also provide an explicit value such as queryString="my-search-param".`,
);
}
return groupId;
}
function useContextValue(): ContextValue {
// TODO not re-render optimized
// See https://thisweekinreact.com/articles/useSyncExternalStore-the-underrated-react-api
const location = useLocation();
const history = useHistory();
const [tabGroupChoicesInStorage, setGroupChoicesInStorage] = useState<
ContextValue['tabGroupChoicesInStorage']
>({});
const [tabGroupChoicesInQueryParams, setGroupChoicesInQueryParams] = useState<
ContextValue['tabGroupChoicesInQueryParams']
>({});
const updateLocalStorage = useCallback(
(groupId: string, newChoice: string) => {
createStorageSlot(`${TAB_CHOICE_PREFIX}${groupId}`).set(newChoice);
},
[],
);
const updateHistory = useCallback(
(searchKey: string, newTabValue: string) => {
const searchParams = new URLSearchParams(location.search);
searchParams.set(searchKey, newTabValue);
history.replace({...location, search: searchParams.toString()});
},
[history, location],
);
useEffect(() => {
try {
const localStorageChoices: {[groupId: string]: string} = {};
listStorageKeys().forEach((storageKey) => {
if (storageKey.startsWith(TAB_CHOICE_PREFIX)) {
const groupIdFromStorage = storageKey.substring(
TAB_CHOICE_PREFIX.length,
);
localStorageChoices[groupIdFromStorage] =
createStorageSlot(storageKey).get()!;
}
});
setGroupChoicesInStorage(localStorageChoices);
} catch (err) {
console.error(err);
}
}, []);
const setTabGroupChoice = useCallback(
(
groupId: string | undefined,
queryString: string | boolean | undefined,
newChoice: string,
) => {
const searchKey = getSearchKey(groupId, queryString);
if (groupId != null) {
setGroupChoicesInStorage((oldChoices) => ({
...oldChoices,
[groupId]: newChoice,
}));
updateLocalStorage(groupId, newChoice);
}
if (searchKey != null) {
setGroupChoicesInQueryParams((oldChoices) => ({
...oldChoices,
[searchKey]: newChoice,
}));
updateHistory(searchKey, newChoice);
}
},
[updateLocalStorage, updateHistory],
);
return useMemo(
() => ({
tabGroupChoicesInStorage,
tabGroupChoicesInQueryParams,
setTabGroupChoice,
}),
[tabGroupChoicesInStorage, tabGroupChoicesInQueryParams, setTabGroupChoice],
);
}
export function TabGroupChoiceProvider({
children,
}: {
children: ReactNode;
}): JSX.Element {
const value = useContextValue();
return <Context.Provider value={value}>{children}</Context.Provider>;
}
type UseTabGroupChoice = {
selectedValue: string | null;
setTabGroupChoice: (newChoice: string) => void;
};
export function useTabGroupChoice(
groupId: string | undefined,
queryString: string | boolean | undefined,
defaultValue: string | null,
values: readonly {value: string}[],
): UseTabGroupChoice {
const searchKey = getSearchKey(groupId, queryString);
const [selectedValue, setSelectedValue] = useState<string | null>(
defaultValue,
);
const context = useContext(Context);
if (context == null) {
throw new ReactContextError('TabGroupChoiceProvider');
}
const setTabGroupChoice = useCallback<UseTabGroupChoice['setTabGroupChoice']>(
(newChoice) => {
setSelectedValue(newChoice);
context.setTabGroupChoice(groupId, queryString, newChoice);
},
[context, groupId, queryString],
);
// Sync storage, query params, and selected state.
useEffect(() => {
const queryParamValue =
searchKey && context.tabGroupChoicesInQueryParams[searchKey];
const storageValue = groupId && context.tabGroupChoicesInStorage[groupId];
const valueToSync = queryParamValue ?? storageValue;
const isValid =
!!valueToSync && values.some(({value}) => value === valueToSync);
if (isValid && valueToSync !== selectedValue) {
setSelectedValue(valueToSync);
}
}, [
context.tabGroupChoicesInQueryParams,
context.tabGroupChoicesInStorage,
groupId,
searchKey,
selectedValue,
values,
]);
return {
selectedValue,
setTabGroupChoice,
};
}