diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/apis/GoogleAuthenticationManager.tsx | 62 | ||||
-rw-r--r-- | src/client/views/nodes/TaskBox.tsx | 43 |
2 files changed, 48 insertions, 57 deletions
diff --git a/src/client/apis/GoogleAuthenticationManager.tsx b/src/client/apis/GoogleAuthenticationManager.tsx index 23e658329..67a6e01e9 100644 --- a/src/client/apis/GoogleAuthenticationManager.tsx +++ b/src/client/apis/GoogleAuthenticationManager.tsx @@ -41,49 +41,6 @@ export class GoogleAuthenticationManager extends ObservableReactComponent<object this.openState && this.resetState(0, 0); } - // public fetchOrGenerateAccessToken = async (displayIfFound = false) => { - // const response = await Networking.FetchFromServer('/readGoogleAccessToken'); - // // if this is an authentication url, activate the UI to register the new access token - - // if (new RegExp(AuthenticationUrl).test(response)) { - // this.isOpen = true; - // this.authenticationLink = response; - - // // GETS STUCK AT THIS PROMISE!! - // return new Promise<string>(resolve => { - // this.disposer?.(); - // this.disposer = reaction( - // () => this.authenticationCode, - // async authenticationCode => { - // if (authenticationCode && /\d{1}\/[\w-]{55}/.test(authenticationCode)) { - // resolve(authenticationCode); - // this.disposer?.(); - // // const response2 = await Networking.PostToServer('/writeGoogleAccessToken', { authenticationCode }); - // // runInAction(() => { - // // this.success = true; - // // this.credentials = response2 as { user_info: { name: string; picture: string }; access_token: string }; - // // }); - // // resolve((response2 as { access_token: string }).access_token); - // this.resetState(); - // } - // } - // ); - // }); - // } - - // // otherwise, we already have a valid, stored access token and user info - // const response2 = JSON.parse(response) as { user_info: { name: string; picture: string }; access_token: string }; - // if (displayIfFound) { - // runInAction(() => { - // this.success = true; - // this.credentials = response2; - // }); - // this.resetState(-1, -1); - // this.isOpen = true; - // } - // return (response2 as { access_token: string }).access_token; - // }; - public fetchOrGenerateAccessToken = async (): Promise<string | undefined> => { const response = await Networking.FetchFromServer('/readGoogleAccessToken'); @@ -111,6 +68,25 @@ export class GoogleAuthenticationManager extends ObservableReactComponent<object } }; + public fetchAccessTokenSilently = async (): Promise<string | undefined> => { + const response = await Networking.FetchFromServer('/readGoogleAccessToken'); + + try { + const parsed = JSON.parse(response) as { access_token: string; user_info: { name: string; picture: string } }; + + runInAction(() => { + this.success = true; + this.credentials = parsed; + }); + + return parsed.access_token; + } catch { + // Do nothing — just return undefined silently + return undefined; + } + }; + + resetState = action((visibleForMS: number = 3000, fadesOutInMS: number = 500) => { if (!visibleForMS && !fadesOutInMS) { runInAction(() => { diff --git a/src/client/views/nodes/TaskBox.tsx b/src/client/views/nodes/TaskBox.tsx index c99a91080..5f46c9b2c 100644 --- a/src/client/views/nodes/TaskBox.tsx +++ b/src/client/views/nodes/TaskBox.tsx @@ -234,24 +234,39 @@ export class TaskBox extends ViewBoxBaseComponent<FieldViewProps>() { handleBlur = (e: React.FocusEvent<HTMLDivElement>) => { // Check if focus is moving outside this component if (!e.currentTarget.contains(e.relatedTarget)) { - this.syncWithGoogleTaskBidirectional(); + this.syncWithGoogleTaskBidirectional(true); } }; - syncWithGoogleTaskBidirectional = async (): Promise<boolean> => { + /** + * Method to sync the task with Google Tasks bidirectionally + * (update Dash from Google and vice versa, based on which is newer) + * @param silent - whether to suppress UI prompts to connect to Google (default: false) + * @returns - a promise that resolves to true if sync was successful, false otherwise + */ + + syncWithGoogleTaskBidirectional = async (silent = false): Promise<boolean> => { const doc = this.Document; - const token = await GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken(); + let token: string | undefined; + try { + token = silent ? await GoogleAuthenticationManager.Instance.fetchAccessTokenSilently() : await GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken(); + } catch (err) { + console.warn('Google auth failed:', err); + return false; + } if (!token) { - const listener = () => { - window.removeEventListener('focusin', listener); - if (confirm('✅ Authorization complete. Try syncing the task again?')) { - // you could refactor the click handler here - this.syncWithGoogleTaskBidirectional(); - } - window.removeEventListener('focusin', listener); - }; - setTimeout(() => window.addEventListener('focusin', listener), 100); + if (!silent) { + const listener = () => { + window.removeEventListener('focusin', listener); + if (confirm('✅ Authorization complete. Try syncing the task again?')) { + // you could refactor the click handler here + this.syncWithGoogleTaskBidirectional(); + } + window.removeEventListener('focusin', listener); + }; + setTimeout(() => window.addEventListener('focusin', listener), 100); + } return false; } @@ -373,7 +388,7 @@ export class TaskBox extends ViewBoxBaseComponent<FieldViewProps>() { console.warn('❌ Google Task creation failed:', result); } } catch (err) { - console.error('❌ Error creating Google Task:', err); + console.warn('❌ Error creating Google Task:', err); } } else if (doc.$googleTaskId) { await this.syncWithGoogleTaskBidirectional(); @@ -492,7 +507,7 @@ export class TaskBox extends ViewBoxBaseComponent<FieldViewProps>() { if (success) { alert('✅ Task successfully synced!'); } else { - alert('❌ Task sync failed.'); + alert('❌ Task sync failed. Try reloading.'); } }; |