From efd9e3aad96f282e1ea46e8d1b72b757266f39ce Mon Sep 17 00:00:00 2001 From: Mike Fortman Date: Fri, 5 Sep 2025 12:16:43 -0500 Subject: [PATCH] update be call --- frontend/src/app/connectors/page.tsx | 96 +++++++++++++--- frontend/src/app/upload/[provider]/page.tsx | 106 +++++++++++++++--- .../src/components/google-drive-picker.tsx | 2 +- 3 files changed, 173 insertions(+), 31 deletions(-) diff --git a/frontend/src/app/connectors/page.tsx b/frontend/src/app/connectors/page.tsx index 9bf206d7..8011d1bd 100644 --- a/frontend/src/app/connectors/page.tsx +++ b/frontend/src/app/connectors/page.tsx @@ -2,6 +2,7 @@ import React, { useState } from "react"; import { GoogleDrivePicker } from "@/components/google-drive-picker" +import { useTask } from "@/contexts/task-context" interface GoogleDriveFile { id: string; @@ -12,21 +13,63 @@ interface GoogleDriveFile { } export default function ConnectorsPage() { + const { addTask } = useTask() const [selectedFiles, setSelectedFiles] = useState([]); + const [isSyncing, setIsSyncing] = useState(false); + const [syncResult, setSyncResult] = useState(null); const handleFileSelection = (files: GoogleDriveFile[]) => { setSelectedFiles(files); }; - const handleSync = () => { - const fileIds = selectedFiles.map(file => file.id); - const body = { - file_ids: fileIds, - folder_ids: [], // Add folder handling if needed - recursive: true, - }; + const handleSync = async (connector: { connectionId: string, type: string }) => { + if (!connector.connectionId || selectedFiles.length === 0) return - console.log('Syncing with:', body); + setIsSyncing(true) + setSyncResult(null) + + try { + const syncBody: { + connection_id: string; + max_files?: number; + selected_files?: string[]; + } = { + connection_id: connector.connectionId, + selected_files: selectedFiles.map(file => file.id) + } + + const response = await fetch(`/api/connectors/${connector.type}/sync`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(syncBody), + }) + + const result = await response.json() + + if (response.status === 201) { + const taskId = result.task_id + if (taskId) { + addTask(taskId) + setSyncResult({ + processed: 0, + total: selectedFiles.length, + status: 'started' + }) + } + } else if (response.ok) { + setSyncResult(result) + } else { + console.error('Sync failed:', result.error) + setSyncResult({ error: result.error || 'Sync failed' }) + } + } catch (error) { + console.error('Sync error:', error) + setSyncResult({ error: 'Network error occurred' }) + } finally { + setIsSyncing(false) + } }; return ( @@ -48,12 +91,37 @@ export default function ConnectorsPage() { {selectedFiles.length > 0 && ( - +
+ + + {syncResult && ( +
+ {syncResult.error ? ( +
Error: {syncResult.error}
+ ) : syncResult.status === 'started' ? ( +
+ Sync started for {syncResult.total} files. Check the task notification for progress. +
+ ) : ( +
+
Processed: {syncResult.processed || 0}
+
Added: {syncResult.added || 0}
+ {syncResult.errors &&
Errors: {syncResult.errors}
} +
+ )} +
+ )} +
)} ); diff --git a/frontend/src/app/upload/[provider]/page.tsx b/frontend/src/app/upload/[provider]/page.tsx index f53c1913..c00391f2 100644 --- a/frontend/src/app/upload/[provider]/page.tsx +++ b/frontend/src/app/upload/[provider]/page.tsx @@ -7,6 +7,7 @@ import { Badge } from "@/components/ui/badge" import { ArrowLeft, AlertCircle } from "lucide-react" import { GoogleDrivePicker } from "@/components/google-drive-picker" import { OneDrivePicker } from "@/components/onedrive-picker" +import { useTask } from "@/contexts/task-context" interface GoogleDriveFile { id: string @@ -42,12 +43,15 @@ export default function UploadProviderPage() { const params = useParams() const router = useRouter() const provider = params.provider as string + const { addTask } = useTask() const [connector, setConnector] = useState(null) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const [accessToken, setAccessToken] = useState(null) const [selectedFiles, setSelectedFiles] = useState([]) + const [isSyncing, setIsSyncing] = useState(false) + const [syncResult, setSyncResult] = useState(null) useEffect(() => { const fetchConnectorInfo = async () => { @@ -132,6 +136,56 @@ export default function UploadProviderPage() { // You can add additional handling here like triggering sync, etc. } + const handleSync = async (connector: CloudConnector) => { + if (!connector.connectionId || selectedFiles.length === 0) return + + setIsSyncing(true) + setSyncResult(null) + + try { + const syncBody: { + connection_id: string; + max_files?: number; + selected_files?: string[]; + } = { + connection_id: connector.connectionId, + selected_files: selectedFiles.map(file => file.id) + } + + const response = await fetch(`/api/connectors/${connector.type}/sync`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(syncBody), + }) + + const result = await response.json() + + if (response.status === 201) { + const taskId = result.task_id + if (taskId) { + addTask(taskId) + setSyncResult({ + processed: 0, + total: selectedFiles.length, + status: 'started' + }) + } + } else if (response.ok) { + setSyncResult(result) + } else { + console.error('Sync failed:', result.error) + setSyncResult({ error: result.error || 'Sync failed' }) + } + } catch (error) { + console.error('Sync error:', error) + setSyncResult({ error: 'Network error occurred' }) + } finally { + setIsSyncing(false) + } + } + const getProviderDisplayName = () => { const nameMap: { [key: string]: string } = { 'google_drive': 'Google Drive', @@ -284,22 +338,42 @@ export default function UploadProviderPage() { {selectedFiles.length > 0 && ( -
- - +
+
+ + +
+ + {syncResult && ( +
+ {syncResult.error ? ( +
Error: {syncResult.error}
+ ) : syncResult.status === 'started' ? ( +
+ Sync started for {syncResult.total} files. Check the task notification for progress. +
+ ) : ( +
+
Processed: {syncResult.processed || 0}
+
Added: {syncResult.added || 0}
+ {syncResult.errors &&
Errors: {syncResult.errors}
} +
+ )} +
+ )}
)}
diff --git a/frontend/src/components/google-drive-picker.tsx b/frontend/src/components/google-drive-picker.tsx index f173f05f..60191261 100644 --- a/frontend/src/components/google-drive-picker.tsx +++ b/frontend/src/components/google-drive-picker.tsx @@ -241,7 +241,7 @@ export function GoogleDrivePicker({ size="sm" variant="outline" > - {isPickerOpen ? 'Opening Picker...' : 'Select Files'} + {isPickerOpen ? 'Opening Picker...' : 'Add Files'}