import { useEffect, useState } from "react"; import { useParams, useNavigate } from "react-router-dom"; import { getRole } from "../api/client"; import { PDFDownloadLink, Document, Page, Text, View, StyleSheet } from "@react-pdf/renderer"; import { api } from "../api/client"; interface LetterData { id: number; title: string | null; content: string; createdAt: string; deliverAt: string | null; } const pdfStyles = StyleSheet.create({ page: { padding: 60, fontFamily: "Helvetica", backgroundColor: "#fdf8f0", }, header: { marginBottom: 32, borderBottomWidth: 1, borderBottomColor: "#e8d5c0", paddingBottom: 16, }, title: { fontSize: 22, marginBottom: 6, color: "#2c1810", }, date: { fontSize: 10, color: "#6b4f3a", }, content: { fontSize: 12, lineHeight: 1.8, color: "#2c1810", }, footer: { position: "absolute", bottom: 40, left: 60, right: 60, textAlign: "center", fontSize: 10, color: "#c9727a", }, }); function stripHtml(html: string): string { return html .replace(/]*>/gi, "[image]") .replace(//gi, "\n") .replace(/<\/p>/gi, "\n\n") .replace(/<\/?(h[1-6]|li|blockquote)[^>]*>/gi, "\n") .replace(/<[^>]+>/g, "") .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/ /g, " ") .replace(/\n{3,}/g, "\n\n") .trim(); } function LetterPDF({ letter }: { letter: LetterData }) { const dateStr = new Date(letter.createdAt).toLocaleDateString("en-US", { weekday: "long", year: "numeric", month: "long", day: "numeric", }); return ( {letter.title && {letter.title}} {dateStr} {stripHtml(letter.content)} hibb ♡ ); } export default function LetterView() { const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); const [letter, setLetter] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); useEffect(() => { if (!id) return; api .getLetter(parseInt(id, 10)) .then(setLetter) .catch(() => setError("Letter not found.")) .finally(() => setLoading(false)); }, [id]); if (loading) { return
Loading...
; } if (error || !letter) { return (
{error || "Letter not found."}
); } const localDate = letter.letterDate ?? letter.createdAt.slice(0, 10); const dateStr = new Date(`${localDate}T00:00:00`).toLocaleDateString("en-US", { weekday: "long", year: "numeric", month: "long", day: "numeric", }); const d = new Date(); const todayLocal = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`; const isToday = localDate === todayLocal; return (
{isToday && getRole() === "writer" && ( )} } fileName={`letter-${letter.createdAt.slice(0, 10)}.pdf`} > {({ loading: pdfLoading }) => ( )}

{dateStr}

); }