courrier refactor rebuild 2

This commit is contained in:
alma 2025-04-27 11:29:18 +02:00
parent c976b23a6c
commit 9f82be44cc
2 changed files with 67 additions and 4 deletions

View File

@ -0,0 +1,38 @@
import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { getUserEmailCredentials } from '@/lib/services/email-service';
export async function GET() {
try {
// Authenticate user
const session = await getServerSession(authOptions);
if (!session || !session.user?.id) {
return NextResponse.json(
{ error: "Not authenticated" },
{ status: 401 }
);
}
// Get user's email credentials
const credentials = await getUserEmailCredentials(session.user.id);
if (!credentials) {
return NextResponse.json(
{ error: "No email credentials found" },
{ status: 404 }
);
}
// Return the email address
return NextResponse.json({
email: credentials.email
});
} catch (error: any) {
console.error("Error fetching user email:", error);
return NextResponse.json(
{ error: "Failed to fetch user email", message: error.message },
{ status: 500 }
);
}
}

View File

@ -101,24 +101,49 @@ export default function CourrierPage() {
const [currentView, setCurrentView] = useState('INBOX');
const [unreadCount, setUnreadCount] = useState(0);
const [loading, setLoading] = useState(false);
const [userEmail, setUserEmail] = useState<string>('');
// Mock accounts for the sidebar
// Get user's email from session data
useEffect(() => {
const getUserEmail = async () => {
try {
// Try to get user email from API
const response = await fetch('/api/user/email');
if (response.ok) {
const data = await response.json();
if (data.email) {
setUserEmail(data.email);
}
}
} catch (error) {
console.error('Error fetching user email:', error);
}
};
getUserEmail();
}, []);
// Accounts for the sidebar (using the actual user email)
const [accounts, setAccounts] = useState<Account[]>([
{ id: 0, name: 'All', email: '', color: 'bg-gray-500' },
{ id: 1, name: 'Mail', email: 'user@example.com', color: 'bg-blue-500', folders: mailboxes }
{ id: 1, name: userEmail || 'Loading...', email: userEmail || '', color: 'bg-blue-500', folders: mailboxes }
]);
const [selectedAccount, setSelectedAccount] = useState<Account | null>(null);
// Update account folders when mailboxes change
// Update account folders and email when mailboxes or user email changes
useEffect(() => {
setAccounts(prev => {
const updated = [...prev];
if (updated[1]) {
updated[1].folders = mailboxes;
if (userEmail) {
updated[1].name = userEmail;
updated[1].email = userEmail;
}
}
return updated;
});
}, [mailboxes]);
}, [mailboxes, userEmail]);
// Calculate unread count (this would be replaced with actual data in production)
useEffect(() => {