mail page rest
This commit is contained in:
parent
0c15835b59
commit
305ca84ac2
@ -7,7 +7,9 @@ import { ImapFlow } from 'imapflow';
|
||||
export async function POST(request: Request) {
|
||||
let client: ImapFlow | null = null;
|
||||
try {
|
||||
// Get the session and validate it
|
||||
const session = await getServerSession(authOptions);
|
||||
console.log('Session:', session); // Debug log
|
||||
|
||||
if (!session?.user?.id) {
|
||||
console.error('No session or user ID found');
|
||||
@ -17,7 +19,9 @@ export async function POST(request: Request) {
|
||||
);
|
||||
}
|
||||
|
||||
// Get the request body
|
||||
const { emailId, isRead } = await request.json();
|
||||
console.log('Request body:', { emailId, isRead }); // Debug log
|
||||
|
||||
if (!emailId || typeof isRead !== 'boolean') {
|
||||
console.error('Invalid request parameters:', { emailId, isRead });
|
||||
@ -30,6 +34,7 @@ export async function POST(request: Request) {
|
||||
// Get the current folder from the request URL
|
||||
const url = new URL(request.url);
|
||||
const folder = url.searchParams.get('folder') || 'INBOX';
|
||||
console.log('Folder:', folder); // Debug log
|
||||
|
||||
try {
|
||||
// Initialize IMAP client with user credentials
|
||||
|
||||
@ -27,6 +27,7 @@ import {
|
||||
AlertOctagon, Archive, RefreshCw
|
||||
} from 'lucide-react';
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import { useSession } from 'next-auth/react';
|
||||
|
||||
interface Account {
|
||||
id: number;
|
||||
@ -593,8 +594,9 @@ const initialSidebarItems = [
|
||||
}
|
||||
];
|
||||
|
||||
export default function MailPage() {
|
||||
export default function CourrierPage() {
|
||||
const router = useRouter();
|
||||
const { data: session } = useSession();
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [accounts, setAccounts] = useState<Account[]>([
|
||||
{ id: 0, name: 'All', email: '', color: 'bg-gray-500' },
|
||||
@ -637,7 +639,46 @@ export default function MailPage() {
|
||||
const [page, setPage] = useState(1);
|
||||
const [hasMore, setHasMore] = useState(true);
|
||||
const [isLoadingMore, setIsLoadingMore] = useState(false);
|
||||
const emailsPerPage = 24;
|
||||
const [isLoadingInitial, setIsLoadingInitial] = useState(true);
|
||||
const [isLoadingSearch, setIsLoadingSearch] = useState(false);
|
||||
const [isLoadingCompose, setIsLoadingCompose] = useState(false);
|
||||
const [isLoadingReply, setIsLoadingReply] = useState(false);
|
||||
const [isLoadingForward, setIsLoadingForward] = useState(false);
|
||||
const [isLoadingDelete, setIsLoadingDelete] = useState(false);
|
||||
const [isLoadingMove, setIsLoadingMove] = useState(false);
|
||||
const [isLoadingStar, setIsLoadingStar] = useState(false);
|
||||
const [isLoadingUnstar, setIsLoadingUnstar] = useState(false);
|
||||
const [isLoadingMarkRead, setIsLoadingMarkRead] = useState(false);
|
||||
const [isLoadingMarkUnread, setIsLoadingMarkUnread] = useState(false);
|
||||
const [isLoadingRefresh, setIsLoadingRefresh] = useState(false);
|
||||
const emailsPerPage = 20;
|
||||
const [isSearching, setIsSearching] = useState(false);
|
||||
const [searchResults, setSearchResults] = useState<Email[]>([]);
|
||||
const [showSearchResults, setShowSearchResults] = useState(false);
|
||||
const [isComposing, setIsComposing] = useState(false);
|
||||
const [composeEmail, setComposeEmail] = useState({
|
||||
to: '',
|
||||
subject: '',
|
||||
body: '',
|
||||
});
|
||||
const [isSending, setIsSending] = useState(false);
|
||||
const [isReplying, setIsReplying] = useState(false);
|
||||
const [isForwarding, setIsForwarding] = useState(false);
|
||||
const [replyToEmail, setReplyToEmail] = useState<Email | null>(null);
|
||||
const [forwardEmail, setForwardEmail] = useState<Email | null>(null);
|
||||
const [replyBody, setReplyBody] = useState('');
|
||||
const [forwardBody, setForwardBody] = useState('');
|
||||
const [replyAttachments, setReplyAttachments] = useState<File[]>([]);
|
||||
const [forwardAttachments, setForwardAttachments] = useState<File[]>([]);
|
||||
const [isSendingReply, setIsSendingReply] = useState(false);
|
||||
const [isSendingForward, setIsSendingForward] = useState(false);
|
||||
const [isDeleting, setIsDeleting] = useState(false);
|
||||
const [isMoving, setIsMoving] = useState(false);
|
||||
const [isStarring, setIsStarring] = useState(false);
|
||||
const [isUnstarring, setIsUnstarring] = useState(false);
|
||||
const [isMarkingRead, setIsMarkingRead] = useState(false);
|
||||
const [isMarkingUnread, setIsMarkingUnread] = useState(false);
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
|
||||
// Debug logging for email distribution
|
||||
useEffect(() => {
|
||||
@ -778,66 +819,60 @@ export default function MailPage() {
|
||||
|
||||
// Update handleEmailSelect to set selectedEmail correctly
|
||||
const handleEmailSelect = async (emailId: number) => {
|
||||
const email = emails.find(e => e.id === emailId);
|
||||
if (!email) {
|
||||
console.error('Email not found in list');
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the selected email first to show preview immediately
|
||||
setSelectedEmail(email);
|
||||
|
||||
// Fetch the full email content
|
||||
const response = await fetch(`/api/mail/${emailId}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch full email content');
|
||||
}
|
||||
|
||||
const fullEmail = await response.json();
|
||||
|
||||
// Update the email in the list and selected email with full content
|
||||
setEmails(prevEmails => prevEmails.map(email =>
|
||||
email.id === emailId
|
||||
? { ...email, body: fullEmail.body }
|
||||
: email
|
||||
));
|
||||
|
||||
setSelectedEmail(prev => prev ? { ...prev, body: fullEmail.body } : prev);
|
||||
|
||||
// Try to mark as read in the background
|
||||
try {
|
||||
// Find the email in the existing emails list for immediate preview
|
||||
const emailToSelect = emails.find(email => email.id === emailId);
|
||||
if (!emailToSelect) {
|
||||
console.error('Email not found in list');
|
||||
return;
|
||||
const markReadResponse = await fetch(`/api/mail/mark-read`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${session?.user?.id}` // Add session token
|
||||
},
|
||||
body: JSON.stringify({
|
||||
emailId,
|
||||
isRead: true,
|
||||
}),
|
||||
});
|
||||
|
||||
if (markReadResponse.ok) {
|
||||
// Only update the emails list if the API call was successful
|
||||
setEmails((prevEmails: Email[]) =>
|
||||
prevEmails.map((email: Email): Email =>
|
||||
email.id === emailId
|
||||
? { ...email, read: true }
|
||||
: email
|
||||
)
|
||||
);
|
||||
} else {
|
||||
console.error('Failed to mark email as read:', await markReadResponse.text());
|
||||
}
|
||||
|
||||
// Set the selected email first to show preview immediately
|
||||
setSelectedEmail(emailToSelect);
|
||||
|
||||
// Fetch the full email content
|
||||
const response = await fetch(`/api/mail/${emailId}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch full email content');
|
||||
}
|
||||
|
||||
const fullEmail = await response.json();
|
||||
|
||||
// Update the email in the list and selected email with full content
|
||||
setEmails(prevEmails => prevEmails.map(email =>
|
||||
email.id === emailId
|
||||
? { ...email, body: fullEmail.body }
|
||||
: email
|
||||
));
|
||||
|
||||
setSelectedEmail(prev => prev ? { ...prev, body: fullEmail.body } : prev);
|
||||
|
||||
// Try to mark as read in the background
|
||||
try {
|
||||
const markReadResponse = await fetch(`/api/mail/mark-read`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
emailId,
|
||||
isRead: true,
|
||||
}),
|
||||
});
|
||||
|
||||
if (markReadResponse.ok) {
|
||||
// Only update the emails list if the API call was successful
|
||||
setEmails((prevEmails: Email[]) =>
|
||||
prevEmails.map((email: Email): Email =>
|
||||
email.id === emailId
|
||||
? { ...email, read: true }
|
||||
: email
|
||||
)
|
||||
);
|
||||
} else {
|
||||
console.error('Failed to mark email as read:', await markReadResponse.text());
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error marking email as read:', error);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error selecting email:', error);
|
||||
setError('Failed to load email content');
|
||||
console.error('Error marking email as read:', error);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user