fix: stabilize responsive layout editing

This commit is contained in:
ajnart committed 2026-08-08 18:02:39 +02:00
1 parent 1877b4f9a4
commit a9fc3c6faa
14 files changed
+168 -60

No files matched your search

@@ -78,6 +78,26 @@ describe("Grid Algorithm", () => {
);
},
);
test("stops projecting a corrupted recursive section graph", () => {
const recursiveSection = algoItem({ id: "recursive", type: "section", width: 3 });
const duplicateRecursiveLayout = algoItem({
id: "recursive",
type: "section",
width: 3,
sectionId: "recursive",
});
const result = generateResponsiveGridFor({
items: [recursiveSection, duplicateRecursiveLayout],
width: 3,
previousWidth: 12,
sectionId: ROOT_SECTION_ID,
});
expect(result.items).toContainEqual(expect.objectContaining({ id: "recursive", sectionId: ROOT_SECTION_ID }));
expect(result.items).toHaveLength(2);
});
});
const algoItem = (item: Partial<GridAlgorithmItem>): GridAlgorithmItem => ({
+11 -5
View File
@@ -71,13 +71,18 @@ export const useLayoutOverride = () => useContext(BoardContext)?.layoutOverrideI
export const getCurrentLayout = (
board: RouterOutputs["board"]["getBoardByName"],
layoutOverrideId: string | null = null,
) => getCurrentLayoutFromLayouts(board.layouts, layoutOverrideId);
const getCurrentLayoutFromLayouts = (
layouts: RouterOutputs["board"]["getBoardByName"]["layouts"],
layoutOverrideId: string | null,
) => {
if (layoutOverrideId && board.layouts.some((layout) => layout.id === layoutOverrideId)) return layoutOverrideId;
if (layoutOverrideId && layouts.some((layout) => layout.id === layoutOverrideId)) return layoutOverrideId;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (typeof window === "undefined") return board.layouts.at(0)!.id;
if (typeof window === "undefined") return layouts.at(0)!.id;
const sortedLayouts = board.layouts.toSorted((layoutA, layoutB) => layoutB.breakpoint - layoutA.breakpoint);
const sortedLayouts = layouts.toSorted((layoutA, layoutB) => layoutB.breakpoint - layoutA.breakpoint);
// Fallback to smallest if none exists with breakpoint smaller than window width
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -88,10 +93,11 @@ export const useCurrentLayout = () => {
const board = useRequiredBoard();
const layoutOverrideId = useLayoutOverride();
const [currentLayout, setCurrentLayout] = useState(getCurrentLayout(board, layoutOverrideId));
const layouts = board.layouts;
const onResize = useCallback(() => {
setCurrentLayout(getCurrentLayout(board, layoutOverrideId));
}, [board, layoutOverrideId]);
setCurrentLayout(getCurrentLayoutFromLayouts(layouts, layoutOverrideId));
}, [layoutOverrideId, layouts]);
useEffect(() => {
if (typeof window === "undefined") return;
+19 -7
View File
@@ -25,7 +25,16 @@ export const generateResponsiveGridFor = ({
previousWidth,
width,
sectionId,
}: GridAlgorithmInput): GridAlgorithmOutput => {
}: GridAlgorithmInput): GridAlgorithmOutput =>
generateResponsiveGridForSection({ items, previousWidth, width, sectionId }, new Set());
const generateResponsiveGridForSection = (
{ items, previousWidth, width, sectionId }: GridAlgorithmInput,
ancestorSectionIds: ReadonlySet<string>,
): GridAlgorithmOutput => {
if (ancestorSectionIds.has(sectionId)) return { height: 0, items: [] };
const nextAncestorSectionIds = new Set(ancestorSectionIds).add(sectionId);
const itemsOfCurrentSection = items
.filter((item) => item.sectionId === sectionId)
.toSorted((itemA, itemB) =>
@@ -38,12 +47,15 @@ export const generateResponsiveGridFor = ({
const newItems: GridAlgorithmItem[] = [];
const dynamicSectionHeightMap = new Map<string, number>();
for (const dynamicSection of normalizedItems.filter((item) => item.type === "section")) {
const result = generateResponsiveGridFor({
items,
previousWidth: dynamicSection.previousWidth,
width: dynamicSection.width,
sectionId: dynamicSection.id,
});
const result = generateResponsiveGridForSection(
{
items,
previousWidth: dynamicSection.previousWidth,
width: dynamicSection.width,
sectionId: dynamicSection.id,
},
nextAncestorSectionIds,
);
newItems.push(...result.items);
dynamicSectionHeightMap.set(dynamicSection.id, result.height);
}