1
- import React from "react" ;
1
+ import { width } from '@mui/system' ;
2
+ import React , { useState } from 'react' ;
3
+ import { ProvConContainerProps } from '../FrontendTypes' ;
2
4
3
- const ProvConContainer = ( ) : JSX . Element => {
4
- return (
5
- < div >
6
- hello
5
+
6
+ const ProvConContainer = ( props : ProvConContainerProps ) : JSX . Element => {
7
+
8
+ const { currentSnapshot } = props
9
+
10
+ console . log ( 'currentSnapshot' , currentSnapshot )
11
+
12
+ const keepContextAndProviderNodes = ( node ) => {
13
+ if ( ! node ) return null ;
14
+
15
+ // Check if this node should be kept
16
+ const hasContext =
17
+ node ?. componentData ?. context && Object . keys ( node . componentData . context ) . length > 0 ;
18
+ const isProvider = node ?. name && node . name . endsWith ( 'Provider' ) ;
19
+ const shouldKeepNode = hasContext || isProvider ;
20
+
21
+ // Process children first
22
+ let processedChildren = [ ] ;
23
+ if ( node . children ) {
24
+ processedChildren = node . children
25
+ . map ( ( child ) => keepContextAndProviderNodes ( child ) )
26
+ . filter ( Boolean ) ; // Remove null results
27
+ }
28
+
29
+ // If this node should be kept or has kept children, return it
30
+ if ( shouldKeepNode || processedChildren . length > 0 ) {
31
+ return {
32
+ ...node ,
33
+ children : processedChildren ,
34
+ } ;
35
+ }
36
+
37
+ // If neither the node should be kept nor it has kept children, filter it out
38
+ return null ;
39
+ } ;
40
+ const contextProvidersOnly = keepContextAndProviderNodes ( currentSnapshot ) ;
41
+ console . log ( 'context only' , contextProvidersOnly )
42
+
43
+ const [ visibleKeys , setVisibleKeys ] = useState ( { } ) ;
44
+
45
+ const toggleInfo = ( key :string ) => {
46
+ setVisibleKeys ( ( prev ) => ( {
47
+ ...prev , //copies all current keys and their visibility from prev object
48
+ [ key ] : ! prev [ key ] // adds or updates the key in the visibleKeys object.
49
+ } ) )
50
+ } ;
51
+ const someInfo = {
52
+ ThemeProvider : {
53
+ UserProvider : {
54
+ UserContext : 'some info herecdxjchffhdjhfdfdfhdjhfdjhfdjhfvhhhh' ,
55
+ } ,
56
+ ThemeContext1 : {
57
+ Nested : 'some info here' } ,
58
+ ThemeConext2 : 'some info here'
59
+ } ,
60
+ }
61
+
62
+
63
+ // Recursive function to render nested objects
64
+ const renderNestedObjec = ( obj , parentKey = '' ) => { //parentKey is unique keys that represent the hierarchy of the object.
65
+ return Object . keys ( obj ) . map ( ( key ) => {
66
+ const fullKey = parentKey ? `${ parentKey } .${ key } ` : key ; // if parentKey does not exisit --> fullKey = key, else fullKey=ParentKey.Key
67
+ return (
68
+ // each key is rendered as a div
69
+ < div key = { fullKey } style = { { width : '100%' , backgroundColor : 'red' } } >
70
+ < div
71
+ style = { { color : 'blue' , cursor : 'pointer' } }
72
+ onClick = { ( ) => toggleInfo ( fullKey ) } // Pass the unique key to toggle visibility
73
+ >
74
+ < strong > { key } </ strong >
75
+ </ div >
76
+ { visibleKeys [ fullKey ] && // Check if the key is visible
77
+ ( typeof obj [ key ] === 'object' //check if the value of the key is an object
78
+ ? renderNestedObjec ( obj [ key ] , fullKey ) // Recursive rendering for nested objects
79
+ //if value is not an object
80
+ : < div > { obj [ key ] } </ div > ) }
7
81
</ div >
8
- )
9
- }
82
+ ) ;
83
+ } ) ;
84
+ } ;
85
+
86
+ return < div style = { { width : '100%' } } > { renderNestedObjec ( someInfo ) } </ div > ;
87
+ } ;
88
+
10
89
11
90
export default ProvConContainer ;
0 commit comments