fix: add context for setting provider
This commit is contained in:
@@ -25,17 +25,15 @@ const SettingContext = createContext({
|
|||||||
reloadSetting: () => {},
|
reloadSetting: () => {},
|
||||||
});
|
});
|
||||||
|
|
||||||
export function SettingProvider({ children, isSettingPage = false }) {
|
export function SettingProvider({ children, context }) {
|
||||||
|
const isOptionsPage = useMemo(() => context === "options", [context]);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: setting,
|
data: setting,
|
||||||
isLoading,
|
isLoading,
|
||||||
update,
|
update,
|
||||||
reload,
|
reload,
|
||||||
} = useStorage(
|
} = useStorage(STOKEY_SETTING, DEFAULT_SETTING, KV_SETTING_KEY);
|
||||||
STOKEY_SETTING,
|
|
||||||
DEFAULT_SETTING,
|
|
||||||
isSettingPage ? KV_SETTING_KEY : ""
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (typeof setting?.darkMode === "boolean") {
|
if (typeof setting?.darkMode === "boolean") {
|
||||||
@@ -47,7 +45,7 @@ export function SettingProvider({ children, isSettingPage = false }) {
|
|||||||
}, [setting?.darkMode, update]);
|
}, [setting?.darkMode, update]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isSettingPage) return;
|
if (!isOptionsPage) return;
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -59,7 +57,7 @@ export function SettingProvider({ children, isSettingPage = false }) {
|
|||||||
logger.error("Failed to fetch log level, using default.", error);
|
logger.error("Failed to fetch log level, using default.", error);
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
}, [isSettingPage, setting?.logLevel]);
|
}, [isOptionsPage, setting?.logLevel]);
|
||||||
|
|
||||||
const updateSetting = useCallback(
|
const updateSetting = useCallback(
|
||||||
(objOrFn) => {
|
(objOrFn) => {
|
||||||
@@ -81,21 +79,21 @@ export function SettingProvider({ children, isSettingPage = false }) {
|
|||||||
|
|
||||||
const value = useMemo(
|
const value = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
isSettingPage,
|
context,
|
||||||
setting,
|
setting,
|
||||||
updateSetting,
|
updateSetting,
|
||||||
updateChild,
|
updateChild,
|
||||||
reloadSetting: reload,
|
reloadSetting: reload,
|
||||||
}),
|
}),
|
||||||
[isSettingPage, setting, updateSetting, updateChild, reload]
|
[context, setting, updateSetting, updateChild, reload]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return isSettingPage ? <Loading /> : null;
|
return isOptionsPage ? <Loading /> : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!setting) {
|
if (!setting) {
|
||||||
return isSettingPage ? (
|
return isOptionsPage ? (
|
||||||
<center>
|
<center>
|
||||||
<Alert severity="error" sx={{ maxWidth: 600, margin: "60px auto" }}>
|
<Alert severity="error" sx={{ maxWidth: 600, margin: "60px auto" }}>
|
||||||
<p>数据加载出错,请刷新页面或卸载后重新安装。</p>
|
<p>数据加载出错,请刷新页面或卸载后重新安装。</p>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { storage } from "../libs/storage";
|
|||||||
import { kissLog } from "../libs/log";
|
import { kissLog } from "../libs/log";
|
||||||
import { syncData } from "../libs/sync";
|
import { syncData } from "../libs/sync";
|
||||||
import { useDebouncedCallback } from "./DebouncedCallback";
|
import { useDebouncedCallback } from "./DebouncedCallback";
|
||||||
import { getContext } from "../libs/browser";
|
import { isOptions } from "../libs/browser";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于将组件状态与 Storage 同步
|
* 用于将组件状态与 Storage 同步
|
||||||
@@ -80,8 +80,7 @@ export function useStorage(key, defaultVal = null, syncKey = "") {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 触发远端同步
|
// 触发远端同步
|
||||||
const context = getContext();
|
if (syncKey && isOptions()) {
|
||||||
if (syncKey && context === "options") {
|
|
||||||
debouncedSync(syncKey, data);
|
debouncedSync(syncKey, data);
|
||||||
}
|
}
|
||||||
}, [key, syncKey, isLoading, data, debouncedSync]);
|
}, [key, syncKey, isLoading, data, debouncedSync]);
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ export const getContext = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const isBg = () => getContext() === "background";
|
export const isBg = () => getContext() === "background";
|
||||||
|
export const isOptions = () => getContext() === "options";
|
||||||
|
|
||||||
export const isBuiltinAIAvailable =
|
export const isBuiltinAIAvailable =
|
||||||
"LanguageDetector" in globalThis && "Translator" in globalThis;
|
"LanguageDetector" in globalThis && "Translator" in globalThis;
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import { blobToBase64 } from "./utils";
|
|||||||
*/
|
*/
|
||||||
export const tryClearCaches = async () => {
|
export const tryClearCaches = async () => {
|
||||||
try {
|
try {
|
||||||
if (isExt && !isBg) {
|
if (isExt && !isBg()) {
|
||||||
await sendBgMsg(MSG_CLEAR_CACHES);
|
await sendBgMsg(MSG_CLEAR_CACHES);
|
||||||
} else {
|
} else {
|
||||||
await caches.delete(CACHE_NAME);
|
await caches.delete(CACHE_NAME);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ globalThis.__KISS_CONTEXT__ = "popup";
|
|||||||
const root = ReactDOM.createRoot(document.getElementById("root"));
|
const root = ReactDOM.createRoot(document.getElementById("root"));
|
||||||
root.render(
|
root.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<SettingProvider>
|
<SettingProvider context="popup">
|
||||||
<ThemeProvider>
|
<ThemeProvider>
|
||||||
<Popup />
|
<Popup />
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ export default function ContentFab({
|
|||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SettingProvider>
|
<SettingProvider context="fab">
|
||||||
<ThemeProvider>
|
<ThemeProvider>
|
||||||
<Draggable
|
<Draggable
|
||||||
key="fab"
|
key="fab"
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ export default function Action({ translator, processActions }) {
|
|||||||
}, [windowSize]);
|
}, [windowSize]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SettingProvider>
|
<SettingProvider context="contentPopup">
|
||||||
<ThemeProvider>
|
<ThemeProvider>
|
||||||
{showPopup && (
|
{showPopup && (
|
||||||
<Draggable
|
<Draggable
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ export default function Options() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SettingProvider isSettingPage={true}>
|
<SettingProvider context="options">
|
||||||
<ThemeProvider>
|
<ThemeProvider>
|
||||||
<AlertProvider>
|
<AlertProvider>
|
||||||
<ConfirmProvider>
|
<ConfirmProvider>
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ export default function TranBox({
|
|||||||
const [mouseHover, setMouseHover] = useState(false);
|
const [mouseHover, setMouseHover] = useState(false);
|
||||||
// todo: 这里的 SettingProvider 不应和 background 的共用
|
// todo: 这里的 SettingProvider 不应和 background 的共用
|
||||||
return (
|
return (
|
||||||
<SettingProvider>
|
<SettingProvider context="tranbox">
|
||||||
<ThemeProvider styles={extStyles}>
|
<ThemeProvider styles={extStyles}>
|
||||||
{showBox && (
|
{showBox && (
|
||||||
<DraggableResizable
|
<DraggableResizable
|
||||||
|
|||||||
Reference in New Issue
Block a user