From 59dd38b54437c3a78f969aa6015f68d1ba1ed217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alper=20Akta=C5=9F?= Date: Tue, 31 Dec 2024 19:09:18 +0300 Subject: [PATCH] separate db data from storage context provider --- src/pages/Home/Home.tsx | 5 +---- src/routes/layout/MainLayout.tsx | 19 +++++++++++-------- src/utils/CurrencyMapProvider.tsx | 19 +++++++++++++++++-- src/utils/StorageProvider.tsx | 10 +--------- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/pages/Home/Home.tsx b/src/pages/Home/Home.tsx index 3c43334..d6fac98 100644 --- a/src/pages/Home/Home.tsx +++ b/src/pages/Home/Home.tsx @@ -1,11 +1,8 @@ import { CurrencyMapProvider } from "@/utils/CurrencyMapProvider"; -import { StorageProvider } from "@/utils/StorageProvider"; import { CalculationView } from "./CalculationView"; export const Home = () => ( - - - + ); diff --git a/src/routes/layout/MainLayout.tsx b/src/routes/layout/MainLayout.tsx index bd00a50..451e3da 100644 --- a/src/routes/layout/MainLayout.tsx +++ b/src/routes/layout/MainLayout.tsx @@ -6,20 +6,23 @@ import { Footer } from "./Footer"; import { AuthGuard } from "@/utils/AuthGuard"; import ErrorBoundary from "@/components/ErrorBoundary"; +import { StorageProvider } from "@/utils/StorageProvider"; export function MainLayout() { return (
- -
- + + +
+ -
- -
-
- +
+ +
+
+ +
); diff --git a/src/utils/CurrencyMapProvider.tsx b/src/utils/CurrencyMapProvider.tsx index efb2091..fdc2557 100644 --- a/src/utils/CurrencyMapProvider.tsx +++ b/src/utils/CurrencyMapProvider.tsx @@ -1,12 +1,16 @@ import { useContext, useEffect, useState, type PropsWithChildren } from "react"; import { collection, getDocs, limit, orderBy, query, where } from "firebase/firestore"; -import { DatabaseContext } from "@/context/DatabaseContext"; +import { sub } from "date-fns"; + +import { useStorage } from "@/hooks/useStorage"; + import { CenterChild } from "@/components/CenterChild"; import { Alert, AlertDescription, AlertTitle } from "@/components/shadcn/Alert"; import { Button } from "@/components/shadcn/Button"; import { Gears } from "@/components/Gears"; + +import { DatabaseContext } from "@/context/DatabaseContext"; import { CurrencyMapContext } from "@/context/CurrencyMapContext"; -import { sub } from "date-fns"; interface CurrencyMapProviderProps extends PropsWithChildren { mode: "latest" | "monthly"; @@ -15,6 +19,7 @@ interface CurrencyMapProviderProps extends PropsWithChildren { export function CurrencyMapProvider({ mode, children }: CurrencyMapProviderProps) { const db = useContext(DatabaseContext); const [currencyMap, setCurrencyMap] = useState(null); + const storage = useStorage(); const [error, setError] = useState(null); const [isLoadingDelayed, setIsLoadingDelayed] = useState(false); @@ -82,6 +87,16 @@ export function CurrencyMapProvider({ mode, children }: CurrencyMapProviderProps return () => clearTimeout(loadingDelayTimeout); }, [db, mode]); + useEffect(() => { + if (currencyMap?.length) { + const latest = currencyMap.slice(-1)[0]; + + if (storage.cache?.meta.createdAt !== latest.meta.createdAt) { + storage.setCache(latest); + } + } + }, [currencyMap, storage.cache]); + if (error) { return ( diff --git a/src/utils/StorageProvider.tsx b/src/utils/StorageProvider.tsx index 0316aff..51f493a 100644 --- a/src/utils/StorageProvider.tsx +++ b/src/utils/StorageProvider.tsx @@ -1,11 +1,9 @@ -import { useEffect, useMemo, useState, type PropsWithChildren } from "react"; +import { useMemo, useState, type PropsWithChildren } from "react"; import { getCache, getPreferences, setCache, setPreferences } from "./storage"; import { StorageContext } from "../context/StorageContext"; -import { useCurrencyMapData } from "../hooks/useCurrencyMap"; export function StorageProvider(props: PropsWithChildren) { - const currencyMap = useCurrencyMapData()![0]; const [cacheState, setCacheState] = useState(getCache()); const [preferencesState, setPreferencesState] = useState(getPreferences()); @@ -37,11 +35,5 @@ export function StorageProvider(props: PropsWithChildren) { [cacheState, preferencesState] ); - useEffect(() => { - if (currencyMap) { - update("cache", currencyMap); - } - }, [currencyMap]); - return {props.children}; }