All files / src/components/rooms RoomCard.tsx

100% Statements 34/34
95.55% Branches 43/45
100% Functions 23/23
100% Lines 32/32

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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292                                                                                                                39x 39x 39x     39x 39x 39x     39x 39x 39x   39x                                                     1x             1x                           2x 2x                     2x 2x                   1x                           1x                                 1x   1x 1x                         1x 1x                                       35x               1x   1x 1x                         1x 1x                                     1x                     1x                           1x                          
import type { ChangeEvent, DragEvent } from "react";
import { Edit2, Trash2, ChevronRight, Upload, Image, Plus } from "lucide-react";
import type { Room, Media, RoomType } from "../../lib/api";
import { getImageUrl } from "../../lib/api";
 
type RoomCardProps = {
	room: Room;
	roomTypes: RoomType[];
	roomMedia: Media[];
	uploadingRoomId: number | null;
	draggedMediaId: number | null;
	dragOverMediaId: number | null;
	draggingInRoom: number | null;
	onEdit: (room: Room) => void;
	onDelete: (roomId: number) => void;
	onSelectRoom: (room: Room) => void;
	onFileSelect: (roomId: number, e: ChangeEvent<HTMLInputElement>) => void;
	onMediaDragStart: (
		e: DragEvent<HTMLElement>,
		mediaId: number,
		roomId: number,
	) => void;
	onMediaDragEnd: (e: DragEvent<HTMLElement>) => void;
	onMediaDragOver: (
		e: DragEvent<HTMLElement>,
		mediaId: number,
		roomId: number,
	) => void;
	onMediaDrop: (
		e: DragEvent<HTMLElement>,
		mediaId: number,
		roomId: number,
	) => void;
	onDeleteMedia: (mediaId: number, roomId: number) => void;
	triggerFileInput: (roomId: number) => void;
};
 
export function RoomCard({
	room,
	roomTypes,
	roomMedia,
	uploadingRoomId,
	draggedMediaId,
	dragOverMediaId,
	draggingInRoom,
	onEdit,
	onDelete,
	onSelectRoom,
	onFileSelect,
	onMediaDragStart,
	onMediaDragEnd,
	onMediaDragOver,
	onMediaDrop,
	onDeleteMedia,
	triggerFileInput,
}: RoomCardProps) {
	const getRoomTypeName = (code: string) => {
		const roomType = roomTypes.find((rt) => rt.code === code);
		return roomType?.displayName || code;
	};
 
	const getRoomIcon = (code: string) => {
		const roomType = roomTypes.find((rt) => rt.code === code);
		return roomType?.icon || "🏠";
	};
 
	const heroImage = roomMedia[0];
	const thumbnails = roomMedia.slice(1, 4);
	const remainingCount = roomMedia.length > 4 ? roomMedia.length - 4 : 0;
 
	return (
		<div className="border border-border-default rounded-lg overflow-hidden bg-background-elevated shadow-sm hover:shadow-md transition-shadow">
			{/* Room Header */}
			<div className="flex items-center justify-between px-4 py-3 bg-background-muted border-b border-border-default">
				<div className="flex items-center gap-2">
					<span className="text-xl">{getRoomIcon(room.roomType)}</span>
					<div>
						<h3 className="font-medium text-sm">
							{room.name || getRoomTypeName(room.roomType)}
						</h3>
						{room.name && (
							<p className="text-xs text-foreground-muted">
								{getRoomTypeName(room.roomType)}
							</p>
						)}
					</div>
					{room.isDefault && (
						<span className="text-xs bg-primary-100 text-primary-700 px-2 py-0.5 rounded ml-2">
							Default
						</span>
					)}
				</div>
				<div className="flex items-center gap-1">
					{/* Upload button */}
					<input
						type="file"
						id={`room-file-input-${room.id}`}
						onChange={(e) => onFileSelect(room.id, e)}
						accept="image/jpeg,image/png,image/webp,image/gif,image/avif,video/mp4,video/webm,video/quicktime"
						multiple
						className="sr-only"
					/>
					<button
						type="button"
						onClick={() => triggerFileInput(room.id)}
						disabled={uploadingRoomId === room.id}
						className="p-1.5 text-foreground-muted hover:text-primary-600 hover:bg-primary-50 rounded"
						title="Upload images"
					>
						{uploadingRoomId === room.id ? (
							<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-primary-600" />
						) : (
							<Upload className="h-4 w-4" />
						)}
					</button>
					<button
						type="button"
						onClick={(e) => {
							e.stopPropagation();
							onEdit(room);
						}}
						className="p-1.5 text-foreground-muted hover:text-primary-600 hover:bg-primary-50 rounded"
						title="Edit room"
					>
						<Edit2 className="h-4 w-4" />
					</button>
					{!room.isDefault && (
						<button
							type="button"
							onClick={(e) => {
								e.stopPropagation();
								onDelete(room.id);
							}}
							className="p-1.5 text-foreground-muted hover:text-error hover:bg-error-light rounded"
							title="Delete room"
						>
							<Trash2 className="h-4 w-4" />
						</button>
					)}
					<button
						type="button"
						onClick={() => onSelectRoom(room)}
						className="p-1.5 text-foreground-muted hover:text-foreground-default hover:bg-background-muted rounded"
						title="View all media"
					>
						<ChevronRight className="h-4 w-4" />
					</button>
				</div>
			</div>
 
			{/* Room Content - Images */}
			<div className="p-3">
				{roomMedia.length === 0 ? (
					<button
						type="button"
						onClick={() => triggerFileInput(room.id)}
						className="flex flex-col items-center justify-center py-8 w-full border-2 border-dashed border-border-default rounded-lg cursor-pointer hover:border-primary-400 hover:bg-primary-50/50 transition-colors"
					>
						<Image className="h-8 w-8 text-foreground-subtle mb-2" />
						<p className="text-sm text-foreground-muted">Click to add images</p>
					</button>
				) : (
					<div className="space-y-2">
						{/* Hero Image */}
						{heroImage && (
							<figure
								className={`relative aspect-video bg-background-muted rounded-lg overflow-hidden cursor-grab active:cursor-grabbing group m-0 ${
									dragOverMediaId === heroImage.id && draggingInRoom === room.id
										? "ring-2 ring-primary-500"
										: ""
								} ${draggedMediaId === heroImage.id ? "opacity-50" : ""}`}
								draggable
								onDragStart={(e) => onMediaDragStart(e, heroImage.id, room.id)}
								onDragEnd={onMediaDragEnd}
								onDragOver={(e) => onMediaDragOver(e, heroImage.id, room.id)}
								onDrop={(e) => onMediaDrop(e, heroImage.id, room.id)}
							>
								<img
									src={getImageUrl(heroImage.storageKey)}
									alt={heroImage.caption || "Room image"}
									className="w-full h-full object-cover"
									draggable={false}
									loading="lazy"
								/>
								{/* Delete button on hover */}
								<button
									type="button"
									onClick={(e) => {
										e.stopPropagation();
										onDeleteMedia(heroImage.id, room.id);
									}}
									className="absolute top-2 right-2 p-1.5 bg-error hover:bg-error/90 rounded-full text-foreground-inverse opacity-0 group-hover:opacity-100 transition-opacity"
									title="Delete"
								>
									<Trash2 className="h-3 w-3" />
								</button>
								{/* Order badge */}
								<div className="absolute top-2 left-2">
									<span className="bg-overlay-dark text-foreground-inverse text-xs font-medium px-2 py-0.5 rounded">
										1
									</span>
								</div>
							</figure>
						)}
 
						{/* Thumbnails */}
						{thumbnails.length > 0 && (
							<div className="flex gap-2">
								{thumbnails.map((media, idx) => (
									<figure
										key={media.id}
										className={`relative w-16 h-16 bg-background-muted rounded-lg overflow-hidden cursor-grab active:cursor-grabbing group flex-shrink-0 m-0 ${
											dragOverMediaId === media.id && draggingInRoom === room.id
												? "ring-2 ring-primary-500"
												: ""
										} ${draggedMediaId === media.id ? "opacity-50" : ""}`}
										draggable
										onDragStart={(e) => onMediaDragStart(e, media.id, room.id)}
										onDragEnd={onMediaDragEnd}
										onDragOver={(e) => onMediaDragOver(e, media.id, room.id)}
										onDrop={(e) => onMediaDrop(e, media.id, room.id)}
									>
										<img
											src={getImageUrl(media.storageKey)}
											alt={media.caption || "Room image"}
											className="w-full h-full object-cover"
											draggable={false}
											loading="lazy"
										/>
										{/* Delete button on hover */}
										<button
											type="button"
											onClick={(e) => {
												e.stopPropagation();
												onDeleteMedia(media.id, room.id);
											}}
											className="absolute top-0.5 right-0.5 p-0.5 bg-error hover:bg-error/90 rounded-full text-foreground-inverse opacity-0 group-hover:opacity-100 transition-opacity"
											title="Delete"
										>
											<Trash2 className="h-2.5 w-2.5" />
										</button>
										{/* Order badge */}
										<div className="absolute top-0.5 left-0.5">
											<span className="bg-overlay-dark text-foreground-inverse text-xs font-medium w-4 h-4 rounded-full flex items-center justify-center">
												{idx + 2}
											</span>
										</div>
									</figure>
								))}
								{/* Show remaining count or view all button */}
								{remainingCount > 0 && (
									<button
										type="button"
										onClick={() => onSelectRoom(room)}
										className="w-16 h-16 bg-background-muted rounded-lg flex items-center justify-center hover:bg-background-subtle transition-colors flex-shrink-0"
									>
										<span className="text-sm font-medium text-foreground-muted">
											+{remainingCount}
										</span>
									</button>
								)}
								{remainingCount === 0 && roomMedia.length <= 4 && (
									<button
										type="button"
										onClick={() => triggerFileInput(room.id)}
										className="w-16 h-16 border-2 border-dashed border-border-default rounded-lg flex items-center justify-center hover:border-primary-400 hover:bg-primary-50/50 transition-colors flex-shrink-0"
										title="Add more images"
									>
										<Plus className="h-5 w-5 text-foreground-subtle" />
									</button>
								)}
							</div>
						)}
 
						{/* If only hero image, show add more button */}
						{roomMedia.length === 1 && (
							<button
								type="button"
								onClick={() => triggerFileInput(room.id)}
								className="w-full py-2 border-2 border-dashed border-border-default rounded-lg flex items-center justify-center gap-2 hover:border-primary-400 hover:bg-primary-50/50 transition-colors text-sm text-foreground-muted"
							>
								<Plus className="h-4 w-4" />
								Add more images
							</button>
						)}
					</div>
				)}
			</div>
		</div>
	);
}