fix(import): autofix missing shapes for sidebars and some sections as well (#2723)

This commit is contained in:
Meier Lukas
2025-03-28 17:44:11 +01:00
committed by GitHub
parent 217818573e
commit 62c7955e48
4 changed files with 113 additions and 44 deletions

View File

@@ -3,11 +3,12 @@ import { isWidgetRestricted } from "@homarr/auth/shared";
import { createId } from "@homarr/db";
import { createDbInsertCollectionForTransaction } from "@homarr/db/collection";
import { logger } from "@homarr/log";
import type { BoardSize } from "@homarr/old-schema";
import type { BoardSize, OldmarrConfig } from "@homarr/old-schema";
import { boardSizes, getBoardSizeName } from "@homarr/old-schema";
import { widgetImports } from "../../../../widgets/src";
import { fixSectionIssues } from "../../fix-section-issues";
import { OldHomarrImportError } from "../../import-error";
import { mapBoard } from "../../mappers/map-board";
import { mapBreakpoint } from "../../mappers/map-breakpoint";
import { mapColumnCount } from "../../mappers/map-column-count";
@@ -61,6 +62,13 @@ export const createBoardInsertCollection = (
logger.debug(`Added apps to board insert collection count=${insertCollection.apps.length}`);
preparedBoards.forEach((board) => {
if (!hasEnoughItemShapes(board.config)) {
throw new OldHomarrImportError(
board.config,
new Error("Your config contains items without shapes for all board sizes."),
);
}
const { wrappers, categories, wrapperIdsToMerge } = fixSectionIssues(board.config);
const { apps, widgets } = moveWidgetsAndAppsIfMerge(board.config, wrapperIdsToMerge, {
...settings,
@@ -130,3 +138,26 @@ export const createBoardInsertCollection = (
return insertCollection;
};
export const hasEnoughItemShapes = (config: {
apps: Pick<OldmarrConfig["apps"][number], "shape">[];
widgets: Pick<OldmarrConfig["widgets"][number], "shape">[];
}) => {
const invalidSizes: BoardSize[] = [];
for (const size of boardSizes) {
if (invalidSizes.includes(size)) continue;
if (config.apps.some((app) => app.shape[size] === undefined)) {
invalidSizes.push(size);
}
if (invalidSizes.includes(size)) continue;
if (config.widgets.some((widget) => widget.shape[size] === undefined)) {
invalidSizes.push(size);
}
}
return invalidSizes.length <= 2;
};

View File

@@ -0,0 +1,53 @@
import { describe, expect, test } from "vitest";
import type { BoardSize } from "@homarr/old-schema";
import { hasEnoughItemShapes } from "../collections/board-collection";
const defaultShape = {
location: {
x: 0,
y: 0,
},
size: {
width: 1,
height: 1,
},
};
describe("hasEnoughItemShapes should check if there are more than one shape available for automatic reconstruction", () => {
test.each([
[true, [], []], // no items, so nothing to check
[true, [{ lg: true }], []], // lg always exists
[true, [], [{ md: true }]], // md always exists
[true, [{ md: true, sm: true }], [{ md: true, lg: true }]], // md always exists
[true, [{ md: true }], [{ md: true }]], // md always exists
[false, [{ md: true }, { md: true }], [{ lg: true }]], // md is missing for widgets
[false, [{ md: true }], [{ lg: true }]], // md is missing for widgets
[false, [{ md: true }], [{ md: true, lg: true }, { lg: true }]], // md is missing for 2. widget
] as [boolean, Shape[], Shape[]][])(
"should return %s if there are more than one shape available",
(returnValue, appShapes, widgetShapes) => {
const result = hasEnoughItemShapes({
apps: appShapes.map((shapes) => ({
shape: {
sm: shapes.sm ? defaultShape : undefined,
md: shapes.md ? defaultShape : undefined,
lg: shapes.lg ? defaultShape : undefined,
},
})),
widgets: widgetShapes.map((shapes) => ({
shape: {
sm: shapes.sm ? defaultShape : undefined,
md: shapes.md ? defaultShape : undefined,
lg: shapes.lg ? defaultShape : undefined,
},
})),
});
expect(result).toBe(returnValue);
},
);
});
type Shape = Partial<Record<BoardSize, true>>;