2023-02-14 13:41:59 +01:00
|
|
|
import {
|
|
|
|
|
MultiSelectProps,
|
|
|
|
|
NumberInputProps,
|
|
|
|
|
SelectProps,
|
|
|
|
|
SliderProps,
|
|
|
|
|
SwitchProps,
|
|
|
|
|
TextInputProps,
|
|
|
|
|
} from '@mantine/core';
|
2023-05-15 17:40:59 +09:00
|
|
|
import { Icon } from '@tabler/icons-react';
|
2023-07-21 18:08:40 +09:00
|
|
|
import React from 'react';
|
2023-04-05 14:16:28 +09:00
|
|
|
|
2023-01-07 20:18:53 +01:00
|
|
|
import { AreaType } from '../types/area';
|
|
|
|
|
import { ShapeType } from '../types/shape';
|
2023-07-29 01:23:46 +02:00
|
|
|
import { boolean } from 'zod';
|
2022-12-18 21:21:23 +01:00
|
|
|
|
2023-02-10 18:20:28 +01:00
|
|
|
// Type of widgets which are saved to config
|
2022-12-18 21:21:23 +01:00
|
|
|
export type IWidget<TKey extends string, TDefinition extends IWidgetDefinition> = {
|
2023-03-30 21:54:44 +02:00
|
|
|
id: string;
|
|
|
|
|
type: TKey;
|
2022-12-18 21:21:23 +01:00
|
|
|
properties: {
|
|
|
|
|
[key in keyof TDefinition['options']]: MakeLessSpecific<
|
|
|
|
|
TDefinition['options'][key]['defaultValue']
|
|
|
|
|
>;
|
|
|
|
|
};
|
|
|
|
|
area: AreaType;
|
|
|
|
|
shape: ShapeType;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Makes the type less specific
|
|
|
|
|
// For example when the type true is used as input the result is boolean
|
|
|
|
|
// By not using this type the definition would always be { property: true }
|
2023-02-10 18:20:28 +01:00
|
|
|
type MakeLessSpecific<T> = T extends boolean ? boolean : T;
|
2022-12-18 21:21:23 +01:00
|
|
|
|
|
|
|
|
// Types of options that can be specified for the widget edit modal
|
2023-07-29 01:23:46 +02:00
|
|
|
export type IWidgetOptionValue = (
|
2022-12-18 21:21:23 +01:00
|
|
|
| IMultiSelectOptionValue
|
|
|
|
|
| ISwitchOptionValue
|
|
|
|
|
| ITextInputOptionValue
|
2023-01-05 22:43:27 +09:00
|
|
|
| ISliderInputOptionValue
|
2023-01-11 08:43:28 +09:00
|
|
|
| ISelectOptionValue
|
2023-02-10 18:20:28 +01:00
|
|
|
| INumberInputOptionValue
|
2023-04-05 14:16:28 +09:00
|
|
|
| IDraggableListInputValue
|
2023-05-15 09:54:50 +02:00
|
|
|
| IDraggableEditableListInputValue<any>
|
2023-06-10 23:53:04 +02:00
|
|
|
| IMultipleTextInputOptionValue
|
2023-07-29 01:31:10 +02:00
|
|
|
| ILocationOptionValue
|
|
|
|
|
) & CommonOptions;
|
2022-12-18 21:21:23 +01:00
|
|
|
|
2023-01-11 08:43:28 +09:00
|
|
|
// Interface for data type
|
|
|
|
|
interface DataType {
|
2023-01-13 09:49:29 +00:00
|
|
|
label: string;
|
|
|
|
|
value: string;
|
2023-01-11 08:43:28 +09:00
|
|
|
}
|
|
|
|
|
|
2023-07-29 01:23:46 +02:00
|
|
|
interface CommonOptions {
|
|
|
|
|
info?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-18 21:21:23 +01:00
|
|
|
// will show a multi-select with specified data
|
|
|
|
|
export type IMultiSelectOptionValue = {
|
|
|
|
|
type: 'multi-select';
|
|
|
|
|
defaultValue: string[];
|
2023-01-11 08:43:28 +09:00
|
|
|
data: DataType[];
|
2023-02-14 13:41:59 +01:00
|
|
|
inputProps?: Partial<MultiSelectProps>;
|
2023-01-11 08:43:28 +09:00
|
|
|
};
|
|
|
|
|
|
2023-02-14 13:41:59 +01:00
|
|
|
// will show a select with specified data
|
2023-01-11 08:43:28 +09:00
|
|
|
export type ISelectOptionValue = {
|
|
|
|
|
type: 'select';
|
|
|
|
|
defaultValue: string;
|
|
|
|
|
data: DataType[];
|
2023-02-14 13:41:59 +01:00
|
|
|
inputProps?: Partial<SelectProps>;
|
2022-12-18 21:21:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// will show a switch
|
|
|
|
|
export type ISwitchOptionValue = {
|
|
|
|
|
type: 'switch';
|
|
|
|
|
defaultValue: boolean;
|
2023-02-14 13:41:59 +01:00
|
|
|
inputProps?: Partial<SwitchProps>;
|
2022-12-18 21:21:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// will show a text-input
|
|
|
|
|
export type ITextInputOptionValue = {
|
|
|
|
|
type: 'text';
|
|
|
|
|
defaultValue: string;
|
2023-02-14 13:41:59 +01:00
|
|
|
inputProps?: Partial<TextInputProps>;
|
2022-12-18 21:21:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// will show a number-input
|
|
|
|
|
export type INumberInputOptionValue = {
|
|
|
|
|
type: 'number';
|
2023-01-05 22:43:27 +09:00
|
|
|
defaultValue: number;
|
2023-02-14 13:41:59 +01:00
|
|
|
inputProps?: Partial<NumberInputProps>;
|
2023-01-05 22:43:27 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// will show a slider-input
|
|
|
|
|
export type ISliderInputOptionValue = {
|
|
|
|
|
type: 'slider';
|
|
|
|
|
defaultValue: number;
|
|
|
|
|
min: number;
|
|
|
|
|
max: number;
|
|
|
|
|
step: number;
|
2023-02-14 13:41:59 +01:00
|
|
|
inputProps?: Partial<SliderProps>;
|
2022-12-18 21:21:23 +01:00
|
|
|
};
|
|
|
|
|
|
2023-07-29 01:23:46 +02:00
|
|
|
// will show a custom location selector
|
2023-06-10 23:53:04 +02:00
|
|
|
type ILocationOptionValue = {
|
|
|
|
|
type: 'location';
|
|
|
|
|
defaultValue: { latitude: number; longitude: number };
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-14 13:41:59 +01:00
|
|
|
// will show a sortable list that can have sub settings
|
2023-02-10 18:20:28 +01:00
|
|
|
export type IDraggableListInputValue = {
|
|
|
|
|
type: 'draggable-list';
|
|
|
|
|
defaultValue: {
|
|
|
|
|
key: string;
|
|
|
|
|
subValues?: Record<string, any>;
|
|
|
|
|
}[];
|
|
|
|
|
items: Record<
|
|
|
|
|
string,
|
|
|
|
|
Record<string, Omit<Exclude<IWidgetOptionValue, IDraggableListInputValue>, 'defaultValue'>>
|
|
|
|
|
>;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-15 09:54:50 +02:00
|
|
|
export type IDraggableEditableListInputValue<TData extends { id: string }> = {
|
|
|
|
|
type: 'draggable-editable-list';
|
|
|
|
|
defaultValue: TData[];
|
|
|
|
|
create: () => TData;
|
|
|
|
|
getLabel: (data: TData) => string | JSX.Element;
|
|
|
|
|
itemComponent: (props: {
|
|
|
|
|
data: TData;
|
|
|
|
|
onChange: (data: TData) => void;
|
|
|
|
|
delete: () => void;
|
|
|
|
|
}) => JSX.Element;
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-05 14:16:28 +09:00
|
|
|
// will show a text-input with a button to add a new line
|
|
|
|
|
export type IMultipleTextInputOptionValue = {
|
|
|
|
|
type: 'multiple-text';
|
|
|
|
|
defaultValue: string[];
|
|
|
|
|
inputProps?: Partial<TextInputProps>;
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-18 21:21:23 +01:00
|
|
|
// is used to type the widget definitions which will be used to display all widgets
|
|
|
|
|
export type IWidgetDefinition<TKey extends string = string> = {
|
|
|
|
|
id: TKey;
|
2023-05-15 17:40:59 +09:00
|
|
|
icon: Icon | string;
|
2022-12-18 21:21:23 +01:00
|
|
|
options: {
|
|
|
|
|
[key: string]: IWidgetOptionValue;
|
|
|
|
|
};
|
2022-12-18 21:50:08 +01:00
|
|
|
gridstack: {
|
|
|
|
|
minWidth: number;
|
|
|
|
|
minHeight: number;
|
|
|
|
|
maxWidth: number;
|
|
|
|
|
maxHeight: number;
|
|
|
|
|
};
|
2022-12-18 21:21:23 +01:00
|
|
|
component: React.ComponentType<any>;
|
|
|
|
|
};
|