-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(platofrm): Added online/offline status detection in the platform (…
- Loading branch information
1 parent
a92925f
commit 89aa84f
Showing
3 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
apps/platform/src/components/common/online-status-handler.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}, []); | ||
} |