import { useEffect, useState } from "react"; import { useNavigate, useParams } from "react-router-dom"; import { API_URL } from "./constants"; import type { Blog } from "./types"; import Markdown from "react-markdown"; import remarkGfm from "remark-gfm"; import remarkRehype from "remark-rehype"; import rehypeRaw from "rehype-raw"; import "../styles/markdown.css"; export function BlogViewer() { const { slug } = useParams<{ slug: string }>(); const [blog, setBlog] = useState(null); const navigate = useNavigate(); const me = localStorage.getItem("user_id"); useEffect(() => { if (!slug) return; fetch(`${API_URL}/blogs/${slug}`, { method: "GET", headers: { "Content-Type": "application/json", }, }) .then((res) => res.json()) .then((data: Blog) => setBlog(data)) .catch((err) => console.error(err)); }, [slug]); if (!blog) return

Loading…

; const isAuthor = me === String(blog.author_id); const handleDelete = async () => { if (!window.confirm(`Are you sure you want to delete ${blog.title}`)) return; try { const res = await fetch(`${API_URL}/blogs/${slug}`, { method: "DELETE", headers: { Accept: "application/json" }, }); if (!res.ok) throw new Error(await res.text()); navigate("/"); } catch (err) { console.error("Delete failed:", err); alert("Could not delete post. See console for details."); } }; return (
{isAuthor && (
)}

{blog.title.toUpperCase()}

By {blog.author_name}

); }