Skip to content

Commit 5701e79

Browse files
committed
added useContext to demo app
1 parent 6f28fb4 commit 5701e79

File tree

7 files changed

+210
-31
lines changed

7 files changed

+210
-31
lines changed

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

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,74 @@
11
import React from 'react';
2+
import { useTheme } from '../../contexts/ThemeContext';
3+
import { useAuth } from '../../contexts/AuthContext';
24

35
function Home(): JSX.Element {
6+
const { theme } = useTheme();
7+
const { user, login, logout } = useAuth();
8+
49
return (
5-
<div className='about'>
10+
<div
11+
className='about'
12+
style={{
13+
backgroundColor: theme.backgroundColor,
14+
color: theme.textColor,
15+
}}
16+
>
617
<h2>REACTIME - DEMO APP</h2>
18+
19+
{user ? (
20+
<div>
21+
<p>Welcome, {user.username}!</p>
22+
<button
23+
onClick={logout}
24+
style={{
25+
backgroundColor: theme.primaryColor,
26+
color: theme.backgroundColor,
27+
padding: '8px 16px',
28+
border: 'none',
29+
borderRadius: '4px',
30+
cursor: 'pointer',
31+
}}
32+
>
33+
Logout
34+
</button>
35+
</div>
36+
) : (
37+
<div>
38+
<p>Please log in:</p>
39+
<button
40+
onClick={() => login('testUser')}
41+
style={{
42+
backgroundColor: theme.primaryColor,
43+
color: theme.backgroundColor,
44+
padding: '8px 16px',
45+
border: 'none',
46+
borderRadius: '4px',
47+
cursor: 'pointer',
48+
}}
49+
>
50+
Login as Test User
51+
</button>
52+
<button
53+
onClick={() => login('admin')}
54+
style={{
55+
backgroundColor: theme.secondaryColor,
56+
color: theme.backgroundColor,
57+
padding: '8px 16px',
58+
border: 'none',
59+
borderRadius: '4px',
60+
marginLeft: '8px',
61+
cursor: 'pointer',
62+
}}
63+
>
64+
Login as Admin
65+
</button>
66+
</div>
67+
)}
68+
769
<p>
870
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
9-
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
10-
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
11-
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
12-
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
13-
</p>
14-
<p>
15-
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
16-
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
17-
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
18-
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
19-
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
71+
ut labore et dolore magna aliqua..."
2072
</p>
2173
</div>
2274
);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import { useTheme } from '../../contexts/ThemeContext';
3+
4+
const ThemeToggle = (): JSX.Element => {
5+
const { theme, toggleTheme } = useTheme();
6+
7+
return (
8+
<button
9+
onClick={toggleTheme}
10+
style={{
11+
backgroundColor: theme.primaryColor,
12+
color: theme.backgroundColor,
13+
padding: '8px 16px',
14+
border: 'none',
15+
borderRadius: '4px',
16+
cursor: 'pointer',
17+
position: 'fixed',
18+
top: '10px',
19+
right: '10px',
20+
}}
21+
>
22+
Toggle Theme
23+
</button>
24+
);
25+
};
26+
27+
export default ThemeToggle;

demo-app/src/client/Router.tsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
// src/client/Router.tsx
12
import * as React from 'react';
23
import * as ReactDOM from 'react-dom';
34
import { createRoot } from 'react-dom/client';
45
import { BrowserRouter, Routes, Route } from 'react-router-dom';
6+
import { ThemeProvider } from '../contexts/ThemeContext';
7+
import { AuthProvider } from '../contexts/AuthContext';
58
import Nav from './Components/Nav';
69
import Board from './Components/Board';
710
import Home from './Components/Home';
811
import Buttons from './Components/Buttons';
912
import ReducerCounter from './Components/ReducerCounter';
1013
import FunctionalReducerCounter from './Components/FunctionalReducerCounter';
11-
// import ButtonsWithMoreHooks from './Components/ButtonsWithMoreHooks';
1214
import FunctionalStateCounter from './Components/FunctionalStateCounter';
15+
import ThemeToggle from './Components/ThemeToggle';
1316

1417
const domNode = document.getElementById('root');
18+
if (!domNode) throw new Error('Root element not found');
1519
const root = createRoot(domNode);
1620

1721
const CounterPage = () => (
@@ -41,21 +45,18 @@ const CounterPage = () => (
4145
);
4246

4347
root.render(
44-
<BrowserRouter key='BrowserRouter'>
45-
<Nav key='Nav' />
46-
<Routes key='Routes'>
47-
<Route path='/' element={<Home key='Home' />} />
48-
<Route path='/tictactoe' element={<Board key='Board' />} />
49-
{/* Switch between the two "buttons" paths below via commenting/uncommenting to alternate between
50-
the public facing Buttons page and the fiber node hooks research page "ButtonsWithMoreHooks" */}
51-
<Route path='/buttons' element={<Buttons key='Buttons' />} />
52-
{/* <Route path='/buttons' element={<ButtonsWithMoreHooks key='ButtonsWithMoreHooks'/>} /> */}
53-
<Route path='/reducer' element={<CounterPage key='CounterPage' />} />
54-
</Routes>
55-
</BrowserRouter>,
56-
57-
/** Comment out everything above this and uncomment the line below as ButtonsWithMoreHooks import statement to skip all of the
58-
* router components and make fiber node hooks research easier */
59-
60-
// <ButtonsWithMoreHooks/>
48+
<AuthProvider>
49+
<ThemeProvider>
50+
<BrowserRouter>
51+
<ThemeToggle />
52+
<Nav />
53+
<Routes>
54+
<Route path='/' element={<Home />} />
55+
<Route path='/tictactoe' element={<Board />} />
56+
<Route path='/buttons' element={<Buttons />} />
57+
<Route path='/reducer' element={<CounterPage />} />
58+
</Routes>
59+
</BrowserRouter>
60+
</ThemeProvider>
61+
</AuthProvider>,
6162
);

demo-app/src/contexts/AuthContext.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { createContext, useState, useContext } from 'react';
2+
3+
type User = {
4+
username: string;
5+
isAdmin: boolean;
6+
} | null;
7+
8+
type AuthContextType = {
9+
user: User;
10+
login: (username: string) => void;
11+
logout: () => void;
12+
};
13+
14+
export const AuthContext = createContext<AuthContextType>({
15+
user: null,
16+
login: () => {},
17+
logout: () => {},
18+
});
19+
20+
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
21+
const [user, setUser] = useState<User>(null);
22+
23+
const login = (username: string) => {
24+
setUser({
25+
username,
26+
isAdmin: username === 'admin',
27+
});
28+
};
29+
30+
const logout = () => {
31+
setUser(null);
32+
};
33+
34+
return <AuthContext.Provider value={{ user, login, logout }}>{children}</AuthContext.Provider>;
35+
};
36+
37+
export const useAuth = () => useContext(AuthContext);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, { createContext, useState, useContext } from 'react';
2+
3+
type Theme = {
4+
backgroundColor: string;
5+
textColor: string;
6+
primaryColor: string;
7+
secondaryColor: string;
8+
};
9+
10+
type ThemeContextType = {
11+
theme: Theme;
12+
toggleTheme: () => void;
13+
};
14+
15+
const defaultTheme: Theme = {
16+
backgroundColor: '#ffffff',
17+
textColor: '#330002',
18+
primaryColor: '#f00008',
19+
secondaryColor: '#62d6fb',
20+
};
21+
22+
const darkTheme: Theme = {
23+
backgroundColor: '#222222',
24+
textColor: '#ffffff',
25+
primaryColor: '#ff6569',
26+
secondaryColor: '#6288fb',
27+
};
28+
29+
export const ThemeContext = createContext<ThemeContextType>({
30+
theme: defaultTheme,
31+
toggleTheme: () => {},
32+
});
33+
34+
export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
35+
const [isDark, setIsDark] = useState(false);
36+
37+
const toggleTheme = () => {
38+
setIsDark(!isDark);
39+
};
40+
41+
const value = {
42+
theme: isDark ? darkTheme : defaultTheme,
43+
toggleTheme,
44+
};
45+
46+
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
47+
};
48+
49+
export const useTheme = () => useContext(ThemeContext);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const ToolTipDataDisplay = ({ containerName, dataObj }) => {
3737
return reducerStates.reduce((acc, reducer) => {
3838
acc[reducer.hookName || 'Reducer'] = {
3939
state: reducer.state,
40-
lastAction: reducer.lastAction,
4140
};
4241
return acc;
4342
}, {});

src/backend/controllers/createTree.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@ export default function createTree(currentFiberNode: Fiber): Tree {
243243
}
244244
}
245245

246+
if (tag === ContextProvider) {
247+
// Extract context information
248+
console.log('ele', elementType);
249+
console.log('memo', memoizedState);
250+
const contextData = {
251+
displayName: elementType._context?.displayName || 'Anonymous Context',
252+
currentValue: memoizedState?.deps?.[0]?._currentValue || memoizedProps?.value,
253+
defaultValue: elementType._context?._defaultValue,
254+
consumers: [], // how do we track consumers?
255+
};
256+
257+
componentData.context = contextData;
258+
}
259+
246260
// -----------------ADD COMPONENT DATA TO THE OUTPUT TREE-------------------
247261

248262
/**

0 commit comments

Comments
 (0)