"use client";
import { motion } from "framer-motion";
import { BookCheck, Star, ChevronDown, ChevronUp } from "lucide-react";
import { useEffect, useState } from "react";
import { useLocale, useTranslations } from "next-intl";
import Image from "next/image";
import { Skeleton } from "./ui/Skeleton";
interface BookReview {
id: string;
hardcover_id?: string;
book_title: string;
book_author: string;
book_image?: string;
rating?: number | null;
review?: string | null;
finished_at?: string;
}
const StarRating = ({ rating }: { rating: number }) => {
return (
{[1, 2, 3, 4, 5].map((star) => (
))}
);
};
const stripHtml = (html: string) => {
if (typeof window === 'undefined') return html; // Fallback for SSR
const doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent || "";
};
const ReadBooks = () => {
const locale = useLocale();
const t = useTranslations("home.about.readBooks");
const [reviews, setReviews] = useState([]);
const [loading, setLoading] = useState(true);
const [expanded, setExpanded] = useState(false);
const INITIAL_SHOW = 3;
useEffect(() => {
const fetchReviews = async () => {
try {
const res = await fetch(
`/api/book-reviews?locale=${encodeURIComponent(locale)}`,
{ cache: "default" }
);
if (!res.ok) {
throw new Error("Failed to fetch");
}
const data = await res.json();
if (data.bookReviews) {
setReviews(data.bookReviews);
} else {
setReviews([]);
}
} catch (error) {
if (process.env.NODE_ENV === "development") {
console.error("Error fetching book reviews:", error);
}
setReviews([]);
} finally {
setLoading(false);
}
};
fetchReviews();
}, [locale]);
if (loading) {
return (
);
}
if (reviews.length === 0) {
return (
{t("empty")}
);
}
const visibleReviews = expanded ? reviews : reviews.slice(0, INITIAL_SHOW);
const hasMore = reviews.length > INITIAL_SHOW;
return (
{/* Header */}
{t("title")} ({reviews.length})
{/* Book Reviews */}
{visibleReviews.map((review, index) => (
{/* Background Blob */}
{/* Book Cover */}
{review.book_image && (
)}
{/* Book Info */}
{review.book_title}
{review.book_author}
{/* Rating (Optional) */}
{review.rating && review.rating > 0 && (
{review.rating}/5
)}
{/* Review Text (Optional) */}
{review.review && (
“{stripHtml(review.review)}”
)}
{/* Finished Date */}
{review.finished_at && (
{t("finishedAt")}{" "}
{new Date(review.finished_at).toLocaleDateString(
locale === "de" ? "de-DE" : "en-US",
{ year: "numeric", month: "short" }
)}
)}
))}
{/* Show More / Show Less */}
{hasMore && (
setExpanded(!expanded)}
className="w-full flex items-center justify-center gap-1.5 py-2.5 text-sm font-medium text-stone-600 dark:text-stone-400 hover:text-stone-800 dark:hover:text-stone-200 rounded-lg border-2 border-dashed border-stone-200 dark:border-stone-700 hover:border-stone-300 dark:hover:border-stone-600 transition-colors duration-300"
>
{expanded ? (
<>
{t("showLess")}
>
) : (
<>
{t("showMore", { count: reviews.length - INITIAL_SHOW })}{" "}
>
)}
)}
);
};
export default ReadBooks;