mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-10 07:25:48 +01:00
Add a module enabler
Add module enabler in settings. This loops over all the exported Modules in the components/modules folder and renders them. (Interpreted language magic/ metaclasses)
This commit is contained in:
43
components/Settings/ModuleEnabler.tsx
Normal file
43
components/Settings/ModuleEnabler.tsx
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { Group, Switch } from '@mantine/core';
|
||||||
|
import * as Modules from '../modules';
|
||||||
|
import { useConfig } from '../../tools/state';
|
||||||
|
|
||||||
|
export default function ModuleEnabler(props: any) {
|
||||||
|
const { config, setConfig } = useConfig();
|
||||||
|
// Loop over each module with their title
|
||||||
|
const modules = Object.keys(Modules);
|
||||||
|
// Match the enabled modules with the modules array
|
||||||
|
let enabledModules = config.settings.enabledModules ?? [];
|
||||||
|
enabledModules = modules.filter((module) => enabledModules.includes(module));
|
||||||
|
return (
|
||||||
|
<Group direction="column">
|
||||||
|
{modules.map((module: string) => (
|
||||||
|
<Switch
|
||||||
|
key={module}
|
||||||
|
size="md"
|
||||||
|
checked={enabledModules.includes(module)}
|
||||||
|
label={`Enable ${module.replace('Module', '')} module`}
|
||||||
|
onChange={(e) => {
|
||||||
|
if (e.currentTarget.checked) {
|
||||||
|
setConfig({
|
||||||
|
...config,
|
||||||
|
settings: {
|
||||||
|
...config.settings,
|
||||||
|
enabledModules: [...enabledModules, module],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setConfig({
|
||||||
|
...config,
|
||||||
|
settings: {
|
||||||
|
...config.settings,
|
||||||
|
enabledModules: enabledModules.filter((m) => m !== module),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</Group>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ import { Settings as SettingsIcon } from 'tabler-icons-react';
|
|||||||
import { useConfig } from '../../tools/state';
|
import { useConfig } from '../../tools/state';
|
||||||
import { ColorSchemeSwitch } from '../ColorSchemeToggle/ColorSchemeSwitch';
|
import { ColorSchemeSwitch } from '../ColorSchemeToggle/ColorSchemeSwitch';
|
||||||
import SaveConfigComponent from '../Config/SaveConfig';
|
import SaveConfigComponent from '../Config/SaveConfig';
|
||||||
|
import ModuleEnabler from './ModuleEnabler';
|
||||||
|
|
||||||
function SettingsMenu(props: any) {
|
function SettingsMenu(props: any) {
|
||||||
const { config, setConfig } = useConfig();
|
const { config, setConfig } = useConfig();
|
||||||
@@ -63,6 +64,7 @@ function SettingsMenu(props: any) {
|
|||||||
label="Enable search bar"
|
label="Enable search bar"
|
||||||
/>
|
/>
|
||||||
</Group>
|
</Group>
|
||||||
|
<ModuleEnabler />
|
||||||
<ColorSchemeSwitch />
|
<ColorSchemeSwitch />
|
||||||
<SaveConfigComponent />
|
<SaveConfigComponent />
|
||||||
<Text
|
<Text
|
||||||
|
|||||||
1
components/modules/calendar/index.ts
Normal file
1
components/modules/calendar/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { CalendarModule } from './CalendarModule';
|
||||||
1
components/modules/date/index.ts
Normal file
1
components/modules/date/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { DateModule } from './DateModule';
|
||||||
2
components/modules/index.ts
Normal file
2
components/modules/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './date';
|
||||||
|
export * from './calendar';
|
||||||
@@ -1,11 +1,18 @@
|
|||||||
import { Card, useMantineTheme } from '@mantine/core';
|
import { Card, useMantineTheme } from '@mantine/core';
|
||||||
|
import { useConfig } from '../../tools/state';
|
||||||
import { IModule } from './modules';
|
import { IModule } from './modules';
|
||||||
|
|
||||||
export default function ModuleWrapper(props: any) {
|
export default function ModuleWrapper(props: any) {
|
||||||
const { module }: { module: IModule } = props;
|
const { module }: { module: IModule } = props;
|
||||||
|
const { config } = useConfig();
|
||||||
|
const enabledModules = config.settings.enabledModules ?? [];
|
||||||
|
// Remove 'Module' from enabled modules titles
|
||||||
|
const enabledModulesTitles = enabledModules.map((module) => module.replace('Module', ''));
|
||||||
|
const isShown = enabledModulesTitles.includes(module.title);
|
||||||
const theme = useMantineTheme();
|
const theme = useMantineTheme();
|
||||||
return (
|
return (
|
||||||
<Card
|
<Card
|
||||||
|
hidden={!isShown}
|
||||||
mx="sm"
|
mx="sm"
|
||||||
radius="lg"
|
radius="lg"
|
||||||
shadow="sm"
|
shadow="sm"
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
export interface Settings {
|
export interface Settings {
|
||||||
searchUrl: string;
|
searchUrl: string;
|
||||||
searchBar: boolean;
|
searchBar: boolean;
|
||||||
|
enabledModules: string[];
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user