Skip to content

Commit 5a25feb

Browse files
committed
parsed tree to exclude context providers and consumers
1 parent 784f646 commit 5a25feb

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/app/components/StateRoute/ComponentMap/ComponentMap.tsx

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,85 @@ export default function ComponentMap({
172172
}
173173
};
174174

175-
collectNodes(currentSnapshot);
175+
const shouldIncludeNode = (node) => {
176+
// Return false if node has any context properties
177+
if (node?.componentData?.context && Object.keys(node.componentData.context).length > 0) {
178+
return false;
179+
}
180+
// Return false if node name ends with 'Provider'
181+
if (node?.name && node.name.endsWith('Provider')) {
182+
return false;
183+
}
184+
return true;
185+
};
186+
187+
const processTreeData = (node) => {
188+
if (!node) return null;
189+
190+
// Create a new node
191+
const newNode = { ...node };
192+
193+
if (node.children) {
194+
// Process all children first
195+
const processedChildren = node.children
196+
.map((child) => processTreeData(child))
197+
.filter(Boolean); // Remove null results
198+
199+
// For each child that shouldn't be included, replace it with its children
200+
newNode.children = processedChildren.reduce((acc, child) => {
201+
if (shouldIncludeNode(child)) {
202+
// If child should be included, add it directly
203+
acc.push(child);
204+
} else {
205+
// If child should be filtered out, add its children instead
206+
if (child.children) {
207+
acc.push(...child.children);
208+
}
209+
}
210+
return acc;
211+
}, []);
212+
}
213+
214+
return newNode;
215+
};
216+
217+
let filtered = processTreeData(currentSnapshot);
218+
console.log('filtered', filtered);
219+
collectNodes(filtered);
220+
221+
const keepContextAndProviderNodes = (node) => {
222+
if (!node) return null;
223+
224+
// Check if this node should be kept
225+
const hasContext =
226+
node?.componentData?.context && Object.keys(node.componentData.context).length > 0;
227+
const isProvider = node?.name && node.name.endsWith('Provider');
228+
const shouldKeepNode = hasContext || isProvider;
229+
230+
// Process children first
231+
let processedChildren = [];
232+
if (node.children) {
233+
processedChildren = node.children
234+
.map((child) => keepContextAndProviderNodes(child))
235+
.filter(Boolean); // Remove null results
236+
}
237+
238+
// If this node should be kept or has kept children, return it
239+
if (shouldKeepNode || processedChildren.length > 0) {
240+
return {
241+
...node,
242+
children: processedChildren,
243+
};
244+
}
245+
246+
// If neither the node should be kept nor it has kept children, filter it out
247+
return null;
248+
};
249+
250+
const contextProvidersOnly = keepContextAndProviderNodes(currentSnapshot);
251+
console.log('context only', contextProvidersOnly);
252+
253+
collectNodes(filtered);
176254

177255
// @ts
178256
// find the node that has been selected and use it as the root

src/backend/controllers/createTree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ export default function createTree(currentFiberNode: Fiber): Tree {
244244
}
245245
if (tag === ContextProvider && !elementType._context.displayName) {
246246
let stateData = memoizedProps.value;
247+
console.log(stateData);
247248
if (stateData === null || typeof stateData !== 'object') {
248249
stateData = { CONTEXT: stateData };
249250
}

0 commit comments

Comments
 (0)