@@ -56,42 +56,82 @@ function App() {
5656 }
5757
5858 // 初始化数据库,然后加载数据
59- const init = async ( ) => {
59+ const init = async ( retryCount = 0 ) => {
6060 try {
6161 await initDatabase ( ) ;
6262 await seedDatabase ( ) ;
63-
64- // 检查是否需要自动同步(双向同步)
65- const settings = useSettingsStore . getState ( ) ;
66- if ( settings . webdavEnabled && settings . webdavAutoSync &&
67- settings . webdavUrl && settings . webdavUsername && settings . webdavPassword ) {
68- console . log ( '🔄 Auto syncing with WebDAV (bidirectional)...' ) ;
63+ await fetchPrompts ( ) ;
64+ await fetchFolders ( ) ;
65+ console . log ( '✅ App initialized' ) ;
66+ } catch ( error ) {
67+ console . error ( '❌ Init failed:' , error ) ;
68+ // 如果是超时错误,尝试重试一次
69+ if ( retryCount < 1 && error instanceof Error && error . message . includes ( 'timeout' ) ) {
70+ console . log ( '🔄 Retrying database initialization...' ) ;
71+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
72+ return init ( retryCount + 1 ) ;
73+ }
74+ } finally {
75+ setIsLoading ( false ) ;
76+ }
77+
78+ // 启动后同步(在数据加载完成后执行,不阻塞 UI)
79+ const settings = useSettingsStore . getState ( ) ;
80+ if ( settings . webdavEnabled && settings . webdavSyncOnStartup &&
81+ settings . webdavUrl && settings . webdavUsername && settings . webdavPassword ) {
82+ const delay = ( settings . webdavSyncOnStartupDelay || 10 ) * 1000 ;
83+ console . log ( `🔄 Will sync with WebDAV in ${ delay / 1000 } s...` ) ;
84+ setTimeout ( async ( ) => {
6985 try {
7086 const result = await autoSync ( {
7187 url : settings . webdavUrl ,
7288 username : settings . webdavUsername ,
7389 password : settings . webdavPassword ,
7490 } ) ;
7591 if ( result . success ) {
76- console . log ( '✅ Auto sync completed:' , result . message ) ;
92+ console . log ( '✅ Startup sync completed:' , result . message ) ;
93+ // 同步后重新加载数据
94+ await fetchPrompts ( ) ;
95+ await fetchFolders ( ) ;
7796 } else {
78- console . log ( '⚠️ Auto sync failed:' , result . message ) ;
97+ console . log ( '⚠️ Startup sync failed:' , result . message ) ;
7998 }
8099 } catch ( syncError ) {
81- console . error ( '⚠️ Auto sync error:' , syncError ) ;
100+ console . error ( '⚠️ Startup sync error:' , syncError ) ;
82101 }
83- }
84-
85- await fetchPrompts ( ) ;
86- await fetchFolders ( ) ;
87- console . log ( '✅ App initialized' ) ;
88- } catch ( error ) {
89- console . error ( '❌ Init failed:' , error ) ;
90- } finally {
91- setIsLoading ( false ) ;
102+ } , delay ) ;
92103 }
93104 } ;
94105 init ( ) ;
106+
107+ // 定时自动同步
108+ const settings = useSettingsStore . getState ( ) ;
109+ let intervalId : NodeJS . Timeout | null = null ;
110+ if ( settings . webdavEnabled && settings . webdavAutoSyncInterval > 0 &&
111+ settings . webdavUrl && settings . webdavUsername && settings . webdavPassword ) {
112+ const intervalMs = settings . webdavAutoSyncInterval * 60 * 1000 ;
113+ console . log ( `🔄 Auto sync interval: ${ settings . webdavAutoSyncInterval } minutes` ) ;
114+ intervalId = setInterval ( async ( ) => {
115+ try {
116+ const result = await autoSync ( {
117+ url : settings . webdavUrl ,
118+ username : settings . webdavUsername ,
119+ password : settings . webdavPassword ,
120+ } ) ;
121+ if ( result . success ) {
122+ console . log ( '✅ Interval sync completed:' , result . message ) ;
123+ await fetchPrompts ( ) ;
124+ await fetchFolders ( ) ;
125+ }
126+ } catch ( e ) {
127+ console . error ( '⚠️ Interval sync error:' , e ) ;
128+ }
129+ } , intervalMs ) ;
130+ }
131+
132+ return ( ) => {
133+ if ( intervalId ) clearInterval ( intervalId ) ;
134+ } ;
95135 } , [ ] ) ;
96136
97137 if ( isLoading ) {
0 commit comments