Vesktop/src/renderer/settings.ts

34 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";
2023-04-10 01:16:44 +02:00
import { SettingsStore } from "shared/utils/SettingsStore";
2023-04-09 22:49:50 +02:00
2023-04-09 00:49:47 +02:00
import { Common } from "./vencord";
export const PlainSettings = VencordDesktopNative.settings.get() as TSettings;
2023-04-10 01:04:41 +02:00
export const Settings = new SettingsStore(PlainSettings);
Settings.addGlobalChangeListener((o, p) => VencordDesktopNative.settings.set(o, p));
2023-04-09 00:49:47 +02:00
export function useSettings() {
const [, update] = Common.React.useReducer(x => x + 1, 0);
2023-04-10 01:04:41 +02:00
2023-04-09 00:49:47 +02:00
Common.React.useEffect(() => {
2023-04-10 01:04:41 +02:00
Settings.addGlobalChangeListener(update);
return () => Settings.removeGlobalChangeListener(update);
2023-04-09 00:49:47 +02:00
}, []);
2023-04-10 01:04:41 +02:00
return Settings.store;
2023-04-09 00:49:47 +02:00
}
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
};
}