User saw entries like "vor 61d · Outlook" under the "Kürzlich blockiert · In den letzten 24h" header. createdAt (when the daemon wrote the mail_blocked row) is always inside the 24h retention window because deleteOldMailBlocked sweeps everything older than that on every fetch — but the row preserves the original receivedAt header from the email, which for old Casino mails the daemon only just got around to scanning can be weeks or months ago. Switched the time-label in MailActivityLog to format createdAt instead. The MailBlockedItem type now carries createdAt explicitly (the backend has been returning it all along, the FE type just hadn't acknowledged it). receivedAt stays in the shape for any future "received vs blocked" comparison view but isn't used in the recent- activity list anymore.
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { useCallback, useEffect, useState } from "react";
|
|
import { apiFetch } from "../lib/api";
|
|
|
|
export type MailBlockedItem = {
|
|
id: string;
|
|
subject: string;
|
|
senderEmail: string;
|
|
senderName: string | null;
|
|
/** Wann die Mail beim Sender raus / im User-Postfach ankam (kann Wochen zurückliegen). */
|
|
receivedAt: string;
|
|
/** Wann der Daemon die Mail geblockt hat — das ist die richtige Zeit für "Kürzlich blockiert". */
|
|
createdAt: string;
|
|
connectionId: string;
|
|
connection?: {
|
|
id: string;
|
|
email: string;
|
|
title: string | null;
|
|
provider: string;
|
|
providerLabel: string;
|
|
} | null;
|
|
};
|
|
|
|
export type MailResultsResponse = {
|
|
results: MailBlockedItem[];
|
|
total: number;
|
|
page: number;
|
|
pages: number;
|
|
};
|
|
|
|
/**
|
|
* GET /api/mail/results — Liste der blockierten Mails mit optionalem Provider-Filter.
|
|
* Backend räumt selbst nach 24h auf (deleteOldMailBlocked) — Retention sollte für
|
|
* den 30-Tage-Bar-Chart auf 90 Tage hochgesetzt werden, sonst sind die Stats leer.
|
|
*/
|
|
export function useMailResults(enabled: boolean = true, provider: string = 'all') {
|
|
const [results, setResults] = useState<MailBlockedItem[]>([]);
|
|
const [total, setTotal] = useState(0);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const refresh = useCallback(async () => {
|
|
if (!enabled) return;
|
|
setLoading(true);
|
|
try {
|
|
const qs = provider !== 'all' ? `?provider=${encodeURIComponent(provider)}` : '';
|
|
const res = await apiFetch<MailResultsResponse>(
|
|
`/api/mail/results${qs}`,
|
|
);
|
|
setResults(res.results ?? []);
|
|
setTotal(res.total ?? 0);
|
|
setError(null);
|
|
} catch (e: any) {
|
|
setError(e?.message ?? "unknown");
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [enabled, provider]);
|
|
|
|
useEffect(() => {
|
|
if (enabled) refresh();
|
|
}, [enabled, refresh]);
|
|
|
|
return { results, total, loading, error, refresh };
|
|
}
|