Lift duplicated patterns from CollectionNode/ValueNodeWrapper into useCommon#312
Merged
Merged
Conversation
Copilot
AI
changed the title
[WIP] Refactor reduce duplication between CollectionNode and ValueNodeWrapper
Lift duplicated patterns from CollectionNode/ValueNodeWrapper into useCommon
Jun 3, 2026
Bundle size impact
|
| Format | Base raw | PR raw | Δ raw | Base gzip | PR gzip | Δ gzip |
|---|---|---|---|---|---|---|
| esm | 54.73 KB | 54.70 KB | 🟢 -33 B (-0.06%) | 18.87 KB | 18.95 KB | 🔺 +86 B (+0.45%) |
| cjs | 56.19 KB | 56.18 KB | 🟢 -15 B (-0.03%) | 18.91 KB | 18.97 KB | 🔺 +61 B (+0.31%) |
Measured from build/index.{cjs,esm}.js. Gzip at level 9.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CollectionNode.tsxandValueNodeWrapper.tsxhad several copy-pasted patterns (emitEditEvent, the three-branchonEdit/onAdd/onDeleteresult dance,keyDisplayProps, thepreviousValuerevert), creating drift risk and bundle weight. Consolidated intouseCommon.Changes
useCommon— destructureonEdit,sort,arrayIndexFromOne,handleKeyboard,customNodeDatafrom props, plussetPreviousValue/getSnapshotfrom the editing store. Expose:emitEditEvent(event, extra?)— replaces the two inlined closures and the four hand-written shapes insidehandleEditKey.handleMutationResult({ result, errorCode, errorValue, cancelEvent?, confirmEvent?, confirmExtra?, onRevert?, onErrorExtra?, onConfirmExtra? })— collapses ~8.then(result => …)branches. Optional callbacks preserve per-site nuances:revertToDataon the value-node paths, the extracloseEdit()on the enum/standard error branch inhandleChangeDataType, the success-onlysetEnumType(null).getNextOrPreviousAtPath(type)— stable wrapper used byKeyDisplay,tabForward/tabBack, and the Tab-redirectuseLayoutEffect.revertPreviousValue()— returnstruewhen a type-change snapshot was applied, soValueNodeWrapper.revertSessioncan fall back torevertToData()andCollectionNode.handleCancelcan simply call it.buildKeyDisplayProps({ handleCancel, getStyles, keyValueArray?, handleClick? })— single source for the ~13 shared fields; collection addskeyValueArray+handleClick.CollectionNode.tsx—handleEdit/handleAdd(object + array) /handleDeletenow callhandleMutationResult;handleCancelcallsrevertPreviousValue;keyDisplayPropscallsbuildKeyDisplayProps. Drops unused imports (EditEvent,getNextOrPrevious) and prop destructures (onEditEvent,arrayIndexFromOne,getSnapshot).ValueNodeWrapper.tsx— both type-change branches plushandleEdit/handleDeleteusehandleMutationResult;revertSessionbecomesif (!revertPreviousValue()) revertToData(); both Tab callbacks and the redirect effect usegetNextOrPreviousAtPath. Drops unused imports/destructures (EditEvent,getNextOrPrevious,onEditEvent,pathString,arrayIndexFromOne,sort, etc.).Example
Before, in both files:
After:
No behavioural change — all 294 existing tests pass unmodified.