import { ActionIcon, Box, Button, Container, LoadingOverlay, Paper, ScrollArea, Text, TextInput, } from "@mantine/core"; import { useForm, zodResolver } from "@mantine/form"; import { useDisclosure } from "@mantine/hooks"; import { IconMail, IconTrash } from "@tabler/icons-react"; // Icon xóa import { nanoid } from "nanoid"; // Để tạo ID cho mỗi email mới import { useEffect, useRef, useState } from "react"; import { z } from "zod"; import { ConfirmModal } from "../components"; import { showNotification } from "../ultils/fn"; const schema = z.object({ email: z.string().email("Invalid email"), }); function MailsPage() { const viewport = useRef(null); const [opened, { open, close }] = useDisclosure(false); const [clickEmail, setClickEmail] = useState(null); const [emails, setEmails] = useState([]); // Dùng để lưu danh sách email const [loading, setLoading] = useState(false); const form = useForm({ initialValues: { email: "", }, validate: zodResolver(schema), }); const scrollToBottom = () => viewport.current!.scrollTo({ top: viewport.current!.scrollHeight, behavior: "smooth", }); // Hàm thêm email vào danh sách const handleAddEmail = async ({ email }: { email: string }) => { if (email.trim() !== "") { try { setLoading(true); const response = await window.ipcRenderer.invoke("add-email", email); if (!response) return; console.log("%csrc/pages/mails.tsx:40 {response}", "color: #007acc;", { response, }); await fetchEmails(); showNotification("Thành công", "Đã thêm email mới"); scrollToBottom(); setClickEmail(null); form.reset(); } catch (error) { console.log("%csrc/pages/mails.tsx:65 error", "color: #007acc;", error); } finally { setLoading(false); } } }; const handleDeleteEmail = async (id: number | undefined) => { try { if (!id) return; setLoading(true); await window.ipcRenderer.invoke("del-email", id); await fetchEmails(); close(); showNotification("Thông báo", "Đã xóa email"); } catch (error) { console.log("%csrc/pages/mails.tsx:88 error", "color: #007acc;", error); } finally { setLoading(false); } }; const fetchEmails = async () => { try { await window.ipcRenderer.invoke("connect-socket"); setLoading(true); const mails = await window.ipcRenderer.invoke("fetchEmails"); console.log("%csrc/App.tsx:29 {mails}", "color: #007acc;", { mails, }); setEmails(mails); } catch (error) { console.error("Failed to connect socket", error); } finally { setLoading(false); } }; useEffect(() => { fetchEmails(); return () => { // window.ipcRenderer.removeListener("new-note", handleNewNote); }; }, []); return (
{emails.length > 0 ? ( emails.map((item) => ( {item.email} { setClickEmail(item); open(); }} color="red" size="md" variant="light" > )) ) : ( No emails added yet. )} handleDeleteEmail(clickEmail?.id)} />
); } export default MailsPage;