runloopable / web /src /settings /ShortcutSettings.tsx
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
dec006b verified
Raw History Blame Contribute Delete
7.09 kB
// ---------------------------------------------------------------------------
// 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>
);
}