Skip to content

Commit f6dab23

Browse files
Merge branch 'dev' into feature/ragad
2 parents 22e1fbc + 2603f35 commit f6dab23

File tree

12 files changed

+615
-110
lines changed

12 files changed

+615
-110
lines changed

demo-app/src/client/Components/FunctionalReducerCounter.tsx

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ type CounterAction =
2323
| { type: 'RESET' }
2424
| { type: 'ADD'; payload: number };
2525

26+
// New secondary reducer state and action types
27+
type SecondaryCounterState = {
28+
count: number;
29+
multiplier: number;
30+
lastOperation: string;
31+
};
32+
33+
type SecondaryCounterAction =
34+
| { type: 'MULTIPLY' }
35+
| { type: 'DIVIDE' }
36+
| { type: 'SET_MULTIPLIER'; payload: number }
37+
| { type: 'RESET' };
38+
2639
function counterReducer(state: CounterState, action: CounterAction, step: number): CounterState {
2740
switch (action.type) {
2841
case 'INCREMENT':
@@ -64,6 +77,41 @@ function counterReducer(state: CounterState, action: CounterAction, step: number
6477
}
6578
}
6679

80+
// New secondary reducer function
81+
function secondaryCounterReducer(
82+
state: SecondaryCounterState,
83+
action: SecondaryCounterAction,
84+
): SecondaryCounterState {
85+
switch (action.type) {
86+
case 'MULTIPLY':
87+
return {
88+
...state,
89+
count: state.count * state.multiplier,
90+
lastOperation: `Multiplied by ${state.multiplier}`,
91+
};
92+
case 'DIVIDE':
93+
return {
94+
...state,
95+
count: state.count / state.multiplier,
96+
lastOperation: `Divided by ${state.multiplier}`,
97+
};
98+
case 'SET_MULTIPLIER':
99+
return {
100+
...state,
101+
multiplier: action.payload,
102+
lastOperation: `Set multiplier to ${action.payload}`,
103+
};
104+
case 'RESET':
105+
return {
106+
count: 0,
107+
multiplier: 2,
108+
lastOperation: 'Reset',
109+
};
110+
default:
111+
return state;
112+
}
113+
}
114+
67115
function FunctionalReducerCounter({
68116
initialCount = 0,
69117
step = 1,
@@ -76,6 +124,8 @@ function FunctionalReducerCounter({
76124
const [clickCount, setClickCount] = useState(0);
77125
const [lastClickTime, setLastClickTime] = useState<Date | null>(null);
78126
const [averageTimeBetweenClicks, setAverageTimeBetweenClicks] = useState<number>(0);
127+
128+
// First reducer
79129
const [state, dispatch] = useReducer(
80130
(state: CounterState, action: CounterAction) => counterReducer(state, action, step),
81131
{
@@ -85,6 +135,13 @@ function FunctionalReducerCounter({
85135
},
86136
);
87137

138+
// Second reducer
139+
const [secondaryState, secondaryDispatch] = useReducer(secondaryCounterReducer, {
140+
count: initialCount,
141+
multiplier: 2,
142+
lastOperation: 'none',
143+
});
144+
88145
return (
89146
<div
90147
className='reducer-counter'
@@ -94,8 +151,10 @@ function FunctionalReducerCounter({
94151
}}
95152
>
96153
<h2>{title}</h2>
154+
155+
{/* Primary Counter Section */}
97156
<div className='counter-value'>
98-
<h3>Current Count: {state.count}</h3>
157+
<h3>Primary Counter: {state.count}</h3>
99158
</div>
100159

101160
<div className='counter-buttons'>
@@ -118,7 +177,36 @@ function FunctionalReducerCounter({
118177
))}
119178
</div>
120179
</div>
180+
181+
{/* Secondary Counter Section */}
182+
<div
183+
className='secondary-counter'
184+
style={{ marginTop: '2rem', borderTop: '1px solid #ccc', paddingTop: '1rem' }}
185+
>
186+
<h3>Secondary Counter: {secondaryState.count}</h3>
187+
<div className='counter-buttons'>
188+
<button onClick={() => secondaryDispatch({ type: 'MULTIPLY' })}>
189+
Multiply by {secondaryState.multiplier}
190+
</button>
191+
<button onClick={() => secondaryDispatch({ type: 'DIVIDE' })}>
192+
Divide by {secondaryState.multiplier}
193+
</button>
194+
<button
195+
onClick={() =>
196+
secondaryDispatch({ type: 'SET_MULTIPLIER', payload: secondaryState.multiplier + 1 })
197+
}
198+
>
199+
Increase Multiplier
200+
</button>
201+
<button onClick={() => secondaryDispatch({ type: 'RESET' })}>Reset</button>
202+
</div>
203+
<div className='counter-info'>
204+
<h4>Last Operation: {secondaryState.lastOperation}</h4>
205+
<h4>Current Multiplier: {secondaryState.multiplier}</h4>
206+
</div>
207+
</div>
121208
</div>
122209
);
123210
}
211+
124212
export default FunctionalReducerCounter;

demo-app/src/client/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
4-
<title>Reactime</title>
4+
<title>Demo App</title>
55
<!-- <link rel="icon" type="image/x-icon" href="../../../assets/logos/blackWhiteSquareIcon.png"> -->
6-
<link rel="stylesheet" type="text/css" href="./style.css">
6+
<link rel="stylesheet" type="text/css" href="./style.css" />
77
</head>
88
<body>
99
<main id="root"></main>

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: 34 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,47 @@ 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+
};
41+
return acc;
42+
}, {});
43+
};
44+
3145
const printableObject = {};
3246

33-
if (!dataObj) {
34-
printableObject[containerName] = dataObj;
47+
if (containerName === 'Reducers') {
48+
if (!dataObj || dataObj.length === 0) {
49+
return null;
50+
}
51+
printableObject[containerName] = formatReducerData(dataObj);
3552
} else {
53+
// Handle normal state/props
3654
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 {
55+
for (const key in dataObj) {
56+
if (typeof dataObj[key] === 'string') {
57+
try {
58+
data[key] = JSON.parse(dataObj[key]);
59+
} catch {
5260
data[key] = dataObj[key];
5361
}
62+
} else {
63+
data[key] = dataObj[key];
5464
}
55-
printableObject[containerName] = data;
5665
}
66+
printableObject[containerName] = data;
5767
}
5868

5969
return (

src/backend/controllers/createTree.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,30 +207,34 @@ export default function createTree(currentFiberNode: Fiber): Tree {
207207
if (memoizedState.queue) {
208208
try {
209209
const hooksStates = getHooksStateAndUpdateMethod(memoizedState);
210-
console.log('hook states', hooksStates);
211-
// Get the hooks names by parsing the elementType
212210
const hooksNames = getHooksNames(elementType.toString());
213-
// Intialize state & index:
211+
214212
componentData.hooksState = {};
215-
componentData.reducerState = null;
213+
componentData.reducerStates = []; // New array to store reducer states
216214
componentData.hooksIndex = [];
217215

218-
hooksStates.forEach(({ state, component, isReducer }, i) => {
216+
hooksStates.forEach(({ state, component, isReducer, lastAction, reducer }, i) => {
219217
componentData.hooksIndex.push(componentActionsRecord.saveNew(component));
220218

221219
if (isReducer) {
222-
// If it's a reducer, store its state
223-
componentData.reducerState = state;
220+
// Store reducer-specific information
221+
componentData.reducerStates.push({
222+
state,
223+
lastAction,
224+
reducerIndex: i,
225+
hookName: hooksNames[i]?.hookName || `Reducer ${i}`,
226+
});
227+
} else {
228+
// Regular useState hook
229+
componentData.hooksState[hooksNames[i]?.varName || `State: ${i}`] = state;
224230
}
225-
// Otherwise treat as useState
226-
componentData.hooksState[hooksNames[i]?.varName || `Reducer: ${i}`] = state;
227231
});
228-
229-
// Pass to front end
230-
newState = componentData.hooksState;
231-
console.log('new state', newState);
232+
newState = {
233+
...componentData.hooksState,
234+
reducers: componentData.reducerStates,
235+
};
232236
} catch (err) {
233-
console.log('Error extracting functional component state:', {
237+
console.log('Error extracting component state:', {
234238
componentName,
235239
memoizedState,
236240
error: err,

0 commit comments

Comments
 (0)