File size: 7,088 Bytes
e88a3a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72f0a1
e88a3a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72f0a1
 
 
 
e88a3a3
 
 
 
 
 
 
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
// ---------------------------------------------------------------------------
// settings / ShortcutSettings.tsx β€” WAVE 52 Β· T19 (owner instruction 21, ruling R7, contract C5).
//
// Owner, verbatim: *"Under setting, we need to be able to toggle sheet's shortcuts. All the
// shortcut available and can change the Letters for it."*
//
// ⭐⭐ RULING R7 β€” THE DELIBERATE DEVIATION. Instructions 20 and 21 conflict: Excel's own Alt+H
// key tips (instruction 20, W52-T17) are NOT rebindable in the real product. This pane implements
// instruction 21 anyway, as a STATED departure rather than a silent one β€” the disclosure line
// below is not decoration, it is the ruling's own requirement.
//
// β›” CONTRACT C5 β€” EVERYTHING HERE IS A READER AND A WRITER OF `sheet/shortcuts.ts`'s OWN
// localStorage store, and NOTHING ELSE. `matchShortcut`'s signature does not change and this file
// never touches `SheetGrid.tsx` (a different lane's fence this wave) β€” the grid already reads the
// SAME store fresh on every keydown, so a change made here takes effect the next time a key is
// pressed in the sheet, with no reload and no server round trip: there is nothing on the wire for
// a server to hold, this is a client-local preference exactly like a browser's own zoom level.
//
// β›” SELF-CONTAINED, LIKE ITS SIBLING PANES. Owns its own React state; no prop drilling, because
// shortcuts are local preferences rather than server data (see `ManageDatabasePane` / `KeychainPane`
// in `SettingsModal.tsx` for the pattern this follows).
// ---------------------------------------------------------------------------

import { useCallback, useState } from "react";
import {
  effectiveKeys,
  effectiveLetterFor,
  REBINDABLE_SHORTCUT_IDS,
  resetShortcutOverrides,
  setShortcutEnabled,
  setShortcutLetter,
  SHORTCUTS,
} from "../sheet/shortcuts";
import type { ShortcutId } from "../sheet/shortcuts";
// The `.set-shortcut-*` row layout only; see that file's own header for why a component-owned
// side-effect import is this repo's existing way to add a wave-52 rule without editing the shared
// `index.css` chain or another lane's stylesheet.
import "../styles/wave52/ribbon.css";

const REBINDABLE = new Set<ShortcutId>(REBINDABLE_SHORTCUT_IDS);

export default function ShortcutSettings() {
  // ⚠ A BUMP COUNTER, NOT A COPY OF THE STORE. `effectiveKeys` / `effectiveLetterFor` read
  // `localStorage` fresh on every call, which is also exactly what `matchShortcut` does on every
  // keydown in the grid β€” so this component needs only a reason to RE-RENDER after a write, not a
  // mirror of the data it re-renders with. A copied `overrides` object would be a second place the
  // same fact could drift from the store the grid actually reads.
  const [, setVersion] = useState(0);
  const refresh = useCallback(() => setVersion((v) => v + 1), []);

  // The single-character draft per rebindable row, so typing does not write on every keystroke β€”
  // only on blur or Enter, matching the ticket's own "simplest, consistent with this file's plain
  // style" instruction.
  const [drafts, setDrafts] = useState<Partial<Record<ShortcutId, string>>>({});
  const [rowErrors, setRowErrors] = useState<Partial<Record<ShortcutId, string>>>({});

  const draftFor = (id: ShortcutId): string => drafts[id] ?? effectiveLetterFor(id);

  const commitLetter = (id: ShortcutId, raw: string) => {
    const trimmed = raw.trim();
    const result = setShortcutLetter(id, trimmed === "" ? null : trimmed);
    if (result.ok) {
      setRowErrors((r) => ({ ...r, [id]: undefined }));
      setDrafts((d) => ({ ...d, [id]: undefined }));
      refresh();
    } else {
      setRowErrors((r) => ({ ...r, [id]: result.reason }));
    }
  };

  return (
    <div className="set-pane">
      <h3 className="set-h">Sheet shortcuts</h3>
      <p className="set-help set-pane-intro">
        Turn any sheet shortcut on or off, and change the letter behind the ones built on
        Ctrl plus a single letter. Rebinding your own shortcuts is a departure from Excel, which
        does not allow it.
      </p>

      <div className="set-actions">
        <button
          type="button"
          className="set-primary"
          onClick={() => {
            resetShortcutOverrides();
            setDrafts({});
            setRowErrors({});
            refresh();
          }}
        >
          Reset to defaults
        </button>
      </div>

      <ul className="set-shortcut-list">
        {SHORTCUTS.map((entry) => {
          const keys = effectiveKeys(entry.id);
          const disabled = keys === "Disabled";
          const rebindable = REBINDABLE.has(entry.id);
          const error = rowErrors[entry.id];
          return (
            <li className="set-shortcut-row" key={entry.id}>
              <div className="set-shortcut-main">
                <span className="set-shortcut-action">{entry.action}</span>
                <span className="set-help set-shortcut-when">{entry.when}</span>
              </div>

              <kbd
                className={
                  "set-shortcut-keys" + (disabled ? " set-shortcut-keys--off" : "")
                }
              >
                {keys}
              </kbd>

              {rebindable ? (
                <label className="set-field set-shortcut-letter">
                  <span className="set-label">Letter</span>
                  <input
                    className="set-input set-shortcut-letter-input"
                    type="text"
                    maxLength={1}
                    value={draftFor(entry.id)}
                    aria-label={`Letter for ${entry.action}`}
                    onChange={(e) => {
                      const value = e.currentTarget.value;
                      setDrafts((d) => ({ ...d, [entry.id]: value }));
                    }}
                    onBlur={(e) => commitLetter(entry.id, e.currentTarget.value)}
                    onKeyDown={(e) => {
                      if (e.key === "Enter") {
                        e.preventDefault();
                        e.currentTarget.blur();
                      }
                    }}
                  />
                </label>
              ) : (
                <span className="set-shortcut-letter set-shortcut-letter--fixed" />
              )}

              <label className="set-check set-shortcut-toggle">
                <input
                  type="checkbox"
                  checked={!disabled}
                  onChange={(e) => {
                    setShortcutEnabled(entry.id, e.currentTarget.checked);
                    refresh();
                  }}
                />
                Enabled
              </label>

              {error ? <div className="set-error set-shortcut-error" role="alert">
                <span>{error}</span>
                <button type="button" className="set-secondary" onClick={() => commitLetter(entry.id, draftFor(entry.id))}>Retry</button>
              </div> : null}
            </li>
          );
        })}
      </ul>
    </div>
  );
}