@@ -7,6 +7,8 @@ import { Loader2 } from 'lucide-react';
77import type React from 'react' ;
88import { useCallback , useState } from 'react' ;
99
10+ import { useOnlineStatus } from '@/hooks/use-online-status' ;
11+
1012interface PullToRefreshProperties {
1113 onRefresh : ( ) => Promise < void > ;
1214 children : React . ReactNode ;
@@ -20,6 +22,7 @@ export const PullToRefresh: React.FC<PullToRefreshProperties> = ({
2022 className,
2123 pullThreshold = 80 ,
2224} ) => {
25+ const isOnline = useOnlineStatus ( ) ;
2326 const [ isRefreshing , setIsRefreshing ] = useState ( false ) ;
2427 const y = useMotionValue ( 0 ) ;
2528
@@ -32,34 +35,34 @@ export const PullToRefresh: React.FC<PullToRefreshProperties> = ({
3235 // Keep it at a fixed position while refreshing
3336 animate ( y , 60 , { type : 'spring' , stiffness : 300 , damping : 30 } ) ;
3437
35- if ( navigator . onLine === false ) {
38+ if ( isOnline ) {
39+ void onRefresh ( ) . finally ( ( ) => {
40+ setIsRefreshing ( false ) ;
41+ animate ( y , 0 , { type : 'spring' , stiffness : 300 , damping : 30 } ) ;
42+ } ) ;
43+ } else {
3644 // we are offline, show the offline logo and
3745 // hide the icon after a second
3846 setTimeout ( ( ) => {
3947 setIsRefreshing ( false ) ;
4048 animate ( y , 0 , { type : 'spring' , stiffness : 300 , damping : 30 } ) ;
4149 } , 1000 ) ;
42- } else {
43- void onRefresh ( ) . finally ( ( ) => {
44- setIsRefreshing ( false ) ;
45- animate ( y , 0 , { type : 'spring' , stiffness : 300 , damping : 30 } ) ;
46- } ) ;
4750 }
4851 } else {
4952 animate ( y , 0 , { type : 'spring' , stiffness : 500 , damping : 30 } ) ;
5053 }
51- } , [ y , pullThreshold , isRefreshing , onRefresh ] ) ;
54+ } , [ y , pullThreshold , isRefreshing , onRefresh , isOnline ] ) ;
5255
5356 return (
5457 < div className = { cn ( 'relative' , className ) } >
5558 { /* Spinner shown only while refreshing */ }
5659 { isRefreshing && (
5760 < div className = "absolute top-4 left-0 z-20 flex w-full justify-center" >
5861 < div className = "flex h-10 w-10 items-center justify-center rounded-full bg-white shadow-md" >
59- { navigator . onLine ? (
62+ { isOnline ? (
6063 < Loader2 className = "text-conveniat-green h-6 w-6 animate-spin" />
6164 ) : (
62- < OfflineLogo />
65+ < OfflineLogo className = "h-6 w-6 text-gray-400" />
6366 ) }
6467 </ div >
6568 </ div >
0 commit comments