feat: add board (#15)

* wip: Add gridstack board
* wip: Centralize board pages, Add board settings page
* fix: remove cyclic dependency and rename widget-sort to kind
* improve: Add header actions as parallel route
* feat: add item select modal, add category edit modal,
* feat: add edit item modal
* feat: add remove item modal
* wip: add category actions
* feat: add saving of board, wip: add app widget
* Merge branch 'main' into add-board
* chore: update turbo dependencies
* chore: update mantine dependencies
* chore: fix typescript errors, lint and format
* feat: add confirm modal to category removal, move items of removed category to above wrapper
* feat: remove app widget to continue in another branch
* feat: add loading spinner until board is initialized
* fix: issue with cellheight of gridstack items
* feat: add translations for board
* fix: issue with translation for settings page
* chore: address pull request feedback
This commit is contained in:
Meier Lukas
2024-02-03 22:26:12 +01:00
committed by GitHub
parent cfd1c14034
commit 9d520874f4
88 changed files with 3431 additions and 262 deletions

View File

@@ -1,10 +1,10 @@
import type { WidgetKind } from "@homarr/definitions";
import { useScopedI18n } from "@homarr/translation/client";
import type { WidgetSort } from "..";
import type { WidgetOptionOfType, WidgetOptionType } from "../options";
export interface CommonWidgetInputProps<TKey extends WidgetOptionType> {
sort: WidgetSort;
kind: WidgetKind;
property: string;
options: Omit<WidgetOptionOfType<TKey>, "defaultValue" | "type">;
}
@@ -15,8 +15,8 @@ type UseWidgetInputTranslationReturnType = (
/**
* Short description why as and unknown convertions are used below:
* Typescript was not smart enought to work with the generic of the WidgetSort to only allow properties that are relying within that specified sort.
* This does not mean, that the function useWidgetInputTranslation can be called with invalid arguments without type errors and rather means that the above widget.<sort>.option.<property> string
* Typescript was not smart enought to work with the generic of the WidgetKind to only allow properties that are relying within that specified kind.
* This does not mean, that the function useWidgetInputTranslation can be called with invalid arguments without type errors and rather means that the above widget.<kind>.option.<property> string
* is not recognized as valid argument for the scoped i18n hook. Because the typesafety should remain outside the usage of those methods I (Meierschlumpf) decided to provide this fully typesafe useWidgetInputTranslation method.
*
* Some notes about it:
@@ -24,10 +24,10 @@ type UseWidgetInputTranslationReturnType = (
* is defined for the option. The method does sadly not reconize issues with those definitions. So it does not yell at you when you somewhere show the label without having it defined in the translations.
*/
export const useWidgetInputTranslation = (
sort: WidgetSort,
kind: WidgetKind,
property: string,
): UseWidgetInputTranslationReturnType => {
return useScopedI18n(
`widget.${sort}.option.${property}` as never, // Because the type is complex and not recognized by typescript, we need to cast it to never to make it work.
`widget.${kind}.option.${property}` as never, // Because the type is complex and not recognized by typescript, we need to cast it to never to make it work.
) as unknown as UseWidgetInputTranslationReturnType;
};

View File

@@ -8,10 +8,10 @@ import { useFormContext } from "./form";
export const WidgetMultiSelectInput = ({
property,
sort,
kind,
options,
}: CommonWidgetInputProps<"multiSelect">) => {
const t = useWidgetInputTranslation(sort, property);
const t = useWidgetInputTranslation(kind, property);
const form = useFormContext();
return (

View File

@@ -8,10 +8,10 @@ import { useFormContext } from "./form";
export const WidgetNumberInput = ({
property,
sort,
kind,
options,
}: CommonWidgetInputProps<"number">) => {
const t = useWidgetInputTranslation(sort, property);
const t = useWidgetInputTranslation(kind, property);
const form = useFormContext();
return (

View File

@@ -8,10 +8,10 @@ import { useFormContext } from "./form";
export const WidgetSelectInput = ({
property,
sort,
kind,
options,
}: CommonWidgetInputProps<"select">) => {
const t = useWidgetInputTranslation(sort, property);
const t = useWidgetInputTranslation(kind, property);
const form = useFormContext();
return (

View File

@@ -8,10 +8,10 @@ import { useFormContext } from "./form";
export const WidgetSliderInput = ({
property,
sort,
kind,
options,
}: CommonWidgetInputProps<"slider">) => {
const t = useWidgetInputTranslation(sort, property);
const t = useWidgetInputTranslation(kind, property);
const form = useFormContext();
return (

View File

@@ -8,10 +8,10 @@ import { useFormContext } from "./form";
export const WidgetSwitchInput = ({
property,
sort,
kind,
options,
}: CommonWidgetInputProps<"switch">) => {
const t = useWidgetInputTranslation(sort, property);
const t = useWidgetInputTranslation(kind, property);
const form = useFormContext();
return (

View File

@@ -8,10 +8,10 @@ import { useFormContext } from "./form";
export const WidgetTextInput = ({
property,
sort: widgetSort,
kind,
options,
}: CommonWidgetInputProps<"text">) => {
const t = useWidgetInputTranslation(widgetSort, property);
const t = useWidgetInputTranslation(kind, property);
const form = useFormContext();
return (

View File

@@ -1,9 +1,9 @@
import type { LoaderComponent } from "next/dynamic";
import type { IntegrationKind } from "@homarr/definitions";
import type { IntegrationKind, WidgetKind } from "@homarr/definitions";
import type { TablerIconsProps } from "@homarr/ui";
import type { WidgetImports, WidgetSort } from ".";
import type { WidgetImports } from ".";
import type {
inferOptionsFromDefinition,
WidgetOptionsRecord,
@@ -11,37 +11,37 @@ import type {
import type { IntegrationSelectOption } from "./widget-integration-select";
export const createWidgetDefinition = <
TSort extends WidgetSort,
TDefinition extends Definition,
TKind extends WidgetKind,
TDefinition extends WidgetDefinition,
>(
sort: TSort,
kind: TKind,
definition: TDefinition,
) => ({
withDynamicImport: (
componentLoader: () => LoaderComponent<WidgetComponentProps<TSort>>,
componentLoader: () => LoaderComponent<WidgetComponentProps<TKind>>,
) => ({
definition: {
sort,
kind,
...definition,
},
componentLoader,
}),
});
interface Definition {
export interface WidgetDefinition {
icon: (props: TablerIconsProps) => JSX.Element;
supportedIntegrations?: IntegrationKind[];
options: WidgetOptionsRecord;
}
export interface WidgetComponentProps<TSort extends WidgetSort> {
options: inferOptionsFromDefinition<WidgetOptionsRecordOf<TSort>>;
export interface WidgetComponentProps<TKind extends WidgetKind> {
options: inferOptionsFromDefinition<WidgetOptionsRecordOf<TKind>>;
integrations: inferIntegrationsFromDefinition<
WidgetImports[TSort]["definition"]
WidgetImports[TKind]["definition"]
>;
}
type inferIntegrationsFromDefinition<TDefinition extends Definition> =
type inferIntegrationsFromDefinition<TDefinition extends WidgetDefinition> =
TDefinition extends {
supportedIntegrations: infer TSupportedIntegrations;
} // check if definition has supportedIntegrations
@@ -57,5 +57,5 @@ interface IntegrationSelectOptionFor<TIntegration extends IntegrationKind> {
kind: TIntegration[number];
}
export type WidgetOptionsRecordOf<TSort extends WidgetSort> =
WidgetImports[TSort]["definition"]["options"];
export type WidgetOptionsRecordOf<TKind extends WidgetKind> =
WidgetImports[TKind]["definition"]["options"];

View File

@@ -1,5 +1,5 @@
import type { WidgetSort } from ".";
import type { WidgetKind } from "@homarr/definitions";
export type WidgetImportRecord = {
[K in WidgetSort]: unknown;
[K in WidgetKind]: unknown;
};

View File

@@ -1,6 +1,8 @@
import type { ComponentType } from "react";
import dynamic from "next/dynamic";
import type { Loader } from "next/dynamic";
import type { WidgetKind } from "@homarr/definitions";
import { Loader as UiLoader } from "@homarr/ui";
import * as clock from "./clock";
@@ -12,21 +14,30 @@ export { reduceWidgetOptionsWithDefaultValues } from "./options";
export { WidgetEditModal } from "./modals/widget-edit-modal";
export const widgetSorts = ["clock", "weather"] as const;
export const widgetImports = {
clock,
weather,
} satisfies WidgetImportRecord;
export type WidgetSort = (typeof widgetSorts)[number];
export type WidgetImports = typeof widgetImports;
export type WidgetImportKey = keyof WidgetImports;
export const loadWidgetDynamic = <TSort extends WidgetSort>(sort: TSort) =>
dynamic<WidgetComponentProps<TSort>>(
widgetImports[sort].componentLoader as Loader<WidgetComponentProps<TSort>>,
const loadedComponents = new Map<
WidgetKind,
ComponentType<WidgetComponentProps<WidgetKind>>
>();
export const loadWidgetDynamic = <TKind extends WidgetKind>(kind: TKind) => {
const existingComponent = loadedComponents.get(kind);
if (existingComponent) return existingComponent;
const newlyLoadedComponent = dynamic<WidgetComponentProps<TKind>>(
widgetImports[kind].componentLoader as Loader<WidgetComponentProps<TKind>>,
{
loading: () => <UiLoader />,
},
);
loadedComponents.set(kind, newlyLoadedComponent as never);
return newlyLoadedComponent;
};

View File

@@ -1,46 +1,46 @@
"use client";
import type { Dispatch, SetStateAction } from "react";
import type { ManagedModal } from "mantine-modal-manager";
import { useScopedI18n } from "@homarr/translation/client";
import type { WidgetKind } from "@homarr/definitions";
import { useI18n } from "@homarr/translation/client";
import { Button, Group, Stack } from "@homarr/ui";
import type { WidgetSort } from "..";
import { widgetImports } from "..";
import { getInputForType } from "../_inputs";
import { FormProvider, useForm } from "../_inputs/form";
import type { WidgetOptionsRecordOf } from "../definition";
import type { WidgetOptionDefinition } from "../options";
import { WidgetIntegrationSelect } from "../widget-integration-select";
import type { IntegrationSelectOption } from "../widget-integration-select";
import { WidgetIntegrationSelect } from "../widget-integration-select";
export interface WidgetEditModalState {
options: Record<string, unknown>;
integrations: string[];
}
interface ModalProps<TSort extends WidgetSort> {
sort: TSort;
state: [WidgetEditModalState, Dispatch<SetStateAction<WidgetEditModalState>>];
definition: WidgetOptionsRecordOf<TSort>;
interface ModalProps<TSort extends WidgetKind> {
kind: TSort;
value: WidgetEditModalState;
onSuccessfulEdit: (value: WidgetEditModalState) => void;
integrationData: IntegrationSelectOption[];
integrationSupport: boolean;
}
export const WidgetEditModal: ManagedModal<ModalProps<WidgetSort>> = ({
export const WidgetEditModal: ManagedModal<ModalProps<WidgetKind>> = ({
actions,
innerProps,
}) => {
const t = useScopedI18n("widget.editModal");
const [value, setValue] = innerProps.state;
const t = useI18n();
const form = useForm({
initialValues: value,
initialValues: innerProps.value,
});
const { definition } = widgetImports[innerProps.kind];
return (
<form
onSubmit={form.onSubmit((v) => {
setValue(v);
innerProps.onSuccessfulEdit(v);
actions.closeModal();
})}
>
@@ -48,12 +48,12 @@ export const WidgetEditModal: ManagedModal<ModalProps<WidgetSort>> = ({
<Stack>
{innerProps.integrationSupport && (
<WidgetIntegrationSelect
label={t("integrations.label")}
label={t("item.edit.field.integrations.label")}
data={innerProps.integrationData}
{...form.getInputProps("integrations")}
/>
)}
{Object.entries(innerProps.definition).map(
{Object.entries(definition.options).map(
([key, value]: [string, WidgetOptionDefinition]) => {
const Input = getInputForType(value.type);
@@ -64,7 +64,7 @@ export const WidgetEditModal: ManagedModal<ModalProps<WidgetSort>> = ({
return (
<Input
key={key}
sort={innerProps.sort}
kind={innerProps.kind}
property={key}
options={value as never}
/>
@@ -73,10 +73,10 @@ export const WidgetEditModal: ManagedModal<ModalProps<WidgetSort>> = ({
)}
<Group justify="right">
<Button onClick={actions.closeModal} variant="subtle" color="gray">
Close
{t("common.action.cancel")}
</Button>
<Button type="submit" color="teal">
Save
{t("common.action.saveChanges")}
</Button>
</Group>
</Stack>

View File

@@ -1,6 +1,9 @@
import { objectEntries } from "@homarr/common";
import type { WidgetKind } from "@homarr/definitions";
import type { z } from "@homarr/validation";
import { widgetImports } from ".";
interface CommonInput<TType> {
defaultValue?: TType;
withDescription?: boolean;
@@ -143,13 +146,16 @@ export const opt = {
};
export const reduceWidgetOptionsWithDefaultValues = (
optionsDefinition: Record<string, WidgetOptionDefinition>,
kind: WidgetKind,
currentValue: Record<string, unknown> = {},
) =>
objectEntries(optionsDefinition).reduce(
) => {
const definition = widgetImports[kind].definition;
const options = definition.options as Record<string, WidgetOptionDefinition>;
return objectEntries(options).reduce(
(prev, [key, value]) => ({
...prev,
[key]: currentValue[key] ?? value.defaultValue,
}),
{} as Record<string, unknown>,
);
};