@@ -2,11 +2,6 @@ import { FC, memo, useCallback, useEffect, useState } from "react";
22import  {  Copy  }  from  "lucide-react" ; 
33import  {  useTranslation  }  from  "react-i18next" ; 
44import  {  v4  as  uuidv4  }  from  "uuid" ; 
5- import  { 
6-   getCurrent  as  getCurrentDeepLinkUrls , 
7-   onOpenUrl , 
8- }  from  "@tauri-apps/plugin-deep-link" ; 
9- import  {  getCurrentWindow  }  from  "@tauri-apps/api/window" ; 
105
116import  {  UserProfile  }  from  "./UserProfile" ; 
127import  {  OpenURLWithBrowser  }  from  "@/utils" ; 
@@ -18,19 +13,21 @@ import { useServers } from "@/hooks/useServers";
1813
1914interface  ServiceAuthProps  { 
2015  setRefreshLoading : ( loading : boolean )  =>  void ; 
21-   refreshClick : ( id : string )  =>  void ; 
16+   refreshClick : ( id : string ,   callback ?:  ( )   =>   void )  =>  void ; 
2217} 
2318
2419const  ServiceAuth  =  memo ( 
2520  ( {  setRefreshLoading,  refreshClick } : ServiceAuthProps )  =>  { 
2621    const  {  t }  =  useTranslation ( ) ; 
2722
23+     const  language  =  useAppStore ( ( state )  =>  state . language ) ; 
24+     const  addError  =  useAppStore ( ( state )  =>  state . addError ) ; 
2825    const  ssoRequestID  =  useAppStore ( ( state )  =>  state . ssoRequestID ) ; 
2926    const  setSSORequestID  =  useAppStore ( ( state )  =>  state . setSSORequestID ) ; 
3027
31-     const  addError  =  useAppStore ( ( state )   =>   state . addError ) ; 
32- 
33-     const   cloudSelectService   =   useConnectStore ( ( state )   =>   state . cloudSelectService ) ; 
28+     const  cloudSelectService  =  useConnectStore ( 
29+        ( state )   =>   state . cloudSelectService 
30+     ) ; 
3431
3532    const  {  logoutServer }  =  useServers ( ) ; 
3633
@@ -64,100 +61,25 @@ const ServiceAuth = memo(
6461      [ logoutServer ] 
6562    ) ; 
6663
67-     const  handleOAuthCallback  =  useCallback ( 
68-       async  ( code : string  |  null ,  serverId : string  |  null )  =>  { 
69-         if  ( ! code  ||  ! serverId )  { 
70-           addError ( "No authorization code received" ) ; 
71-           return ; 
72-         } 
73- 
74-         try  { 
75-           console . log ( "Handling OAuth callback:" ,  {  code,  serverId } ) ; 
76-           await  platformAdapter . commands ( "handle_sso_callback" ,  { 
77-             serverId : serverId ,  // Make sure 'server_id' is the correct argument 
78-             requestId : ssoRequestID ,  // Make sure 'request_id' is the correct argument 
79-             code : code , 
80-           } ) ; 
81- 
82-           if  ( serverId  !=  null )  { 
83-             refreshClick ( serverId ) ; 
84-           } 
85- 
86-           getCurrentWindow ( ) . setFocus ( ) ; 
87-         }  catch  ( e )  { 
88-           console . error ( "Sign in failed:" ,  e ) ; 
89-         }  finally  { 
90-           setLoading ( false ) ; 
91-         } 
92-       } , 
93-       [ ssoRequestID ] 
94-     ) ; 
95- 
96-     const  handleUrl  =  ( url : string )  =>  { 
97-       try  { 
98-         const  urlObject  =  new  URL ( url . trim ( ) ) ; 
99-         console . log ( "handle urlObject:" ,  urlObject ) ; 
100- 
101-         // pass request_id and check with local, if the request_id are same, then continue 
102-         const  reqId  =  urlObject . searchParams . get ( "request_id" ) ; 
103-         const  code  =  urlObject . searchParams . get ( "code" ) ; 
104- 
105-         if  ( reqId  !=  ssoRequestID )  { 
106-           console . log ( "Request ID not matched, skip" ) ; 
107-           addError ( "Request ID not matched, skip" ) ; 
108-           return ; 
109-         } 
110- 
111-         const  serverId  =  cloudSelectService ?. id ; 
112-         handleOAuthCallback ( code ,  serverId ) ; 
113-       }  catch  ( err )  { 
114-         console . error ( "Failed to parse URL:" ,  err ) ; 
115-         addError ( "Invalid URL format: "  +  err ) ; 
116-       } 
117-     } ; 
118- 
119-     // Fetch the initial deep link intent 
64+     // handle oauth success event 
12065    useEffect ( ( )  =>  { 
121-       // Function to handle pasted URL 
122-       const  handlePaste  =  ( event : any )  =>  { 
123-         const  pastedText  =  event . clipboardData . getData ( "text" ) . trim ( ) ; 
124-         console . log ( "handle paste text:" ,  pastedText ) ; 
125-         if  ( isValidCallbackUrl ( pastedText ) )  { 
126-           // Handle the URL as if it's a deep link 
127-           console . log ( "handle callback on paste:" ,  pastedText ) ; 
128-           handleUrl ( pastedText ) ; 
129-         } 
130-       } ; 
131- 
132-       // Function to check if the pasted URL is valid for our deep link scheme 
133-       const  isValidCallbackUrl  =  ( url : string )  =>  { 
134-         return  url  &&  url . startsWith ( "coco://oauth_callback" ) ; 
135-       } ; 
136- 
137-       // Adding event listener for paste events 
138-       document . addEventListener ( "paste" ,  handlePaste ) ; 
139- 
140-       getCurrentDeepLinkUrls ( ) 
141-         . then ( ( urls )  =>  { 
142-           console . log ( "URLs:" ,  urls ) ; 
143-           if  ( urls  &&  urls . length  >  0 )  { 
144-             if  ( isValidCallbackUrl ( urls [ 0 ] . trim ( ) ) )  { 
145-               handleUrl ( urls [ 0 ] ) ; 
146-             } 
66+       const  unlistenOAuth  =  platformAdapter . listenEvent ( 
67+         "oauth_success" , 
68+         ( event )  =>  { 
69+           const  {  serverId }  =  event . payload ; 
70+           if  ( serverId )  { 
71+             refreshClick ( serverId ,  ( )  =>  { 
72+               setLoading ( false ) ; 
73+             } ) ; 
74+             addError ( language  ===  "zh"  ? "登录成功"  : "Login Success" ,  "info" ) ; 
14775          } 
148-         } ) 
149-         . catch ( ( err )  =>  { 
150-           console . error ( "Failed to get initial URLs:" ,  err ) ; 
151-           addError ( "Failed to get initial URLs: "  +  err ) ; 
152-         } ) ; 
153- 
154-       const  unlisten  =  onOpenUrl ( ( urls )  =>  handleUrl ( urls [ 0 ] ) ) ; 
76+         } 
77+       ) ; 
15578
15679      return  ( )  =>  { 
157-         unlisten . then ( ( fn )  =>  fn ( ) ) ; 
158-         document . removeEventListener ( "paste" ,  handlePaste ) ; 
80+         unlistenOAuth . then ( ( fn )  =>  fn ( ) ) ; 
15981      } ; 
160-     } ,  [ ssoRequestID ] ) ; 
82+     } ,  [ refreshClick ] ) ; 
16183
16284    useEffect ( ( )  =>  { 
16385      setLoading ( false ) ; 
@@ -214,7 +136,9 @@ const ServiceAuth = memo(
214136              < button 
215137                className = "text-xs text-[#0096FB] dark:text-blue-400 block" 
216138                onClick = { ( )  => 
217-                   OpenURLWithBrowser ( cloudSelectService ?. provider ?. privacy_policy ) 
139+                   OpenURLWithBrowser ( 
140+                     cloudSelectService ?. provider ?. privacy_policy 
141+                   ) 
218142                } 
219143              > 
220144                { t ( "cloud.privacyPolicy" ) } 
0 commit comments