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