Skip to content

Commit

Permalink
detect offline client (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
norswap authored Apr 2, 2024
2 parents 5080439 + 4680398 commit a037159
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/webapp/src/hooks/useOfflineCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect, useState } from "react"

import { toast } from "sonner"

function useOfflineCheck() {
const [isOnline, setIsOnline] = useState(true)

useEffect(() => {
const handleOnline = () => setIsOnline(true)
const handleOffline = () => setIsOnline(false)

window.addEventListener("online", handleOnline)
window.addEventListener("offline", handleOffline)

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

useEffect(() => {
if (!isOnline) {
toast.error("App is offline. Please check your internet connection.", {
dismissible: false,
duration: Infinity,
})
} else {
toast.dismiss()
}
}, [isOnline])
}

export default useOfflineCheck
2 changes: 2 additions & 0 deletions packages/webapp/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import jotaiDebug from "src/components/lib/jotaiDebug"
import { GlobalErrorModal } from "src/components/modals/globalErrorModal"
import { Toaster } from "src/components/ui/sonner"
import { useIsHydrated } from "src/hooks/useIsHydrated"
import useOfflineCheck from "src/hooks/useOfflineCheck"
import { useErrorConfig } from "src/store/hooks"

import "src/styles/globals.css"
Expand All @@ -33,6 +34,7 @@ export type FablePage = NextPage<{ isHydrated: boolean }>
// =================================================================================================

const MyApp: AppType = ({ Component, pageProps }) => {
useOfflineCheck()
return (
<>
<Head>
Expand Down

0 comments on commit a037159

Please sign in to comment.