feat(PROJ-70): Frontend für IMAP-Rückholung (Opt-in + Restore-Button)

- MeResponse um imap_restore_enabled erweitert
- setImapRestore() + restoreMail() API-Funktionen (index.ts re-export)
- useImapRestore-Hook + ImapRestoreSection (Switch + Passwort-Dialog) in Settings
- RestoreMailButton in Mail-Detailansicht (Sichtbarkeit + Konto-Select)
- Spec/INDEX auf In Review

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
sysops
2026-07-07 00:43:24 +02:00
co-authored by Claude Sonnet 5
parent c00bf27fea
commit 4b3a4ef996
9 changed files with 479 additions and 1 deletions
+171
View File
@@ -0,0 +1,171 @@
"use client";
import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label";
import {
Dialog,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
} from "@/components/ui/dialog";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { MailPlus } from "lucide-react";
import { getImapAccounts, restoreMail, type ImapAccount } from "@/lib/api";
interface RestoreMailButtonProps {
mailId: string;
// PROJ-70: gate visibility on the user's opt-in flag.
enabled: boolean;
}
// PROJ-70: "Zurück ins Postfach" — copies an archived mail into the user's
// external IMAP mailbox (INBOX). Only rendered when the opt-in is active and
// at least one IMAP account is configured.
export function RestoreMailButton({ mailId, enabled }: RestoreMailButtonProps) {
const [accounts, setAccounts] = useState<ImapAccount[]>([]);
const [accountsLoaded, setAccountsLoaded] = useState(false);
const [dialogOpen, setDialogOpen] = useState(false);
const [selected, setSelected] = useState<string>("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [success, setSuccess] = useState("");
useEffect(() => {
if (!enabled) return;
let active = true;
getImapAccounts()
.then((data) => {
if (!active) return;
setAccounts(data);
if (data.length > 0) setSelected(String(data[0].id));
})
.catch(() => {
// Ignore — without accounts the button stays hidden.
})
.finally(() => {
if (active) setAccountsLoaded(true);
});
return () => {
active = false;
};
}, [enabled]);
// Hidden until we know the opt-in is on and there is a target mailbox.
if (!enabled || !accountsLoaded || accounts.length === 0) {
return null;
}
function openDialog() {
setError("");
setDialogOpen(true);
}
async function handleRestore() {
const accountId = parseInt(selected, 10);
if (!accountId) {
setError("Bitte ein Ziel-Postfach auswählen.");
return;
}
setLoading(true);
setError("");
setSuccess("");
try {
const res = await restoreMail(mailId, accountId);
const acc = accounts.find((a) => a.id === res.account);
setSuccess(
`Mail wurde nach ${res.mailbox} in "${acc?.name ?? res.account}" zurückgeholt.`
);
setDialogOpen(false);
} catch (err) {
setError(err instanceof Error ? err.message : "Rückholung fehlgeschlagen.");
} finally {
setLoading(false);
}
}
return (
<>
<Button variant="outline" size="sm" onClick={openDialog}>
<MailPlus className="mr-1.5 h-4 w-4" aria-hidden="true" />
Zurück ins Postfach
</Button>
{success && (
<Alert className="basis-full">
<AlertDescription>{success}</AlertDescription>
</Alert>
)}
{error && !dialogOpen && (
<Alert variant="destructive" className="basis-full">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Mail zurück ins Postfach</DialogTitle>
<DialogDescription>
Es wird eine Kopie dieser archivierten Mail in den Ordner INBOX
des gewählten Postfachs übertragen. Das Archiv bleibt unverändert.
</DialogDescription>
</DialogHeader>
<div className="space-y-3 py-2">
{accounts.length > 1 ? (
<div className="space-y-2">
<Label htmlFor="restore-account">Ziel-Postfach</Label>
<Select value={selected} onValueChange={setSelected}>
<SelectTrigger id="restore-account">
<SelectValue placeholder="Postfach auswählen" />
</SelectTrigger>
<SelectContent>
{accounts.map((acc) => (
<SelectItem key={acc.id} value={String(acc.id)}>
{acc.name} ({acc.username})
</SelectItem>
))}
</SelectContent>
</Select>
</div>
) : (
<p className="text-sm text-muted-foreground">
Ziel-Postfach: {accounts[0].name} ({accounts[0].username})
</p>
)}
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setDialogOpen(false)}
disabled={loading}
>
Abbrechen
</Button>
<Button type="button" onClick={handleRestore} disabled={loading}>
{loading ? "Übertragen..." : "Zurückholen"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
}
@@ -0,0 +1,143 @@
"use client";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { Alert, AlertDescription } from "@/components/ui/alert";
import {
Dialog,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
} from "@/components/ui/dialog";
import { MailPlus } from "lucide-react";
import { useImapRestore } from "@/hooks/useImapRestore";
interface ImapRestoreSectionProps {
initialEnabled: boolean;
onChanged: (enabled: boolean) => void;
}
// PROJ-70: Opt-in toggle that lets a user restore archived mails back into
// their own external IMAP mailbox. Off by default; enabling requires the
// current login password.
export function ImapRestoreSection({
initialEnabled,
onChanged,
}: ImapRestoreSectionProps) {
const {
enabled,
dialogOpen,
setDialogOpen,
password,
setPassword,
error,
success,
loading,
handleToggle,
confirmEnable,
cancelDialog,
} = useImapRestore(initialEnabled, onChanged);
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-3">
<MailPlus className="h-5 w-5" aria-hidden="true" />
Rückholung ins Postfach
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<p className="text-sm text-muted-foreground">
Wenn aktiviert, können Sie einzelne archivierte Mails aus der
Detailansicht als Kopie zurück in Ihr eigenes IMAP-Postfach (Ordner
INBOX) übertragen. Das Archiv selbst bleibt unverändert. Standardmäßig
ist diese Funktion deaktiviert.
</p>
<div className="flex items-center justify-between gap-4 rounded-md border px-4 py-3">
<div className="space-y-0.5">
<Label htmlFor="imap-restore-switch" className="text-sm font-medium">
Rückholung erlauben
</Label>
<p className="text-xs text-muted-foreground">
{enabled ? "Aktiv" : "Deaktiviert"}
</p>
</div>
<Switch
id="imap-restore-switch"
checked={enabled}
disabled={loading}
onCheckedChange={handleToggle}
aria-label="Rückholung ins Postfach erlauben"
/>
</div>
{success && (
<Alert>
<AlertDescription>{success}</AlertDescription>
</Alert>
)}
{error && !dialogOpen && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
</CardContent>
<Dialog
open={dialogOpen}
onOpenChange={(open) => {
if (!open) cancelDialog();
else setDialogOpen(true);
}}
>
<DialogContent>
<form onSubmit={confirmEnable}>
<DialogHeader>
<DialogTitle>Rückholung aktivieren</DialogTitle>
<DialogDescription>
Bitte bestätigen Sie mit Ihrem aktuellen Passwort, um die
Rückholung ins Postfach zu aktivieren.
</DialogDescription>
</DialogHeader>
<div className="space-y-2 py-4">
<Label htmlFor="imap-restore-password">Aktuelles Passwort</Label>
<Input
id="imap-restore-password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
autoComplete="current-password"
autoFocus
aria-label="Aktuelles Passwort"
/>
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={cancelDialog}
disabled={loading}
>
Abbrechen
</Button>
<Button type="submit" disabled={loading}>
{loading ? "Aktivieren..." : "Aktivieren"}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
</Card>
);
}