47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
import { useState, useCallback } from 'react';
|
|
|
|
export interface Mail {
|
|
id: string;
|
|
from: string;
|
|
to: string;
|
|
subject: string;
|
|
body: string;
|
|
date: string;
|
|
read: boolean;
|
|
starred: boolean;
|
|
folder: string;
|
|
cc?: string[];
|
|
bcc?: string[];
|
|
flags?: string[];
|
|
}
|
|
|
|
export const useMail = () => {
|
|
const [mails, setMails] = useState<Mail[]>([]);
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const fetchMails = useCallback(async () => {
|
|
setIsLoading(true);
|
|
setError(null);
|
|
try {
|
|
const response = await fetch('/api/mail');
|
|
if (!response.ok) {
|
|
throw new Error('Failed to fetch mails');
|
|
}
|
|
const data = await response.json();
|
|
setMails(data.emails || []);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Failed to fetch mails');
|
|
setMails([]);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
return {
|
|
mails,
|
|
isLoading,
|
|
error,
|
|
fetchMails,
|
|
};
|
|
};
|