From b4290e0b0eea055b680f9b8eb75e779a1790e1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Schm=C3=B6cker?= Date: Wed, 21 Aug 2024 18:30:12 +0200 Subject: [PATCH] feat: add list type with basic UI --- controller/thymis_controller/models/module.py | 8 +++- controller/thymis_controller/models/state.py | 4 +- .../thymis_controller/modules/thymis.py | 19 ++++++++ frontend/src/lib/config/ConfigDrawer.svelte | 9 +++- frontend/src/lib/config/ConfigList.svelte | 43 +++++++++++++++++++ frontend/src/lib/config/ModuleCard.svelte | 11 +++-- frontend/src/lib/searchParamHelpers.ts | 6 --- frontend/src/lib/state.ts | 12 +++++- frontend/src/locales/de.json | 1 + frontend/src/locales/en.json | 1 + 10 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 frontend/src/lib/config/ConfigList.svelte diff --git a/controller/thymis_controller/models/module.py b/controller/thymis_controller/models/module.py index 79c5c832..f98843d7 100644 --- a/controller/thymis_controller/models/module.py +++ b/controller/thymis_controller/models/module.py @@ -9,7 +9,11 @@ class SelectOneType(BaseModel): select_one: List[str] = Field(serialization_alias="select-one") -Types = Union[ValueTypes, SelectOneType] +class ListType(BaseModel): + settings: dict[str, "Setting"] = Field(serialization_alias="list-of") + + +Types = Union[ValueTypes, SelectOneType, ListType] class Setting(BaseModel): @@ -28,4 +32,4 @@ class Module(BaseModel): settings: dict[str, Setting] -__all__ = ["Setting", "Module", "SelectOneType"] +__all__ = ["Setting", "Module", "SelectOneType", "ListType"] diff --git a/controller/thymis_controller/models/state.py b/controller/thymis_controller/models/state.py index 8e1ad92b..dd76af7b 100644 --- a/controller/thymis_controller/models/state.py +++ b/controller/thymis_controller/models/state.py @@ -1,6 +1,6 @@ from typing import Dict, List, Optional -from pydantic import BaseModel +from pydantic import BaseModel, JsonValue from thymis_controller import migration @@ -12,7 +12,7 @@ class Repo(BaseModel): class ModuleSettings(BaseModel): type: str # type of module this settings object is for - settings: Dict[str, str | int | float | bool] + settings: JsonValue class Device(BaseModel): diff --git a/controller/thymis_controller/modules/thymis.py b/controller/thymis_controller/modules/thymis.py index 5a499f26..d455f252 100644 --- a/controller/thymis_controller/modules/thymis.py +++ b/controller/thymis_controller/modules/thymis.py @@ -39,6 +39,25 @@ class ThymisDevice(modules.Module): order=10, ) + dev_1 = models.Setting( + name="thymis.config.dev-1", + type=models.ListType( + settings={ + "ssh-key": models.Setting( + name="name", + type="string", + default="", + description="", + example="", + ), + } + ), + default=[], + description="", + example='[""]', + order=15, + ) + device_name = models.Setting( name="thymis.config.device-name", type="string", diff --git a/frontend/src/lib/config/ConfigDrawer.svelte b/frontend/src/lib/config/ConfigDrawer.svelte index 81b31306..f0c17cc8 100644 --- a/frontend/src/lib/config/ConfigDrawer.svelte +++ b/frontend/src/lib/config/ConfigDrawer.svelte @@ -1,9 +1,10 @@ {#if settingIsBool(setting)} @@ -49,4 +54,6 @@ {:else if settingIsSelectOne(setting)} +{:else if settingIsListOf(setting)} + {/if} diff --git a/frontend/src/lib/config/ConfigList.svelte b/frontend/src/lib/config/ConfigList.svelte new file mode 100644 index 00000000..4594590a --- /dev/null +++ b/frontend/src/lib/config/ConfigList.svelte @@ -0,0 +1,43 @@ + + +
+
+ {#each values as item} + {#each Object.entries(settings.type['list-of']) as [key, setting]} + { + const newValues = [...values]; + newValues[values.indexOf(item)] = { ...item, [key]: value }; + onChange(newValues); + }} + /> + {/each} + {/each} +
+ + {$t('config.add_list_item')} +
+{#if disabled} + {$t('config.editDisabled')} +{/if} diff --git a/frontend/src/lib/config/ModuleCard.svelte b/frontend/src/lib/config/ModuleCard.svelte index 497ca16f..5e29af29 100644 --- a/frontend/src/lib/config/ModuleCard.svelte +++ b/frontend/src/lib/config/ModuleCard.svelte @@ -43,7 +43,7 @@

{$t(`options.nix.${setting.name}`, { default: setting.name })}

-
+
setSetting(module, key, '')} + on:click={() => + setSetting( + module, + key, + typeof setting.type === 'object' && setting.type.hasOwnProperty('list-of') ? [] : '' + )} > @@ -133,7 +138,7 @@ {/if} {/if}
-

{setting.description}

+

{setting.description}

{:else}
{$t('options.no-settings')}
{/each} diff --git a/frontend/src/lib/searchParamHelpers.ts b/frontend/src/lib/searchParamHelpers.ts index 8fadac6a..51da6a0a 100644 --- a/frontend/src/lib/searchParamHelpers.ts +++ b/frontend/src/lib/searchParamHelpers.ts @@ -27,12 +27,6 @@ export const buildConfigSelectModuleSearchParam = ( contextIdentifier: string | null | undefined, module: Module | null | undefined ) => { - console.log('search', search); - console.log('targetType', targetType); - console.log('target', target); - console.log('contextType', contextType); - console.log('contextIdentifier', contextIdentifier); - console.log('module', module); const params = new URLSearchParams(search); targetType ? params.set('global-nav-target-type', targetType) diff --git a/frontend/src/lib/state.ts b/frontend/src/lib/state.ts index b8020bac..c14f8e2d 100644 --- a/frontend/src/lib/state.ts +++ b/frontend/src/lib/state.ts @@ -30,7 +30,17 @@ export type SelectOneSettingType = { 'select-one': string[]; }; -export type SettingType = 'string' | 'number' | 'bool' | 'textarea' | SelectOneSettingType; +export type ListSettingType = { + 'list-of': Setting[]; +}; + +export type SettingType = + | 'string' + | 'number' + | 'bool' + | 'textarea' + | SelectOneSettingType + | ListSettingType; export type Setting = { name: string; diff --git a/frontend/src/locales/de.json b/frontend/src/locales/de.json index ed703af8..0b3ee4ae 100644 --- a/frontend/src/locales/de.json +++ b/frontend/src/locales/de.json @@ -145,6 +145,7 @@ "installed-elsewhere": "Auf anderen Geräten oder Tags installiert", "available": "Verfügbar" }, + "add_list_item": "Element Hinzufügen", "add_module": "Modul hinzufügen", "remove_module": "Modul entfernen", "edit_tag_modules": "Tag Module bearbeiten", diff --git a/frontend/src/locales/en.json b/frontend/src/locales/en.json index 66c18f16..909aa235 100644 --- a/frontend/src/locales/en.json +++ b/frontend/src/locales/en.json @@ -146,6 +146,7 @@ "installed-elsewhere": "Installed on other tags or devices", "available": "Available" }, + "add_list_item": "Add List Element", "add_module": "Add Module", "remove_module": "Remove Module", "edit_tag_modules": "Edit Tag Modules",