mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-05 00:28:51 +01:00
Six inline-edit toggles across two files all hand-rolled the same state machine: editing/draft/saving + start/cancel/setSaving wrap. Pull that into hooks/use-inline-edit.ts. Migrated: - ProjectInlineEditors.tsx: NameEditor, DescriptionEditor, StatusEditor, MaturityEditor (4 editors, 221 → 200 lines) - PersonDetail.tsx: name and description toggles (-30 lines of plumbing) Each call site keeps its own validation/equality/transform — the hook only owns the lifecycle. Net: -32 lines across the two files, and adding a new inline editor anywhere is now ~3 calls of useInlineEdit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
|
|
/**
|
|
* Shared state machine for inline-edit toggles (display ↔ <input>).
|
|
* Tracks `editing`, a typed `draft`, and a `saving` flag. Validation /
|
|
* transform / equality checks stay at the call site — this hook only
|
|
* owns the lifecycle.
|
|
*
|
|
* Usage:
|
|
* const ie = useInlineEdit<string>("");
|
|
* const handleCommit = () => {
|
|
* const trimmed = ie.draft.trim();
|
|
* if (!trimmed || trimmed === value) { ie.cancel(); return; }
|
|
* ie.commit(() => onSave(trimmed));
|
|
* };
|
|
* if (ie.editing) return <input value={ie.draft} onChange={(e) => ie.setDraft(e.target.value)} ... />;
|
|
* return <h2 onClick={() => ie.start(value)}>{value}</h2>;
|
|
*/
|
|
export function useInlineEdit<T>(initial: T) {
|
|
const [editing, setEditing] = useState(false);
|
|
const [draft, setDraft] = useState<T>(initial);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
return {
|
|
editing,
|
|
draft,
|
|
saving,
|
|
setDraft,
|
|
/** Enter edit mode, seeding the draft with `current`. */
|
|
start: (current: T) => { setDraft(current); setEditing(true); },
|
|
/** Exit edit mode without saving. */
|
|
cancel: () => setEditing(false),
|
|
/**
|
|
* Run the save action with saving-flag wrapping. Exits edit mode
|
|
* after the action resolves (success or error).
|
|
*/
|
|
commit: async (action: () => Promise<void> | void) => {
|
|
setSaving(true);
|
|
try { await action(); } finally {
|
|
setSaving(false);
|
|
setEditing(false);
|
|
}
|
|
},
|
|
};
|
|
}
|