2022-04-27 03:12:45 +02:00
|
|
|
// src/context/state.js
|
|
|
|
|
import { createContext, ReactNode, useContext, useState } from 'react';
|
2022-05-02 15:08:52 +02:00
|
|
|
import { Config, serviceItem } from './types';
|
2022-04-27 03:12:45 +02:00
|
|
|
|
2022-05-02 15:08:52 +02:00
|
|
|
type configContextType = {
|
|
|
|
|
config: Config;
|
|
|
|
|
setConfig: (newconfig: Config) => void;
|
2022-04-27 03:12:45 +02:00
|
|
|
addService: (service: serviceItem) => void;
|
|
|
|
|
removeService: (name: string) => void;
|
2022-05-02 15:08:52 +02:00
|
|
|
saveConfig: (newconfig: Config) => void;
|
2022-04-27 03:12:45 +02:00
|
|
|
};
|
|
|
|
|
|
2022-05-02 15:08:52 +02:00
|
|
|
const configContext = createContext<configContextType>({
|
|
|
|
|
config: {
|
|
|
|
|
services: [],
|
|
|
|
|
settings: {
|
|
|
|
|
searchBar: true,
|
|
|
|
|
searchUrl: 'https://www.google.com/search?q=',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
setConfig: () => {},
|
2022-04-27 03:12:45 +02:00
|
|
|
addService: () => {},
|
|
|
|
|
removeService: () => {},
|
2022-05-02 15:08:52 +02:00
|
|
|
saveConfig: () => {},
|
2022-04-27 03:12:45 +02:00
|
|
|
});
|
|
|
|
|
|
2022-05-02 15:08:52 +02:00
|
|
|
export function useConfig() {
|
|
|
|
|
const context = useContext(configContext);
|
2022-04-27 03:12:45 +02:00
|
|
|
if (context === undefined) {
|
2022-05-02 15:08:52 +02:00
|
|
|
throw new Error('useConfig must be used within a ConfigProvider');
|
2022-04-27 03:12:45 +02:00
|
|
|
}
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-02 15:08:52 +02:00
|
|
|
export function ConfigProvider({ children }: Props) {
|
|
|
|
|
const [config, setConfigInternal] = useState<Config>({
|
|
|
|
|
services: [
|
|
|
|
|
{
|
|
|
|
|
type: 'Other',
|
|
|
|
|
name: 'example',
|
|
|
|
|
icon: 'https://c.tenor.com/o656qFKDzeUAAAAC/rick-astley-never-gonna-give-you-up.gif',
|
|
|
|
|
url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
settings: {
|
|
|
|
|
searchBar: true,
|
|
|
|
|
searchUrl: 'https://www.google.com/search?q=',
|
2022-04-27 03:12:45 +02:00
|
|
|
},
|
2022-05-02 15:08:52 +02:00
|
|
|
});
|
2022-04-27 03:12:45 +02:00
|
|
|
|
2022-05-02 15:08:52 +02:00
|
|
|
function setConfig(newConfig: Config) {
|
|
|
|
|
setConfigInternal(newConfig);
|
|
|
|
|
saveConfig(newConfig);
|
2022-04-27 03:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addService(item: serviceItem) {
|
2022-05-02 15:08:52 +02:00
|
|
|
setConfigInternal({
|
|
|
|
|
...config,
|
|
|
|
|
services: [...config.services, item],
|
|
|
|
|
});
|
|
|
|
|
saveConfig({
|
|
|
|
|
...config,
|
|
|
|
|
services: [...config.services, item],
|
|
|
|
|
});
|
2022-04-27 03:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeService(name: string) {
|
2022-05-02 15:08:52 +02:00
|
|
|
// Remove the service with name in config item
|
|
|
|
|
setConfigInternal({
|
|
|
|
|
...config,
|
|
|
|
|
services: config.services.filter((service) => service.name !== name),
|
|
|
|
|
});
|
|
|
|
|
saveConfig({
|
|
|
|
|
...config,
|
|
|
|
|
services: config.services.filter((service) => service.name !== name),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function saveConfig(newconfig: Config) {
|
|
|
|
|
if (!newconfig) return;
|
|
|
|
|
localStorage.setItem('config', JSON.stringify(newconfig));
|
2022-04-27 03:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const value = {
|
2022-05-02 15:08:52 +02:00
|
|
|
config,
|
|
|
|
|
setConfig,
|
2022-04-27 03:12:45 +02:00
|
|
|
addService,
|
|
|
|
|
removeService,
|
2022-05-02 15:08:52 +02:00
|
|
|
saveConfig,
|
2022-04-27 03:12:45 +02:00
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2022-05-02 15:08:52 +02:00
|
|
|
<configContext.Provider value={value}>{children}</configContext.Provider>
|
2022-04-27 03:12:45 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|