|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import React, { createContext, useContext, useState, useCallback, useMemo } from 'react'; |
| 10 | +import type { PageVariable } from '@object-ui/types'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Page variables context value. |
| 14 | + * Provides access to page-level state variables. |
| 15 | + */ |
| 16 | +export interface PageVariablesContextValue { |
| 17 | + /** Current variable values */ |
| 18 | + variables: Record<string, any>; |
| 19 | + /** Set a single variable value */ |
| 20 | + setVariable: (name: string, value: any) => void; |
| 21 | + /** Set multiple variable values at once */ |
| 22 | + setVariables: (updates: Record<string, any>) => void; |
| 23 | + /** Reset all variables to their default values */ |
| 24 | + resetVariables: () => void; |
| 25 | +} |
| 26 | + |
| 27 | +const PageVariablesContext = createContext<PageVariablesContextValue | null>(null); |
| 28 | + |
| 29 | +/** |
| 30 | + * Initialize page variables from their definitions. |
| 31 | + * Sets each variable to its defaultValue or type-appropriate default. |
| 32 | + */ |
| 33 | +function initializeVariables(definitions?: PageVariable[]): Record<string, any> { |
| 34 | + if (!definitions || definitions.length === 0) return {}; |
| 35 | + |
| 36 | + const initial: Record<string, any> = {}; |
| 37 | + for (const def of definitions) { |
| 38 | + if (def.defaultValue !== undefined) { |
| 39 | + initial[def.name] = def.defaultValue; |
| 40 | + } else { |
| 41 | + // Type-appropriate defaults |
| 42 | + switch (def.type) { |
| 43 | + case 'number': |
| 44 | + initial[def.name] = 0; |
| 45 | + break; |
| 46 | + case 'boolean': |
| 47 | + initial[def.name] = false; |
| 48 | + break; |
| 49 | + case 'object': |
| 50 | + initial[def.name] = {}; |
| 51 | + break; |
| 52 | + case 'array': |
| 53 | + initial[def.name] = []; |
| 54 | + break; |
| 55 | + case 'string': |
| 56 | + default: |
| 57 | + initial[def.name] = ''; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return initial; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Props for PageVariablesProvider |
| 67 | + */ |
| 68 | +export interface PageVariablesProviderProps { |
| 69 | + /** Variable definitions from PageSchema.variables */ |
| 70 | + definitions?: PageVariable[]; |
| 71 | + /** Child components */ |
| 72 | + children: React.ReactNode; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * PageVariablesProvider — Provides page-level state variables to the component tree. |
| 77 | + * |
| 78 | + * Initializes variables from their definitions and provides read/write access |
| 79 | + * to all child components via the usePageVariables hook. |
| 80 | + * |
| 81 | + * @example |
| 82 | + * ```tsx |
| 83 | + * <PageVariablesProvider definitions={[ |
| 84 | + * { name: 'selectedId', type: 'string', defaultValue: '' }, |
| 85 | + * { name: 'count', type: 'number', defaultValue: 0 }, |
| 86 | + * ]}> |
| 87 | + * <MyComponents /> |
| 88 | + * </PageVariablesProvider> |
| 89 | + * ``` |
| 90 | + */ |
| 91 | +export const PageVariablesProvider: React.FC<PageVariablesProviderProps> = ({ |
| 92 | + definitions, |
| 93 | + children, |
| 94 | +}) => { |
| 95 | + const [variables, setVariablesState] = useState<Record<string, any>>(() => |
| 96 | + initializeVariables(definitions) |
| 97 | + ); |
| 98 | + |
| 99 | + const setVariable = useCallback((name: string, value: any) => { |
| 100 | + setVariablesState((prev) => ({ ...prev, [name]: value })); |
| 101 | + }, []); |
| 102 | + |
| 103 | + const setVariables = useCallback((updates: Record<string, any>) => { |
| 104 | + setVariablesState((prev) => ({ ...prev, ...updates })); |
| 105 | + }, []); |
| 106 | + |
| 107 | + const resetVariables = useCallback(() => { |
| 108 | + setVariablesState(initializeVariables(definitions)); |
| 109 | + }, [definitions]); |
| 110 | + |
| 111 | + const value = useMemo<PageVariablesContextValue>( |
| 112 | + () => ({ variables, setVariable, setVariables, resetVariables }), |
| 113 | + [variables, setVariable, setVariables, resetVariables] |
| 114 | + ); |
| 115 | + |
| 116 | + return ( |
| 117 | + <PageVariablesContext.Provider value={value}> |
| 118 | + {children} |
| 119 | + </PageVariablesContext.Provider> |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +PageVariablesProvider.displayName = 'PageVariablesProvider'; |
| 124 | + |
| 125 | +/** |
| 126 | + * Hook to access page-level variables. |
| 127 | + * |
| 128 | + * Returns the current variable values and setter functions. |
| 129 | + * Returns a no-op fallback if used outside a PageVariablesProvider. |
| 130 | + * |
| 131 | + * @example |
| 132 | + * ```tsx |
| 133 | + * const { variables, setVariable } = usePageVariables(); |
| 134 | + * const userId = variables.selectedId; |
| 135 | + * setVariable('selectedId', '123'); |
| 136 | + * ``` |
| 137 | + */ |
| 138 | +export function usePageVariables(): PageVariablesContextValue { |
| 139 | + const ctx = useContext(PageVariablesContext); |
| 140 | + if (!ctx) { |
| 141 | + // Graceful fallback — allows components to work outside a Page |
| 142 | + return { |
| 143 | + variables: {}, |
| 144 | + setVariable: () => {}, |
| 145 | + setVariables: () => {}, |
| 146 | + resetVariables: () => {}, |
| 147 | + }; |
| 148 | + } |
| 149 | + return ctx; |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Hook to check if a PageVariablesProvider is available. |
| 154 | + */ |
| 155 | +export function useHasPageVariables(): boolean { |
| 156 | + return useContext(PageVariablesContext) !== null; |
| 157 | +} |
0 commit comments