All files / src/components/dashboard layouts.ts

100% Statements 18/18
100% Branches 2/2
100% Functions 7/7
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                                3x 3x           3x                     3x 3x       214x 2095x                                             214x 2095x 2095x 2095x                           1881x 214x 214x 2095x 2095x 2095x      
import type { Layout, Layouts } from "react-grid-layout";
 
/**
 * How a board is laid out at each width.
 *
 * A board's placement is stored once, against a 12-column grid. Narrower
 * grids are *derived* from it here rather than left to react-grid-layout,
 * whose own fallback clamps each tile into the narrower grid where it stood:
 * tiles pile up on the right edge and the board reads as a staircase.
 *
 * Widths are the canvas's own, not the window's — `WidthProvider` measures
 * the grid element, which sits beside the sidebar. The old thresholds were
 * window-sized (12 columns only from 1200px of canvas), so a 1280px laptop
 * window drew every board in the fallback. Twelve columns stay legible well
 * below that: at 880px a 3-wide stat tile is still about 200px.
 */
export const BREAKPOINTS = { lg: 880, md: 600, xs: 0 } as const;
export const COLS = { lg: 12, md: 8, xs: 1 } as const;
 
export type Breakpoint = keyof typeof COLS;
 
/** The breakpoint whose layout is the stored one — the only one a board can
 *  be arranged at, so a derived layout is never saved as the real one. */
export const AUTHORED_BREAKPOINT: Breakpoint = "lg";
 
export interface Placement {
  id: string;
  x: number;
  y: number;
  w: number;
  h: number;
}
 
/** Smallest a tile may be resized to on the authored grid. */
const MIN_W = 2;
const MIN_H = 2;
 
/** Every breakpoint's layout, from the stored 12-column placement. */
export function responsiveLayouts(placements: Placement[]): Layouts {
  return {
    lg: placements.map((p) => ({
      i: p.id,
      x: p.x,
      y: p.y,
      w: p.w,
      h: p.h,
      minW: MIN_W,
      minH: MIN_H,
    })),
    md: scaled(placements, COLS.lg, COLS.md),
    xs: stacked(placements),
  };
}
 
/**
 * Each tile's left and right edges mapped onto the narrower grid, rounded.
 *
 * Rounding is monotonic, so an edge that sat left of another still does:
 * tiles side by side stay side by side and none overlap, and a row that
 * filled twelve columns fills eight. A tile is never narrower than one
 * column.
 */
function scaled(placements: Placement[], from: number, to: number): Layout[] {
  return placements.map((p) => {
    const x = Math.min(Math.round((p.x * to) / from), to - 1);
    const end = Math.min(Math.round(((p.x + p.w) * to) / from), to);
    return {
      i: p.id,
      x,
      y: p.y,
      w: Math.max(1, end - x),
      h: p.h,
      minW: 1,
      minH: MIN_H,
    };
  });
}
 
/** One column, in reading order: row by row, left to right. */
function stacked(placements: Placement[]): Layout[] {
  const ordered = [...placements].sort((a, b) => a.y - b.y || a.x - b.x);
  let y = 0;
  return ordered.map((p) => {
    const item = { i: p.id, x: 0, y, w: 1, h: p.h, minW: 1, minH: MIN_H };
    y += p.h;
    return item;
  });
}