From ee0d2e779ee10d8f85b7bc36def6b2939b9eedd7 Mon Sep 17 00:00:00 2001 From: Chris Combe <24692334+ccombe@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:18:19 +1100 Subject: [PATCH] Build every slice initial state from its own factory initial.ts still inlined open, focus, and edit while drag, selection, and dnd already came from factories. Each store now gets those three from the slice factories too. Co-authored-by: Cursor --- modules/react-arborist/src/state/edit-slice.ts | 4 +++- modules/react-arborist/src/state/focus-slice.ts | 4 +++- modules/react-arborist/src/state/initial.ts | 9 ++++++--- modules/react-arborist/src/state/open-slice.ts | 7 ++++++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/modules/react-arborist/src/state/edit-slice.ts b/modules/react-arborist/src/state/edit-slice.ts index 297f620d..6df9f2c9 100644 --- a/modules/react-arborist/src/state/edit-slice.ts +++ b/modules/react-arborist/src/state/edit-slice.ts @@ -1,13 +1,15 @@ /* Types */ export type EditState = { id: string | null }; +export const initialEditState = (): EditState => ({ id: null }); + /* Actions */ export function edit(id: string | null) { return { type: "EDIT" as const, id }; } /* Reducer */ -export function reducer(state: EditState = { id: null }, action: ReturnType) { +export function reducer(state: EditState = initialEditState(), action: ReturnType) { if (action.type === "EDIT") { return { ...state, id: action.id }; } else { diff --git a/modules/react-arborist/src/state/focus-slice.ts b/modules/react-arborist/src/state/focus-slice.ts index d38377c9..fe23154f 100644 --- a/modules/react-arborist/src/state/focus-slice.ts +++ b/modules/react-arborist/src/state/focus-slice.ts @@ -2,6 +2,8 @@ export type FocusState = { id: string | null; treeFocused: boolean }; +export const initialFocusState = (): FocusState => ({ id: null, treeFocused: false }); + /* Actions */ export function focus(id: string | null) { @@ -15,7 +17,7 @@ export function treeBlur() { /* Reducer */ export function reducer( - state: FocusState = { id: null, treeFocused: false }, + state: FocusState = initialFocusState(), action: ReturnType | ReturnType, ) { if (action.type === "FOCUS") { diff --git a/modules/react-arborist/src/state/initial.ts b/modules/react-arborist/src/state/initial.ts index 055198c8..addff19e 100644 --- a/modules/react-arborist/src/state/initial.ts +++ b/modules/react-arborist/src/state/initial.ts @@ -2,14 +2,17 @@ import type { TreeProps } from "../types/tree-props.js"; import type { RootState } from "./root-reducer.js"; import { initialDndState } from "./dnd-slice.js"; import { initialDragState } from "./drag-slice.js"; +import { initialEditState } from "./edit-slice.js"; +import { initialFocusState } from "./focus-slice.js"; +import { initialOpenState } from "./open-slice.js"; import { initialSelectionState } from "./selection-slice.js"; export const initialState = (props?: TreeProps): RootState => ({ nodes: { // Changes together - open: { filtered: {}, unfiltered: props?.initialOpenState ?? {} }, - focus: { id: null, treeFocused: false }, - edit: { id: null }, + open: initialOpenState(props?.initialOpenState), + focus: initialFocusState(), + edit: initialEditState(), drag: initialDragState(), selection: initialSelectionState(), }, diff --git a/modules/react-arborist/src/state/open-slice.ts b/modules/react-arborist/src/state/open-slice.ts index 5bf9766a..f0e5a2a6 100644 --- a/modules/react-arborist/src/state/open-slice.ts +++ b/modules/react-arborist/src/state/open-slice.ts @@ -4,6 +4,11 @@ import { ActionTypes } from "../types/utils.js"; export type OpenMap = { [id: string]: boolean }; export type OpenSlice = { unfiltered: OpenMap; filtered: OpenMap }; +export const initialOpenState = (unfiltered: OpenMap = {}): OpenSlice => ({ + filtered: {}, + unfiltered, +}); + /* Actions */ export const actions = { open(id: string, filtered: boolean) { @@ -38,7 +43,7 @@ function openMapReducer(state: OpenMap = {}, action: ActionTypes } export function reducer( - state: OpenSlice = { filtered: {}, unfiltered: {} }, + state: OpenSlice = initialOpenState(), action: ActionTypes, ): OpenSlice { if (!action.type.startsWith("VISIBILITY")) return state;