Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat([characterdetaileditor-listrow-row-draft-hook]): extract ListRow's multi-column draft state into useRowDraft hook

[._.]/ Adam Eivy8355f347ff8e
diff --git a/client/src/components/universe/CharacterDetailEditor.jsx b/client/src/components/universe/CharacterDetailEditor.jsx
index bb2dcf0dd7..32116a09eb 100644
--- a/client/src/components/universe/CharacterDetailEditor.jsx
+++ b/client/src/components/universe/CharacterDetailEditor.jsx
@@ -17,6 +17,7 @@ import {
} from 'lucide-react';
import { BIBLE_LIMITS as L } from '../../lib/bibleLimits';
import useFieldDraft from '../../hooks/useFieldDraft';
+import useRowDraft from '../../hooks/useRowDraft';
const SECTIONS = Object.freeze([
{
@@ -132,26 +133,9 @@ function DraftField({ field, value, onCommit, disabled, idPrefix }) {
}
// Generic list editor row — one input per `columns` spec, plus delete.
+// Multi-column draft+blur (with sibling ride-along) lives in `useRowDraft`.
function ListRow({ row, idx, columns, swatchHex, onChange, onDelete, disabled }) {
- const [drafts, setDrafts] = useState({});
- const draftFor = (col) => (col in drafts ? drafts[col] : (row[col] || ''));
- const setDraft = (col, v) => setDrafts((p) => ({ ...p, [col]: v }));
- const commit = (col) => {
- if (!(col in drafts)) return;
- const v = drafts[col];
- if (v === (row[col] || '')) {
- setDrafts((prev) => { const next = { ...prev }; delete next[col]; return next; });
- return;
- }
- // Spread `row` AND any other pending drafts so a fast A-blur → B-blur
- // sequence doesn't lose column A: if the parent hasn't re-rendered with
- // the committed A value by the time B blurs, B's commit would otherwise
- // spread the stale `row` prop and overwrite A back to its original. By
- // merging in `drafts`, the in-flight edits on sibling columns ride along.
- const nextRow = { ...row, ...drafts, [col]: v };
- setDrafts((prev) => { const next = { ...prev }; delete next[col]; return next; });
- onChange(nextRow);
- };
+ const { draftFor, setDraft, commit } = useRowDraft(row, onChange);
return (
<div className="flex items-start gap-1.5">
{swatchHex ? (
diff --git a/client/src/hooks/useRowDraft.js b/client/src/hooks/useRowDraft.js
new file mode 100644
index 0000000000..31cb1c13af
--- /dev/null
+++ b/client/src/hooks/useRowDraft.js
@@ -0,0 +1,35 @@
+// Multi-column row-draft buffer keyed by column name. Row-level analogue of
+// `useFieldDraft`. Sibling-draft "ride-along" race protection: when a fast
+// A-blur → B-blur sequence commits B before the parent has re-rendered with
+// A's committed value, spreading the stale `row` prop alone would overwrite
+// A back to its original. Merging the pending `drafts` map into the commit
+// payload lets A's in-flight edit ride along on B's commit.
+import { useState } from 'react';
+
+export default function useRowDraft(row, onChange) {
+ const [drafts, setDrafts] = useState({});
+
+ const draftFor = (col) => (col in drafts ? drafts[col] : (row[col] || ''));
+
+ const setDraft = (col, v) => setDrafts((p) => ({ ...p, [col]: v }));
+
+ const clearDraft = (col) => setDrafts((prev) => {
+ const next = { ...prev };
+ delete next[col];
+ return next;
+ });
+
+ const commit = (col) => {
+ if (!(col in drafts)) return;
+ const v = drafts[col];
+ if (v === (row[col] || '')) {
+ clearDraft(col);
+ return;
+ }
+ const nextRow = { ...row, ...drafts, [col]: v };
+ clearDraft(col);
+ onChange(nextRow);
+ };
+
+ return { draftFor, setDraft, commit };
+}
diff --git a/client/src/hooks/useRowDraft.test.js b/client/src/hooks/useRowDraft.test.js
new file mode 100644
index 0000000000..744d004c68
--- /dev/null
+++ b/client/src/hooks/useRowDraft.test.js
@@ -0,0 +1,96 @@
+import { describe, it, expect, vi } from 'vitest';
+import { renderHook, act } from '@testing-library/react';
+import useRowDraft from './useRowDraft';
+
+describe('useRowDraft', () => {
+ it('draftFor returns the persisted column value when no draft is set', () => {
+ const row = { name: 'amber', hex: '#f59e0b', role: 'jacket' };
+ const { result } = renderHook(() => useRowDraft(row, vi.fn()));
+ expect(result.current.draftFor('name')).toBe('amber');
+ expect(result.current.draftFor('hex')).toBe('#f59e0b');
+ expect(result.current.draftFor('role')).toBe('jacket');
+ });
+
+ it('draftFor coerces a missing/null persisted column to "" so the input stays controlled', () => {
+ const row = { name: 'amber', hex: null };
+ const { result } = renderHook(() => useRowDraft(row, vi.fn()));
+ expect(result.current.draftFor('hex')).toBe('');
+ expect(result.current.draftFor('absent')).toBe('');
+ });
+
+ it('setDraft buffers the value locally without calling onChange', () => {
+ const row = { name: 'amber' };
+ const onChange = vi.fn();
+ const { result } = renderHook(() => useRowDraft(row, onChange));
+ act(() => result.current.setDraft('name', 'crimson'));
+ expect(result.current.draftFor('name')).toBe('crimson');
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('commit fires onChange with the merged row when the draft differs from persisted', () => {
+ const row = { name: 'amber', hex: '#f59e0b' };
+ const onChange = vi.fn();
+ const { result } = renderHook(() => useRowDraft(row, onChange));
+ act(() => result.current.setDraft('name', 'crimson'));
+ act(() => result.current.commit('name'));
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith({ name: 'crimson', hex: '#f59e0b' });
+ });
+
+ it('commit is a no-op when no draft for that column exists', () => {
+ const row = { name: 'amber' };
+ const onChange = vi.fn();
+ const { result } = renderHook(() => useRowDraft(row, onChange));
+ act(() => result.current.commit('name'));
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('commit clears the draft without firing onChange when value equals persisted', () => {
+ const row = { name: 'amber' };
+ const onChange = vi.fn();
+ const { result } = renderHook(() => useRowDraft(row, onChange));
+ act(() => result.current.setDraft('name', 'amber'));
+ act(() => result.current.commit('name'));
+ expect(onChange).not.toHaveBeenCalled();
+ // After commit, draftFor falls back to the persisted value.
+ expect(result.current.draftFor('name')).toBe('amber');
+ });
+
+ it('commit treats null/undefined persisted as "" so an empty draft against an absent column is a no-op', () => {
+ const row = { name: 'amber' }; // hex absent
+ const onChange = vi.fn();
+ const { result } = renderHook(() => useRowDraft(row, onChange));
+ act(() => result.current.setDraft('hex', ''));
+ act(() => result.current.commit('hex'));
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('ride-along: committing column B while column A still has a pending draft ships both in one onChange', () => {
+ const row = { name: 'amber', hex: '#f59e0b' };
+ const onChange = vi.fn();
+ const { result } = renderHook(() => useRowDraft(row, onChange));
+ act(() => {
+ result.current.setDraft('name', 'crimson');
+ result.current.setDraft('hex', '#dc2626');
+ });
+ act(() => result.current.commit('hex'));
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith({ name: 'crimson', hex: '#dc2626' });
+ });
+
+ it('commit clears only the committed column from drafts; sibling drafts remain pending', () => {
+ const row = { name: 'amber', hex: '#f59e0b' };
+ const onChange = vi.fn();
+ const { result } = renderHook(() => useRowDraft(row, onChange));
+ act(() => {
+ result.current.setDraft('name', 'crimson');
+ result.current.setDraft('hex', '#dc2626');
+ });
+ act(() => result.current.commit('name'));
+ // name's draft cleared (falls back to whatever the parent re-renders
+ // with — in this test the `row` prop is stable, so persisted wins).
+ expect(result.current.draftFor('name')).toBe('amber');
+ // hex's draft still pending.
+ expect(result.current.draftFor('hex')).toBe('#dc2626');
+ });
+});