All files / src/components/browser/dashboard DashboardBrowser.tsx

0% Statements 0/55
0% Branches 0/20
0% Functions 0/9
0% Lines 0/52

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 90 91 92 93 94 95 96 97                                                                                                                                                                                                 
import React from "react";
import DashboardGrid from "../../grid/dashboard/DashboardGrid";
import { GridCellProps } from "../../grid_cells/GridCell/GridCell";
import { DynamicGridCellProps } from "../../grid_cells/DynamicGridCell/DynamicGridCell";
import { getAllIndexes } from "@/util/array";
 
const COLS = 10;
 
function getTroughs(data: number[], val: number) {
  const indexes = getAllIndexes<number>(data, val);
  const troughs = [];
  let trough: number[] = [];
  indexes.forEach((index, i) => {
    if (trough.length == 0 || index == indexes[i - 1] + 1) {
      trough.push(index);
    } else {
      troughs.push(trough);
      trough = [];
      trough.push(index);
    }
  });
  if (trough.length != 0) {
    troughs.push(trough);
  }
  troughs.sort((a, b) => {
    return a.length > b.length ? a.length : b.length;
  });
  return troughs;
}
 
function getBestTrough(data: number[]) {
  const vals = [...new Set(data)].sort((a, b) => a - b);
  for (const val of vals) {
    const troughs = getTroughs(data, val);
    for (const t of troughs) {
      if (t.length > 2) {
        return t;
      }
    }
  }
  return [];
}
 
export interface DashboardBrowserProps {
  cells: GridCellProps[];
}
 
const DashboardBrowser: React.FC<DashboardBrowserProps> = (props) => {
  const screenPctg = window.screen.width / screen.width;
 
  const max_ys: number[] = Array.from({ length: COLS }, () => 0);
  let row = -1;
  let col = 0;
  const widgets: DynamicGridCellProps[] = [];
  props.cells.forEach((cell) => {
    let w = 3 + Math.floor(Math.random() * 6);
    const min_h = cell.min_height ?? 2;
    const max_h = cell.max_height !== null ? cell.max_height - min_h : 6;
    const h = min_h + Math.floor(Math.random() * max_h);
    let x: number, y: number;
    const trough: number[] = getBestTrough(max_ys);
    if (trough.length > 0) {
      x = trough[0];
      y = max_ys[x];
      row = y;
      if (x > 0 || trough.length < 9) {
        w = trough.length; //2 + Math.floor(Math.random() * 8);
      } else {
        w = 3 + Math.floor(Math.random() * 6);
      }
 
      trough.slice(0, w).forEach((idx) => {
        max_ys[idx] = y + h;
      });
    } else {
      row = row + 1;
      col = 0;
 
      x = col;
      y = row;
 
      for (let i = 0; i < w; i++) {
        max_ys[x + i] = y + h;
      }
    }
    col = x + w;
    widgets.push(new DynamicGridCellProps(x, y, w, h, cell));
  });
  return (
    // <div className="w-full h-full">
    <DashboardGrid>{widgets}</DashboardGrid>
    // </div>
  );
};
 
export default DashboardBrowser;