Skip to content

Commit

Permalink
feat(platofrm): Added online/offline status detection in the platform (
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan2000-Git authored Dec 14, 2024
1 parent a92925f commit 89aa84f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion apps/platform/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Toaster } from '@/components/ui/sonner'
import './global.css'
import JotaiProvider from '@/components/jotaiProvider'
import OnlineStatusHandler from '@/components/common/online-status-handler'

export const metadata = {
title: 'Keyshade',
Expand All @@ -15,7 +16,10 @@ export default function RootLayout({
return (
<html lang="en">
<body>
<JotaiProvider>{children}</JotaiProvider>
<JotaiProvider>
<OnlineStatusHandler />
{children}
</JotaiProvider>
</body>
<Toaster richColors />
</html>
Expand Down
10 changes: 10 additions & 0 deletions apps/platform/src/components/common/online-status-handler.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use client'

import { useOnlineStatus } from "@/hooks/use-online-status";

function OnlineStatusHandler() {
useOnlineStatus();
return null;
}

export default OnlineStatusHandler;
33 changes: 33 additions & 0 deletions apps/platform/src/hooks/use-online-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect, useRef } from "react"
import { toast } from "sonner";

export const useOnlineStatus = () => {
const statusTimeout = useRef<NodeJS.Timeout | null>(null);

const statusHandler = () => {
if (statusTimeout.current) {
clearTimeout(statusTimeout.current);
}

statusTimeout.current = setTimeout(() => {
if (navigator.onLine) {
toast.success("You are back online! Refreshing...");
setTimeout(() => {
window.location.reload();
}, 1000);
} else {
toast.error("You are offline");
}
}, 1000);
};

useEffect(() => {
window.addEventListener("online", statusHandler);
window.addEventListener("offline", statusHandler);

return () => {
window.removeEventListener("online", statusHandler);
window.removeEventListener("offline", statusHandler);
}
}, []);
}

0 comments on commit 89aa84f

Please sign in to comment.