tremor-tracker/src/app/layout.tsx

74 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-04-28 19:03:29 +01:00
"use client";
2025-05-19 13:11:02 +01:00
import axios from "axios";
import { useEffect } from "react";
2025-02-24 12:37:15 +00:00
import type { Metadata } from "next";
2025-05-12 13:25:57 +01:00
import "./globals.css";
2025-02-24 12:37:15 +00:00
2025-05-12 13:25:57 +01:00
import { action, createStore, StoreProvider } from "easy-peasy";
2025-05-19 13:11:02 +01:00
import { useStoreActions } from "@hooks/store";
2025-05-12 13:25:57 +01:00
import { Inter } from "next/font/google";
2025-04-28 19:03:29 +01:00
2025-05-12 14:00:03 +01:00
import { StoreModel } from "@appTypes/StoreModel";
import Navbar from "@components/Navbar";
2025-04-28 19:03:29 +01:00
2025-03-19 19:20:18 +00:00
const inter = Inter({
2025-04-28 19:03:29 +01:00
subsets: ["latin"],
variable: "--font-inter",
2025-02-24 12:37:15 +00:00
});
2025-04-28 19:03:29 +01:00
const store = createStore<StoreModel>({
currency: {
2025-05-09 10:30:12 +01:00
selectedCurrency: "EUR",
2025-04-28 19:03:29 +01:00
setSelectedCurrency: action((state, payload) => {
state.selectedCurrency = payload;
}),
currencies: ["GBP", "USD", "EUR"],
2025-05-09 10:30:12 +01:00
conversionRates: { GBP: 0.85, USD: 1.14, EUR: 1 },
2025-04-28 19:03:29 +01:00
tickers: { GBP: "£", USD: "$", EUR: "€" },
},
2025-05-13 22:53:17 +01:00
user: null,
setUser: action((state, payload) => {
state.user = payload;
}),
2025-04-28 19:03:29 +01:00
});
2025-02-24 12:37:15 +00:00
2025-05-19 13:11:02 +01:00
function UserFetcher() {
const setUser = useStoreActions((actions) => actions.setUser);
useEffect(() => {
async function fetchUser() {
try {
const response = await axios.post("/api/get-user");
if (response.status === 200 && response.data.user) {
setUser(response.data.user);
} else {
setUser(null); // Clear user if no user found
}
} catch (error) {
console.error("Error fetching user:", error);
setUser(null);
}
}
fetchUser();
}, [setUser]); // Run once on mount
return null; // No UI needed
}
2025-02-24 12:37:15 +00:00
export default function RootLayout({
2025-04-28 19:03:29 +01:00
children,
2025-02-24 12:37:15 +00:00
}: Readonly<{
2025-04-28 19:03:29 +01:00
children: React.ReactNode;
2025-02-24 12:37:15 +00:00
}>) {
2025-04-28 19:03:29 +01:00
return (
<html lang="en">
<StoreProvider store={store}>
<body className={`${inter.variable} h-[calc(100vh-3.5rem)] flex flex-col min-h-screen antialiased`}>
2025-05-19 13:11:02 +01:00
<UserFetcher></UserFetcher>
2025-04-28 19:03:29 +01:00
<Navbar></Navbar>
<div className="flex-1 overflow-y-auto">{children}</div>
</body>
</StoreProvider>
</html>
);
2025-02-24 12:37:15 +00:00
}