Skip to content

Commit a141020

Browse files
committed
updated how reducers are rendered on the component map
1 parent 36c6ef3 commit a141020

File tree

5 files changed

+105
-44
lines changed

5 files changed

+105
-44
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,12 @@ export default function ComponentMap({
520520
}
521521
/>
522522
{/* Add this new container for reducer state */}
523-
{/* {tooltipData.componentData.reducerState && (
523+
{tooltipData.componentData.reducerStates && (
524524
<ToolTipDataDisplay
525-
containerName='Reducer State'
526-
dataObj={tooltipData.componentData.reducerState}
525+
containerName='Reducers'
526+
dataObj={tooltipData.componentData.reducerStates}
527527
/>
528-
)} */}
528+
)}
529529
</div>
530530
</div>
531531
</TooltipInPortal>

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

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import React from 'react';
22
import { JSONTree } from 'react-json-tree';
33

4-
/*
5-
Code that show's the tooltip of our JSON tree
6-
*/
7-
84
const colors = {
95
scheme: 'paraiso',
106
author: 'jan t. sott',
@@ -17,8 +13,7 @@ const colors = {
1713
base06: '#b9b6b0',
1814
base07: '#e7e9db',
1915
base08: '#ef6155',
20-
base09: '#824508', //base09 is orange for booleans and numbers. This base in particular fails to match the entered color.
21-
// base09: '#592bad', // alternative purple
16+
base09: '#824508',
2217
base0A: '#fec418',
2318
base0B: '#48b685',
2419
base0C: '#5bc4bf',
@@ -28,32 +23,48 @@ const colors = {
2823
};
2924

3025
const ToolTipDataDisplay = ({ containerName, dataObj }) => {
26+
// If there's no data to display, don't render anything
27+
if (
28+
!dataObj ||
29+
(Array.isArray(dataObj) && dataObj.length === 0) ||
30+
Object.keys(dataObj).length === 0
31+
) {
32+
return null;
33+
}
34+
35+
const formatReducerData = (reducerStates) => {
36+
// Transform the array of reducers into an object with hook names as keys
37+
return reducerStates.reduce((acc, reducer) => {
38+
acc[reducer.hookName || 'Reducer'] = {
39+
state: reducer.state,
40+
lastAction: reducer.lastAction,
41+
};
42+
return acc;
43+
}, {});
44+
};
45+
3146
const printableObject = {};
3247

33-
if (!dataObj) {
34-
printableObject[containerName] = dataObj;
48+
if (containerName === 'Reducers') {
49+
if (!dataObj || dataObj.length === 0) {
50+
return null;
51+
}
52+
printableObject[containerName] = formatReducerData(dataObj);
3553
} else {
54+
// Handle normal state/props
3655
const data = {};
37-
38-
// Handle reducer state if present
39-
if (containerName === 'State' && dataObj.reducerState) {
40-
printableObject[containerName] = dataObj.reducerState;
41-
}
42-
// Otherwise handle normal state/props
43-
else {
44-
for (const key in dataObj) {
45-
if (typeof dataObj[key] === 'string') {
46-
try {
47-
data[key] = JSON.parse(dataObj[key]);
48-
} catch {
49-
data[key] = dataObj[key];
50-
}
51-
} else {
56+
for (const key in dataObj) {
57+
if (typeof dataObj[key] === 'string') {
58+
try {
59+
data[key] = JSON.parse(dataObj[key]);
60+
} catch {
5261
data[key] = dataObj[key];
5362
}
63+
} else {
64+
data[key] = dataObj[key];
5465
}
55-
printableObject[containerName] = data;
5666
}
67+
printableObject[containerName] = data;
5768
}
5869

5970
return (

src/backend/controllers/createTree.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ export default function createTree(currentFiberNode: Fiber): Tree {
207207
if (memoizedState.queue) {
208208
try {
209209
const hooksStates = getHooksStateAndUpdateMethod(memoizedState);
210+
console.log(hooksStates);
210211
const hooksNames = getHooksNames(elementType.toString());
212+
console.log(hooksNames);
211213

212214
componentData.hooksState = {};
213215
componentData.reducerStates = []; // New array to store reducer states
@@ -222,7 +224,7 @@ export default function createTree(currentFiberNode: Fiber): Tree {
222224
state,
223225
lastAction,
224226
reducerIndex: i,
225-
hookName: hooksNames[i]?.varName || `Reducer: ${i}`,
227+
hookName: hooksNames[i]?.hookName || `Reducer ${i}`,
226228
});
227229
} else {
228230
// Regular useState hook
@@ -233,6 +235,7 @@ export default function createTree(currentFiberNode: Fiber): Tree {
233235
...componentData.hooksState,
234236
reducers: componentData.reducerStates,
235237
};
238+
console.log('new state', newState);
236239
} catch (err) {
237240
console.log('Error extracting component state:', {
238241
componentName,

src/backend/controllers/statePropExtractors.ts

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,38 +129,80 @@ export function getHooksNames(elementType: string): { hookName: string; varName:
129129
return null;
130130
};
131131

132+
const extractReducerName = (node: any): string | null => {
133+
if (node.type === 'Identifier') {
134+
return node.name;
135+
}
136+
137+
if (node.type === 'ArrowFunctionExpression' && node.body.type === 'CallExpression') {
138+
if (node.body.callee.type === 'Identifier') {
139+
return node.body.callee.name;
140+
}
141+
}
142+
143+
return null;
144+
};
145+
132146
function traverse(node: Node) {
133147
if (!node) return;
134148

135149
if (node.type === 'VariableDeclaration') {
136150
node.declarations.forEach((declaration) => {
137151
if (declaration.init?.type === 'CallExpression') {
138-
// Check for Webpack transformed pattern: (0, react__WEBPACK_IMPORTED_MODULE_0__.useState)(0)
152+
// Check for Webpack transformed pattern
139153
const isWebpackPattern =
140154
declaration.init.callee?.type === 'SequenceExpression' &&
141-
declaration.init.callee.expressions?.length === 2 &&
142-
declaration.init.callee.expressions[1]?.type === 'MemberExpression' &&
143-
declaration.init.callee.expressions[1].property &&
144-
isIdentifierWithName(declaration.init.callee.expressions[1].property, 'useState');
155+
declaration.init.callee.expressions?.[1]?.type === 'MemberExpression';
156+
157+
// Get the hook name for Webpack pattern
158+
let webpackHookName: string | null = null;
159+
if (isWebpackPattern && declaration.init.callee?.type === 'SequenceExpression') {
160+
const memberExpr = declaration.init.callee.expressions[1] as any;
161+
if (memberExpr?.property?.type === 'Identifier') {
162+
webpackHookName = memberExpr.property.name;
163+
}
164+
}
145165

146-
// Check for direct useState pattern: useState("test")
166+
// Check for direct pattern
167+
const directCallee = declaration.init.callee as any;
147168
const isDirectPattern =
148-
declaration.init.callee?.type === 'Identifier' &&
149-
declaration.init.callee.name === 'useState';
169+
directCallee?.type === 'Identifier' &&
170+
(directCallee.name === 'useState' || directCallee.name === 'useReducer');
150171

151-
// Check for namespaced useState pattern: React.useState("test")
172+
// Check for namespaced pattern
152173
const isNamespacedPattern =
153174
declaration.init.callee?.type === 'MemberExpression' &&
154-
declaration.init.callee.property &&
155-
isIdentifierWithName(declaration.init.callee.property, 'useState');
175+
(declaration.init.callee as any).property?.type === 'Identifier' &&
176+
((declaration.init.callee as any).property.name === 'useState' ||
177+
(declaration.init.callee as any).property.name === 'useReducer');
156178

157179
if (isWebpackPattern || isDirectPattern || isNamespacedPattern) {
158180
const arrayPattern = processArrayPattern(declaration.id);
159181
if (arrayPattern) {
160-
statements.push({
161-
hookName: arrayPattern.setter,
162-
varName: arrayPattern.getter,
163-
});
182+
const isReducer =
183+
webpackHookName === 'useReducer' ||
184+
(isDirectPattern && directCallee?.name === 'useReducer') ||
185+
(isNamespacedPattern &&
186+
(declaration.init.callee as any).property?.name === 'useReducer');
187+
188+
if (isReducer) {
189+
// Handle useReducer
190+
if (declaration.init.arguments?.length > 0) {
191+
const reducerName = extractReducerName(declaration.init.arguments[0]);
192+
if (reducerName) {
193+
statements.push({
194+
hookName: reducerName,
195+
varName: arrayPattern.getter,
196+
});
197+
}
198+
}
199+
} else {
200+
// Handle useState
201+
statements.push({
202+
hookName: arrayPattern.setter,
203+
varName: arrayPattern.getter,
204+
});
205+
}
164206
}
165207
}
166208
}

src/backend/controllers/timeJump.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ async function updateReactFiberTree(
9494
for (let i = 0; i < stateEntries.length; i++) {
9595
const [key, value] = stateEntries[i];
9696
if (functionalComponent[i]?.dispatch) {
97+
console.log('Dispatching state update:', {
98+
key,
99+
value,
100+
});
97101
await functionalComponent[i].dispatch(value);
98102
} else {
99103
console.warn(`No dispatch found for hook ${i}:`, {
@@ -112,6 +116,7 @@ async function updateReactFiberTree(
112116
for (const reducerState of reducerStates) {
113117
const { state, reducerIndex } = reducerState;
114118
const reducer = functionalComponent[reducerIndex];
119+
console.log('reducer', reducer);
115120

116121
if (reducer?.dispatch) {
117122
console.log('Dispatching reducer update:', {

0 commit comments

Comments
 (0)