* fix(deps): update dependency drizzle-zod to ^0.8.2 * chore: update zod to v4 import * fix: path is no longer available in transform context * fix: AnyZodObject does no longer exist * fix: auth env.ts using wrong createEnv and remove unused file env-validation.ts * fix: required_error no longer exists on z.string * fix: zod error map is deprecated and replaced with config * fix: default requires callback now * fix: migrate zod resolver for mantine * fix: remove unused form translation file * fix: wrong enum type * fix: record now requires two arguments * fix: add-confirm-password-refinement type issues * fix: add missing first record argument for entityStateSchema * fix: migrate superrefine to check * fix(deps): upgrade zod-form-data to v3 * fix: migrate superRefine to check for mediaUploadSchema * fix: authProvidersSchema default is array * fix: use stringbool instead of custom implementation * fix: record requires first argument * fix: migrate superRefine to check for certificate router * fix: confirm pasword refinement is overwriting types * fix: email optional not working * fix: migrate intersection to object converter * fix: safe parse return value rename * fix: easier access for min and max number value * fix: migrate superRefine to check for oldmarr import file * fix: inference of enum shape for old-import board-size wrong * fix: errors renamed to issues * chore: address pull request feedback * fix: zod form requires object * fix: inference for use-zod-form not working * fix: remove unnecessary convertion * fix(deps): upgrade trpc-to-openapi to v3 * fix: build error * fix: migrate missing zod imports to v4 * fix: migrate zod records to v4 * fix: missing core package dependency in api module * fix: unable to convert custom zod schema to openapi schema * fix(deps): upgrade zod to v4 * chore(renovate): enable zod dependency updates * test: add simple unit test for convertIntersectionToZodObject --------- Co-authored-by: homarr-renovate[bot] <158783068+homarr-renovate[bot]@users.noreply.github.com>
161 lines
5.3 KiB
TypeScript
161 lines
5.3 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { Button, Group, Stack } from "@mantine/core";
|
|
import { zod4Resolver } from "mantine-form-zod-resolver";
|
|
import { z } from "zod/v4";
|
|
|
|
import { objectEntries } from "@homarr/common";
|
|
import type { WidgetKind } from "@homarr/definitions";
|
|
import { createModal, useModalAction } from "@homarr/modals";
|
|
import type { SettingsContextProps } from "@homarr/settings/creator";
|
|
import { useI18n } from "@homarr/translation/client";
|
|
import { zodErrorMap } from "@homarr/validation/form/i18n";
|
|
|
|
import { widgetImports } from "..";
|
|
import { getInputForType } from "../_inputs";
|
|
import { FormProvider, useForm } from "../_inputs/form";
|
|
import type { BoardItemAdvancedOptions } from "../../../validation/src/shared";
|
|
import type { OptionsBuilderResult } from "../options";
|
|
import type { IntegrationSelectOption } from "../widget-integration-select";
|
|
import { WidgetIntegrationSelect } from "../widget-integration-select";
|
|
import { WidgetAdvancedOptionsModal } from "./widget-advanced-options-modal";
|
|
|
|
export interface WidgetEditModalState {
|
|
options: Record<string, unknown>;
|
|
integrationIds: string[];
|
|
advancedOptions: BoardItemAdvancedOptions;
|
|
}
|
|
|
|
interface ModalProps<TSort extends WidgetKind> {
|
|
kind: TSort;
|
|
value: WidgetEditModalState;
|
|
onSuccessfulEdit: (value: WidgetEditModalState) => void;
|
|
integrationData: IntegrationSelectOption[];
|
|
integrationSupport: boolean;
|
|
settings: SettingsContextProps;
|
|
}
|
|
|
|
export const WidgetEditModal = createModal<ModalProps<WidgetKind>>(({ actions, innerProps }) => {
|
|
const t = useI18n();
|
|
const [advancedOptions, setAdvancedOptions] = useState<BoardItemAdvancedOptions>(innerProps.value.advancedOptions);
|
|
|
|
// Translate the error messages
|
|
z.config({
|
|
customError: zodErrorMap(t),
|
|
});
|
|
const { definition } = widgetImports[innerProps.kind];
|
|
const options = definition.createOptions(innerProps.settings) as Record<string, OptionsBuilderResult[string]>;
|
|
|
|
const form = useForm({
|
|
mode: "controlled",
|
|
initialValues: innerProps.value,
|
|
validate: zod4Resolver(
|
|
z.object({
|
|
options: z.object(
|
|
objectEntries(options).reduce(
|
|
(acc, [key, value]: [string, { type: string; validate?: z.ZodType<unknown> }]) => {
|
|
if (value.validate) {
|
|
acc[key] = value.type === "multiText" ? z.array(value.validate).optional() : value.validate;
|
|
}
|
|
|
|
return acc;
|
|
},
|
|
{} as Record<string, z.ZodType<unknown>>,
|
|
),
|
|
),
|
|
integrationIds: z.array(z.string()),
|
|
advancedOptions: z.object({
|
|
customCssClasses: z.array(z.string()),
|
|
borderColor: z.string(),
|
|
}),
|
|
}),
|
|
),
|
|
validateInputOnBlur: true,
|
|
validateInputOnChange: true,
|
|
});
|
|
const { openModal } = useModalAction(WidgetAdvancedOptionsModal);
|
|
|
|
return (
|
|
<form
|
|
onSubmit={form.onSubmit((values) => {
|
|
innerProps.onSuccessfulEdit({
|
|
...values,
|
|
advancedOptions,
|
|
});
|
|
actions.closeModal();
|
|
})}
|
|
>
|
|
<FormProvider form={form}>
|
|
<Stack>
|
|
{innerProps.integrationSupport && (
|
|
<WidgetIntegrationSelect
|
|
label={t("item.edit.field.integrations.label")}
|
|
data={innerProps.integrationData}
|
|
{...form.getInputProps("integrationIds")}
|
|
/>
|
|
)}
|
|
{Object.entries(options).map(([key, value]) => {
|
|
const Input = getInputForType(value.type);
|
|
|
|
if (
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
!Input ||
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
value.shouldHide?.(
|
|
form.values.options as never,
|
|
innerProps.integrationData
|
|
.filter(({ id }) => form.values.integrationIds.includes(id))
|
|
.map(({ kind }) => kind),
|
|
)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<Input
|
|
key={key}
|
|
kind={innerProps.kind}
|
|
property={key}
|
|
options={value as never}
|
|
initialOptions={innerProps.value.options}
|
|
/>
|
|
);
|
|
})}
|
|
<Group justify="space-between">
|
|
<Button
|
|
variant="subtle"
|
|
onClick={() =>
|
|
openModal({
|
|
advancedOptions,
|
|
onSuccess(options) {
|
|
setAdvancedOptions(options);
|
|
innerProps.onSuccessfulEdit({
|
|
...innerProps.value,
|
|
advancedOptions: options,
|
|
});
|
|
},
|
|
})
|
|
}
|
|
>
|
|
{t("item.edit.advancedOptions.label")}
|
|
</Button>
|
|
<Group justify="end" w={{ base: "100%", xs: "auto" }}>
|
|
<Button onClick={actions.closeModal} variant="subtle" color="gray">
|
|
{t("common.action.cancel")}
|
|
</Button>
|
|
<Button type="submit">{t("common.action.saveChanges")}</Button>
|
|
</Group>
|
|
</Group>
|
|
</Stack>
|
|
</FormProvider>
|
|
</form>
|
|
);
|
|
}).withOptions({
|
|
keepMounted: true,
|
|
defaultTitle(t) {
|
|
return t("item.edit.title");
|
|
},
|
|
size: "lg",
|
|
});
|