All files / src/components/grid/dashboard DashboardGrid.tsx

0% Statements 0/8
100% Branches 0/0
0% Functions 0/5
0% Lines 0/7

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                                                                                                                 
import React, { useState, useEffect } from "react";
import GridLayout, { Layout } from "react-grid-layout";
import DynamicGridCell, {
  DynamicGridCellProps,
} from "../../grid_cells/DynamicGridCell/DynamicGridCell";
 
export interface DashboardGridProps {
  children: DynamicGridCellProps[];
}
 
const DashboardGrid: React.FC<DashboardGridProps> = ({ children }) => {
  const initialLayout: Layout[] = children.map((item) => ({
    i: item.cell.key,
    x: item.x,
    y: item.y,
    w: item.w,
    h: item.h,
  }));
 
  const [layout, setLayout] = useState<Layout[]>(() => {
    return initialLayout;
    // const savedLayout = localStorage.getItem("dashboard-layout");
    // console.log(savedLayout);
    // console.log(JSON.parse(savedLayout ?? ""));
    // console.log();
    // return savedLayout ? JSON.parse(savedLayout) : initialLayout;
  });
 
  // Save layout changes to localStorage
  useEffect(() => {
    // localStorage.setItem("dashboard-layout", JSON.stringify(layout));
  }, [layout]);
 
  return (
    <GridLayout
      className="rounded-lg"
      layout={layout}
      cols={10}
      rowHeight={50} // Dynamic row height
      width={window.innerWidth * 0.8} // Auto-adjust based on screen width
      onLayoutChange={setLayout}
      // draggableHandle="absolute top-[5px] left-[10px] cursor-grab text-red bg-red z-10"
    >
      {children.map((item, idx) => (
        <div
          key={item.cell.key}
          className="flex flex-wrap justify-between relative bg-gray-900 text-white w-full h-full"
        >
          <DynamicGridCell {...item} key={idx}></DynamicGridCell>
        </div>
      ))}
    </GridLayout>
  );
};
 
export default DashboardGrid;