🎨 Refactor repeating code

This commit is contained in:
Tagaishi
2023-07-29 01:23:46 +02:00
parent a79e110aa3
commit a0052f88f4
2 changed files with 36 additions and 47 deletions

View File

@@ -137,10 +137,10 @@ const WidgetOptionTypeSwitch: FC<{
}> = ({ option, widgetId, propName: key, value, handleChange }) => { }> = ({ option, widgetId, propName: key, value, handleChange }) => {
const { t } = useTranslation([`modules/${widgetId}`, 'common']); const { t } = useTranslation([`modules/${widgetId}`, 'common']);
const { colorScheme } = useMantineTheme(); const { colorScheme } = useMantineTheme();
const info = option.info ?? false;
switch (option.type) { switch (option.type) {
case 'switch': case 'switch':
var info = option.info !== undefined ? option.info : false;
return ( return (
<Group align="center" spacing="sm"> <Group align="center" spacing="sm">
<Switch <Switch
@@ -149,37 +149,36 @@ const WidgetOptionTypeSwitch: FC<{
onChange={(ev) => handleChange(key, ev.currentTarget.checked)} onChange={(ev) => handleChange(key, ev.currentTarget.checked)}
{...option.inputProps} {...option.inputProps}
/> />
{info? {info &&
<Tooltip.Floating <Tooltip.Floating
label={t(`descriptor.settings.${key}.info`)} label={t(`descriptor.settings.${key}.info`)}
position="right-start" position="right-start"
c={ colorScheme === 'light' ? "black" : "dark.0" } c={ colorScheme === 'light' ? "black" : "dark.0" }
color={ colorScheme === 'light' ? "dark.0" : "dark.6" } color={ colorScheme === 'light' ? "dark.0" : "dark.6" }
styles={{ tooltip: { '&': { maxWidth: 300, }, }, }} styles={{ tooltip: { maxWidth: 300, }, }}
multiline multiline
> >
<IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} /> <IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} />
</Tooltip.Floating> : undefined </Tooltip.Floating>
} }
</Group> </Group>
); );
case 'text': case 'text':
var info = option.info !== undefined ? option.info : false;
return ( return (
<Stack spacing={0}> <Stack spacing={0}>
<Group align="center" spacing="sm"> <Group align="center" spacing="sm">
<Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text> <Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text>
{info? {info &&
<Tooltip.Floating <Tooltip.Floating
label={t(`descriptor.settings.${key}.info`)} label={t(`descriptor.settings.${key}.info`)}
position="right-start" position="right-start"
c={ colorScheme === 'light' ? "black" : "dark.0" } c={ colorScheme === 'light' ? "black" : "dark.0" }
color={ colorScheme === 'light' ? "dark.0" : "dark.6" } color={ colorScheme === 'light' ? "dark.0" : "dark.6" }
styles={{ tooltip: { '&': { maxWidth: 300, }, }, }} styles={{ tooltip: { maxWidth: 300, }, }}
multiline multiline
> >
<IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} /> <IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} />
</Tooltip.Floating> : undefined </Tooltip.Floating>
} }
</Group> </Group>
<TextInput <TextInput
@@ -190,22 +189,21 @@ const WidgetOptionTypeSwitch: FC<{
</Stack> </Stack>
); );
case 'multi-select': case 'multi-select':
var info = option.info !== undefined ? option.info : false;
return ( return (
<Stack spacing={0}> <Stack spacing={0}>
<Group align="center" spacing="sm"> <Group align="center" spacing="sm">
<Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text> <Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text>
{info? {info &&
<Tooltip.Floating <Tooltip.Floating
label={t(`descriptor.settings.${key}.info`)} label={t(`descriptor.settings.${key}.info`)}
position="right-start" position="right-start"
c={ colorScheme === 'light' ? "black" : "dark.0" } c={ colorScheme === 'light' ? "black" : "dark.0" }
color={ colorScheme === 'light' ? "dark.0" : "dark.6" } color={ colorScheme === 'light' ? "dark.0" : "dark.6" }
styles={{ tooltip: { '&': { maxWidth: 300, }, }, }} styles={{ tooltip: { maxWidth: 300, }, }}
multiline multiline
> >
<IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} /> <IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} />
</Tooltip.Floating> : undefined </Tooltip.Floating>
} }
</Group> </Group>
<MultiSelect <MultiSelect
@@ -218,22 +216,21 @@ const WidgetOptionTypeSwitch: FC<{
</Stack> </Stack>
); );
case 'select': case 'select':
var info = option.info !== undefined ? option.info : false;
return ( return (
<Stack spacing={0}> <Stack spacing={0}>
<Group align="center" spacing="sm"> <Group align="center" spacing="sm">
<Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text> <Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text>
{info? {info &&
<Tooltip.Floating <Tooltip.Floating
label={t(`descriptor.settings.${key}.info`)} label={t(`descriptor.settings.${key}.info`)}
position="right-start" position="right-start"
c={ colorScheme === 'light' ? "black" : "dark.0" } c={ colorScheme === 'light' ? "black" : "dark.0" }
color={ colorScheme === 'light' ? "dark.0" : "dark.6" } color={ colorScheme === 'light' ? "dark.0" : "dark.6" }
styles={{ tooltip: { '&': { maxWidth: 300, }, }, }} styles={{ tooltip: { maxWidth: 300, }, }}
multiline multiline
> >
<IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} /> <IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} />
</Tooltip.Floating> : undefined </Tooltip.Floating>
} }
</Group> </Group>
<Select <Select
@@ -246,22 +243,21 @@ const WidgetOptionTypeSwitch: FC<{
</Stack> </Stack>
); );
case 'number': case 'number':
var info = option.info !== undefined ? option.info : false;
return ( return (
<Stack spacing={0}> <Stack spacing={0}>
<Group align="center" spacing="sm"> <Group align="center" spacing="sm">
<Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text> <Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text>
{info? {info &&
<Tooltip.Floating <Tooltip.Floating
label={t(`descriptor.settings.${key}.info`)} label={t(`descriptor.settings.${key}.info`)}
position="right-start" position="right-start"
c={ colorScheme === 'light' ? "black" : "dark.0" } c={ colorScheme === 'light' ? "black" : "dark.0" }
color={ colorScheme === 'light' ? "dark.0" : "dark.6" } color={ colorScheme === 'light' ? "dark.0" : "dark.6" }
styles={{ tooltip: { '&': { maxWidth: 300, }, }, }} styles={{ tooltip: { maxWidth: 300, }, }}
multiline multiline
> >
<IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} /> <IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} />
</Tooltip.Floating> : undefined </Tooltip.Floating>
} }
</Group> </Group>
<NumberInput <NumberInput
@@ -272,22 +268,21 @@ const WidgetOptionTypeSwitch: FC<{
</Stack> </Stack>
); );
case 'slider': case 'slider':
var info = option.info !== undefined ? option.info : false;
return ( return (
<Stack spacing={0}> <Stack spacing={0}>
<Group align="center" spacing="sm"> <Group align="center" spacing="sm">
<Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text> <Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text>
{info? {info &&
<Tooltip.Floating <Tooltip.Floating
label={t(`descriptor.settings.${key}.info`)} label={t(`descriptor.settings.${key}.info`)}
position="right-start" position="right-start"
c={ colorScheme === 'light' ? "black" : "dark.0" } c={ colorScheme === 'light' ? "black" : "dark.0" }
color={ colorScheme === 'light' ? "dark.0" : "dark.6" } color={ colorScheme === 'light' ? "dark.0" : "dark.6" }
styles={{ tooltip: { '&': { maxWidth: 300, }, }, }} styles={{ tooltip: { maxWidth: 300, }, }}
multiline multiline
> >
<IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} /> <IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} />
</Tooltip.Floating> : undefined </Tooltip.Floating>
} }
</Group> </Group>
<Slider <Slider
@@ -312,7 +307,6 @@ const WidgetOptionTypeSwitch: FC<{
); );
case 'draggable-list': case 'draggable-list':
var info = option.info !== undefined ? option.info : false;
/* eslint-disable no-case-declarations */ /* eslint-disable no-case-declarations */
const typedVal = value as IDraggableListInputValue['defaultValue']; const typedVal = value as IDraggableListInputValue['defaultValue'];
@@ -339,17 +333,17 @@ const WidgetOptionTypeSwitch: FC<{
<Stack spacing="xs"> <Stack spacing="xs">
<Group align="center" spacing="sm"> <Group align="center" spacing="sm">
<Text>{t(`descriptor.settings.${key}.label`)}</Text> <Text>{t(`descriptor.settings.${key}.label`)}</Text>
{info? {info &&
<Tooltip.Floating <Tooltip.Floating
label={t(`descriptor.settings.${key}.info`)} label={t(`descriptor.settings.${key}.info`)}
position="right-start" position="right-start"
c={ colorScheme === 'light' ? "black" : "dark.0" } c={ colorScheme === 'light' ? "black" : "dark.0" }
color={ colorScheme === 'light' ? "dark.0" : "dark.6" } color={ colorScheme === 'light' ? "dark.0" : "dark.6" }
styles={{ tooltip: { '&': { maxWidth: 300, }, }, }} styles={{ tooltip: { maxWidth: 300, }, }}
multiline multiline
> >
<IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} /> <IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} />
</Tooltip.Floating> : undefined </Tooltip.Floating>
} }
</Group> </Group>
<StaticDraggableList <StaticDraggableList
@@ -375,22 +369,21 @@ const WidgetOptionTypeSwitch: FC<{
</Stack> </Stack>
); );
case 'multiple-text': case 'multiple-text':
var info = option.info !== undefined ? option.info : false;
return ( return (
<Stack spacing={0}> <Stack spacing={0}>
<Group align="center" spacing="sm"> <Group align="center" spacing="sm">
<Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text> <Text size="0.875rem" weight="500">{t(`descriptor.settings.${key}.label`)}</Text>
{info? {info &&
<Tooltip.Floating <Tooltip.Floating
label={t(`descriptor.settings.${key}.info`)} label={t(`descriptor.settings.${key}.info`)}
position="right-start" position="right-start"
c={ colorScheme === 'light' ? "black" : "dark.0" } c={ colorScheme === 'light' ? "black" : "dark.0" }
color={ colorScheme === 'light' ? "dark.0" : "dark.6" } color={ colorScheme === 'light' ? "dark.0" : "dark.6" }
styles={{ tooltip: { '&': { maxWidth: 300, }, }, }} styles={{ tooltip: { maxWidth: 300, }, }}
multiline multiline
> >
<IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} /> <IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} />
</Tooltip.Floating> : undefined </Tooltip.Floating>
} }
</Group> </Group>
<MultiSelect <MultiSelect
@@ -411,23 +404,22 @@ const WidgetOptionTypeSwitch: FC<{
</Stack> </Stack>
); );
case 'draggable-editable-list': case 'draggable-editable-list':
var info = option.info !== undefined ? option.info : false;
const { t: translateDraggableList } = useTranslation('widgets/draggable-list'); const { t: translateDraggableList } = useTranslation('widgets/draggable-list');
return ( return (
<Stack spacing="xs"> <Stack spacing="xs">
<Group align="center" spacing="sm"> <Group align="center" spacing="sm">
<Text>{t(`descriptor.settings.${key}.label`)}</Text> <Text>{t(`descriptor.settings.${key}.label`)}</Text>
{info? {info &&
<Tooltip.Floating <Tooltip.Floating
label={t(`descriptor.settings.${key}.info`)} label={t(`descriptor.settings.${key}.info`)}
position="right-start" position="right-start"
c={ colorScheme === 'light' ? "black" : "dark.0" } c={ colorScheme === 'light' ? "black" : "dark.0" }
color={ colorScheme === 'light' ? "dark.0" : "dark.6" } color={ colorScheme === 'light' ? "dark.0" : "dark.6" }
styles={{ tooltip: { '&': { maxWidth: 300, }, }, }} styles={{ tooltip: { maxWidth: 300, }, }}
multiline multiline
> >
<IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} /> <IconAlertCircle size="1.25rem" style={{ display: 'block', opacity: 0.5 }} />
</Tooltip.Floating> : undefined </Tooltip.Floating>
} }
</Group> </Group>
<DraggableList <DraggableList

View File

@@ -11,6 +11,7 @@ import React from 'react';
import { AreaType } from '../types/area'; import { AreaType } from '../types/area';
import { ShapeType } from '../types/shape'; import { ShapeType } from '../types/shape';
import { boolean } from 'zod';
// Type of widgets which are saved to config // Type of widgets which are saved to config
export type IWidget<TKey extends string, TDefinition extends IWidgetDefinition> = { export type IWidget<TKey extends string, TDefinition extends IWidgetDefinition> = {
@@ -31,7 +32,7 @@ export type IWidget<TKey extends string, TDefinition extends IWidgetDefinition>
type MakeLessSpecific<T> = T extends boolean ? boolean : T; type MakeLessSpecific<T> = T extends boolean ? boolean : T;
// Types of options that can be specified for the widget edit modal // Types of options that can be specified for the widget edit modal
export type IWidgetOptionValue = export type IWidgetOptionValue = (
| IMultiSelectOptionValue | IMultiSelectOptionValue
| ISwitchOptionValue | ISwitchOptionValue
| ITextInputOptionValue | ITextInputOptionValue
@@ -41,7 +42,7 @@ export type IWidgetOptionValue =
| IDraggableListInputValue | IDraggableListInputValue
| IDraggableEditableListInputValue<any> | IDraggableEditableListInputValue<any>
| IMultipleTextInputOptionValue | IMultipleTextInputOptionValue
| ILocationOptionValue; | ILocationOptionValue) & CommonOptions;
// Interface for data type // Interface for data type
interface DataType { interface DataType {
@@ -49,11 +50,14 @@ interface DataType {
value: string; value: string;
} }
interface CommonOptions {
info?: boolean;
};
// will show a multi-select with specified data // will show a multi-select with specified data
export type IMultiSelectOptionValue = { export type IMultiSelectOptionValue = {
type: 'multi-select'; type: 'multi-select';
defaultValue: string[]; defaultValue: string[];
info?: boolean;
data: DataType[]; data: DataType[];
inputProps?: Partial<MultiSelectProps>; inputProps?: Partial<MultiSelectProps>;
}; };
@@ -62,7 +66,6 @@ export type IMultiSelectOptionValue = {
export type ISelectOptionValue = { export type ISelectOptionValue = {
type: 'select'; type: 'select';
defaultValue: string; defaultValue: string;
info?: boolean;
data: DataType[]; data: DataType[];
inputProps?: Partial<SelectProps>; inputProps?: Partial<SelectProps>;
}; };
@@ -71,7 +74,6 @@ export type ISelectOptionValue = {
export type ISwitchOptionValue = { export type ISwitchOptionValue = {
type: 'switch'; type: 'switch';
defaultValue: boolean; defaultValue: boolean;
info?: boolean;
inputProps?: Partial<SwitchProps>; inputProps?: Partial<SwitchProps>;
}; };
@@ -79,7 +81,6 @@ export type ISwitchOptionValue = {
export type ITextInputOptionValue = { export type ITextInputOptionValue = {
type: 'text'; type: 'text';
defaultValue: string; defaultValue: string;
info?: boolean;
inputProps?: Partial<TextInputProps>; inputProps?: Partial<TextInputProps>;
}; };
@@ -87,7 +88,6 @@ export type ITextInputOptionValue = {
export type INumberInputOptionValue = { export type INumberInputOptionValue = {
type: 'number'; type: 'number';
defaultValue: number; defaultValue: number;
info?: boolean;
inputProps?: Partial<NumberInputProps>; inputProps?: Partial<NumberInputProps>;
}; };
@@ -95,13 +95,13 @@ export type INumberInputOptionValue = {
export type ISliderInputOptionValue = { export type ISliderInputOptionValue = {
type: 'slider'; type: 'slider';
defaultValue: number; defaultValue: number;
info?: boolean;
min: number; min: number;
max: number; max: number;
step: number; step: number;
inputProps?: Partial<SliderProps>; inputProps?: Partial<SliderProps>;
}; };
// will show a custom location selector
type ILocationOptionValue = { type ILocationOptionValue = {
type: 'location'; type: 'location';
defaultValue: { latitude: number; longitude: number }; defaultValue: { latitude: number; longitude: number };
@@ -114,7 +114,6 @@ export type IDraggableListInputValue = {
key: string; key: string;
subValues?: Record<string, any>; subValues?: Record<string, any>;
}[]; }[];
info?: boolean;
items: Record< items: Record<
string, string,
Record<string, Omit<Exclude<IWidgetOptionValue, IDraggableListInputValue>, 'defaultValue'>> Record<string, Omit<Exclude<IWidgetOptionValue, IDraggableListInputValue>, 'defaultValue'>>
@@ -124,7 +123,6 @@ export type IDraggableListInputValue = {
export type IDraggableEditableListInputValue<TData extends { id: string }> = { export type IDraggableEditableListInputValue<TData extends { id: string }> = {
type: 'draggable-editable-list'; type: 'draggable-editable-list';
defaultValue: TData[]; defaultValue: TData[];
info?: boolean;
create: () => TData; create: () => TData;
getLabel: (data: TData) => string | JSX.Element; getLabel: (data: TData) => string | JSX.Element;
itemComponent: (props: { itemComponent: (props: {
@@ -138,7 +136,6 @@ export type IDraggableEditableListInputValue<TData extends { id: string }> = {
export type IMultipleTextInputOptionValue = { export type IMultipleTextInputOptionValue = {
type: 'multiple-text'; type: 'multiple-text';
defaultValue: string[]; defaultValue: string[];
info?: boolean;
inputProps?: Partial<TextInputProps>; inputProps?: Partial<TextInputProps>;
}; };