USP-Confirmed: Outlook-OAuth Casino-Bonus-Mail wurde end-to-end gefiltert (User-verifiziert). Mit dieser Welle ist der Daemon plus alle Scan-Pfade OAuth-aware. Backend — Mail-Stack (mo): - backend/server/utils/mail-auth.ts NEU: zentraler resolveImapAuth-Helper kapselt OAuth-vs-AppPassword-Entscheidung. 5-min-Token-Expiry-Puffer, race-condition-sicheres Refresh via refreshAndSaveTokens. - scan.post.ts + scan-internal.post.ts nutzen jetzt resolveImapAuth statt decrypt(passwordEncrypted). Vorher: Outlook-Connections wurden still übersprungen weil passwordEncrypted='' → decrypt failed. Cron + manueller Scan-Button funktionieren jetzt für OAuth-Connections. - imap-idle: Initial-Sweep via triggerScan(conn) direkt nach Connect-Success. Neue Outlook-Connections kriegen sofort einen Full-Folder-Scan statt bis zu 30 Min Cron-Lag zu warten. scan-internal scannt ohnehin schon alle Folders via imap.list() (Junk, Spam, Archive, Custom) — Multi-Folder- Anforderung ist damit erfüllt. Frontend — Mail-Page Polish v4 (rebreak-native-ui): - MailDistributionChart: Donut zurück auf 200px (240 wuchs auch in der Breite und quetschte die Legend), "Live"-Pill-Header komplett raus (paddingTop von 16 auf 13 reduziert für tighteres Layout) - mail.tsx Page-Hierarchie: "Mehr Infos"-Collapsible wandert von unter der Postfach-Liste direkt unter den Hero-Donut. Sub-Beschreibung "Blockiert — letzte 30 Tage" entfernt — Title reicht. - Account-Card Expanded: adaptive Bar-Chart über Connection-Age (too-new <24h zeigt Empty-State, 1-14d Day-Buckets via Backend ?connectionId=, 15-90d client-Week-Aggregation, >90d Month) - Account-Card Expanded: Scan-Button "Jetzt scannen" mit Refresh-Icon (Memory: kein Pen-Icon, refresh ok). Spinner während Scan, Feedback mit Blocked-Count nach Success. Eskalations-Hinweis (nicht in dieser Welle): - POST /api/mail/scan akzeptiert noch keinen connectionId-Filter → Scan-Button-Tap scannt aktuell alle Connections statt nur die angeklickte. Kleiner Folge-Patch, nicht blocking. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
155 lines
4.5 KiB
TypeScript
155 lines
4.5 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { apiFetch } from '../lib/api';
|
|
|
|
export type BlockedByDayEntry = {
|
|
date: string;
|
|
count: number;
|
|
};
|
|
|
|
export type BlockedByConnectionEntry = {
|
|
connectionId: string;
|
|
title: string | null;
|
|
email: string;
|
|
providerLabel: string;
|
|
count: number;
|
|
};
|
|
|
|
type MailStatsState = {
|
|
blockedByDay: BlockedByDayEntry[];
|
|
blockedByConnection: BlockedByConnectionEntry[];
|
|
loading: boolean;
|
|
};
|
|
|
|
type ConnectionAgeGranularity = 'too-new' | 'day' | 'week' | 'month';
|
|
|
|
function resolveGranularity(createdAt: string | null | undefined): ConnectionAgeGranularity {
|
|
if (!createdAt) return 'day';
|
|
const ageMs = Date.now() - new Date(createdAt).getTime();
|
|
const ageH = ageMs / 3_600_000;
|
|
if (ageH < 24) return 'too-new';
|
|
const ageD = ageH / 24;
|
|
if (ageD <= 14) return 'day';
|
|
if (ageD <= 90) return 'week';
|
|
return 'month';
|
|
}
|
|
|
|
type ConnectionStatsState = {
|
|
data: BlockedByDayEntry[];
|
|
granularity: ConnectionAgeGranularity;
|
|
loading: boolean;
|
|
};
|
|
|
|
export function useMailConnectionStats(
|
|
connectionId: string,
|
|
createdAt: string | null | undefined,
|
|
enabled: boolean,
|
|
) {
|
|
const fetchedRef = useRef(false);
|
|
const [state, setState] = useState<ConnectionStatsState>({
|
|
data: [],
|
|
granularity: resolveGranularity(createdAt),
|
|
loading: false,
|
|
});
|
|
|
|
const granularity = resolveGranularity(createdAt);
|
|
|
|
const fetch = useCallback(async () => {
|
|
if (!enabled || !connectionId) return;
|
|
setState((s) => ({ ...s, loading: true }));
|
|
try {
|
|
const ageDays = createdAt
|
|
? Math.max(1, Math.ceil((Date.now() - new Date(createdAt).getTime()) / 86_400_000))
|
|
: 30;
|
|
const days = Math.min(30, ageDays);
|
|
|
|
const raw = await apiFetch<BlockedByDayEntry[]>(
|
|
`/api/mail/stats/blocked-by-day?days=${days}&connectionId=${connectionId}`,
|
|
);
|
|
|
|
const nonEmpty = raw.filter((e) => e.count > 0);
|
|
let data: BlockedByDayEntry[];
|
|
|
|
if (granularity === 'week') {
|
|
data = aggregateToWeeks(raw);
|
|
} else if (granularity === 'month') {
|
|
data = aggregateToMonths(raw);
|
|
} else if (nonEmpty.length > 0 && days <= 7) {
|
|
// Short window: keep only days with data + days between first and last hit
|
|
const firstDate = nonEmpty[0].date;
|
|
const lastDate = nonEmpty[nonEmpty.length - 1].date;
|
|
data = raw.filter((e) => e.date >= firstDate && e.date <= lastDate);
|
|
} else {
|
|
data = raw;
|
|
}
|
|
|
|
setState({ data, granularity, loading: false });
|
|
} catch {
|
|
setState((s) => ({ ...s, loading: false }));
|
|
}
|
|
}, [enabled, connectionId, granularity, createdAt]);
|
|
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
if (fetchedRef.current) return;
|
|
fetchedRef.current = true;
|
|
fetch();
|
|
}, [enabled, fetch]);
|
|
|
|
return { ...state, refresh: fetch };
|
|
}
|
|
|
|
function aggregateToWeeks(entries: BlockedByDayEntry[]): BlockedByDayEntry[] {
|
|
const buckets: Map<string, number> = new Map();
|
|
for (const e of entries) {
|
|
const d = new Date(e.date + 'T00:00:00');
|
|
const day = d.getDay();
|
|
const diff = d.getDate() - day + (day === 0 ? -6 : 1);
|
|
const monday = new Date(d);
|
|
monday.setDate(diff);
|
|
const key = monday.toISOString().slice(0, 10);
|
|
buckets.set(key, (buckets.get(key) ?? 0) + e.count);
|
|
}
|
|
return [...buckets.entries()]
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
.map(([date, count]) => ({ date, count }));
|
|
}
|
|
|
|
function aggregateToMonths(entries: BlockedByDayEntry[]): BlockedByDayEntry[] {
|
|
const buckets: Map<string, number> = new Map();
|
|
for (const e of entries) {
|
|
const key = e.date.slice(0, 7);
|
|
buckets.set(key, (buckets.get(key) ?? 0) + e.count);
|
|
}
|
|
return [...buckets.entries()]
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
.map(([date, count]) => ({ date, count }));
|
|
}
|
|
|
|
export function useMailStats(enabled: boolean) {
|
|
const [state, setState] = useState<MailStatsState>({
|
|
blockedByDay: [],
|
|
blockedByConnection: [],
|
|
loading: false,
|
|
});
|
|
|
|
const fetch = useCallback(async () => {
|
|
if (!enabled) return;
|
|
setState((s) => ({ ...s, loading: true }));
|
|
try {
|
|
const [byDay, byConn] = await Promise.all([
|
|
apiFetch<BlockedByDayEntry[]>('/api/mail/stats/blocked-by-day?days=30'),
|
|
apiFetch<BlockedByConnectionEntry[]>('/api/mail/stats/blocked-by-connection'),
|
|
]);
|
|
setState({ blockedByDay: byDay, blockedByConnection: byConn, loading: false });
|
|
} catch {
|
|
setState((s) => ({ ...s, loading: false }));
|
|
}
|
|
}, [enabled]);
|
|
|
|
useEffect(() => {
|
|
fetch();
|
|
}, [fetch]);
|
|
|
|
return { ...state, refresh: fetch };
|
|
}
|