2022-05-18 22:11:37 +02:00
|
|
|
import { Card, Menu, Switch, useMantineTheme } from '@mantine/core';
|
2022-05-10 20:33:11 +02:00
|
|
|
import { useConfig } from '../../tools/state';
|
2022-05-10 18:57:04 +02:00
|
|
|
import { IModule } from './modules';
|
|
|
|
|
|
2022-05-17 21:22:14 +02:00
|
|
|
export function ModuleWrapper(props: any) {
|
2022-05-10 18:57:04 +02:00
|
|
|
const { module }: { module: IModule } = props;
|
2022-05-18 22:11:37 +02:00
|
|
|
const { config, setConfig } = useConfig();
|
2022-05-22 20:42:10 +02:00
|
|
|
const enabledModules = config.modules ?? {};
|
2022-05-10 20:33:11 +02:00
|
|
|
// Remove 'Module' from enabled modules titles
|
2022-05-22 20:42:10 +02:00
|
|
|
const isShown = enabledModules[module.title]?.enabled ?? false;
|
2022-05-10 18:57:04 +02:00
|
|
|
const theme = useMantineTheme();
|
2022-05-18 22:11:37 +02:00
|
|
|
const items: JSX.Element[] = [];
|
|
|
|
|
if (module.options) {
|
|
|
|
|
const keys = Object.keys(module.options);
|
|
|
|
|
const values = Object.values(module.options);
|
|
|
|
|
// Get the value and the name of the option
|
|
|
|
|
const types = values.map((v) => typeof v.value);
|
|
|
|
|
// Loop over all the types with a for each loop
|
|
|
|
|
types.forEach((type, index) => {
|
|
|
|
|
const optionName = `${module.title}.${keys[index]}`;
|
2022-05-22 20:42:10 +02:00
|
|
|
const moduleInConfig = config.modules?.[module.title];
|
2022-05-18 22:11:37 +02:00
|
|
|
// TODO: Add support for other types
|
|
|
|
|
if (type === 'boolean') {
|
|
|
|
|
items.push(
|
|
|
|
|
<Switch
|
|
|
|
|
defaultChecked={
|
|
|
|
|
// Set default checked to the value of the option if it exists
|
2022-05-22 20:42:10 +02:00
|
|
|
moduleInConfig?.options?.[keys[index]]?.value ?? false
|
2022-05-18 22:11:37 +02:00
|
|
|
}
|
|
|
|
|
key={keys[index]}
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
setConfig({
|
|
|
|
|
...config,
|
2022-05-22 20:42:10 +02:00
|
|
|
modules: {
|
|
|
|
|
...config.modules,
|
|
|
|
|
[module.title]: {
|
|
|
|
|
...config.modules[module.title],
|
|
|
|
|
options: {
|
|
|
|
|
...config.modules[module.title].options,
|
|
|
|
|
[keys[index]]: {
|
|
|
|
|
...config.modules[module.title].options?.[keys[index]],
|
|
|
|
|
value: e.currentTarget.checked,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-05-18 22:11:37 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
label={values[index].name}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-05-10 20:56:48 +02:00
|
|
|
if (!isShown) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-05-10 18:57:04 +02:00
|
|
|
return (
|
2022-05-16 15:55:22 +02:00
|
|
|
<Card hidden={!isShown} mx="sm" withBorder radius="lg" shadow="sm">
|
2022-05-18 22:11:37 +02:00
|
|
|
{module.options && (
|
|
|
|
|
<Menu
|
|
|
|
|
size="md"
|
|
|
|
|
shadow="xl"
|
|
|
|
|
closeOnItemClick={false}
|
|
|
|
|
radius="md"
|
|
|
|
|
position="left"
|
|
|
|
|
styles={{
|
|
|
|
|
root: {
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
top: 15,
|
|
|
|
|
right: 15,
|
|
|
|
|
},
|
|
|
|
|
body: {
|
|
|
|
|
backgroundColor:
|
|
|
|
|
theme.colorScheme === 'dark' ? theme.colors.dark[5] : theme.colors.gray[1],
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Menu.Label>Settings</Menu.Label>
|
|
|
|
|
{items.map((item) => (
|
|
|
|
|
<Menu.Item key={item.key}>{item}</Menu.Item>
|
|
|
|
|
))}
|
|
|
|
|
</Menu>
|
|
|
|
|
)}
|
2022-05-10 19:03:41 +02:00
|
|
|
<module.component />
|
2022-05-10 18:57:04 +02:00
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|