File size: 5,596 Bytes
f530297
 
 
82cdb59
 
 
 
 
f530297
 
 
 
 
 
 
 
 
89af3b2
f530297
89af3b2
 
 
 
 
f530297
 
 
 
 
 
 
 
89af3b2
 
f530297
 
82cdb59
 
f530297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89af3b2
f530297
89af3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f530297
 
89af3b2
 
 
f530297
 
 
 
89af3b2
f530297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82cdb59
f530297
 
 
82cdb59
f530297
89af3b2
 
 
 
f530297
 
 
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
// sheet/CellActionBar.tsx — the non-modal bar that appears beside a selected cell (W49-T05, R11).
//
// ⭐⭐ NON-MODAL IS THE RULING, AND IT DICTATES THREE THINGS THIS COMPONENT DOES NOT DO. R11 says a
// cell click SELECTS and the bar carries Edit / View data. W56-T23 (owner instruction 16) retired
// the Refresh entry because the ribbon already carries one. So it does NOT trap focus, it does NOT
// render a backdrop, and it does NOT autofocus anything: the grid keeps the keyboard the whole time,
// which is what lets someone arrow to the next cell without dismissing anything. That is the opposite
// discipline from `LinkGridModal`, which is modal and traps focus deliberately.
//
// ⚠ `onMouseDown` PREVENTS DEFAULT ON THE BAR ITSELF. Without it, pressing a button blurs the grid
// canvas, glide-data-grid drops its selection, and the bar unmounts before the click lands. The
// button then does nothing and it looks like a dead control.
//
// ⚠ DESIGN CONTRACT. Sentence case; quiet buttons are muted ink with a hover wash; icons are inline
// SVG on `currentColor`; motion is 150 ms ease-out on opacity and transform only; no emoji; no em
// dash or en dash in any string; z-order rides a `--cg-z-*` token, never a literal.

import { useEffect, useState } from "react";
import type { CSSProperties } from "react";
import { fetchWorkspace } from "../customer-grid/apiBridge";
import type { SurfaceScope } from "../customer-grid/apiBridge";
import { cellHasLimits, cellLimitSummary } from "./cellLimits";
import type { CellLimitField } from "./cellLimits";
import type { Cell } from "./types";

export interface CellActionBarProps {
  /** Viewport rect of the selected cell. The bar sits just above it, clamped to the viewport. */
  anchor: { x: number; y: number; width: number; height: number };
  /** R17: a formula cell cannot drill, and the reason is named on hover. */
  drill: { enabled: boolean; reason: string };
  canEdit: boolean;
  busy?: boolean;
  cell?: Cell | null;
  limitFields?: readonly CellLimitField[];
  onEdit: () => void;
  onViewRows: () => void;
  /** W56-T23 retired the button; kept only so sibling SheetGrid compiles. Drop once that file is free. */
  onRefresh?: () => void;
}

function IconEdit() {
  return (
    <svg viewBox="0 0 16 16" aria-hidden="true" className="sheet-action-icon">
      <path d="M2.8 13.2h3l7-7a1.6 1.6 0 0 0 0-2.2l-.8-.8a1.6 1.6 0 0 0-2.2 0l-7 7z" />
      <path d="M9.4 4.6l2 2" />
    </svg>
  );
}

function IconRows() {
  return (
    <svg viewBox="0 0 16 16" aria-hidden="true" className="sheet-action-icon">
      <rect x="2.4" y="3.2" width="11.2" height="9.6" rx="1.2" />
      <path d="M2.4 6.4h11.2M2.4 9.6h11.2" />
    </svg>
  );
}

export default function CellActionBar({
  anchor, drill, canEdit, busy = false, cell, limitFields, onEdit, onViewRows,
}: CellActionBarProps) {
  const database = cellHasLimits(cell) && typeof cell?.binding?.database === "string" ? cell.binding.database : "";
  const [schema, setSchema] = useState<{ database: string; fields: CellLimitField[] } | null>(null);
  useEffect(() => {
    if (limitFields || !database) return;
    let live = true;
    const scope = database === "customer_data" ? "customer" : database === "product_data"
      ? "product" : database.startsWith("ut_") ? database as SurfaceScope : null;
    if (!scope) return;
    void fetchWorkspace(scope, { fieldsOnly: true, allowCached: true }).then(workspace => {
      if (live) setSchema({ database, fields: workspace?.fields ?? [] });
    }).catch(() => { if (live) setSchema({ database, fields: [] }); });
    return () => { live = false; };
  }, [database, limitFields]);
  const limitNote = cellLimitSummary(cell, limitFields ?? (schema?.database === database ? schema.fields : []));
  const width = Math.min(520, Math.max(0, window.innerWidth - 16));
  // Clamped so a cell at the very top or the right edge still shows its bar on screen.
  const style: CSSProperties = {
    left: Math.max(8, Math.min(anchor.x, window.innerWidth - (limitNote ? width : 232) - 8)),
    top: Math.max(8, anchor.y - (limitNote ? 64 : 40)),
    ...(limitNote ? { width, flexWrap: "wrap", boxSizing: "border-box" } : {}),
  };

  return (
    <div
      className={`sheet-action-bar${limitNote ? " sheet-action-bar--limited" : ""}`}
      style={style}
      role="toolbar"
      aria-label="Cell actions"
      onMouseDown={(e) => e.preventDefault()}
    >
      <button type="button" className="sheet-action" onClick={onEdit} disabled={!canEdit || busy}>
        <IconEdit />
        <span>Edit</span>
      </button>
      <button
        type="button"
        className="sheet-action"
        onClick={onViewRows}
        disabled={!drill.enabled || busy}
        // ⭐ R17: the reason is NAMED rather than left to a disabled control with no explanation.
        // `title` carries it on hover and `aria-description` carries it to a screen reader, so the
        // two audiences get the same sentence.
        title={drill.enabled ? "View the data behind this cell" : drill.reason}
        aria-description={drill.enabled ? undefined : drill.reason}
      >
        <IconRows />
        <span>View data</span>
      </button>
      {limitNote && <span className="sheet-cell-limit" role="note" title={limitNote.full}
        style={{ flexBasis: "100%", minWidth: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap",
          color: "var(--lp-yellow-deep)", fontSize: "var(--lp-fs-2xs)" }}
        aria-label={`${limitNote.short}\n${limitNote.full}`}>{limitNote.short}</span>}
    </div>
  );
}