diff --git a/src/apps/officer-web/Screens/Dashboard/Email/EmailList.tsx b/src/apps/officer-web/Screens/Dashboard/Email/EmailList.tsx index e72bef88..99e7fa47 100644 --- a/src/apps/officer-web/Screens/Dashboard/Email/EmailList.tsx +++ b/src/apps/officer-web/Screens/Dashboard/Email/EmailList.tsx @@ -6,14 +6,13 @@ import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { useClient } from 'hooks/useClient'; import { useGlobal } from 'hooks/useGlobal'; -import { useJobs } from 'hooks/useJobs'; import type { EmailSummary } from 'types'; -type GoogleStatus = { - configured: boolean; - connected: boolean; - email: string | null; - picture: string | null; +type EmailAccountRow = { + id: number; + provider: string; + email: string; + status: string; }; const LIMIT = 50; @@ -42,13 +41,13 @@ export const EmailList = () => { const [selectedId, setSelectedId] = useGlobal('EMAIL_SELECTED', null); const [folder, setFolder] = useGlobal('EMAIL_FOLDER', 'inbox'); const [page, setPage] = useState(1); - const { jobs, createJob } = useJobs({ type: 'gmail-sync' }); - const isSyncing = jobs.some((j) => j.status === 'queued' || j.status === 'running'); - - const { data: googleStatus } = useQuery({ - queryKey: ['google-status'], - queryFn: () => client.get('/integrations/google/status'), + const { data: emailAccounts = [], refetch: refetchAccounts } = useQuery({ + queryKey: ['email-accounts'], + queryFn: () => client.get('/email/accounts'), }); + const syncableAccount = emailAccounts.find((a) => a.status === 'connected' || a.status === 'synced'); + const isSyncing = emailAccounts.some((a) => a.status === 'syncing' || a.status === 'queued'); + const hasAccounts = emailAccounts.length > 0; const { data, isLoading } = useQuery({ queryKey: ['email-messages', page, folder], @@ -75,17 +74,23 @@ export const EmailList = () => { }; const handleSync = async () => { + if (!syncableAccount) return; try { - await createJob({ lane: 'google-api', type: 'gmail-sync', notify: false }); - toast.success('Gmail sync started'); - } catch { - toast.error('Failed to start sync'); + await client.post(`/email/accounts/${syncableAccount.id}/sync`, {}); + toast.success('Sync started'); + refetchAccounts(); + } catch (err) { + toast.error(err instanceof Error ? err.message : 'Failed to start sync'); } }; - // Refresh email list when a sync job completes + // Poll accounts while syncing, refresh email list when done const prevSyncing = useRef(false); useEffect(() => { + if (isSyncing) { + const interval = setInterval(refetchAccounts, 5000); + return () => clearInterval(interval); + } if (prevSyncing.current && !isSyncing) { queryClient.invalidateQueries({ queryKey: ['email-messages'] }); } @@ -134,16 +139,14 @@ export const EmailList = () => { return (
- {googleStatus && !googleStatus.configured ? ( - Google integration not configured. Contact your administrator. - ) : googleStatus && !googleStatus.connected ? ( + {!hasAccounts ? (
- Connect your Google account to sync emails + Add an email account to get started
- ) : ( + ) : syncableAccount ? (
No emails synced yet
+ ) : isSyncing ? ( +
+ + Syncing emails... +
+ ) : ( + No emails synced yet )}
); @@ -179,18 +189,17 @@ export const EmailList = () => { })} {total} - + + ) : null} {totalPages > 1 && (