replace imap gmail sync with mbsync + maildir import, app password UI

- rewrite gmail-sync handler: mbsync downloads to local Maildir, then import to sqlite
- add app password field to google integration config and API
- gmail sync section independent from oauth in settings UI
- live mbsync progress streaming to job status
- recoverable failure email with instructions for overquota/auth errors
- sync meta persisted to job on failure for richer notifications

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-02 20:00:45 +00:00
co-authored by Claude Opus 4.6
parent 927267e041
commit 583580eb10
6 changed files with 472 additions and 256 deletions
@@ -10,6 +10,7 @@ type GoogleStatus = {
email: string | null;
picture: string | null;
configured: boolean;
hasAppPassword: boolean;
};
const formatTime = (ts: number | string) => {
@@ -20,7 +21,10 @@ const formatTime = (ts: number | string) => {
export const GoogleAccount = () => {
const client = useClient();
const [isLoading, setIsLoading] = useState(true);
const [status, setStatus] = useState<GoogleStatus>({ connected: false, email: null, picture: null, configured: false });
const [status, setStatus] = useState<GoogleStatus>({ connected: false, email: null, picture: null, configured: false, hasAppPassword: false });
const [appPassword, setAppPassword] = useState('');
const [showPasswordInput, setShowPasswordInput] = useState(false);
const [savingPassword, setSavingPassword] = useState(false);
const [lastSyncAt, setLastSyncAt] = useState<string | null>(null);
const [dismissedError, setDismissedError] = useState(false);
const { jobs, createJob, refetch } = useJobs({ type: 'gmail-sync' });
@@ -88,38 +92,93 @@ export const GoogleAccount = () => {
}
};
const handleSaveAppPassword = async () => {
if (!appPassword.trim()) return;
setSavingPassword(true);
try {
await client.put('/integrations/google/app-password', { appPassword: appPassword.trim() });
setStatus({ ...status, hasAppPassword: true });
setAppPassword('');
setShowPasswordInput(false);
toast.success('App password saved');
} catch {
toast.error('Failed to save app password');
} finally {
setSavingPassword(false);
}
};
if (isLoading) return null;
if (!status.configured) {
return (
<div className="grid gap-4">
<p className="text-sm text-duck-dark/60 dark:text-foreground/60">
Google integration has not been configured yet. Ask your administrator to set up Google OAuth credentials in
the Enterprise settings.
</p>
</div>
);
}
const currentStep = activeJob?.steps[activeJob.currentStep];
const progress = currentStep?.progress;
if (status.connected) {
const currentStep = activeJob?.steps[activeJob.currentStep];
const progress = currentStep?.progress;
return (
return (
<div className="grid gap-6">
{/* Gmail Sync — independent of OAuth */}
<div className="grid gap-4">
<div className="flex items-center gap-3 rounded-lg border border-duck-dark/10 dark:border-foreground/10 p-4">
<div className="h-2.5 w-2.5 rounded-full bg-green-500 shrink-0" />
<div className="min-w-0 flex-1">
<p className="text-sm font-medium text-duck-dark dark:text-foreground">Connected</p>
<p className="text-xs text-duck-dark/50 dark:text-foreground/50 truncate">{status.email}</p>
</div>
{status.picture && (
<img src={status.picture} alt="" className="h-9 w-9 rounded-full shrink-0" referrerPolicy="no-referrer" />
<div>
<p className="text-sm font-medium text-duck-dark dark:text-foreground">Gmail Sync</p>
<p className="text-xs text-duck-dark/50 dark:text-foreground/50 mt-1">
Import and sync your Gmail emails with your Officer inbox. This uses a Google App Password for a direct IMAP
connection the only thing it can do is download your emails. It cannot send, delete, or modify anything in
your account.
</p>
</div>
<div className="rounded-lg border border-duck-dark/10 dark:border-foreground/10 p-4 grid gap-2">
<p className="text-sm font-medium text-duck-dark dark:text-foreground">App Password</p>
{status.hasAppPassword && !showPasswordInput ? (
<div className="flex items-center gap-2">
<span className="text-xs text-green-600 dark:text-green-400">Configured</span>
<button
onClick={() => setShowPasswordInput(true)}
className="text-xs text-duck-dark/50 dark:text-foreground/50 underline hover:opacity-70 cursor-pointer"
>
Replace
</button>
</div>
) : (
<>
<div className="text-xs text-duck-dark/50 dark:text-foreground/50 grid gap-1.5">
<p>To create an App Password:</p>
<ol className="list-decimal ml-4 grid gap-0.5">
<li>
Go to{' '}
<a
href="https://myaccount.google.com/apppasswords"
target="_blank"
rel="noopener noreferrer"
className="underline hover:opacity-70"
>
Google App Passwords
</a>
</li>
<li>You may need to enable 2-Step Verification first</li>
<li>Enter a name (e.g. "Officer") and click Create</li>
<li>Copy the 16-character password and paste it below</li>
</ol>
</div>
<div className="flex gap-2">
<input
type="password"
value={appPassword}
onChange={(ev) => setAppPassword(ev.target.value)}
placeholder="xxxx xxxx xxxx xxxx"
className="flex-1 h-9 rounded-md border border-duck-dark/10 dark:border-foreground/10 bg-transparent px-3 text-sm"
/>
<Button
type="button"
variant="outline"
disabled={!appPassword.trim() || savingPassword}
onClick={handleSaveAppPassword}
className="h-9 cursor-pointer"
>
{savingPassword ? <Loader2 className="h-4 w-4 animate-spin" /> : 'Save'}
</Button>
</div>
</>
)}
</div>
<p className="text-xs text-duck-dark/40 dark:text-foreground/40">
Officer has access to your Google Calendar, Gmail, and other enabled services.
</p>
{activeJob && (
<div className="flex items-center gap-3 rounded-lg border border-duck-dark/10 dark:border-foreground/10 p-3">
<Loader2 className="h-4 w-4 animate-spin text-duck-dark/60 dark:text-foreground/60 shrink-0" />
@@ -163,37 +222,52 @@ export const GoogleAccount = () => {
<Button
type="button"
variant="outline"
disabled={!!activeJob}
disabled={!!activeJob || !status.hasAppPassword}
onClick={() => handleSync()}
className="w-full h-11 cursor-pointer gap-2"
>
<RefreshCw className="h-4 w-4" />
Sync Gmail Inbox
</Button>
<Button
type="button"
variant="outline"
onClick={handleDisconnect}
className="w-full h-11 cursor-pointer"
>
Disconnect
</Button>
</div>
);
}
return (
<div className="grid gap-4">
<p className="text-sm text-duck-dark/60 dark:text-foreground/60">
Connect your Google account to give Officer access to your Calendar, Gmail, and other Google services.
</p>
<Button
type="button"
onClick={handleConnect}
className="w-full h-11 bg-duck-yellow hover:bg-duck-yellow/90 text-duck-teal font-bold transition-all duration-200 hover:scale-105 cursor-pointer"
>
Connect Google Account
</Button>
{/* OAuth — for Calendar and other Google services */}
{status.configured && (
<div className="grid gap-4 border-t border-duck-dark/10 dark:border-foreground/10 pt-6">
<div>
<p className="text-sm font-medium text-duck-dark dark:text-foreground">Google Account</p>
<p className="text-xs text-duck-dark/50 dark:text-foreground/50 mt-1">
Connect your Google account for Calendar, Contacts, and other Google services. This is separate from Gmail
sync above.
</p>
</div>
{status.connected ? (
<>
<div className="flex items-center gap-3 rounded-lg border border-duck-dark/10 dark:border-foreground/10 p-4">
<div className="h-2.5 w-2.5 rounded-full bg-green-500 shrink-0" />
<div className="min-w-0 flex-1">
<p className="text-sm font-medium text-duck-dark dark:text-foreground">Connected</p>
<p className="text-xs text-duck-dark/50 dark:text-foreground/50 truncate">{status.email}</p>
</div>
{status.picture && (
<img src={status.picture} alt="" className="h-9 w-9 rounded-full shrink-0" referrerPolicy="no-referrer" />
)}
</div>
<Button type="button" variant="outline" onClick={handleDisconnect} className="w-full h-11 cursor-pointer">
Disconnect
</Button>
</>
) : (
<Button
type="button"
onClick={handleConnect}
className="w-full h-11 bg-duck-yellow hover:bg-duck-yellow/90 text-duck-teal font-bold transition-all duration-200 hover:scale-105 cursor-pointer"
>
Connect Google Account
</Button>
)}
</div>
)}
</div>
);
};