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:
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user