All files / src/components/projects ProjectListItem.tsx

62.5% Statements 10/16
95% Branches 19/20
53.84% Functions 7/13
62.5% Lines 10/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                                    18x         18x                 2x               18x 18x                                                                                         1x                   1x                                             1x                     1x                   1x                                                                                                                                            
import { memo } from "react";
import { Link, useNavigate } from "@tanstack/react-router";
import {
	Trash2,
	Image as ImageIcon,
	Eye,
	Archive,
	ArchiveRestore,
	MoreVertical,
	Clapperboard,
} from "lucide-react";
import { Button } from "../ui/button";
import { DropdownMenu } from "../ui/dropdown-menu";
import { type Project, getImageUrl } from "../../lib/api";
import { formatRelativeDate } from "../../lib/format-date";
 
interface ProjectListItemProps {
	project: Project;
	onDelete: (projectId: string) => void;
	onManagePhotos: (project: Project) => void;
	onPublish: (projectId: string) => void;
	onArchive: (projectId: string) => void;
	onUnarchive: (projectId: string) => void;
}
 
function getStatusBadge(status: string) {
	const styles: Record<string, string> = {
		draft: "bg-warning-light text-warning",
		published: "bg-success-light text-success",
		archived: "bg-background-muted text-foreground-muted",
	};
	return (
		<span
			className={`px-2 py-0.5 text-xs font-medium rounded-full shrink-0 ${styles[status] || styles.draft}`}
		>
			{status.charAt(0).toUpperCase() + status.slice(1)}
		</span>
	);
}
 
export const ProjectListItem = memo(function ProjectListItem({
	project,
	onDelete,
	onManagePhotos,
	onPublish,
	onArchive,
	onUnarchive,
}: ProjectListItemProps) {
	const navigate = useNavigate();
	return (
		<div className="flex items-center gap-3 sm:gap-4 p-3 border border-border-default rounded-lg bg-background-elevated hover:bg-background-muted/50 transition-colors">
			{/* Thumbnail */}
			<Link
				to="/projects/$projectId"
				params={{ projectId: project.id }}
				className="shrink-0"
			>
				<div className="h-12 w-12 rounded bg-background-muted flex items-center justify-center overflow-hidden">
					{project.coverImage ? (
						<img
							src={getImageUrl(project.coverImage)}
							alt={project.title}
							className="w-full h-full object-cover"
							loading="lazy"
						/>
					) : (
						<ImageIcon className="h-5 w-5 text-foreground-subtle" />
					)}
				</div>
			</Link>
 
			{/* Title + Status */}
			<div className="flex items-center gap-2 min-w-0 flex-1">
				<Link
					to="/projects/$projectId"
					params={{ projectId: project.id }}
					className="font-semibold text-sm text-foreground-default hover:text-primary-600 transition-colors truncate max-w-[200px]"
					title={project.title}
				>
					{project.title}
				</Link>
				{getStatusBadge(project.status)}
			</div>
 
			{/* Updated date — hidden on mobile */}
			<span className="hidden sm:block text-xs text-foreground-subtle shrink-0 w-20 text-right">
				{formatRelativeDate(project.dateUpdated)}
			</span>
 
			{/* Desktop action buttons */}
			<div className="hidden sm:flex items-center gap-1 shrink-0">
				<Button
					variant="ghost"
					size="sm"
					onClick={() => onManagePhotos(project)}
					title="Manage Photos"
					className="p-2"
				>
					<ImageIcon className="h-4 w-4" />
				</Button>
				{project.status === "draft" && (
					<Button
						variant="ghost"
						size="sm"
						onClick={() => onPublish(project.id)}
						title="Publish"
						className="p-2"
					>
						<Eye className="h-4 w-4" />
					</Button>
				)}
				{project.status === "published" && (
					<Link to="/social-studio/create" search={{ projectId: project.id }}>
						<Button
							variant="ghost"
							size="sm"
							title="Create Reel"
							className="p-2"
						>
							<Clapperboard className="h-4 w-4" />
						</Button>
					</Link>
				)}
				{project.status === "published" && (
					<Button
						variant="ghost"
						size="sm"
						onClick={() => onArchive(project.id)}
						title="Archive"
						className="p-2"
					>
						<Archive className="h-4 w-4" />
					</Button>
				)}
				{project.status === "archived" && (
					<Button
						variant="ghost"
						size="sm"
						onClick={() => onUnarchive(project.id)}
						title="Move to Draft"
						className="p-2"
					>
						<ArchiveRestore className="h-4 w-4" />
					</Button>
				)}
				<Button
					variant="ghost"
					size="sm"
					onClick={() => onDelete(project.id)}
					title="Delete"
					className="p-2 text-error hover:text-error hover:bg-error-light"
				>
					<Trash2 className="h-4 w-4" />
				</Button>
			</div>
 
			{/* Mobile overflow menu */}
			<div className="flex sm:hidden shrink-0">
				<DropdownMenu
					trigger={
						<Button variant="ghost" size="sm" className="p-2">
							<MoreVertical className="h-4 w-4" />
						</Button>
					}
					items={[
						{
							label: "Manage Photos",
							icon: <ImageIcon className="h-4 w-4" />,
							onClick: () => onManagePhotos(project),
						},
						...(project.status === "draft"
							? [
									{
										label: "Publish",
										icon: <Eye className="h-4 w-4" />,
										onClick: () => onPublish(project.id),
									},
								]
							: []),
						...(project.status === "published"
							? [
									{
										label: "Create Reel",
										icon: <Clapperboard className="h-4 w-4" />,
										onClick: () => navigate({ to: "/social-studio/create", search: { projectId: project.id } }),
									},
								]
							: []),
						...(project.status === "published"
							? [
									{
										label: "Archive",
										icon: <Archive className="h-4 w-4" />,
										onClick: () => onArchive(project.id),
									},
								]
							: []),
						...(project.status === "archived"
							? [
									{
										label: "Move to Draft",
										icon: <ArchiveRestore className="h-4 w-4" />,
										onClick: () => onUnarchive(project.id),
									},
								]
							: []),
						{
							label: "Delete",
							icon: <Trash2 className="h-4 w-4" />,
							onClick: () => onDelete(project.id),
							variant: "destructive" as const,
						},
					]}
				/>
			</div>
		</div>
	);
});