File size: 3,989 Bytes
c7fbc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use client";

import { format } from "date-fns";
import { Trash2, Edit2, Play } from "lucide-react";
import { useState } from "react";
import { useRouter } from "next/navigation";

export default function PostCard({ post, isPast }: { post: any, isPast?: boolean }) {
    const router = useRouter();
    const [isDeleting, setIsDeleting] = useState(false);

    const handleDelete = async (e: React.MouseEvent) => {
        e.stopPropagation();
        if (!confirm("Delete post?")) return;
        setIsDeleting(true);
        try {
            await fetch(`/api/posts?id=${post.id}`, { method: "DELETE" });
            router.refresh();
        } catch (e) { console.error(e); }
        finally { setIsDeleting(false); }
    };

    return (
        <div className={`bg-white dark:bg-zinc-900 border border-gray-200 dark:border-zinc-800 rounded-lg overflow-hidden shadow-sm hover:shadow-md transition-shadow ${isPast ? 'opacity-75 grayscale-[0.3]' : ''}`}>
            {/* Media */}
            <div className="h-48 bg-gray-100 dark:bg-zinc-800 relative">
                {post.mediaType === "VIDEO" ? (
                    <div className="w-full h-full flex items-center justify-center bg-black/5">
                        <Play className="text-gray-400" />
                    </div>
                ) : (
                    <img src={post.mediaUrl.split(',')[0]} alt="" className="w-full h-full object-cover" />
                )}

                <div className="absolute top-2 right-2 flex gap-1">
                    <span className={`text-[10px] font-bold px-2 py-0.5 rounded border bg-white dark:bg-zinc-900 ${post.status === 'PUBLISHED' ? 'text-green-600 border-green-200' : 'text-gray-600 border-gray-200'}`}>
                        {post.status}
                    </span>
                </div>
            </div>

            {/* Content */}
            <div className="p-4">
                <div className="flex justify-between items-start mb-2">
                    <div className="flex items-center gap-2">
                        <div className="w-6 h-6 rounded-full bg-gray-200 dark:bg-zinc-800 overflow-hidden flex items-center justify-center border border-gray-100 dark:border-zinc-700">
                            {post.accountProfilePictureUrl ? (
                                <img src={post.accountProfilePictureUrl} alt={post.accountUsername} className="w-full h-full object-cover" />
                            ) : (
                                <span className="text-[10px] font-bold text-gray-500">{(post.accountUsername || "U")[0].toUpperCase()}</span>
                            )}
                        </div>
                        <p className="text-xs font-bold text-gray-500">@{post.accountUsername}</p>
                    </div>
                    <p className="text-xs text-gray-400">
                        {post.scheduledAt ? format(new Date(post.scheduledAt), "MMM d") : "Draft"}
                    </p>
                </div>
                <p className="text-sm text-gray-900 dark:text-white line-clamp-2 min-h-[2.5rem]">
                    {post.caption || "No caption"}
                </p>

                {/* Actions */}
                <div className="mt-4 pt-4 border-t border-gray-100 dark:border-zinc-800 flex justify-end gap-2">
                    <button
                        onClick={() => router.push(`/dashboard/create/editor?edit=${post.id}`)}
                        className="px-3 py-1.5 text-xs text-gray-600 border border-gray-200 rounded hover:bg-gray-50"
                    >
                        Edit
                    </button>
                    <button
                        onClick={handleDelete}
                        className="px-3 py-1.5 text-xs text-red-600 border border-red-200 rounded hover:bg-red-50"
                    >
                        {isDeleting ? "..." : "Delete"}
                    </button>
                </div>
            </div>
        </div>
    )
}