database wf 13

This commit is contained in:
alma 2025-04-17 13:38:35 +02:00
parent d62f02b1df
commit ec8304cd38

View File

@ -4,7 +4,7 @@ import { useEffect, useState } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { RefreshCw, Mail } from "lucide-react";
import { useSession, signIn } from "next-auth/react";
import { useSession } from "next-auth/react";
import { formatDistance } from 'date-fns/formatDistance';
import { fr } from 'date-fns/locale/fr';
import { useRouter } from "next/navigation";
@ -32,29 +32,29 @@ export function Email() {
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [refreshing, setRefreshing] = useState(false);
const { status } = useSession();
const { data: session, status } = useSession();
const router = useRouter();
const fetchEmails = async (isRefresh = false) => {
if (status !== 'authenticated') {
setError('Please sign in to view emails');
setLoading(false);
setRefreshing(false);
return;
}
if (isRefresh) setRefreshing(true);
if (!isRefresh) setLoading(true);
try {
console.log('Starting fetch request...');
const response = await fetch('/api/mail');
console.log('Response status:', response.status);
if (!response.ok) {
if (response.status === 401) {
signIn();
return;
}
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to fetch emails');
}
const data = await response.json();
console.log('Parsed API Response:', data);
if (data.error) {
throw new Error(data.error);
@ -71,12 +71,10 @@ export function Email() {
folder: email.folder || 'INBOX'
}));
console.log('Processed emails:', validatedEmails);
setEmails(validatedEmails);
setMailUrl(data.mailUrl || 'https://espace.slm-lab.net/apps/mail/');
setError(null);
} catch (err) {
console.error('Fetch error:', err);
setError(err instanceof Error ? err.message : 'Error fetching emails');
setEmails([]);
} finally {
@ -85,29 +83,13 @@ export function Email() {
}
};
// Helper functions to parse email addresses
const extractSenderName = (from: string): string => {
if (!from) return 'Unknown';
const match = from.match(/^([^<]+)?/);
if (match && match[1]) {
return match[1].trim().replace(/"/g, '');
}
return from;
};
const extractEmailAddress = (from: string): string => {
if (!from) return '';
const match = from.match(/<([^>]+)>/);
if (match && match[1]) {
return match[1];
}
return from;
};
// Initial fetch
useEffect(() => {
if (status === 'authenticated') {
fetchEmails();
} else if (status === 'unauthenticated') {
setError('Please sign in to view emails');
setLoading(false);
}
}, [status]);
@ -130,7 +112,6 @@ export function Email() {
locale: fr
});
} catch (err) {
console.error('Error formatting date:', err);
return dateString;
}
};
@ -199,9 +180,7 @@ export function Email() {
<span className="text-xs text-gray-500">{formatDate(email.date)}</span>
</div>
</div>
<h3 className="text-sm font-semibold text-gray-800 line-clamp-2" title={email.subject}>
{email.subject}
</h3>
<p className="text-sm text-gray-800 truncate">{email.subject}</p>
</div>
))
)}