Vesktop/src/renderer/settings.ts

36 lines
1 KiB
TypeScript
Raw Normal View History

2023-04-09 22:49:50 +02:00
/*
* SPDX-License-Identifier: GPL-3.0
* Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
2023-04-09 00:49:47 +02:00
import type { Settings as TSettings } from "shared/settings";
import { makeChangeListenerProxy } from "shared/utils/makeChangeListenerProxy";
2023-04-09 22:49:50 +02:00
2023-04-09 00:49:47 +02:00
import { Common } from "./vencord";
const signals = new Set<() => void>();
export const PlainSettings = VencordDesktopNative.settings.get() as TSettings;
2023-04-09 00:49:47 +02:00
export const Settings = makeChangeListenerProxy(PlainSettings, s => {
VencordDesktopNative.settings.set(s);
2023-04-09 00:49:47 +02:00
signals.forEach(fn => fn());
});
export function useSettings() {
const [, update] = Common.React.useReducer(x => x + 1, 0);
Common.React.useEffect(() => {
signals.add(update);
return () => signals.delete(update);
}, []);
return Settings;
}
export function getValueAndOnChange(key: keyof TSettings) {
return {
value: Settings[key] as any,
2023-04-09 22:49:50 +02:00
onChange: (value: any) => (Settings[key] = value)
2023-04-09 00:49:47 +02:00
};
}