fix: Improve Google Drive picker API loading and error handling

This commit is contained in:
Taylor Wilsdon (aider) 2024-12-15 16:33:15 -05:00
parent 1cd43b122b
commit b2dc6fef9f

View File

@ -1,6 +1,6 @@
// Google Drive Picker API configuration // Google Drive Picker API configuration
const API_KEY = 'YOUR_API_KEY'; const API_KEY = import.meta.env.VITE_GOOGLE_API_KEY;
const CLIENT_ID = 'YOUR_CLIENT_ID'; const CLIENT_ID = import.meta.env.VITE_GOOGLE_CLIENT_ID;
const SCOPE = ['https://www.googleapis.com/auth/drive.readonly']; const SCOPE = ['https://www.googleapis.com/auth/drive.readonly'];
let pickerApiLoaded = false; let pickerApiLoaded = false;
@ -8,26 +8,41 @@ let oauthToken: string | null = null;
export const loadGoogleDriveApi = () => { export const loadGoogleDriveApi = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const script = document.createElement('script'); if (typeof gapi === 'undefined') {
script.src = 'https://apis.google.com/js/api.js'; const script = document.createElement('script');
script.onload = () => { script.src = 'https://apis.google.com/js/api.js';
gapi.load('picker', () => { script.onload = () => {
pickerApiLoaded = true; gapi.load('picker', {
resolve(true); 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 = () => { export const loadGoogleAuthApi = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const script = document.createElement('script'); if (typeof google === 'undefined') {
script.src = 'https://accounts.google.com/gsi/client'; const script = document.createElement('script');
script.onload = resolve; script.src = 'https://accounts.google.com/gsi/client';
script.onerror = reject; script.onload = resolve;
document.body.appendChild(script); script.onerror = reject;
document.body.appendChild(script);
} else {
resolve(true);
}
}); });
}; };