All files / src/components/blogs ImageGalleryModal.tsx

100% Statements 25/25
100% Branches 9/9
100% Functions 8/8
100% Lines 24/24

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                                                                    22x 22x     22x       22x   22x   21x 2x 2x 2x     21x 3x               1x 2x 2x   1x       21x 12x     21x                                                                                                             12x         1x                           1x 1x                   3x 3x                                                                                        
import { useState } from "react";
import { createPortal } from "react-dom";
import { notify } from "../../lib/notify";
import {
	X,
	Image as ImageIcon,
	Trash2,
} from "lucide-react";
import { Button } from "../ui/button";
import { Tabs, TabsList, TabsTrigger, TabsContent } from "../ui/tabs";
import { proApi } from "../../lib/api";
import { useConfirmDialog } from "../../hooks";
import type { BlogImage } from "../../lib/api/blog-images";
import { useBlogImages } from "../../hooks/queries/useBlogImageQueries";
import { useDeleteBlogImage } from "../../hooks/mutations/useBlogImageMutations";
import { UploadImageTab } from "./UploadImageTab";
import { PexelsImageTab } from "./PexelsImageTab";
import { ProjectImageTab } from "./ProjectImageTab";
 
interface ImageGalleryModalProps {
	isOpen: boolean;
	proId: string;
	blogId: string;
	onClose: () => void;
	onInsertImage: (image: { url: string; alt: string }) => void;
}
 
export function ImageGalleryModal({
	isOpen,
	proId,
	blogId,
	onClose,
	onInsertImage,
}: ImageGalleryModalProps) {
	const { confirm, dialog: confirmDialog } = useConfirmDialog();
	const [activeTab, setActiveTab] = useState("gallery");
 
	// Gallery data
	const { data: blogImages = [], isLoading: imagesLoading } = useBlogImages(
		proId,
		blogId,
	);
	const deleteMutation = useDeleteBlogImage(proId, blogId);
 
	if (!isOpen) return null;
 
	const handleInsertImage = (image: BlogImage) => {
		const url = proApi.getBlogImageUrl(image);
		onInsertImage({ url, alt: image.altText });
		onClose();
	};
 
	const handleDeleteImage = async (imageId: number) => {
		if (
			!(await confirm({
				title: "Delete image",
				description: "Are you sure you want to delete this image?",
				confirmLabel: "Delete",
				variant: "destructive",
			}))
		)
			return;
		try {
			await deleteMutation.mutateAsync(imageId);
		} catch {
			notify.error("Failed to delete image. Please try again.");
		}
	};
 
	const getImageUrl = (image: BlogImage): string => {
		return proApi.getBlogImageUrl(image);
	};
 
	return createPortal(
		<div
			data-testid="image-gallery-modal"
			role="dialog"
			aria-label="Insert Image"
			className="fixed inset-0 z-[100] flex items-center justify-center"
		>
			<button
				type="button"
				aria-label="Close modal"
				className="fixed inset-0 bg-black/50 cursor-default"
				onClick={onClose}
			/>
			<div className="relative bg-background-elevated rounded-lg shadow-xl w-full max-w-4xl p-6 m-4 max-h-[90vh] overflow-y-auto">
				<div className="flex items-center justify-between mb-4">
					<h2 className="text-lg font-semibold">Insert Image</h2>
					<button
						type="button"
						onClick={onClose}
						className="text-foreground-subtle hover:text-foreground-muted"
					>
						<X className="h-5 w-5" />
					</button>
				</div>
 
				<Tabs value={activeTab} onValueChange={setActiveTab}>
					<TabsList className="grid w-full grid-cols-4">
						<TabsTrigger value="gallery">
							Gallery ({blogImages.length})
						</TabsTrigger>
						<TabsTrigger value="upload">Upload</TabsTrigger>
						<TabsTrigger value="pexels">Stock Photos</TabsTrigger>
						<TabsTrigger value="projects">My Projects</TabsTrigger>
					</TabsList>
 
					{/* Gallery Tab */}
					<TabsContent value="gallery" className="mt-4">
						{imagesLoading ? (
							<div className="text-center py-12">
								<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-500 mx-auto" />
								<p className="mt-2 text-sm text-foreground-muted">
									Loading images...
								</p>
							</div>
						) : blogImages.length === 0 ? (
							<div className="text-center py-12 text-foreground-muted">
								<ImageIcon className="h-12 w-12 mx-auto mb-3 text-foreground-subtle" />
								<p>No images in gallery yet.</p>
								<p className="text-sm mt-1">
									Upload, add stock photos, or use project images.
								</p>
							</div>
						) : (
							<div data-testid="gallery-grid" className="grid grid-cols-3 gap-4">
								{blogImages.map((image) => (
									<button
										type="button"
										key={image.id}
										data-testid="gallery-image"
										className="group relative aspect-video bg-background-subtle rounded-lg overflow-hidden border border-default hover:border-primary-500 cursor-pointer transition-all text-left"
										onClick={() => handleInsertImage(image)}
									>
										<img
											src={getImageUrl(image)}
											alt={image.altText}
											className="w-full h-full object-cover"
											loading="lazy"
										/>
										<div className="absolute inset-0 bg-black/0 group-hover:bg-black/50 transition-colors flex items-center justify-center gap-2">
											<Button
												size="sm"
												variant="secondary"
												className="opacity-0 group-hover:opacity-100 transition-opacity"
												onClick={(e) => {
													e.stopPropagation();
													handleInsertImage(image);
												}}
											>
												Insert
											</Button>
											<Button
												size="sm"
												variant="destructive"
												className="opacity-0 group-hover:opacity-100 transition-opacity"
												onClick={(e) => {
													e.stopPropagation();
													handleDeleteImage(image.id);
												}}
											>
												<Trash2 className="h-4 w-4" />
											</Button>
										</div>
										<div className="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/70 to-transparent p-2">
											<p className="text-foreground-inverse text-xs truncate">
												{image.altText}
											</p>
										</div>
									</button>
								))}
							</div>
						)}
					</TabsContent>
 
					{/* Upload Tab */}
					<TabsContent value="upload" className="mt-4">
						<UploadImageTab proId={proId} blogId={blogId} onInsertImage={onInsertImage} onClose={onClose} />
					</TabsContent>
 
					{/* Pexels Tab */}
					<TabsContent value="pexels" className="mt-4">
						<PexelsImageTab proId={proId} blogId={blogId} onInsertImage={onInsertImage} onClose={onClose} />
					</TabsContent>
 
					{/* Projects Tab */}
					<TabsContent value="projects" className="mt-4">
						<ProjectImageTab proId={proId} blogId={blogId} onInsertImage={onInsertImage} onClose={onClose} />
					</TabsContent>
				</Tabs>
 
				<div className="flex justify-end mt-6 pt-4 border-t">
					<Button variant="outline" onClick={onClose}>
						Close
					</Button>
				</div>
			</div>
			{confirmDialog}
		</div>,
		document.body,
	);
}