From b2dc6fef9fe4994fde8547730fb3b68d87266cee Mon Sep 17 00:00:00 2001 From: "Taylor Wilsdon (aider)" Date: Sun, 15 Dec 2024 16:33:15 -0500 Subject: [PATCH] fix: Improve Google Drive picker API loading and error handling --- src/lib/utils/google-drive-picker.ts | 47 ++++++++++++++++++---------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/lib/utils/google-drive-picker.ts b/src/lib/utils/google-drive-picker.ts index 941b1fce3..4a332c79a 100644 --- a/src/lib/utils/google-drive-picker.ts +++ b/src/lib/utils/google-drive-picker.ts @@ -1,6 +1,6 @@ // Google Drive Picker API configuration -const API_KEY = 'YOUR_API_KEY'; -const CLIENT_ID = 'YOUR_CLIENT_ID'; +const API_KEY = import.meta.env.VITE_GOOGLE_API_KEY; +const CLIENT_ID = import.meta.env.VITE_GOOGLE_CLIENT_ID; const SCOPE = ['https://www.googleapis.com/auth/drive.readonly']; let pickerApiLoaded = false; @@ -8,26 +8,41 @@ let oauthToken: string | null = null; export const loadGoogleDriveApi = () => { return new Promise((resolve, reject) => { - const script = document.createElement('script'); - script.src = 'https://apis.google.com/js/api.js'; - script.onload = () => { - gapi.load('picker', () => { - pickerApiLoaded = true; - resolve(true); + if (typeof gapi === 'undefined') { + const script = document.createElement('script'); + script.src = 'https://apis.google.com/js/api.js'; + script.onload = () => { + gapi.load('picker', { + callback: () => { + pickerApiLoaded = true; + resolve(true); + } + }); + }; + script.onerror = reject; + document.body.appendChild(script); + } else { + gapi.load('picker', { + callback: () => { + pickerApiLoaded = true; + resolve(true); + } }); - }; - script.onerror = reject; - document.body.appendChild(script); + } }); }; export const loadGoogleAuthApi = () => { return new Promise((resolve, reject) => { - const script = document.createElement('script'); - script.src = 'https://accounts.google.com/gsi/client'; - script.onload = resolve; - script.onerror = reject; - document.body.appendChild(script); + if (typeof google === 'undefined') { + const script = document.createElement('script'); + script.src = 'https://accounts.google.com/gsi/client'; + script.onload = resolve; + script.onerror = reject; + document.body.appendChild(script); + } else { + resolve(true); + } }); };