feat: add import for config files from oldmarr (#1019)

* wip: add oldmarr config import

* wip: add support for wrong amount of categories / sections with autofix, color mapping, position adjustments of wrappers

* fix: lockfile broken

* feat: add support for form data trpc requests

* wip: improve file upload

* refactor: restructure import, add import configuration

* wip: add configurations for import to modal

* refactor: move oldmarr import to old-import package

* fix: column count not respects screen size for board

* feat: add beta badge for oldmarr config import

* chore: address pull request feedback

* fix: format issues

* fix: inconsistent versions

* fix: deepsource issues

* fix: revert {} to Record<string, never> convertion to prevent typecheck issue

* fix: inconsistent zod version

* fix: format issue

* chore: address pull request feedback

* fix: wrong import

* fix: broken lock file

* fix: inconsistent versions

* fix: format issues
This commit is contained in:
Meier Lukas
2024-09-07 18:13:24 +02:00
committed by GitHub
parent fc1bff2110
commit 5404cebf5b
65 changed files with 2132 additions and 34 deletions

View File

@@ -1,4 +1,5 @@
import { z } from "zod";
import { zfd } from "zod-form-data";
import {
backgroundImageAttachments,
@@ -8,6 +9,7 @@ import {
} from "@homarr/definitions";
import { zodEnumFromArray } from "./enums";
import { createCustomErrorParams } from "./form/i18n";
import { createSavePermissionsSchema } from "./permissions";
import { commonItemSchema, createSectionSchema } from "./shared";
@@ -67,6 +69,61 @@ const permissionsSchema = z.object({
id: z.string(),
});
export const createOldmarrImportConfigurationSchema = (existingBoardNames: string[]) =>
z.object({
name: boardNameSchema.refine(
(value) => {
return existingBoardNames.every((name) => name.toLowerCase().trim() !== value.toLowerCase().trim());
},
{
params: createCustomErrorParams("boardAlreadyExists"),
},
),
onlyImportApps: z.boolean().default(false),
distinctAppsByHref: z.boolean().default(true),
screenSize: z.enum(["lg", "md", "sm"]).default("lg"),
sidebarBehaviour: z.enum(["remove-items", "last-section"]).default("last-section"),
});
export type OldmarrImportConfiguration = z.infer<ReturnType<typeof createOldmarrImportConfigurationSchema>>;
export const superRefineJsonImportFile = (value: File | null, context: z.RefinementCtx) => {
if (!value) {
return context.addIssue({
code: "invalid_type",
expected: "object",
received: "null",
});
}
if (value.type !== "application/json") {
return context.addIssue({
code: "custom",
params: createCustomErrorParams({
key: "invalidFileType",
params: { expected: "JSON" },
}),
});
}
if (value.size > 1024 * 1024) {
return context.addIssue({
code: "custom",
params: createCustomErrorParams({
key: "fileTooLarge",
params: { maxSize: "1 MB" },
}),
});
}
return null;
};
const importJsonFileSchema = zfd.formData({
file: zfd.file().superRefine(superRefineJsonImportFile),
configuration: zfd.json(createOldmarrImportConfigurationSchema([])),
});
const savePermissionsSchema = createSavePermissionsSchema(zodEnumFromArray(boardPermissions));
z.object({
@@ -88,4 +145,5 @@ export const boardSchemas = {
changeVisibility: changeVisibilitySchema,
permissions: permissionsSchema,
savePermissions: savePermissionsSchema,
importOldmarrConfig: importJsonFileSchema,
};

View File

@@ -1,3 +1,4 @@
import type { ParamsObject } from "international-types";
import type { ErrorMapCtx, z, ZodTooBigIssue, ZodTooSmallIssue } from "zod";
import { ZodIssueCode } from "zod";
@@ -114,16 +115,17 @@ const handleZodError = (issue: z.ZodIssueOptionalMessage, ctx: ErrorMapCtx) => {
if (issue.code === ZodIssueCode.too_big) {
return handleTooBigError(issue);
}
if (issue.code === ZodIssueCode.invalid_type && ctx.data === "") {
if (issue.code === ZodIssueCode.invalid_type && (ctx.data === "" || issue.received === "null")) {
return {
key: "errors.required",
params: {},
} as const;
}
if (issue.code === ZodIssueCode.custom && issue.params?.i18n) {
const { i18n } = issue.params as CustomErrorParams;
const { i18n } = issue.params as CustomErrorParams<CustomErrorKey>;
return {
key: `errors.custom.${i18n.key}`,
params: i18n.params,
} as const;
}
@@ -132,12 +134,17 @@ const handleZodError = (issue: z.ZodIssueOptionalMessage, ctx: ErrorMapCtx) => {
};
};
export interface CustomErrorParams {
type CustomErrorKey = keyof TranslationObject["common"]["zod"]["errors"]["custom"];
export interface CustomErrorParams<TKey extends CustomErrorKey> {
i18n: {
key: keyof TranslationObject["common"]["zod"]["errors"]["custom"];
params?: Record<string, unknown>;
key: TKey;
params: ParamsObject<TranslationObject["common"]["zod"]["errors"]["custom"][TKey]>;
};
}
export const createCustomErrorParams = (i18n: CustomErrorParams["i18n"] | CustomErrorParams["i18n"]["key"]) =>
typeof i18n === "string" ? { i18n: { key: i18n } } : { i18n };
export const createCustomErrorParams = <TKey extends CustomErrorKey>(
i18n: keyof CustomErrorParams<TKey>["i18n"]["params"] extends never
? CustomErrorParams<TKey>["i18n"]["key"]
: CustomErrorParams<TKey>["i18n"],
) => (typeof i18n === "string" ? { i18n: { key: i18n, params: {} } } : { i18n });

View File

@@ -26,3 +26,5 @@ export {
type BoardItemAdvancedOptions,
} from "./shared";
export { passwordRequirements } from "./user";
export { createOldmarrImportConfigurationSchema, superRefineJsonImportFile } from "./board";
export type { OldmarrImportConfiguration } from "./board";