1 import { createContext } from "react"
2
3 /**
4 * Bridges the `LayoutComposer`'s edit session to nested, self-contained
5 * sortables rendered inside its entries (e.g. a nav group's child links).
6 * Nested orders are stored in the same edit `draft` — keyed by their own ids —
7 * so they are drafted, saved, and canceled together with the top-level layout,
8 * and respect the personal/default scope. Each entry only ever orders its own
9 * children, which keeps reordering scoped (no cross-group moves).
10 */
11 export type LayoutEditContextValue = {
12 /** Whether the surrounding composer is currently in edit mode. */
13 editMode: boolean
14 /**
15 * Returns `children` sorted by the order stored for each child's id in the
16 * active preference (draft while editing, persisted otherwise). A stable sort
17 * keeps unset entries in source order.
18 */
19 orderChildren: <T>(children: T[], getId: (child: T) => string) => T[]
20 /** Persists `orderedIds` (index = order) for these children into the draft. */
21 setChildrenOrder: (orderedIds: string[]) => void
22 /**
23 * Whether the entry/child with this id is hidden in the active preference.
24 * Lets nested children be hidden individually, keyed by their own id in the
25 * same preference map as top-level entries.
26 */
27 isHidden: (id: string) => boolean
28 /** Toggles the hidden flag for this id in the draft. */
29 toggleHidden: (id: string) => void
30 }
31
32 export const LayoutEditContext = createContext<LayoutEditContextValue>({
33 editMode: false,
34 orderChildren: (children) => children,
35 setChildrenOrder: () => {},
36 isHidden: () => false,
37 toggleHidden: () => {},
38 })