Priceless learning and extremely satisfied code quality. It's all about consistency.
What are the new additions?
user.ts data
AuthContex.tsx
ModalContext.tsx
ThemeContext.tsx
AppModals.tsx
What did I have to figure out?
Explaining my code: How does everything connect?
types/user.ts
Typescript
data/user.ts
Typescript
Since I don’t have a backend yet, I use mockUser to simulate a real user—makes development smoother and lets me test login/register logic.
AuthContext.tsx
Typescript
AuthProvider wraps the app and lets me read or write user info from anywhere! Login, logout, and register become simple function calls. The user data lives in context, so I won’t need to pass it around with props.
For now, it uses mock data and fake password checking—real authentication always belongs on the server!
ModalContext.tsx
Typescript
ModalProvider lets me show modals from anywhere with functions: showModal for info modals, showConfirmModal for confirm/cancel modals.
Modal state and controls live in context, so UI stays organized and powerful.
AppModals.tsx
Typescript
AppModal listens to the modal context and renders a visually appealing popup whenever modal.isOpen is true.
It automatically offers confirm/cancel buttons if it’s a confirm modal, or just OK otherwise.
Thanks to ModalContext, I never need dozens of different modal components—just this one does it all!
ThemeContext.tsx
Typescript
ThemeProvider tracks the current theme (light or dark) and lets me swap modes from anywhere in the app.
Want to add a dark mode toggle? Just call toggleTheme wherever I want the button to live.
Right now, this just changes a value—it’s not wired up to real dark mode styling yet since I still need to set up the CSS logic for dark colors.
How does the data flow step by step?
- I wrap my pages in the context providers (AuthProvider, ModalProvider, ThemeProvider) at the top level.
- Any component can useAuth, useModal, or useTheme to get or set user, modal, or theme info.
- When user interacts:
- Login or register: useAuth().login() or register() is called, state updates, and AuthContext lets all components “know” the current user.
- Show feedback: Call useModal().showModal() or showConfirmModal() to open a popup instantly. AppModal listens, shows the modal, and handles user actions.
- Toggle UI mode: Call useTheme().toggleTheme() to switch between light and dark mode everywhere.
- As these contexts update, my UI updates reactively and I never need to prop-drill or duplicate logic.
What's next?
If you have advice, questions, or just want to say hi, leave a comment on my linkedin post, always appreciated. Stay consistent, keep building, and have a wonderful day.