mirror of
https://github.com/actualbudget/actual.git
synced 2026-05-08 04:49:45 -05:00
49 lines
2.0 KiB
JavaScript
49 lines
2.0 KiB
JavaScript
/**
|
|
* 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, } from 'react';
|
|
import { createStorageSlot, listStorageKeys } from '../utils/storageUtils';
|
|
import { ReactContextError } from '../utils/reactUtils';
|
|
const TAB_CHOICE_PREFIX = 'docusaurus.tab.';
|
|
const Context = React.createContext(undefined);
|
|
function useContextValue() {
|
|
const [tabGroupChoices, setChoices] = useState({});
|
|
const setChoiceSyncWithLocalStorage = useCallback((groupId, newChoice) => {
|
|
createStorageSlot(`${TAB_CHOICE_PREFIX}${groupId}`).set(newChoice);
|
|
}, []);
|
|
useEffect(() => {
|
|
try {
|
|
const localStorageChoices = {};
|
|
listStorageKeys().forEach((storageKey) => {
|
|
if (storageKey.startsWith(TAB_CHOICE_PREFIX)) {
|
|
const groupId = storageKey.substring(TAB_CHOICE_PREFIX.length);
|
|
localStorageChoices[groupId] = createStorageSlot(storageKey).get();
|
|
}
|
|
});
|
|
setChoices(localStorageChoices);
|
|
}
|
|
catch (err) {
|
|
console.error(err);
|
|
}
|
|
}, []);
|
|
const setTabGroupChoices = useCallback((groupId, newChoice) => {
|
|
setChoices((oldChoices) => ({ ...oldChoices, [groupId]: newChoice }));
|
|
setChoiceSyncWithLocalStorage(groupId, newChoice);
|
|
}, [setChoiceSyncWithLocalStorage]);
|
|
return useMemo(() => ({ tabGroupChoices, setTabGroupChoices }), [tabGroupChoices, setTabGroupChoices]);
|
|
}
|
|
export function TabGroupChoiceProvider({ children, }) {
|
|
const value = useContextValue();
|
|
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
}
|
|
export function useTabGroupChoice() {
|
|
const context = useContext(Context);
|
|
if (context == null) {
|
|
throw new ReactContextError('TabGroupChoiceProvider');
|
|
}
|
|
return context;
|
|
}
|
|
//# sourceMappingURL=tabGroupChoice.js.map
|