import { useCallback } from "react"
import {
    Alert,
    AlertDescription,
    AlertTitle,
} from "@/components/ui/alert";
import { AlertCircle } from "lucide-react";
import { base62 } from "@/lib/utils";
import { BlogAPI } from "@/lib/api/server";

export default async function Blog() {
    const formatNumber = useCallback((num: number) => {
        if (num >= 1000) {
            return (num / 1000).toFixed(1) + 'K';
        } else if (num >= 1000000) {
            return (num / 1000000).toFixed(1) + 'M';
        }
        return num.toString();
    }, []);

    let errorMsg = '';
    const blogs = await BlogAPI.list().catch(e => { errorMsg = `${e}`; return null });

    return (
        <div className="max-w-120 w-auto mx-auto my-10 flex flex-col gap-8">
            {
                errorMsg && (
                    <Alert variant="destructive" className="w-full">
                        <AlertCircle className="h-4 w-4" />
                        <AlertTitle>出错啦</AlertTitle>
                        <AlertDescription>
                            {errorMsg}
                        </AlertDescription>
                    </Alert>
                )
            }
            {
                blogs && blogs.map((blog) => (
                    <div className="w-full px-5 cursor-default" key={blog.id}>
                        <a className="text-2xl font-medium cursor-pointer hover:underline" target="_blank" href={`/blog/${base62.encode(Buffer.from(blog.id.replace(/-/g, ''), 'hex'))}`}>{blog.title}</a>
                        <p className="text-sm font-medium text-zinc-400">{blog.description}</p>
                        <p className="text-sm font-medium text-zinc-400 mt-3">{new Date(blog.createdAt).toLocaleString()} · {formatNumber(blog.viewCount)} 次访问</p>
                    </div>
                ))
            }
        </div>
    )
}