diff --git a/components/email.tsx b/components/email.tsx index 1b1521e8..d9107ba2 100644 --- a/components/email.tsx +++ b/components/email.tsx @@ -5,101 +5,20 @@ 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 { formatDistance } from 'date-fns/formatDistance'; -import { fr } from 'date-fns/locale/fr'; - -interface Email { - id: string; - subject: string; - sender: { - name: string; - email: string; - }; - date: string; - isUnread: boolean; -} - -interface EmailResponse { - emails: Email[]; - mailUrl: string; - error?: string; -} export function Email() { - const [emails, setEmails] = useState([]); - const [mailUrl, setMailUrl] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); - const [refreshing, setRefreshing] = useState(false); const { status } = useSession(); - const fetchEmails = async (isRefresh = false) => { - if (isRefresh) setRefreshing(true); - if (!isRefresh) setLoading(true); - - try { - const response = await fetch('/api/emails'); - const data: EmailResponse = await response.json(); - - if (!response.ok) { - // Handle session expiration - if (response.status === 401) { - signIn(); // Redirect to login - return; - } - - // Handle specific error messages - if (response.status === 404) { - setError("L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur."); - return; - } - - throw new Error(data.error || 'Failed to fetch emails'); - } - - setEmails(data.emails || []); - setMailUrl(data.mailUrl); - setError(null); - } catch (err) { - console.error('Error fetching emails:', err); - setError(err instanceof Error ? err.message : 'Erreur lors de la récupération des emails'); - } finally { - setLoading(false); - setRefreshing(false); - } - }; - - // Initial fetch useEffect(() => { if (status === 'authenticated') { - fetchEmails(); + setLoading(false); + } else if (status === 'unauthenticated') { + signIn(); } }, [status]); - // Auto-refresh every 5 minutes - useEffect(() => { - if (status !== 'authenticated') return; - - const interval = setInterval(() => { - fetchEmails(true); - }, 5 * 60 * 1000); - - return () => clearInterval(interval); - }, [status]); - - const formatDate = (dateString: string) => { - try { - const date = new Date(dateString); - return formatDistance(date, new Date(), { - addSuffix: true, - locale: fr - }); - } catch (err) { - console.error('Error formatting date:', err); - return dateString; - } - }; - if (status === 'loading' || loading) { return ( @@ -107,7 +26,7 @@ export function Email() {
- Emails non lus + Emails
@@ -126,50 +45,16 @@ export function Email() {
- Emails non lus + Emails
- - - {error ? ( -

{error}

- ) : ( -
- {emails.length === 0 ? ( -

Aucun email non lu

- ) : ( - emails.map((email) => ( -
mailUrl && window.open(mailUrl, '_blank')} - > -
- - {email.sender.name} - -
- - {formatDate(email.date)} -
-
-

- {email.subject} -

-
- )) - )} -
- )} + +