🏗️ 💥 Change the whole folder structure.
Now using src as a subfolder to the source files
This commit is contained in:
94
src/tools/state.tsx
Normal file
94
src/tools/state.tsx
Normal file
@@ -0,0 +1,94 @@
|
||||
// src/context/state.js
|
||||
import { showNotification } from '@mantine/notifications';
|
||||
import axios from 'axios';
|
||||
import { createContext, ReactNode, useContext, useState } from 'react';
|
||||
import { Check, X } from 'tabler-icons-react';
|
||||
import { Config } from './types';
|
||||
|
||||
type configContextType = {
|
||||
config: Config;
|
||||
setConfig: (newconfig: Config) => void;
|
||||
loadConfig: (name: string) => void;
|
||||
getConfigs: () => Promise<string[]>;
|
||||
};
|
||||
|
||||
const configContext = createContext<configContextType>({
|
||||
config: {
|
||||
name: 'default',
|
||||
services: [],
|
||||
settings: {
|
||||
searchBar: true,
|
||||
searchUrl: 'https://www.google.com/search?q=',
|
||||
enabledModules: [],
|
||||
},
|
||||
},
|
||||
setConfig: () => {},
|
||||
loadConfig: async (name: string) => {},
|
||||
getConfigs: async () => [],
|
||||
});
|
||||
|
||||
export function useConfig() {
|
||||
const context = useContext(configContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useConfig must be used within a ConfigProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
type Props = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
export function ConfigProvider({ children }: Props) {
|
||||
const [config, setConfigInternal] = useState<Config>({
|
||||
name: 'default',
|
||||
services: [],
|
||||
settings: {
|
||||
searchBar: true,
|
||||
searchUrl: 'https://www.google.com/search?q=',
|
||||
enabledModules: [],
|
||||
},
|
||||
});
|
||||
|
||||
async function loadConfig(configName: string) {
|
||||
try {
|
||||
const response = await axios.get(`/api/configs/${configName}`);
|
||||
setConfigInternal(response.data);
|
||||
showNotification({
|
||||
title: 'Config',
|
||||
icon: <Check />,
|
||||
color: 'green',
|
||||
autoClose: 1500,
|
||||
radius: 'md',
|
||||
message: `Loaded config : ${configName}`,
|
||||
});
|
||||
} catch (error) {
|
||||
showNotification({
|
||||
title: 'Config',
|
||||
icon: <X />,
|
||||
color: 'red',
|
||||
autoClose: 1500,
|
||||
radius: 'md',
|
||||
message: `Error loading config : ${configName}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function setConfig(newconfig: Config) {
|
||||
axios.put(`/api/configs/${newconfig.name}`, newconfig);
|
||||
setConfigInternal(newconfig);
|
||||
}
|
||||
|
||||
async function getConfigs(): Promise<string[]> {
|
||||
const response = await axios.get('/api/configs');
|
||||
return response.data;
|
||||
}
|
||||
|
||||
const value = {
|
||||
config,
|
||||
setConfig,
|
||||
loadConfig,
|
||||
getConfigs,
|
||||
};
|
||||
return <configContext.Provider value={value}>{children}</configContext.Provider>;
|
||||
}
|
||||
3
src/tools/theme.ts
Normal file
3
src/tools/theme.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { MantineProviderProps } from '@mantine/core';
|
||||
|
||||
export const theme: MantineProviderProps['theme'] = {};
|
||||
38
src/tools/types.ts
Normal file
38
src/tools/types.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export interface Settings {
|
||||
searchUrl: string;
|
||||
searchBar: boolean;
|
||||
enabledModules: string[];
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface Config {
|
||||
name: string;
|
||||
services: serviceItem[];
|
||||
settings: Settings;
|
||||
}
|
||||
|
||||
export const ServiceTypeList = [
|
||||
'Other',
|
||||
'Sonarr',
|
||||
'Radarr',
|
||||
'Lidarr',
|
||||
'qBittorrent',
|
||||
'Plex',
|
||||
'Emby',
|
||||
];
|
||||
export type ServiceType =
|
||||
| 'Other'
|
||||
| 'Sonarr'
|
||||
| 'Radarr'
|
||||
| 'Lidarr'
|
||||
| 'qBittorrent'
|
||||
| 'Plex'
|
||||
| 'Emby';
|
||||
|
||||
export interface serviceItem {
|
||||
[x: string]: any;
|
||||
name: string;
|
||||
type: string;
|
||||
url: string;
|
||||
icon: string;
|
||||
}
|
||||
Reference in New Issue
Block a user