From 14aa60421e7b6e2f5a66a4a04b1c11d32ed82760 Mon Sep 17 00:00:00 2001 From: karansaini47 Date: Fri, 15 May 2026 21:57:16 +0530 Subject: [PATCH] add gmail invoice compose handoff --- client/src/lib/gmail.ts | 45 ++++++++++++++++++++++++++ client/src/pages/InvoiceDetailPage.tsx | 39 +++++++++++----------- client/src/pages/InvoiceFormPage.tsx | 28 ++++++++++------ client/src/pages/InvoicesPage.tsx | 22 ++++++++----- 4 files changed, 96 insertions(+), 38 deletions(-) create mode 100644 client/src/lib/gmail.ts diff --git a/client/src/lib/gmail.ts b/client/src/lib/gmail.ts new file mode 100644 index 0000000..3ef4c3b --- /dev/null +++ b/client/src/lib/gmail.ts @@ -0,0 +1,45 @@ +import type { Invoice } from "@/types/invoice"; + +type GmailInvoice = Pick; + +const GMAIL_COMPOSE_URL = "https://mail.google.com/mail/"; + +const buildInvoiceEmailBody = (invoice: GmailInvoice) => + [ + `Hi ${invoice.clientName},`, + "", + `Please find invoice ${invoice.number} attached.`, + "", + "Thank you.", + ].join("\n"); + +export const buildInvoiceGmailComposeUrl = (invoice: GmailInvoice) => { + const url = new URL(GMAIL_COMPOSE_URL); + + url.searchParams.set("view", "cm"); + url.searchParams.set("fs", "1"); + url.searchParams.set("to", invoice.clientEmail); + url.searchParams.set("su", `Invoice ${invoice.number}`); + url.searchParams.set("body", buildInvoiceEmailBody(invoice)); + + return url.toString(); +}; + +export const openInvoiceInGmail = (invoice: GmailInvoice) => { + window.open(buildInvoiceGmailComposeUrl(invoice), "_blank", "noopener,noreferrer"); +}; + +export const getInvoicePdfFilename = (invoiceNumber: string) => + `invoice-${invoiceNumber}.pdf`; + +export const downloadBlobAsFile = (blob: Blob, filename: string) => { + const url = window.URL.createObjectURL(blob); + const link = document.createElement("a"); + + link.href = url; + link.download = filename; + document.body.appendChild(link); + link.click(); + link.remove(); + window.URL.revokeObjectURL(url); +}; diff --git a/client/src/pages/InvoiceDetailPage.tsx b/client/src/pages/InvoiceDetailPage.tsx index f65d903..8e9685b 100644 --- a/client/src/pages/InvoiceDetailPage.tsx +++ b/client/src/pages/InvoiceDetailPage.tsx @@ -7,6 +7,11 @@ import { Toast } from "@/components/Toast"; import { TopLoadingBar, useLoadingBar } from "@/components/TopLoadingBar"; import { invoicesApi } from "@/lib/api/invoices"; import { getApiErrorMessage } from "@/lib/apiErrors"; +import { + downloadBlobAsFile, + getInvoicePdfFilename, + openInvoiceInGmail, +} from "@/lib/gmail"; import { Page } from "@/pages/Page"; import type { Invoice, InvoiceStatus } from "@/types/invoice"; @@ -81,7 +86,7 @@ export function InvoiceDetailPage() { const [loading, setLoading] = useState(true); const [downloadLoading, setDownloadLoading] = useState(false); const [downloaded, setDownloaded] = useState(false); - const [sendLoading, setSendLoading] = useState(false); + const [gmailLoading, setGmailLoading] = useState(false); const [copied, setCopied] = useState(false); const [toast, setToast] = useState(null); const [error, setError] = useState(null); @@ -140,14 +145,7 @@ export function InvoiceDetailPage() { try { setDownloadLoading(true); const blob = await invoicesApi.downloadPdf(invoice.id); - const url = window.URL.createObjectURL(blob); - const link = document.createElement("a"); - link.href = url; - link.download = `invoice-${invoice.number}.pdf`; - document.body.appendChild(link); - link.click(); - link.remove(); - window.URL.revokeObjectURL(url); + downloadBlobAsFile(blob, getInvoicePdfFilename(invoice.number)); setDownloaded(true); downloadedTimer.current = window.setTimeout(() => setDownloaded(false), 2000); } catch (err) { @@ -158,22 +156,23 @@ export function InvoiceDetailPage() { } }; - const handleSendInvoice = async () => { + const handleOpenInGmail = async () => { if (!invoice) { return; } try { - setSendLoading(true); - const updatedInvoice = await invoicesApi.send(invoice.id); - setInvoice(updatedInvoice); - setToast(`Invoice sent to ${invoice.clientEmail}`); + setGmailLoading(true); + openInvoiceInGmail(invoice); + const blob = await invoicesApi.downloadPdf(invoice.id); + downloadBlobAsFile(blob, getInvoicePdfFilename(invoice.number)); + setToast("Gmail opened with the client email filled. Attach the downloaded PDF before sending."); setError(null); } catch (error) { - setError(getApiErrorMessage(error, "Failed to send invoice")); - console.error("Error sending invoice:", error); + setError(getApiErrorMessage(error, "Failed to prepare invoice for Gmail")); + console.error("Error preparing invoice for Gmail:", error); } finally { - setSendLoading(false); + setGmailLoading(false); } }; @@ -195,7 +194,7 @@ export function InvoiceDetailPage() { } if (event.key.toLowerCase() === "s" && invoice?.status === "DRAFT") { - void handleSendInvoice(); + void handleOpenInGmail(); } if (event.key === "Escape" || event.key === "Backspace") { @@ -267,8 +266,8 @@ export function InvoiceDetailPage() { Edit {invoice.status === "DRAFT" ? ( - ) : null} - diff --git a/client/src/pages/InvoicesPage.tsx b/client/src/pages/InvoicesPage.tsx index 3bef7d2..1c0e7e0 100644 --- a/client/src/pages/InvoicesPage.tsx +++ b/client/src/pages/InvoicesPage.tsx @@ -7,6 +7,11 @@ import { StatusBadge } from "@/components/StatusBadge"; import { Toast } from "@/components/Toast"; import { TopLoadingBar, useLoadingBar } from "@/components/TopLoadingBar"; import { invoicesApi } from "@/lib/api/invoices"; +import { + downloadBlobAsFile, + getInvoicePdfFilename, + openInvoiceInGmail, +} from "@/lib/gmail"; import { Page } from "@/pages/Page"; import { getApiErrorMessage } from "@/lib/apiErrors"; import { useAuthStore } from "@/store/auth"; @@ -147,16 +152,17 @@ export function InvoicesPage() { } }; - const handleSendInvoice = async (invoice: Invoice) => { + const handleOpenInGmail = async (invoice: Invoice) => { try { setSendingInvoiceId(invoice.id); - const updatedInvoice = await invoicesApi.send(invoice.id); - setInvoices((items) => items.map((item) => (item.id === invoice.id ? updatedInvoice : item))); - setToast(`Invoice sent to ${invoice.clientEmail}`); + openInvoiceInGmail(invoice); + const blob = await invoicesApi.downloadPdf(invoice.id); + downloadBlobAsFile(blob, getInvoicePdfFilename(invoice.number)); + setToast("Gmail opened with the client email filled. Attach the downloaded PDF before sending."); setError(null); } catch (error) { - setError(getApiErrorMessage(error, "Failed to send invoice")); - console.error("Error sending invoice:", error); + setError(getApiErrorMessage(error, "Failed to prepare invoice for Gmail")); + console.error("Error preparing invoice for Gmail:", error); } finally { setSendingInvoiceId(null); } @@ -366,11 +372,11 @@ export function InvoicesPage() { disabled={sendingInvoiceId === invoice.id} onClick={(event) => { event.stopPropagation(); - void handleSendInvoice(invoice); + void handleOpenInGmail(invoice); }} type="button" > - {sendingInvoiceId === invoice.id ? "Sending..." : "Send"} + {sendingInvoiceId === invoice.id ? "Preparing..." : "Open Gmail"} ) : null} {invoice.status === "SENT" ? (