Refactor patchStorage to improve theme handling

This commit is contained in:
OTKUSteyler 2026-01-01 23:21:10 +00:00 • committed by GitHub
parent dc93b81ae1
commit 0990faa41d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,22 +12,39 @@ const mmkvStorage = proxyLazy(() => {
export default function patchStorage() {
const patchedKeys = new Set(["ThemeStore", "SelectivelySyncedUserSettingsStore"]);
const sanitizedKeys = new Set<string>();
const patches = [
after("get", mmkvStorage, ([key], ret) => {
if (!_colorRef.current || !patchedKeys.has(key)) return;
if (!patchedKeys.has(key)) return;
const state = findInTree(ret._state, s => typeof s.theme === "string");
if (state) state.theme = _colorRef.key;
if (!state) return;
const lastSetDiscordTheme = _colorRef.lastSetDiscordTheme ?? "darker";
if (/^bn-theme-\d+$/.test(state.theme) && !/^bn-theme-\d+$/.test(lastSetDiscordTheme) && !sanitizedKeys.has(key)) {
try {
const newVal = { ...ret._state, theme: lastSetDiscordTheme };
mmkvStorage.set(key, newVal);
sanitizedKeys.add(key);
} catch (e) {
}
state.theme = lastSetDiscordTheme;
return;
}
if (_colorRef.current) state.theme = _colorRef.key;
}),
before("set", mmkvStorage, ([key, value]) => {
if (!patchedKeys.has(key)) return;
const json = JSON.stringify(value);
const lastSetDiscordTheme = _colorRef.lastSetDiscordTheme ?? "darker";
const replacementTheme = /^bn-theme-\d+$/.test(lastSetDiscordTheme) ? "darker" : lastSetDiscordTheme;
const replaced = json.replace(
/"theme":"bn-theme-\d+"/,
`"theme":${JSON.stringify(lastSetDiscordTheme)}`
`"theme":${JSON.stringify(replacementTheme)}`
);
return [key, JSON.parse(replaced)];