-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStore.tsx
46 lines (41 loc) · 1.61 KB
/
Store.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React from 'react';
import GlobalInterface from './interfaces/GlobalInterface';
const electronStore = window.require('electron-store');
const path = window.require('path');
const startup = window.require('user-startup');
const execPath = `${path.join(path.resolve(), '/node_modules/.bin/electron')}`;
const execArgs = [`${path.join(path.resolve(), '/build/electron.js')}`];
const config = new electronStore();
const StateContext = React.createContext<GlobalInterface | undefined>(undefined);
const SetStateContext = React.createContext<any | undefined>(undefined);
if (!config.get('xmoji')) config.set('xmoji', { set: 'apple', darkMode: true, startup: true });
export const useGlobalState = () => {
const context = React.useContext(StateContext);
if (context === undefined) {
throw new Error('useGlobalState must be used within a Provider');
}
return context;
};
export const useSetGlobaleState = () => {
const context = React.useContext(SetStateContext);
if (context === undefined) {
throw new Error('useSetGlobalState must be used within a Provider');
}
return context;
};
export const StateProvider = ({ children }: { children: React.ReactNode }) => {
const [state, setState] = React.useState(config.get('xmoji'));
React.useEffect(() => {
config.set('xmoji', state);
if (state.startup) {
startup.add('Xmoji', execPath, execArgs);
} else if (!state.startup) {
startup.remove('Xmoji');
}
}, [state]);
return (
<StateContext.Provider value={state}>
<SetStateContext.Provider value={setState}>{children}</SetStateContext.Provider>
</StateContext.Provider>
);
};