courrier redis
This commit is contained in:
parent
b2d356a6b2
commit
d73442573d
@ -209,7 +209,9 @@ export default function CourrierPage() {
|
||||
// Handle loading more emails on scroll
|
||||
const handleLoadMore = () => {
|
||||
if (hasMoreEmails && !isLoading) {
|
||||
// Increment the page and call loadEmails with isLoadMore=true
|
||||
setPage(page + 1);
|
||||
// Note: loadEmails will be called automatically due to the page dependency in useEffect
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -76,13 +76,6 @@ export const useCourrier = () => {
|
||||
const { data: session } = useSession();
|
||||
const { toast } = useToast();
|
||||
|
||||
// Load emails when folder or page changes
|
||||
useEffect(() => {
|
||||
if (session?.user?.id) {
|
||||
loadEmails();
|
||||
}
|
||||
}, [currentFolder, page, perPage, session?.user?.id]);
|
||||
|
||||
// Load emails from the server
|
||||
const loadEmails = useCallback(async (isLoadMore = false) => {
|
||||
if (!session?.user?.id) return;
|
||||
@ -96,10 +89,33 @@ export const useCourrier = () => {
|
||||
if (cachedEmails) {
|
||||
// Ensure cached data has emails array property
|
||||
if (Array.isArray(cachedEmails.emails)) {
|
||||
setEmails(prevEmails => isLoadMore ? [...prevEmails, ...cachedEmails.emails] : cachedEmails.emails);
|
||||
if (isLoadMore) {
|
||||
// When loading more, always append to the existing list
|
||||
setEmails(prevEmails => {
|
||||
// Create a Set of existing email IDs to avoid duplicates
|
||||
const existingIds = new Set(prevEmails.map(email => email.id));
|
||||
// Filter out any duplicates before appending
|
||||
const newEmails = cachedEmails.emails.filter((email: Email) => !existingIds.has(email.id));
|
||||
return [...prevEmails, ...newEmails];
|
||||
});
|
||||
} else {
|
||||
// For initial load, replace emails
|
||||
setEmails(cachedEmails.emails);
|
||||
}
|
||||
} else if (Array.isArray(cachedEmails)) {
|
||||
// Direct array response
|
||||
setEmails(prevEmails => isLoadMore ? [...prevEmails, ...cachedEmails] : cachedEmails);
|
||||
if (isLoadMore) {
|
||||
setEmails(prevEmails => {
|
||||
// Create a Set of existing email IDs to avoid duplicates
|
||||
const existingIds = new Set(prevEmails.map(email => email.id));
|
||||
// Filter out any duplicates before appending
|
||||
const newEmails = cachedEmails.filter((email: Email) => !existingIds.has(email.id));
|
||||
return [...prevEmails, ...newEmails];
|
||||
});
|
||||
} else {
|
||||
// For initial load, replace emails
|
||||
setEmails(cachedEmails);
|
||||
}
|
||||
} else {
|
||||
console.warn('Invalid cache format:', cachedEmails);
|
||||
}
|
||||
@ -136,7 +152,13 @@ export const useCourrier = () => {
|
||||
|
||||
// Update state with the fetched data
|
||||
if (isLoadMore) {
|
||||
setEmails(prev => [...prev, ...data.emails]);
|
||||
setEmails(prev => {
|
||||
// Create a Set of existing email IDs to avoid duplicates
|
||||
const existingIds = new Set(prev.map(email => email.id));
|
||||
// Filter out any duplicates before appending
|
||||
const newEmails = data.emails.filter((email: Email) => !existingIds.has(email.id));
|
||||
return [...prev, ...newEmails];
|
||||
});
|
||||
} else {
|
||||
// Ensure we always set an array even if API returns invalid data
|
||||
setEmails(Array.isArray(data.emails) ? data.emails : []);
|
||||
@ -158,7 +180,9 @@ export const useCourrier = () => {
|
||||
} catch (err) {
|
||||
console.error('Error loading emails:', err);
|
||||
// Set emails to empty array on error to prevent runtime issues
|
||||
setEmails([]);
|
||||
if (!isLoadMore) {
|
||||
setEmails([]);
|
||||
}
|
||||
setError(err instanceof Error ? err.message : 'Failed to load emails');
|
||||
toast({
|
||||
variant: "destructive",
|
||||
@ -170,6 +194,15 @@ export const useCourrier = () => {
|
||||
}
|
||||
}, [currentFolder, page, perPage, searchQuery, session?.user?.id, toast]);
|
||||
|
||||
// Load emails when folder or page changes
|
||||
useEffect(() => {
|
||||
if (session?.user?.id) {
|
||||
// If page is greater than 1, we're loading more emails
|
||||
const isLoadingMore = page > 1;
|
||||
loadEmails(isLoadingMore);
|
||||
}
|
||||
}, [currentFolder, page, perPage, session?.user?.id, loadEmails]);
|
||||
|
||||
// Fetch a single email's content
|
||||
const fetchEmailContent = useCallback(async (emailId: string) => {
|
||||
try {
|
||||
|
||||
@ -84,7 +84,7 @@ export const KEYS = {
|
||||
// TTL constants in seconds
|
||||
export const TTL = {
|
||||
CREDENTIALS: 60 * 60 * 24, // 24 hours
|
||||
SESSION: 60 * 30, // 30 minutes
|
||||
SESSION: 60 * 60 * 4, // 4 hours (increased from 30 minutes)
|
||||
EMAIL_LIST: 60 * 5, // 5 minutes
|
||||
EMAIL_CONTENT: 60 * 15 // 15 minutes
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user