-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSchemaRendererContext.tsx
More file actions
43 lines (37 loc) · 1.17 KB
/
SchemaRendererContext.tsx
File metadata and controls
43 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React, { createContext, useContext, useMemo } from 'react';
interface SchemaRendererContextType {
dataSource: any;
debug?: boolean;
}
const SchemaRendererContext = createContext<SchemaRendererContextType | null>(null);
export { SchemaRendererContext };
export const SchemaRendererProvider = ({
children,
dataSource,
debug
}: {
children: React.ReactNode;
dataSource: any;
debug?: boolean
}) => {
const value = useMemo(() => ({ dataSource, debug }), [dataSource, debug]);
return (
<SchemaRendererContext.Provider value={value}>
{children}
</SchemaRendererContext.Provider>
);
};
export const useSchemaContext = () => {
const context = useContext(SchemaRendererContext);
if (!context) {
throw new Error('useSchemaContext must be used within a SchemaRendererProvider');
}
return context;
};
export const useDataScope = (path?: string) => {
const context = useContext(SchemaRendererContext);
const dataSource = context?.dataSource;
if (!dataSource || !path) return dataSource;
// Simple path resolution for now. In real app might be more complex
return path.split('.').reduce((acc, part) => acc && acc[part], dataSource);
}