diff --git a/app/scripts/page.tsx b/app/scripts/page.tsx index 8914499..7f0207a 100644 --- a/app/scripts/page.tsx +++ b/app/scripts/page.tsx @@ -12,7 +12,6 @@ export default function Page() { const [selectedScript, setSelectedScript] = useState(null); const [cacheExpiryTime, setCacheExpiryTime] = useState(null); const [isCacheEnabled, setIsCacheEnabled] = useState(true); - const [showCacheControls, setShowCacheControls] = useState(true); const fetchLinks = useCallback(async (forceUpdate: boolean = false) => { try { @@ -81,10 +80,11 @@ export default function Page() { } setLinks(res as unknown as Category[]); - setIsLoading(false); } catch (error) { console.error("Error fetching links:", error); setIsLoading(false); + } finally { + setIsLoading(false); } }, [isCacheEnabled]); @@ -100,20 +100,17 @@ export default function Page() { fetchLinks(true); }; - const toggleCache = () => { + const toggleCache = async () => { const newCacheState = !isCacheEnabled; setIsCacheEnabled(newCacheState); localStorage.setItem("isCacheEnabled", String(newCacheState)); - }; - const handleScriptSelect = (script: string | null) => { - if (!selectedScript && script) { - setShowCacheControls(false); - setTimeout(() => { - setShowCacheControls(true); - }, 50); + if (!newCacheState) { + localStorage.removeItem("scripts"); + localStorage.removeItem("cacheTime"); + } else { + await fetchLinks(true); } - setSelectedScript(script); }; return ( @@ -126,26 +123,28 @@ export default function Page() {
- {links.length > 0 && showCacheControls && ( - - )}
+ {links.length > 0 && ( +
+ +
+ )} ) : (
diff --git a/components/CacheControls.tsx b/components/CacheControls.tsx index 224e187..b11d278 100644 --- a/components/CacheControls.tsx +++ b/components/CacheControls.tsx @@ -1,5 +1,4 @@ import { Button } from "@/components/ui/button"; -import { useEffect, useState } from "react"; interface CacheControlsProps { isCacheEnabled: boolean; @@ -8,49 +7,50 @@ interface CacheControlsProps { cacheExpiryTime: Date | null; } -const CacheControls: React.FC = ({ isCacheEnabled, toggleCache, handleForceUpdate, cacheExpiryTime }) => { - const [initialized, setInitialized] = useState(false); - - useEffect(() => { - setInitialized(true); - }, []); - - if (!initialized) return null; - +const CacheControls: React.FC = ({ + isCacheEnabled, + toggleCache, + handleForceUpdate, + cacheExpiryTime, +}) => { return ( -
- {isCacheEnabled ? ( - <> -

- The scripts are cached in your browser to optimize performance. Use the button below - to re-poll the server for changes. -

- {cacheExpiryTime && ( +
+
+ {isCacheEnabled ? ( + <>

- Cache will expire automatically at {cacheExpiryTime.toLocaleTimeString()} + The scripts are cached in your browser to optimize performance. + Use the button below to re-poll the server for changes.

- )} -
+ {cacheExpiryTime && ( +

+ Cache will expire automatically at{" "} + {cacheExpiryTime.toLocaleTimeString()} +

+ )} + + ) : ( +

+ The cache is disabled. All data will be fetched from the server. +

+ )} +
+
+ {isCacheEnabled ? ( + <> -
- - ) : ( -
-

- The cache is disabled. All data will be fetched from the server. -

-
- -
-
- )} + + ) : ( + + )} +
); };