From 85508106a8ecbfde153404dc4e213b2cc8b367d8 Mon Sep 17 00:00:00 2001 From: "Taylor Wilsdon (aider)" Date: Sun, 15 Dec 2024 16:36:45 -0500 Subject: [PATCH] fix: Improve Google Drive picker initialization and error handling --- src/lib/components/chat/MessageInput.svelte | 4 ++++ src/lib/utils/google-drive-picker.ts | 13 +++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 1303cf373..1789983fc 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -498,11 +498,15 @@ }} uploadGoogleDriveHandler={async () => { try { + if (!import.meta.env.VITE_GOOGLE_API_KEY || !import.meta.env.VITE_GOOGLE_CLIENT_ID) { + throw new Error('Google Drive API credentials not configured'); + } const fileData = await createPicker(); if (fileData) { dispatch('upload', { type: 'google-drive', data: fileData }); } } catch (error) { + console.error('Google Drive Error:', error); toast.error('Error accessing Google Drive: ' + error.message); } }} diff --git a/src/lib/utils/google-drive-picker.ts b/src/lib/utils/google-drive-picker.ts index 4a332c79a..cfd030c20 100644 --- a/src/lib/utils/google-drive-picker.ts +++ b/src/lib/utils/google-drive-picker.ts @@ -5,6 +5,7 @@ const SCOPE = ['https://www.googleapis.com/auth/drive.readonly']; let pickerApiLoaded = false; let oauthToken: string | null = null; +let initialized = false; export const loadGoogleDriveApi = () => { return new Promise((resolve, reject) => { @@ -62,13 +63,17 @@ export const getAuthToken = async () => { return oauthToken; }; +const initialize = async () => { + if (!initialized) { + await Promise.all([loadGoogleDriveApi(), loadGoogleAuthApi()]); + initialized = true; + } +}; + export const createPicker = () => { return new Promise(async (resolve, reject) => { try { - if (!pickerApiLoaded) { - await loadGoogleDriveApi(); - } - + await initialize(); const token = await getAuthToken(); if (!token) { throw new Error('Unable to get OAuth token');