All files / src/entities/project/ui/_shared ProjectCard.tsx

36.66% Statements 11/30
45.16% Branches 14/31
37.5% Functions 3/8
37.93% Lines 11/29

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                                          9x                     13x 13x   13x 13x 13x   13x                 13x                                     13x                                                                                                         4x               4x                                                                
import React, { useState } from 'react';
import { motion } from 'framer-motion';
import { useNavigate } from 'react-router-dom';
import { useAppDispatch } from '@/store/hooks';
import type { Project } from '@/shared/types';
import { PROJECT_PATHS } from '@/shared/constants';
import { projectService } from '@/entities/project/model';
import { activeProjectService } from '@/entities/settings/model';
import { ProjectCardButton, ProjectCardDates } from '@/entities/project/ui/project-card';
import {
	CheckIcon,
	EditIcon,
	OpenIcon,
	TrashIcon,
} from '@/entities/project/ui/_shared/ui/icon';
 
export type ProjectCardData = Pick<Project, 'id' | 'name' | 'createdAt' | 'updatedAt'> & {
	createdAt: number | string | Date;
	updatedAt: number | string | Date;
};
 
export const ProjectCard = React.memo(function ProjectCard({
	project,
	onEditClick,
	onDeleteClick,
	'data-testid': testId = 'project-card',
}: {
	project?: ProjectCardData;
	onEditClick?: (project: ProjectCardData) => void;
	onDeleteClick?: (project: ProjectCardData) => void;
	'data-testid'?: string;
}) {
	const navigate = useNavigate();
	const dispatch = useAppDispatch();
 
	const [flipped, setFlipped] = useState(false);
	const [shake, setShake] = useState(false);
	const [showBack, setShowBack] = useState(false);
 
	const triggerShakeAndFlip = () => {
		setShake(true);
		setTimeout(() => {
			setShake(false);
			setShowBack(true);
			setFlipped(true);
		}, 400);
	};
 
	const handleOpenClick = async () => {
		if (!project?.id) {
			triggerShakeAndFlip();
			return;
		}
 
		try {
			const existingProject = await projectService.getById(project.id);
			if (!existingProject) {
				triggerShakeAndFlip();
				return;
			}
			await activeProjectService.setActiveProject(dispatch, project.id);
			navigate(PROJECT_PATHS.EDITOR_BY_ID(project.id));
		} catch {
			triggerShakeAndFlip();
		}
	};
 
	const handleOk = () => {
		setFlipped(false);
		setTimeout(() => setShowBack(false), 600);
	};
 
	return (
		<div
			className="relative w-full max-w-[280px] h-[200px] perspective-[1000px]"
			data-testid={testId}
		>
			{/* FRONT SIDE */}
			<motion.div
				animate={
					shake
						? { x: [-4, 4, -3, 3, -2, 2, 0] }
						: flipped
							? { rotateY: 180 }
							: { rotateY: 0 }
				}
				transition={{
					duration: shake ? 0.4 : 0.6,
					ease: 'easeInOut',
				}}
				className="absolute w-full h-full backface-hidden"
			>
				<div className="bg-gradient-to-br from-white to-gray-50 border border-gray-200 shadow-sm hover:shadow-md rounded-2xl p-5 flex flex-col justify-between transition-all duration-200 w-full h-full">
					<div>
						<h3 className="text-lg font-semibold text-gray-900 mb-3 truncate">
							{project?.name ?? 'Unknown project'}
						</h3>
						<div className="w-10 border-t border-indigo-500 mb-4" />
						{project && (
							<ProjectCardDates
								createdAt={project.createdAt}
								updatedAt={project.updatedAt}
							/>
						)}
					</div>
 
					<div className="flex justify-between items-center mt-5">
						{/* OPEN BUTTON */}
						<ProjectCardButton
							label="Open"
							testId={`open-button-${project?.id ?? 'unknown'}`}
							color="indigo"
							onClick={handleOpenClick}
							icon={<OpenIcon />}
						/>
 
						<ProjectCardButton
							label="Update"
							testId={`update-button-${project?.id ?? 'unknown'}`}
							color="indigo"
							onClick={() => project && onEditClick?.(project)}
							icon={<EditIcon />}
						/>
 
						<ProjectCardButton
							label="Delete"
							testId={`delete-button-${project?.id ?? 'unknown'}`}
							color="rose"
							onClick={() => project && onDeleteClick?.(project)}
							icon={<TrashIcon />}
						/>
					</div>
				</div>
			</motion.div>
 
			{/* BACK SIDE */}
			{showBack && (
				<motion.div
					initial={{ rotateY: 180 }}
					animate={{ rotateY: flipped ? 0 : 180 }}
					transition={{ duration: 0.6 }}
					className="absolute w-full h-full backface-hidden [transform:rotateY(180deg)]"
				>
					<div className="flex flex-col justify-center items-center bg-rose-50 border border-rose-200 rounded-2xl w-full h-full text-center p-4">
						<p className="text-rose-600 font-semibold mb-3">
							Project {project?.name ?? 'Unknown'} not found
						</p>
						<ProjectCardButton
							label="OK"
							testId={`ok-button-${project?.id ?? 'unknown'}`}
							color="rose"
							onClick={handleOk}
							icon={<CheckIcon />}
						/>
					</div>
				</motion.div>
			)}
		</div>
	);
});