All files / src/components/dashboard DashboardCanvas.tsx

61.29% Statements 19/31
38.46% Branches 10/26
36.36% Functions 4/11
64.28% Lines 18/28

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                                                2x         2x                                       2x 6x     6x         6x 6x     2x                       3x         3x     3x   3x                         3x                                                               6x 6x 6x 6x   6x                                                            
import {
  DashboardWidget,
  DataKind,
  RSSPayload,
  WeatherPayload,
  WidgetPlacement,
  WidgetTypeSpec,
} from "@/objects/Dashboard";
import React from "react";
import { Layout, Responsive, WidthProvider } from "react-grid-layout";
import "react-grid-layout/css/styles.css";
import "react-resizable/css/styles.css";
import { Tone } from "@/components/appearance/tones";
import {
  AUTHORED_BREAKPOINT,
  BREAKPOINTS,
  Breakpoint,
  COLS,
  responsiveLayouts,
} from "./layouts";
import WidgetFrame from "./WidgetFrame";
import { assuranceSource } from "./widgets/AssuranceWidgets";
import { presenterFor } from "./widgets/registry";
 
const ResponsiveGrid = WidthProvider(Responsive);
 
/** Widgets are authored against the 12-column `lg` grid; the narrower grids
 *  are derived from it (`layouts.ts`) so a board keeps its shape on a
 *  tablet and stacks in reading order on a phone. */
const ROW_HEIGHT = 48;
 
export interface DashboardCanvasProps {
  widgets: DashboardWidget[];
  tone: Tone;
  /** Drag and resize are only enabled while the board is in edit mode. */
  editing: boolean;
  /** A built-in board: tiles show no controls at all. */
  readOnly?: boolean;
  /** The backend registry, keyed by type — supplies provenance and dynamism. */
  specs: Map<string, WidgetTypeSpec>;
  onLayoutChange: (placements: WidgetPlacement[]) => void;
  onEditWidget: (widget: DashboardWidget) => void;
  onDeleteWidget: (widget: DashboardWidget) => void;
  onRefreshWidget: (widget: DashboardWidget) => void;
  onRemoveItem: (widget: DashboardWidget, itemId: string) => void;
}
 
/** The named origin shown on a tile: a feed's, read from whatever it cached,
 *  or the platform benchmark's, which is CHAIN-R's rather than the viewer's. */
const sourceNameOf = (widget: DashboardWidget): string | null => {
  Iif (widget.widget_type === "weather") {
    return (widget.cache_payload as unknown as WeatherPayload | null)?.source ?? null;
  }
  Iif (widget.widget_type === "rss") {
    return (
      (widget.cache_payload as unknown as RSSPayload | null)?.feed_title ?? null
    );
  }
  Iif (widget.widget_type === "assurance") return assuranceSource(widget);
  return null;
};
 
const DashboardCanvas: React.FC<DashboardCanvasProps> = ({
  widgets,
  tone,
  editing,
  readOnly = false,
  specs,
  onLayoutChange,
  onEditWidget,
  onDeleteWidget,
  onRefreshWidget,
  onRemoveItem,
}) => {
  const layouts = React.useMemo(() => responsiveLayouts(widgets), [widgets]);
 
  // The grid starts at the widest breakpoint (`WidthProvider` assumes a wide
  // window until it has measured), and reports a change once it has.
  const [breakpoint, setBreakpoint] =
    React.useState<Breakpoint>(AUTHORED_BREAKPOINT);
  // Only the stored layout can be arranged: a drag on a derived grid would
  // save its narrower coordinates as the board's real placement.
  const arranging = editing && breakpoint === AUTHORED_BREAKPOINT;
 
  const handleChange = (next: Layout[]) => {
    if (!arranging) return;
    onLayoutChange(
      next.map((item) => ({
        id: item.i,
        x: item.x,
        y: item.y,
        w: item.w,
        h: item.h,
      })),
    );
  };
 
  return (
    <>
      {/* Said here rather than by the page, because only the grid knows
          which width it is laying out for. */}
      {arranging && (
        <p className="mb-3 rounded-lg bg-emerald-50 dark:bg-emerald-900/20 px-3 py-2 text-xs text-emerald-700 dark:text-emerald-300">
          Drag a widget by its header to move it, or pull its bottom-right
          corner to resize. Changes save as you go.
        </p>
      )}
      {editing && !arranging && (
        <p className="mb-3 rounded-lg bg-amber-50 dark:bg-amber-900/20 px-3 py-2 text-xs text-amber-800 dark:text-amber-200">
          This window is too narrow to arrange the board: its tiles are laid out
          for this width automatically. Widen the window to move or resize them.
        </p>
      )}
      <ResponsiveGrid
        className="-mx-2"
        layouts={layouts}
        breakpoints={BREAKPOINTS}
        cols={COLS}
        rowHeight={ROW_HEIGHT}
        margin={[12, 12]}
        isDraggable={arranging}
        isResizable={arranging}
        onBreakpointChange={(next) => setBreakpoint(next as Breakpoint)}
        draggableHandle=".widget-drag-handle"
        onDragStop={handleChange}
        onResizeStop={handleChange}
        compactType="vertical"
      >
        {widgets.map((widget) => {
          const presenter = presenterFor(widget.widget_type);
          const Body = presenter.body;
          const spec = specs.get(widget.widget_type);
          const dataKind: DataKind = spec?.data_kind ?? "stored";
 
          return (
            <div key={widget.id}>
              <WidgetFrame
                widget={widget}
                tone={presenter.tone ?? tone}
                icon={presenter.icon}
                dataKind={dataKind}
                sourceName={sourceNameOf(widget)}
                editing={arranging}
                isDynamic={Boolean(spec?.is_dynamic)}
                readOnly={readOnly}
                onEdit={() => onEditWidget(widget)}
                onDelete={() => onDeleteWidget(widget)}
                onRefresh={() => onRefreshWidget(widget)}
              >
                <Body
                  widget={widget}
                  tone={presenter.tone ?? tone}
                  onRemoveItem={(itemId) => onRemoveItem(widget, itemId)}
                />
              </WidgetFrame>
            </div>
          );
        })}
      </ResponsiveGrid>
    </>
  );
};
 
export default DashboardCanvas;