fix: Improve Google Drive picker initialization and error handling

This commit is contained in:
Taylor Wilsdon (aider)
2024-12-15 16:36:45 -05:00
parent eed7cfd2a2
commit 85508106a8
2 changed files with 13 additions and 4 deletions

View File

@@ -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');