diff --git a/web/e2e/helpers/grid-layout.ts b/web/e2e/helpers/grid-layout.ts index a3d8d97e87..bc51b192f9 100644 --- a/web/e2e/helpers/grid-layout.ts +++ b/web/e2e/helpers/grid-layout.ts @@ -13,7 +13,8 @@ * Tiles measured in separate round-trips can straddle that reflow and disagree * on scale, so cameraBoxes() takes every measurement in one evaluate. * - * Used by live-grid-aspect-modes.spec.ts and masonry-live-grid.spec.ts. + * Used by live-grid-aspect-modes.spec.ts, live-grid-legacy-layouts.spec.ts and + * masonry-live-grid.spec.ts. */ import { expect, type Page } from "@playwright/test"; diff --git a/web/e2e/pages/live.page.ts b/web/e2e/pages/live.page.ts index 4f9697f373..282c949510 100644 --- a/web/e2e/pages/live.page.ts +++ b/web/e2e/pages/live.page.ts @@ -45,6 +45,11 @@ export class LivePage extends BasePage { ); } + /** Birdseye tile on the draggable grid, the one without a camera card. */ + get birdseyeTile(): Locator { + return this.page.locator(".react-grid-item:not(:has([data-camera]))"); + } + /** Edit-layout toggle on the draggable grid (desktop, custom groups). */ get editLayoutButton(): Locator { return this.page.getByTestId("toggle-edit-layout"); diff --git a/web/e2e/specs/live-grid-aspect-modes.spec.ts b/web/e2e/specs/live-grid-aspect-modes.spec.ts index 7ce92568ff..f2b45d5be8 100644 --- a/web/e2e/specs/live-grid-aspect-modes.spec.ts +++ b/web/e2e/specs/live-grid-aspect-modes.spec.ts @@ -89,8 +89,8 @@ test.describe("Live grid aspect modes @critical", () => { }); // 0.17/0.18 shape: bare array on a 12-column grid, 4x4 standard tiles. - // backyard was manually resized to 8x8 and sits beside front_door's column, - // front_door is a standard tile on the row below. + // backyard was manually resized to 8x8 and sits beside front_door's column. + // front_door is stored on the row below, but 0.18 compacted it to the top. const key = await persistedLayoutKey(frigateApp.page, GROUP); await seedLayout(frigateApp.page, key, [ { i: "backyard", x: 4, y: 0, w: 8, h: 8, moved: false, static: false }, @@ -127,21 +127,21 @@ test.describe("Live grid aspect modes @critical", () => { stored.layout.find((i: LayoutItem) => i.i === "front_door"), ).toMatchObject({ x: 0, - y: 144, + y: 0, w: 32, h: 72, }); - // arrangement on screen: backyard indented, front_door below it + // arrangement on screen: front_door top-left, backyard beside it const { backyard, front_door: frontDoor } = await cameraBoxes( frigateApp.page, ["backyard", "front_door"] as const, ); expect(backyard.x).toBeGreaterThan(frontDoor.x + frontDoor.w / 2); - expect(frontDoor.y).toBeGreaterThan(backyard.y + backyard.h / 2); + expect(Math.abs(frontDoor.y - backyard.y)).toBeLessThan(2); }); - test("conversion is a pure scale, so odd sizes and positions survive", async ({ + test("conversion scales odd sizes and keeps tiles adjacent", async ({ frigateApp, }) => { await frigateApp.goto(`/?group=${GROUP}`); @@ -175,8 +175,9 @@ test.describe("Live grid aspect modes @critical", () => { )!; const backyard = stored.layout.find((i: LayoutItem) => i.i === "backyard")!; - expect(frontDoor).toMatchObject({ x: 0, y: 54, w: 40, h: 90 }); - expect(backyard).toMatchObject({ x: 40, y: 54, w: 56, h: 90 }); + // the empty rows above both tiles compact away, as they did in 0.18 + expect(frontDoor).toMatchObject({ x: 0, y: 0, w: 40, h: 90 }); + expect(backyard).toMatchObject({ x: 40, y: 0, w: 56, h: 90 }); // still adjacent, still inside the grid, still not overlapping expect(frontDoor.x + frontDoor.w).toBe(backyard.x); diff --git a/web/e2e/specs/live-grid-legacy-layouts.spec.ts b/web/e2e/specs/live-grid-legacy-layouts.spec.ts new file mode 100644 index 0000000000..52132c82a2 --- /dev/null +++ b/web/e2e/specs/live-grid-legacy-layouts.spec.ts @@ -0,0 +1,157 @@ +/** + * Live grid layouts saved by 0.18. + * + * 0.18 stored every new tile at y 0 and let the grid compact the tiles at + * render, in render order (Birdseye, then camera order). Its stored layouts + * can share cells and hold gaps that never showed. Conversion compacts the same + * way, so each layout opens exactly as 0.18 displayed it. + * + * Each stored layout below was captured from 0.18's IndexedDB, and each + * expected cell is where 0.18 rendered that tile. + */ + +import { test, expect } from "../fixtures/frigate-test"; +import { BASE_CONFIG } from "../fixtures/mock-data/config"; +import { LivePage } from "../pages/live.page"; +import { + persistedLayoutKey, + readLayout, + seedLayout, + type LayoutItem, +} from "../helpers/grid-layout"; + +// one 0.18 cell is 32 columns by 72 rows on the current grid +const CELL_W = 32; +const CELL_H = 72; + +const tile = (i: string, x: number, y: number) => ({ i, x, y, w: 4, h: 4 }); + +type Scenario = { + name: string; + cameras: string[]; + stored: LayoutItem[]; + rendered: Record; +}; + +const SCENARIOS: Scenario[] = [ + { + name: "a group created with Birdseye", + cameras: ["birdseye", "front_door", "backyard", "garage"], + stored: [ + tile("birdseye", 0, 0), + tile("front_door", 4, 0), + tile("backyard", 8, 0), + tile("garage", 0, 0), + ], + rendered: { + birdseye: [0, 0], + front_door: [1, 0], + backyard: [2, 0], + garage: [0, 1], + }, + }, + { + name: "Birdseye and a camera added after a drag", + cameras: [ + "front_door", + "backyard", + "garage", + "porch", + "driveway", + "birdseye", + ], + stored: [ + tile("front_door", 0, 4), + tile("backyard", 4, 0), + tile("garage", 8, 0), + tile("porch", 0, 0), + tile("driveway", 4, 0), + tile("birdseye", 0, 0), + ], + rendered: { + birdseye: [0, 0], + backyard: [1, 0], + garage: [2, 0], + porch: [0, 1], + driveway: [1, 1], + front_door: [0, 2], + }, + }, + { + name: "a camera added ahead of the others in camera order", + cameras: ["backyard", "garage", "porch", "driveway", "front_door"], + stored: [ + tile("backyard", 0, 0), + tile("garage", 4, 0), + tile("porch", 8, 0), + tile("driveway", 0, 0), + tile("front_door", 0, 0), + ], + rendered: { + front_door: [0, 0], + garage: [1, 0], + porch: [2, 0], + backyard: [0, 1], + driveway: [0, 2], + }, + }, + { + name: "a camera removed after a drag", + cameras: ["front_door", "backyard", "garage"], + stored: [ + tile("front_door", 0, 4), + tile("backyard", 4, 0), + tile("garage", 8, 0), + ], + rendered: { + front_door: [0, 0], + backyard: [1, 0], + garage: [2, 0], + }, + }, +]; + +const frontDoor = BASE_CONFIG.cameras.front_door; + +test.describe("Live grid 0.18 layouts @critical", () => { + test.skip( + ({ frigateApp }) => frigateApp.isMobile, + "Draggable grid is desktop-only", + ); + + SCENARIOS.forEach(({ name, cameras, stored, rendered }, index) => { + test(`opens ${name} as 0.18 showed it`, async ({ frigateApp }) => { + const group = `legacy${index}`; + await frigateApp.installDefaults({ + config: { + cameras: { + porch: { ...frontDoor, name: "porch" }, + driveway: { ...frontDoor, name: "driveway" }, + }, + camera_groups: { [group]: { cameras, icon: "LuCar", order: 1 } }, + }, + }); + await frigateApp.goto(`/?group=${group}`); + const key = await persistedLayoutKey(frigateApp.page, group); + await seedLayout(frigateApp.page, key, stored); + await frigateApp.page.reload(); + + await expect + .poll(async () => (await readLayout(frigateApp.page, key))?.version, { + timeout: 10_000, + }) + .toBe(2); + + const layout = (await readLayout(frigateApp.page, key))!.layout; + const cells = Object.fromEntries( + layout.map((item) => [item.i, [item.x / CELL_W, item.y / CELL_H]]), + ); + expect(cells).toEqual(rendered); + + if (cameras.includes("birdseye")) { + await new LivePage(frigateApp.page, true).birdseyeTile.click(); + await expect(frigateApp.page).toHaveURL(/#birdseye/); + } + }); + }); +}); diff --git a/web/src/views/live/DraggableGridLayout.tsx b/web/src/views/live/DraggableGridLayout.tsx index f1bcb0d097..a015431e65 100644 --- a/web/src/views/live/DraggableGridLayout.tsx +++ b/web/src/views/live/DraggableGridLayout.tsx @@ -18,7 +18,11 @@ import { LayoutItem, ResponsiveGridLayout as Responsive, } from "react-grid-layout"; -import { aspectRatio, getCompactor } from "react-grid-layout/core"; +import { + aspectRatio, + getCompactor, + verticalCompactor, +} from "react-grid-layout/core"; import "react-grid-layout/css/styles.css"; import "react-resizable/css/styles.css"; import { @@ -311,10 +315,17 @@ export default function DraggableGridLayout({ [layoutAspects], ); + const cameraNames = useMemo( + () => + includeBirdseye && birdseyeConfig?.enabled + ? ["birdseye", ...cameras.map((camera) => camera?.name || "")] + : cameras.map((camera) => camera?.name || ""), + [cameras, includeBirdseye, birdseyeConfig], + ); + // Rescale a pre-masonry layout onto the current grid. Both axes scale by a - // constant, so tiles the user resized keep their size and their arrangement - // stays intact. Only meaningful in bucketed mode, where a tile still has the - // shape those coordinates assumed. + // constant, so tiles the user resized keep their size. Only meaningful in + // bucketed mode, where a tile still has the shape those coordinates assumed. const convertLegacyLayout = useCallback( (stored: unknown): Layout | undefined => { if (naturalAspectLayout || !Array.isArray(stored) || !stored.length) { @@ -349,9 +360,17 @@ export default function DraggableGridLayout({ }); } - return converted; + // 0.18 stored new tiles at y 0 and compacted the grid at render, in + // render order. Compacting the same way reproduces what 0.18 showed. + + return verticalCompactor.compact( + converted + .filter((item) => cameraNames.includes(item.i)) + .sort((a, b) => cameraNames.indexOf(a.i) - cameraNames.indexOf(b.i)), + GRID_COLS, + ); }, - [naturalAspectLayout], + [naturalAspectLayout, cameraNames], ); const generateLayout = useCallback( @@ -360,11 +379,6 @@ export default function DraggableGridLayout({ return; } - const cameraNames = - includeBirdseye && birdseyeConfig?.enabled - ? ["birdseye", ...cameras.map((camera) => camera?.name || "")] - : cameras.map((camera) => camera?.name || ""); - const existing: LayoutItem[] = baseLayout ? baseLayout.filter((layout) => cameraNames.includes(layout.i)) : []; @@ -426,7 +440,7 @@ export default function DraggableGridLayout({ return result; }, - [cameras, isGridLayoutLoaded, includeBirdseye, birdseyeConfig, dimsFor], + [cameraNames, isGridLayoutLoaded, dimsFor], ); useEffect(() => {