@buoy-gg/storage 7.0.30 → 7.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/lib/commonjs/storage/components/GameUIStorageBrowser.js +92 -21
  2. package/lib/commonjs/storage/components/StorageBrowserMode.js +8 -2
  3. package/lib/commonjs/storage/components/StorageKeyDetailScreen.js +572 -0
  4. package/lib/commonjs/storage/components/StorageKeyRow.js +21 -2
  5. package/lib/commonjs/storage/components/StorageKeySection.js +44 -21
  6. package/lib/commonjs/storage/components/StorageModalWithTabs.js +25 -2
  7. package/lib/commonjs/storage/components/StorageSaveNotice.js +171 -0
  8. package/lib/commonjs/storage/components/StorageValueEditModal.js +11 -364
  9. package/lib/commonjs/storage/hooks/useKeyEditor.js +12 -85
  10. package/lib/commonjs/storage/hooks/useTreeDraft.js +7 -100
  11. package/lib/module/storage/components/GameUIStorageBrowser.js +94 -23
  12. package/lib/module/storage/components/StorageBrowserMode.js +8 -2
  13. package/lib/module/storage/components/StorageKeyDetailScreen.js +570 -0
  14. package/lib/module/storage/components/StorageKeyRow.js +20 -3
  15. package/lib/module/storage/components/StorageKeySection.js +43 -21
  16. package/lib/module/storage/components/StorageModalWithTabs.js +26 -3
  17. package/lib/module/storage/components/StorageSaveNotice.js +168 -0
  18. package/lib/module/storage/components/StorageValueEditModal.js +13 -366
  19. package/lib/module/storage/hooks/useKeyEditor.js +14 -87
  20. package/lib/module/storage/hooks/useTreeDraft.js +9 -102
  21. package/lib/typescript/storage/components/GameUIStorageBrowser.d.ts +13 -2
  22. package/lib/typescript/storage/components/GameUIStorageBrowser.d.ts.map +1 -1
  23. package/lib/typescript/storage/components/StorageBrowserMode.d.ts +7 -2
  24. package/lib/typescript/storage/components/StorageBrowserMode.d.ts.map +1 -1
  25. package/lib/typescript/storage/components/StorageKeyDetailScreen.d.ts +46 -0
  26. package/lib/typescript/storage/components/StorageKeyDetailScreen.d.ts.map +1 -0
  27. package/lib/typescript/storage/components/StorageKeyRow.d.ts +9 -0
  28. package/lib/typescript/storage/components/StorageKeyRow.d.ts.map +1 -1
  29. package/lib/typescript/storage/components/StorageKeySection.d.ts +19 -1
  30. package/lib/typescript/storage/components/StorageKeySection.d.ts.map +1 -1
  31. package/lib/typescript/storage/components/StorageModalWithTabs.d.ts.map +1 -1
  32. package/lib/typescript/storage/components/StorageSaveNotice.d.ts +6 -0
  33. package/lib/typescript/storage/components/StorageSaveNotice.d.ts.map +1 -0
  34. package/lib/typescript/storage/components/StorageValueEditModal.d.ts +9 -31
  35. package/lib/typescript/storage/components/StorageValueEditModal.d.ts.map +1 -1
  36. package/lib/typescript/storage/hooks/useKeyEditor.d.ts +7 -31
  37. package/lib/typescript/storage/hooks/useKeyEditor.d.ts.map +1 -1
  38. package/lib/typescript/storage/hooks/useTreeDraft.d.ts +7 -66
  39. package/lib/typescript/storage/hooks/useTreeDraft.d.ts.map +1 -1
  40. package/lib/typescript/storage/sync/storageSyncAdapter.d.ts +1 -1
  41. package/package.json +5 -5
@@ -4,374 +4,21 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.StorageValueEditModal = StorageValueEditModal;
7
- var _react = require("react");
8
- var _reactNative = require("react-native");
9
- var _sharedUi = require("@buoy-gg/shared-ui");
10
7
  var _dataViewer = require("@buoy-gg/shared-ui/dataViewer");
11
8
  var _jsxRuntime = require("react/jsx-runtime");
12
9
  /**
13
- * Editing one value: a panel at the top of the screen, with Cancel and Save.
10
+ * Storage's value-typing panel.
14
11
  *
15
- * This started as a pushed full screen and that was more ceremony than the task
16
- * deserves — changing one string is the most ordinary thing in this tool, and
17
- * it shouldn't cost a navigation. A modal is what people expect, and the reason
18
- * it's pinned to the TOP rather than centred is the keyboard: a centred panel
19
- * gets shoved up or covered the moment the field focuses, and a bottom sheet is
20
- * simply underneath it. At the top it stays put and stays readable.
21
- *
22
- * The field is multiline for anything textual. Stored values are routinely
23
- * tokens, URLs and serialized blobs, and a single line shows you roughly forty
24
- * characters of them, scrolling sideways — which is how you edit the wrong part
25
- * of a string without noticing.
26
- *
27
- * Type is preserved by construction: the ORIGINAL value's type picks the editor
28
- * AND decides how the draft is read back, so editing a number can only produce
29
- * a number. Raw JSON — the escape hatch for containers — re-checks the parsed
30
- * result and refuses a different shape.
31
- *
32
- * Nothing here writes. `onSave` hands the value to whoever owns it.
12
+ * The panel itself — top-pinned so the keyboard can't reach it, type preserved
13
+ * by construction, raw-JSON shape check — lives in shared-ui's
14
+ * {@link ValueEditModal}, shared with the Zustand store editor. This wrapper
15
+ * only pins storage's testID namespace so the Detox suite's handles
16
+ * (`buoy-storage-value-input`, `-save`, …) stay exactly what they were.
33
17
  */
34
18
 
35
- /**
36
- * How this value gets edited. Driven by the value's own type, which is also
37
- * what makes the result type-safe without asking the user to declare anything.
38
- */
39
-
40
- function modeFor(value) {
41
- if (typeof value === "boolean") return "boolean";
42
- if (typeof value === "number") return "number";
43
- if (value !== null && typeof value === "object") return "raw";
44
- return "text";
45
- }
46
- function initialText(value, mode) {
47
- if (mode === "raw") return JSON.stringify(value, null, 2);
48
- if (value === null || value === undefined) return "";
49
- return String(value);
50
- }
51
- function hintFor(mode, type) {
52
- switch (mode) {
53
- case "number":
54
- return "Numeric only — anything else is refused, so this stays a number.";
55
- case "raw":
56
- return `Raw JSON. Must stay ${type === "array" ? "an array" : "an object"} — a different shape is refused.`;
57
- default:
58
- return "Saves as a string. The type never changes here.";
59
- }
60
- }
61
- function StorageValueEditModal({
62
- path,
63
- value,
64
- onSave,
65
- onCancel,
66
- saveLabel = "Save",
67
- busy = false
68
- }) {
69
- const mode = modeFor(value);
70
- const type = (0, _dataViewer.typeLabel)(value);
71
- const [text, setText] = (0, _react.useState)(() => initialText(value, mode));
72
-
73
- /**
74
- * Advisory only — {@link commit} re-derives it, so a disabled button and a
75
- * refused save can never disagree about what's valid.
76
- */
77
- const error = validate(text, mode, type);
78
- const blocked = busy || error != null;
79
- const commit = () => {
80
- if (busy || validate(text, mode, type) != null) return;
81
- onSave(parse(text, mode, value));
82
- };
83
- return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
84
- style: styles.overlay,
85
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableOpacity, {
86
- style: styles.scrim,
87
- activeOpacity: 1,
88
- onPress: busy ? undefined : onCancel,
89
- testID: "buoy-storage-value-scrim",
90
- accessibilityRole: "button",
91
- accessibilityLabel: "Dismiss editor"
92
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
93
- style: styles.panel,
94
- pointerEvents: "box-none",
95
- children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
96
- style: styles.card,
97
- children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
98
- style: styles.headerRow,
99
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
100
- style: styles.title,
101
- children: "Edit value"
102
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
103
- style: styles.typeBadge,
104
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
105
- style: styles.typeBadgeText,
106
- children: type
107
- })
108
- })]
109
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
110
- style: styles.path,
111
- numberOfLines: 1,
112
- children: (0, _dataViewer.breadcrumb)(path)
113
- }), mode === "boolean" ? /*#__PURE__*/(0, _jsxRuntime.jsx)(BooleanChoice, {
114
- current: value === true,
115
- onPick: onSave
116
- }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.ScrollView, {
117
- style: styles.fieldScroll,
118
- keyboardShouldPersistTaps: "handled",
119
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TextInput, {
120
- testID: "buoy-storage-value-input",
121
- accessibilityLabel: "Value",
122
- value: text,
123
- onChangeText: setText,
124
- autoFocus: true,
125
- autoCapitalize: "none",
126
- autoCorrect: false,
127
- spellCheck: false,
128
- editable: !busy
129
- // Multiline for anything textual: stored values are long more
130
- // often than they're short, and a single line hides that.
131
- ,
132
- multiline: mode !== "number"
133
- // ASCII for raw JSON, because iOS Smart Punctuation is ON by
134
- // default and rewrites the quotes you type as curly ones —
135
- // which makes the text unparseable, disables Save, and leaves
136
- // an "invalid JSON" message whose cause is invisible at a
137
- // glance. An ASCII keyboard doesn't do the substitution.
138
- ,
139
- keyboardType: mode === "number" ? "numeric" : mode === "raw" ? "ascii-capable" : "default",
140
- onSubmitEditing: mode === "number" ? commit : undefined,
141
- placeholder: mode === "raw" ? "{ }" : "value",
142
- placeholderTextColor: _sharedUi.night.color.placeholder,
143
- style: [styles.input, mode === "raw" && styles.inputRaw, mode !== "number" && styles.inputMultiline, error != null && styles.inputInvalid]
144
- })
145
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
146
- style: error != null ? styles.error : styles.hint,
147
- children: error ?? hintFor(mode, type)
148
- }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
149
- style: styles.actions,
150
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableOpacity, {
151
- onPress: onCancel,
152
- disabled: busy,
153
- testID: "buoy-storage-value-cancel",
154
- accessibilityRole: "button",
155
- accessibilityLabel: "Cancel edit",
156
- style: styles.cancelButton,
157
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
158
- style: styles.cancelText,
159
- children: "Cancel"
160
- })
161
- }), mode === "boolean" ? null : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableOpacity, {
162
- onPress: commit,
163
- disabled: blocked,
164
- testID: "buoy-storage-value-save",
165
- accessibilityRole: "button",
166
- accessibilityLabel: saveLabel,
167
- style: [styles.saveButton, blocked && styles.saveDisabled],
168
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
169
- style: styles.saveText,
170
- children: busy ? "Saving…" : saveLabel
171
- })
172
- })]
173
- })]
174
- })
175
- })]
19
+ function StorageValueEditModal(props) {
20
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dataViewer.ValueEditModal, {
21
+ ...props,
22
+ testIDPrefix: "buoy-storage-value"
176
23
  });
177
- }
178
- function BooleanChoice({
179
- current,
180
- onPick
181
- }) {
182
- return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
183
- style: styles.boolRow,
184
- children: [true, false].map(option => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableOpacity, {
185
- onPress: () => onPick(option),
186
- testID: `buoy-storage-value-${option}`,
187
- accessibilityRole: "button",
188
- accessibilityLabel: `Set ${option}`,
189
- style: [styles.boolButton, current === option && styles.boolActive],
190
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
191
- style: [styles.boolText, current === option && styles.boolTextActive],
192
- children: String(option)
193
- })
194
- }, String(option)))
195
- });
196
- }
197
-
198
- /** Null when the draft is applicable, else why it isn't. */
199
- function validate(text, mode, type) {
200
- if (mode === "number") {
201
- if (text.trim() === "") return "Enter a number.";
202
- if (Number.isNaN(Number(text))) return `"${text}" isn't a number.`;
203
- return null;
204
- }
205
- if (mode === "raw") {
206
- let parsed;
207
- try {
208
- parsed = JSON.parse(text);
209
- } catch (e) {
210
- return e instanceof Error ? e.message : "Invalid JSON.";
211
- }
212
- if ((0, _dataViewer.typeLabel)(parsed) !== type) {
213
- return `Must stay ${type} — that parses as ${(0, _dataViewer.typeLabel)(parsed)}.`;
214
- }
215
- return null;
216
- }
217
- return null;
218
- }
219
- function parse(text, mode, original) {
220
- if (mode === "number") return Number(text);
221
- if (mode === "raw") return JSON.parse(text);
222
- // An emptied field on a null keeps it null rather than silently minting an
223
- // empty string, which would change the type this promised to preserve.
224
- if (original === null && text === "") return null;
225
- return text;
226
- }
227
- const styles = _reactNative.StyleSheet.create({
228
- overlay: {
229
- ..._sharedUi.absoluteFill,
230
- zIndex: 20
231
- },
232
- scrim: {
233
- ..._sharedUi.absoluteFill,
234
- backgroundColor: "rgba(0,0,0,0.55)"
235
- },
236
- /** Pinned to the top so the keyboard can't reach it. */
237
- panel: {
238
- padding: 12
239
- },
240
- card: {
241
- backgroundColor: _sharedUi.night.color.surface,
242
- borderRadius: 14,
243
- borderWidth: 1,
244
- borderColor: _sharedUi.night.color.border,
245
- padding: 14,
246
- gap: 8
247
- },
248
- headerRow: {
249
- flexDirection: "row",
250
- alignItems: "center",
251
- gap: 8
252
- },
253
- title: {
254
- flex: 1,
255
- color: _sharedUi.night.color.text,
256
- fontSize: 14,
257
- fontWeight: "700"
258
- },
259
- typeBadge: {
260
- paddingHorizontal: 8,
261
- paddingVertical: 2,
262
- borderRadius: 6,
263
- backgroundColor: _sharedUi.night.color.surfaceElevated
264
- },
265
- typeBadgeText: {
266
- color: _sharedUi.macOSColors.semantic.warning,
267
- fontSize: 10,
268
- fontWeight: "700",
269
- fontFamily: "monospace"
270
- },
271
- path: {
272
- color: _sharedUi.night.color.textTertiary,
273
- fontSize: 11,
274
- fontFamily: "monospace"
275
- },
276
- /** Caps the panel's height so a huge blob can't push the buttons off-screen. */
277
- fieldScroll: {
278
- maxHeight: 240
279
- },
280
- input: {
281
- color: _sharedUi.night.color.text,
282
- backgroundColor: _sharedUi.night.color.surfaceElevated,
283
- borderWidth: 1,
284
- borderColor: _sharedUi.night.color.border,
285
- borderRadius: 10,
286
- paddingHorizontal: 12,
287
- paddingVertical: 10,
288
- fontSize: 14,
289
- fontFamily: "monospace"
290
- },
291
- inputMultiline: {
292
- minHeight: 110,
293
- lineHeight: 20,
294
- textAlignVertical: "top"
295
- },
296
- inputRaw: {
297
- minHeight: 180,
298
- fontSize: 12,
299
- lineHeight: 18
300
- },
301
- inputInvalid: {
302
- borderColor: _sharedUi.night.color.danger
303
- },
304
- hint: {
305
- color: _sharedUi.night.color.textTertiary,
306
- fontSize: 11,
307
- lineHeight: 16
308
- },
309
- error: {
310
- color: _sharedUi.night.color.danger,
311
- fontSize: 11,
312
- lineHeight: 16,
313
- fontFamily: "monospace"
314
- },
315
- actions: {
316
- flexDirection: "row",
317
- gap: 8,
318
- paddingTop: 2
319
- },
320
- cancelButton: {
321
- flex: 1,
322
- minHeight: 44,
323
- alignItems: "center",
324
- justifyContent: "center",
325
- borderRadius: 10,
326
- borderWidth: 1,
327
- borderColor: _sharedUi.night.color.border
328
- },
329
- cancelText: {
330
- color: _sharedUi.night.color.textSecondary,
331
- fontSize: 14,
332
- fontWeight: "600"
333
- },
334
- saveButton: {
335
- flex: 1.4,
336
- minHeight: 44,
337
- alignItems: "center",
338
- justifyContent: "center",
339
- borderRadius: 10,
340
- backgroundColor: _sharedUi.night.color.accent
341
- },
342
- saveDisabled: {
343
- opacity: 0.35
344
- },
345
- saveText: {
346
- color: _sharedUi.night.color.onAccent,
347
- fontSize: 15,
348
- fontWeight: "700"
349
- },
350
- boolRow: {
351
- flexDirection: "row",
352
- gap: 10
353
- },
354
- boolButton: {
355
- flex: 1,
356
- minHeight: 64,
357
- alignItems: "center",
358
- justifyContent: "center",
359
- borderRadius: 12,
360
- borderWidth: 1,
361
- borderColor: _sharedUi.night.color.border,
362
- backgroundColor: _sharedUi.night.color.surfaceElevated
363
- },
364
- boolActive: {
365
- borderColor: _sharedUi.macOSColors.semantic.warning,
366
- backgroundColor: _sharedUi.macOSColors.semantic.warning + "18"
367
- },
368
- boolText: {
369
- color: _sharedUi.night.color.textTertiary,
370
- fontSize: 16,
371
- fontWeight: "700",
372
- fontFamily: "monospace"
373
- },
374
- boolTextActive: {
375
- color: _sharedUi.macOSColors.semantic.warning
376
- }
377
- });
24
+ }
@@ -6,19 +6,13 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.useKeyEditor = useKeyEditor;
7
7
  var _react = require("react");
8
8
  var _dataViewer = require("@buoy-gg/shared-ui/dataViewer");
9
- var _useTreeDraft = require("./useTreeDraft");
10
9
  /**
11
- * Everything editing a key involves, minus how it looks.
10
+ * Storage's view of the shared tree editor.
12
11
  *
13
- * The phone and the dashboard run the same editor: one buffered draft, one
14
- * write on save, a selection the structural actions operate on, a modal for
15
- * typing a value, and a guard so closing can't silently drop unsaved work.
16
- * What differs is presentation — thumb-sized tiles under a full screen versus
17
- * chips docked in a panel — which is a rendering concern and nothing more.
18
- *
19
- * Keeping the rules here means the two surfaces can't drift on the things that
20
- * would actually hurt: whether a boolean opens an editor, whether a failed
21
- * write closes the editor anyway, what "dirty" means.
12
+ * Everything editing a key involves — buffered draft, selection, value modal,
13
+ * dirty-close guard — lives in shared-ui's `useTreeEditor`, shared with the
14
+ * Zustand store editor so the two can't drift. Storage's single write takes
15
+ * the SERIALIZED value, so this wrapper is exactly the stringify.
22
16
  */
23
17
 
24
18
  function useKeyEditor({
@@ -27,78 +21,11 @@ function useKeyEditor({
27
21
  onClose,
28
22
  confirmDiscard
29
23
  }) {
30
- const draft = (0, _useTreeDraft.useTreeDraft)(value, save);
31
- const [selection, setSelection] = (0, _react.useState)(null);
32
- const [valuePath, setValuePath] = (0, _react.useState)(null);
33
- const {
34
- begin
35
- } = draft;
36
- (0, _react.useEffect)(() => {
37
- begin();
38
- // Seed the draft ONCE, when the editor opens. `begin` closes over the live
39
- // value, whose identity changes every time the key list refreshes
40
- // underneath us (a parsed JSON string is a new object each pass), so
41
- // re-running would reset the draft mid-edit and discard the user's work.
42
- // eslint-disable-next-line react-hooks/exhaustive-deps
43
- }, []);
44
-
45
- /**
46
- * "Change this node". For a boolean that's a flip rather than a modal — the
47
- * same rule `treeActionsFor` applies to the action controls, so the gesture
48
- * and the button can't mean different things.
49
- */
50
- const openValueEditor = (0, _react.useCallback)(path => {
51
- const current = (0, _dataViewer.getAtPath)(draft.value, path);
52
- if (typeof current === "boolean") {
53
- draft.apply({
54
- kind: "replace",
55
- path,
56
- value: !current
57
- });
58
- return;
59
- }
60
- setValuePath(path);
61
- }, [draft]);
62
- const applyValue = (0, _react.useCallback)(next => {
63
- draft.apply({
64
- kind: "replace",
65
- path: valuePath ?? [],
66
- value: next
67
- });
68
- setValuePath(null);
69
- }, [draft, valuePath]);
70
-
71
- // Only leave once the write actually landed — `commit` resolves false and
72
- // keeps the draft on screen when the device refuses it, and closing anyway
73
- // would throw the work away while showing an error nobody ever sees.
74
- const saveAndClose = (0, _react.useCallback)(() => {
75
- void draft.commit().then(saved => saved && onClose());
76
- }, [draft, onClose]);
77
- const requestClose = (0, _react.useCallback)(() => {
78
- if (!draft.isDirty) {
79
- onClose();
80
- return;
81
- }
82
- // The draft is the only copy of this work, so closing over it silently is
83
- // the one way this editor can lose something the user can't get back.
84
- confirmDiscard({
85
- save: saveAndClose,
86
- discard: () => {
87
- draft.discard();
88
- onClose();
89
- }
90
- });
91
- }, [confirmDiscard, draft, onClose, saveAndClose]);
92
- return {
93
- draft,
94
- selection,
95
- setSelection,
96
- valuePath,
97
- openValueEditor,
98
- closeValueEditor: () => setValuePath(null),
99
- applyValue,
100
- openRawEditor: () => setValuePath([]),
101
- requestClose,
102
- saveAndClose
103
- };
24
+ const saveValue = (0, _react.useMemo)(() => next => save(JSON.stringify(next)), [save]);
25
+ return (0, _dataViewer.useTreeEditor)({
26
+ value,
27
+ save: saveValue,
28
+ onClose,
29
+ confirmDiscard
30
+ });
104
31
  }
@@ -7,28 +7,12 @@ exports.useTreeDraft = useTreeDraft;
7
7
  var _react = require("react");
8
8
  var _dataViewer = require("@buoy-gg/shared-ui/dataViewer");
9
9
  /**
10
- * A local draft of a storage value, edited in place and written ONCE.
10
+ * Storage's view of the shared buffered tree draft.
11
11
  *
12
- * Applying each tree op straight to the device was the obvious first cut and it
13
- * behaves badly for two compounding reasons:
14
- *
15
- * 1. **Every op is a round trip.** The write lands, the device emits a storage
16
- * event, a snapshot comes back, and the browser re-reads every backend — so
17
- * a six-tap reorder is six writes and six full refreshes. The tree rebuilds
18
- * under the user mid-gesture.
19
- * 2. **The highlight and the value fall out of step.** Selection is local and
20
- * moves instantly; the value it points at only catches up when the round
21
- * trip completes, so for a beat the highlight sits on the old contents.
22
- *
23
- * Optimistic updates — the React Query pattern of rendering the expected result
24
- * and reconciling later — fix (2) but not (1): they still write per change.
25
- * Buffering fixes both, and it's what every editor that owns a document does
26
- * (a form's dirty state, an IDE's unsaved buffer, JSON Editor's in-memory doc
27
- * with an explicit save).
28
- *
29
- * The trade is that the buffer can go stale if the app writes the same key
30
- * while you're editing. That's the same last-write-wins a text editor has, and
31
- * `isDirty` at least makes it visible that you're holding unsaved changes.
12
+ * The draft itself lives in shared-ui (`useTreeDraft` there — one editor for
13
+ * storage keys and zustand stores, so the rules can't drift). Storage's write
14
+ * path takes the SERIALIZED value — `setItem(key, raw)` — so this wrapper is
15
+ * exactly the stringify.
32
16
  */
33
17
 
34
18
  /**
@@ -36,83 +20,6 @@ var _dataViewer = require("@buoy-gg/shared-ui/dataViewer");
36
20
  * @param save persists the serialized draft; rejecting keeps edit mode open
37
21
  */
38
22
  function useTreeDraft(liveValue, save) {
39
- const [draft, setDraft] = (0, _react.useState)(undefined);
40
- const [isEditing, setIsEditing] = (0, _react.useState)(false);
41
- const [isSaving, setIsSaving] = (0, _react.useState)(false);
42
- const [error, setError] = (0, _react.useState)(null);
43
- const [selectPath, setSelectPath] = (0, _react.useState)();
44
- /**
45
- * The value as it was when editing began. Compared against for dirtiness
46
- * rather than against `liveValue`, which can move underneath us if the app
47
- * writes the same key — that would otherwise flip a clean draft to dirty.
48
- */
49
- const [baseline, setBaseline] = (0, _react.useState)("");
50
- const value = isEditing && draft !== undefined ? draft : liveValue;
51
- const isDirty = isEditing && JSON.stringify(draft) !== baseline;
52
- const begin = (0, _react.useCallback)(() => {
53
- setDraft(liveValue);
54
- setBaseline(JSON.stringify(liveValue));
55
- setSelectPath(undefined);
56
- setError(null);
57
- setIsEditing(true);
58
- }, [liveValue]);
59
- const apply = (0, _react.useCallback)(op => {
60
- // Purely local — this is the whole point. No write, so no event, no
61
- // snapshot, no refresh, and the tree re-renders from state synchronously.
62
- setDraft(prev => (0, _dataViewer.applyJsonOp)(prev, op));
63
- const next = (0, _dataViewer.selectionPathAfterOp)(op);
64
- if (next !== undefined) setSelectPath(next);
65
- }, []);
66
- const selectAt = (0, _react.useCallback)(path => {
67
- setSelectPath(path);
68
- }, []);
69
-
70
- /** Leave edit mode entirely. Only correct on the way out (after a write). */
71
- const reset = (0, _react.useCallback)(() => {
72
- setIsEditing(false);
73
- setDraft(undefined);
74
- setSelectPath(undefined);
75
- setError(null);
76
- }, []);
77
-
78
- /** Re-seed the draft from what's stored, staying in edit mode. */
79
- const discard = (0, _react.useCallback)(() => {
80
- setDraft(liveValue);
81
- setBaseline(JSON.stringify(liveValue));
82
- setSelectPath(undefined);
83
- setError(null);
84
- setIsEditing(true);
85
- }, [liveValue]);
86
- const commit = (0, _react.useCallback)(async () => {
87
- if (!isDirty || !save) {
88
- reset();
89
- return true;
90
- }
91
- setIsSaving(true);
92
- setError(null);
93
- try {
94
- await save(JSON.stringify(draft));
95
- reset();
96
- return true;
97
- } catch (e) {
98
- // Stay in edit mode so the work isn't lost to a failed write.
99
- setError(e instanceof Error ? e.message : String(e));
100
- return false;
101
- } finally {
102
- setIsSaving(false);
103
- }
104
- }, [draft, isDirty, reset, save]);
105
- return {
106
- isEditing,
107
- value,
108
- isDirty,
109
- isSaving,
110
- error,
111
- selectPath,
112
- begin,
113
- apply,
114
- selectAt,
115
- commit,
116
- discard
117
- };
23
+ const saveValue = (0, _react.useMemo)(() => save ? value => save(JSON.stringify(value)) : undefined, [save]);
24
+ return (0, _dataViewer.useTreeDraft)(liveValue, saveValue);
118
25
  }