11import { useAtom } from "jotai" ;
2- import { useRef , useEffect } from "react" ;
32import { colorSchemeAtom } from "~/hooks/useDark" ;
3+ import { useRef , useState , useEffect } from "react" ;
44import { readingHistoryAtom } from "~/atoms/historyAtom" ;
55import { readingStateAtom } from "~/atoms/readingStateAtom" ;
66import { preprocessMetadata , primitiveMetadataAtom } from "~/atoms/primitiveMetadataAtom" ;
@@ -21,11 +21,12 @@ export function useSyncHub() {
2121 const [ readingState , setReadingState ] = useAtom ( readingStateAtom ) ;
2222 const [ metadata , setMetadata ] = useAtom ( primitiveMetadataAtom ) ;
2323 const [ colorScheme , setColorScheme ] = useAtom ( colorSchemeAtom ) ;
24- const loaded = useRef ( false ) ;
24+ const loading = useRef ( false ) ;
25+ const [ ready , setReady ] = useState ( false ) ;
2526
2627 useEffect ( ( ) => {
27- if ( ! isSyncHubConfigured ( ) || loaded . current ) return ;
28- loaded . current = true ;
28+ if ( ! isSyncHubConfigured ( ) || loading . current || ready ) return ;
29+ loading . current = true ;
2930 void Promise . all ( [ downloadReadingHistory ( ) , downloadFavorites ( ) , downloadPreferences ( ) ] )
3031 . then ( ( [ remoteHistory , remoteState , remotePreferences ] ) => {
3132 if ( Array . isArray ( remoteHistory ) ) setHistory ( remoteHistory ) ;
@@ -40,33 +41,34 @@ export function useSyncHub() {
4041 setMetadata ( preprocessMetadata ( remotePreferences . metadata ) ) ;
4142 }
4243 }
44+ setReady ( true ) ;
4345 } )
4446 . catch ( ( ) => {
45- loaded . current = false ;
47+ loading . current = false ;
4648 } ) ;
47- } , [ setColorScheme , setHistory , setMetadata , setReadingState ] ) ;
49+ } , [ ready , setColorScheme , setHistory , setMetadata , setReadingState ] ) ;
4850
4951 useEffect ( ( ) => {
50- if ( ! loaded . current || ! isSyncHubConfigured ( ) ) return undefined ;
52+ if ( ! ready || ! isSyncHubConfigured ( ) ) return undefined ;
5153 const timer = setTimeout ( ( ) => {
5254 void uploadReadingHistory ( history ) . catch ( ( ) => undefined ) ;
5355 } , debounceMs ) ;
5456 return ( ) => clearTimeout ( timer ) ;
55- } , [ history ] ) ;
57+ } , [ history , ready ] ) ;
5658
5759 useEffect ( ( ) => {
58- if ( ! loaded . current || ! isSyncHubConfigured ( ) ) return undefined ;
60+ if ( ! ready || ! isSyncHubConfigured ( ) ) return undefined ;
5961 const timer = setTimeout ( ( ) => {
6062 void uploadFavorites ( readingState . favorites ) . catch ( ( ) => undefined ) ;
6163 } , debounceMs ) ;
6264 return ( ) => clearTimeout ( timer ) ;
63- } , [ readingState ] ) ;
65+ } , [ readingState , ready ] ) ;
6466
6567 useEffect ( ( ) => {
66- if ( ! loaded . current || ! isSyncHubConfigured ( ) ) return undefined ;
68+ if ( ! ready || ! isSyncHubConfigured ( ) ) return undefined ;
6769 const timer = setTimeout ( ( ) => {
6870 void uploadPreferences ( { colorScheme, metadata } ) . catch ( ( ) => undefined ) ;
6971 } , debounceMs ) ;
7072 return ( ) => clearTimeout ( timer ) ;
71- } , [ colorScheme , metadata ] ) ;
73+ } , [ colorScheme , metadata , ready ] ) ;
7274}
0 commit comments