feat: rebuild board grid with dnd-kit
This commit is contained in:
1 parent
5bde4d3cc5
commit
1f5311e93b
107 files changed
+5628
-3209
No files matched your search
@@ -7,7 +7,7 @@ import { createId } from "@homarr/common";
|
||||
import type { Database } from "@homarr/db";
|
||||
import { and, eq } from "@homarr/db";
|
||||
import { createDb } from "@homarr/db/test";
|
||||
import { categorySectionOptionsSchema, dynamicSectionOptionsSchema } from "@homarr/validation/shared";
|
||||
import { containerSectionOptionsSchema } from "@homarr/validation/shared";
|
||||
|
||||
import {
|
||||
boards,
|
||||
@@ -19,10 +19,10 @@ import {
|
||||
sections,
|
||||
users,
|
||||
} from "../../schema";
|
||||
import { migrateCategoriesToUnifiedSectionsAsync } from "./0004_unify_sections_and_gutters";
|
||||
import { migrateLegacySectionsToContainersAsync } from "./0004_unify_sections_and_gutters";
|
||||
|
||||
describe("unified section migration", () => {
|
||||
test("converts a rail category into a resizable section inside a board gutter", async () => {
|
||||
describe("legacy section migration", () => {
|
||||
test("converts a rail category into a resizable container inside a board gutter", async () => {
|
||||
const db = createDb();
|
||||
const userId = createId();
|
||||
const boardId = createId();
|
||||
@@ -39,16 +39,17 @@ describe("unified section migration", () => {
|
||||
{
|
||||
id: categoryId,
|
||||
boardId,
|
||||
kind: "category",
|
||||
kind: "category" as never,
|
||||
name: "Pinned apps",
|
||||
xOffset: 0,
|
||||
yOffset: 1,
|
||||
options: stringify(
|
||||
categorySectionOptionsSchema.parse({
|
||||
railPlacement: "left",
|
||||
columnCount: 2,
|
||||
}),
|
||||
),
|
||||
options: stringify({
|
||||
showLabel: false,
|
||||
collapsible: true,
|
||||
showOpenAll: true,
|
||||
railPlacement: "left",
|
||||
columnCount: 2,
|
||||
}),
|
||||
},
|
||||
]);
|
||||
await db.insert(items).values({ id: itemId, boardId, kind: "clock" });
|
||||
@@ -68,7 +69,7 @@ describe("unified section migration", () => {
|
||||
height: 1,
|
||||
});
|
||||
|
||||
await migrateCategoriesToUnifiedSectionsAsync(db as unknown as Database);
|
||||
await migrateLegacySectionsToContainersAsync(db as unknown as Database);
|
||||
|
||||
const migratedLayout = await db.query.layouts.findFirst({ where: eq(layouts.id, layoutId) });
|
||||
expect(migratedLayout?.leftGutterColumnCount).toBe(2);
|
||||
@@ -79,8 +80,15 @@ describe("unified section migration", () => {
|
||||
expect(gutterRoot).toBeDefined();
|
||||
|
||||
const migratedCategory = await db.query.sections.findFirst({ where: eq(sections.id, categoryId) });
|
||||
expect(migratedCategory?.kind).toBe("dynamic");
|
||||
expect(dynamicSectionOptionsSchema.parse(parse(migratedCategory?.options ?? "{}")).title).toBe("Pinned apps");
|
||||
expect(migratedCategory?.kind).toBe("container");
|
||||
expect(containerSectionOptionsSchema.parse(parse(migratedCategory?.options ?? "{}"))).toEqual({
|
||||
title: "Pinned apps",
|
||||
customCssClasses: [],
|
||||
borderColor: "",
|
||||
showLabel: false,
|
||||
collapsible: true,
|
||||
showOpenAll: true,
|
||||
});
|
||||
const migratedCollapseState = await db.query.sectionCollapseStates.findFirst({
|
||||
where: and(eq(sectionCollapseStates.sectionId, categoryId), eq(sectionCollapseStates.userId, userId)),
|
||||
});
|
||||
@@ -100,4 +108,61 @@ describe("unified section migration", () => {
|
||||
});
|
||||
expect(nestedItem?.sectionId).toBe(categoryId);
|
||||
});
|
||||
|
||||
test("renames a dynamic section and preserves all container options and geometry", async () => {
|
||||
const db = createDb();
|
||||
const userId = createId();
|
||||
const boardId = createId();
|
||||
const layoutId = createId();
|
||||
const rootId = createId();
|
||||
const sectionId = createId();
|
||||
const options = {
|
||||
title: "Operations",
|
||||
customCssClasses: ["dense", "accent"],
|
||||
borderColor: "#123456",
|
||||
showLabel: false,
|
||||
collapsible: true,
|
||||
showOpenAll: true,
|
||||
};
|
||||
|
||||
await db.insert(users).values({ id: userId });
|
||||
await db.insert(boards).values({ id: boardId, name: "dynamic-migration", creatorId: userId });
|
||||
await db.insert(layouts).values({ id: layoutId, name: "Base", boardId, columnCount: 8, breakpoint: 0 });
|
||||
await db.insert(sections).values([
|
||||
{ id: rootId, boardId, kind: "empty", xOffset: 0, yOffset: 0 },
|
||||
{
|
||||
id: sectionId,
|
||||
boardId,
|
||||
kind: "dynamic" as never,
|
||||
options: stringify(options),
|
||||
},
|
||||
]);
|
||||
await db.insert(sectionLayouts).values({
|
||||
sectionId,
|
||||
layoutId,
|
||||
parentSectionId: rootId,
|
||||
xOffset: 2,
|
||||
yOffset: 3,
|
||||
width: 4,
|
||||
height: 5,
|
||||
});
|
||||
|
||||
await migrateLegacySectionsToContainersAsync(db as unknown as Database);
|
||||
await migrateLegacySectionsToContainersAsync(db as unknown as Database);
|
||||
|
||||
const migrated = await db.query.sections.findFirst({ where: eq(sections.id, sectionId) });
|
||||
expect(migrated?.kind).toBe("container");
|
||||
expect(containerSectionOptionsSchema.parse(parse(migrated?.options ?? "{}"))).toEqual(options);
|
||||
expect(
|
||||
await db.query.sectionLayouts.findFirst({
|
||||
where: and(eq(sectionLayouts.sectionId, sectionId), eq(sectionLayouts.layoutId, layoutId)),
|
||||
}),
|
||||
).toMatchObject({
|
||||
parentSectionId: rootId,
|
||||
xOffset: 2,
|
||||
yOffset: 3,
|
||||
width: 4,
|
||||
height: 5,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,10 @@
|
||||
import { parse, stringify } from "superjson";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { createId } from "@homarr/common";
|
||||
import type { BoardLane } from "@homarr/definitions";
|
||||
import { emptySuperJSON, getRootSectionLane, rootSectionOffsets } from "@homarr/definitions";
|
||||
import { categorySectionOptionsSchema, dynamicSectionOptionsSchema } from "@homarr/validation/shared";
|
||||
import { containerSectionOptionsSchema } from "@homarr/validation/shared";
|
||||
|
||||
import { and, eq, handleTransactionsAsync, inArray } from "../..";
|
||||
import type { Database } from "../..";
|
||||
@@ -50,13 +51,30 @@ interface MigrationOperation {
|
||||
applySync(db: HomarrDatabase): void;
|
||||
}
|
||||
|
||||
const legacyCategoryOptionsDefaults = {
|
||||
showLabel: true,
|
||||
collapsible: true,
|
||||
showOpenAll: true,
|
||||
railPlacement: "main",
|
||||
columnCount: 2,
|
||||
} as const;
|
||||
|
||||
const legacyCategoryOptionsSchema = z
|
||||
.object({
|
||||
showLabel: z.boolean().default(legacyCategoryOptionsDefaults.showLabel),
|
||||
collapsible: z.boolean().default(legacyCategoryOptionsDefaults.collapsible),
|
||||
showOpenAll: z.boolean().default(legacyCategoryOptionsDefaults.showOpenAll),
|
||||
railPlacement: z.enum(["main", "left", "right"]).default(legacyCategoryOptionsDefaults.railPlacement),
|
||||
columnCount: z.number().int().min(1).max(24).default(legacyCategoryOptionsDefaults.columnCount),
|
||||
})
|
||||
.default(legacyCategoryOptionsDefaults);
|
||||
|
||||
/**
|
||||
* Categories and dynamic sections used to be separate concepts. Categories
|
||||
* were full-width roots and also doubled as side rails. Convert every category
|
||||
* into the same resizable section used by the canvas, and create internal
|
||||
* gutter roots for the board-level left/right sidebar settings.
|
||||
* Convert legacy categories and dynamic sections into durable containers.
|
||||
* Categories were full-width roots and also doubled as side rails, so they
|
||||
* additionally need placement and gutter migration.
|
||||
*/
|
||||
export async function migrateCategoriesToUnifiedSectionsAsync(db: Database) {
|
||||
export async function migrateLegacySectionsToContainersAsync(db: Database) {
|
||||
const existingBoards = await db.query.boards.findMany({
|
||||
with: {
|
||||
layouts: true,
|
||||
@@ -74,15 +92,37 @@ export async function migrateCategoriesToUnifiedSectionsAsync(db: Database) {
|
||||
},
|
||||
});
|
||||
|
||||
let migratedCategoryCount = 0;
|
||||
let migratedSectionCount = 0;
|
||||
|
||||
for (const board of existingBoards) {
|
||||
const categories = board.sections.filter((section) => section.kind === "category");
|
||||
if (categories.length === 0) continue;
|
||||
const categories = board.sections.filter((section) => (section.kind as string) === "category");
|
||||
const legacyContainers = board.sections.filter((section) => (section.kind as string) === "dynamic");
|
||||
if (categories.length === 0 && legacyContainers.length === 0) continue;
|
||||
const operations: MigrationOperation[] = [];
|
||||
|
||||
const appendLegacyContainerOperations = () => {
|
||||
for (const section of legacyContainers) {
|
||||
operations.push(
|
||||
updateSectionOperation(section.id, {
|
||||
kind: "container",
|
||||
xOffset: null,
|
||||
yOffset: null,
|
||||
name: null,
|
||||
options: stringify(containerSectionOptionsSchema.parse(parse(section.options ?? emptySuperJSON))),
|
||||
}),
|
||||
);
|
||||
migratedSectionCount += 1;
|
||||
}
|
||||
};
|
||||
|
||||
if (categories.length === 0) {
|
||||
appendLegacyContainerOperations();
|
||||
await executeBoardMigrationOperationsAsync(db, operations);
|
||||
continue;
|
||||
}
|
||||
|
||||
const categoryDetails = categories.map((category) => {
|
||||
const options = categorySectionOptionsSchema.parse(parse(category.options ?? emptySuperJSON));
|
||||
const options = legacyCategoryOptionsSchema.parse(parse(category.options ?? emptySuperJSON));
|
||||
return {
|
||||
category,
|
||||
lane: options.railPlacement as BoardLane,
|
||||
@@ -247,12 +287,12 @@ export async function migrateCategoriesToUnifiedSectionsAsync(db: Database) {
|
||||
|
||||
operations.push(
|
||||
updateSectionOperation(category.id, {
|
||||
kind: "dynamic",
|
||||
kind: "container",
|
||||
xOffset: null,
|
||||
yOffset: null,
|
||||
name: null,
|
||||
options: stringify(
|
||||
dynamicSectionOptionsSchema.parse({
|
||||
containerSectionOptionsSchema.parse({
|
||||
title: (category.name ?? "").slice(0, 64),
|
||||
customCssClasses: [],
|
||||
borderColor: "",
|
||||
@@ -263,9 +303,11 @@ export async function migrateCategoriesToUnifiedSectionsAsync(db: Database) {
|
||||
),
|
||||
}),
|
||||
);
|
||||
migratedCategoryCount += 1;
|
||||
migratedSectionCount += 1;
|
||||
}
|
||||
|
||||
appendLegacyContainerOperations();
|
||||
|
||||
const retainedRootIds = new Set(Array.from(rootByLane.values(), (root) => root.id));
|
||||
const obsoleteRootIds = emptyRoots.filter((root) => !retainedRootIds.has(root.id)).map((root) => root.id);
|
||||
if (obsoleteRootIds.length > 0) {
|
||||
@@ -275,8 +317,8 @@ export async function migrateCategoriesToUnifiedSectionsAsync(db: Database) {
|
||||
await executeBoardMigrationOperationsAsync(db, operations);
|
||||
}
|
||||
|
||||
if (migratedCategoryCount > 0) {
|
||||
console.log(`Migrated categories to unified sections count="${migratedCategoryCount}"`);
|
||||
if (migratedSectionCount > 0) {
|
||||
console.log(`Migrated legacy sections to containers count="${migratedSectionCount}"`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,7 +341,7 @@ const getDirectPlacements = (board: MigrationBoard, layoutId: string, sectionId:
|
||||
: [];
|
||||
}),
|
||||
...board.sections.flatMap((section) => {
|
||||
if (section.kind !== "dynamic") return [];
|
||||
if (section.kind !== "dynamic" && section.kind !== "container") return [];
|
||||
const layout = section.layouts.find(
|
||||
(candidate) => candidate.layoutId === layoutId && candidate.parentSectionId === sectionId,
|
||||
);
|
||||
|
||||
@@ -3,12 +3,12 @@ import { migrateReleaseWidgetProviderToOptionsAsync } from "./0000_release_widge
|
||||
import { migrateOpnsenseCredentialsAsync } from "./0001_opnsense_credentials";
|
||||
import { migrateAppWidgetShowDescriptionTooltipToDisplayModeAsync } from "./0002_app_widget_show_description_tooltip_to_display_mode";
|
||||
import { migrateWidgetOnlyIntegrationsToOptionsAsync } from "./0003_remove_widget_only_integrations";
|
||||
import { migrateCategoriesToUnifiedSectionsAsync } from "./0004_unify_sections_and_gutters";
|
||||
import { migrateLegacySectionsToContainersAsync } from "./0004_unify_sections_and_gutters";
|
||||
|
||||
export const applyCustomMigrationsAsync = async (db: Database) => {
|
||||
await migrateReleaseWidgetProviderToOptionsAsync(db);
|
||||
await migrateOpnsenseCredentialsAsync(db);
|
||||
await migrateAppWidgetShowDescriptionTooltipToDisplayModeAsync(db);
|
||||
await migrateWidgetOnlyIntegrationsToOptionsAsync(db);
|
||||
await migrateCategoriesToUnifiedSectionsAsync(db);
|
||||
await migrateLegacySectionsToContainersAsync(db);
|
||||
};
|
||||
Reference in new issue
Block a user