From 119f2d7e51c205aca7d3966013ca42a5780e5aee Mon Sep 17 00:00:00 2001 From: ajnart Date: Wed, 18 May 2022 22:11:37 +0200 Subject: [PATCH] :sparkles: Add a proceudally generated options manager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows for options in settings generated based on their name in module config. Very important change 🧙‍ --- src/components/modules/date/DateModule.tsx | 31 ++++++---- src/components/modules/moduleWrapper.tsx | 67 +++++++++++++++++++++- src/components/modules/modules.tsx | 11 +++- 3 files changed, 94 insertions(+), 15 deletions(-) diff --git a/src/components/modules/date/DateModule.tsx b/src/components/modules/date/DateModule.tsx index ba653f862..7b31f859e 100644 --- a/src/components/modules/date/DateModule.tsx +++ b/src/components/modules/date/DateModule.tsx @@ -2,6 +2,7 @@ import { Group, Text, Title } from '@mantine/core'; import dayjs from 'dayjs'; import { useEffect, useState } from 'react'; import { Clock } from 'tabler-icons-react'; +import { useConfig } from '../../../tools/state'; import { IModule } from '../modules'; export const DateModule: IModule = { @@ -9,33 +10,39 @@ export const DateModule: IModule = { description: 'Show the current time and date in a card', icon: Clock, component: DateComponent, + options: { + full: { + name: 'Display full time (24-hour)', + value: true, + }, + }, }; export default function DateComponent(props: any) { const [date, setDate] = useState(new Date()); + const { config } = useConfig(); const hours = date.getHours(); const minutes = date.getMinutes(); - + const fullSetting = config.settings[`${DateModule.title}.full`]; // Change date on minute change // Note: Using 10 000ms instead of 1000ms to chill a little :) useEffect(() => { setInterval(() => { setDate(new Date()); - }, 10000); + }, 1000 * 60); }, []); + const timeString = `${hours < 10 ? `0${hours}` : hours}:${ + minutes < 10 ? `0${minutes}` : minutes + }`; + const halfTimeString = `${hours < 10 ? `${hours % 12}` : hours % 12}:${ + minutes < 10 ? `0${minutes}` : minutes + } ${hours < 12 ? 'AM' : 'PM'}`; + const finalTimeString = fullSetting ? timeString : halfTimeString; return ( - - {hours < 10 ? `0${hours}` : hours}:{minutes < 10 ? `0${minutes}` : minutes} - - - { - // Use dayjs to format the date - // https://day.js.org/en/getting-started/installation/ - dayjs(date).format('dddd, MMMM D') - } - + {finalTimeString} + {dayjs(date).format('dddd, MMMM D')} ); } diff --git a/src/components/modules/moduleWrapper.tsx b/src/components/modules/moduleWrapper.tsx index 55b421a5e..d081f013d 100644 --- a/src/components/modules/moduleWrapper.tsx +++ b/src/components/modules/moduleWrapper.tsx @@ -1,19 +1,82 @@ -import { Card, useMantineTheme } from '@mantine/core'; +import { Card, Menu, Switch, useMantineTheme } from '@mantine/core'; import { useConfig } from '../../tools/state'; import { IModule } from './modules'; export default function ModuleWrapper(props: any) { const { module }: { module: IModule } = props; - const { config } = useConfig(); + const { config, setConfig } = useConfig(); const enabledModules = config.settings.enabledModules ?? []; // Remove 'Module' from enabled modules titles const isShown = enabledModules.includes(module.title); const theme = useMantineTheme(); + 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]}`; + // TODO: Add support for other types + if (type === 'boolean') { + items.push( + { + setConfig({ + ...config, + settings: { + ...config.settings, + enabledModules: [...config.settings.enabledModules], + [optionName]: e.currentTarget.checked, + }, + }); + }} + label={values[index].name} + /> + ); + } + }); + } + // Sussy baka if (!isShown) { return null; } return ( ); diff --git a/src/components/modules/modules.tsx b/src/components/modules/modules.tsx index 60ce7d55d..c3c3ef1dd 100644 --- a/src/components/modules/modules.tsx +++ b/src/components/modules/modules.tsx @@ -7,5 +7,14 @@ export interface IModule { description: string; icon: React.ReactNode; component: React.ComponentType; - props?: any; + options?: Option; +} + +interface Option { + [x: string]: OptionValues; +} + +interface OptionValues { + name: string; + value: boolean; }