All files / src/components/blogs AllProsImageTab.tsx

97.14% Statements 34/35
92.3% Branches 24/26
100% Functions 14/14
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                                              22x 22x 22x 22x   22x 22x 22x   22x 22x   22x 2x 2x     22x 3x 3x     22x 2x 2x 1x 1x 1x 2x   1x       22x         2x   2x                               3x       44x                                                       7x           2x                                                         1x               1x                              
import { useState } from "react";
import { notify } from "../../lib/notify";
import { Image as ImageIcon, Search } from "lucide-react";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { getImageUrl } from "../../lib/api/base";
import { proApi } from "../../lib/api";
import { useAllProsMediaSearch } from "../../hooks/queries/useAdminBlogImageQueries";
import { useAdminAddMediaImage } from "../../hooks/mutations/useAdminBlogImageMutations";
import { useAdminProLookup } from "../../hooks/queries/useAdminQueries";
import type { BlogMediaItem } from "../../lib/api/blog-images";
 
interface AllProsImageTabProps {
	blogId: string;
	onInsertImage: (image: { url: string; alt: string }) => void;
	onClose: () => void;
}
 
export function AllProsImageTab({
	blogId,
	onInsertImage,
	onClose,
}: AllProsImageTabProps) {
	const [search, setSearch] = useState("");
	const [debouncedSearch, setDebouncedSearch] = useState("");
	const [selectedProId, setSelectedProId] = useState<string | undefined>(undefined);
	const [page, setPage] = useState(1);
 
	const { data: prosData = [] } = useAdminProLookup();
	const { data, isLoading } = useAllProsMediaSearch(blogId, debouncedSearch, selectedProId, page);
	const addMediaMutation = useAdminAddMediaImage(blogId);
 
	const items = data?.data ?? [];
	const meta = data?.meta;
 
	const handleSearch = () => {
		setDebouncedSearch(search);
		setPage(1);
	};
 
	const handleProFilter = (proId: string) => {
		setSelectedProId(proId || undefined);
		setPage(1);
	};
 
	const handlePickItem = async (item: BlogMediaItem) => {
		try {
			const added = await addMediaMutation.mutateAsync({ mediaId: item.id });
			Iif (!added) return;
			const url = proApi.getBlogImageUrl(added);
			onInsertImage({ url, alt: added.altText ?? "" });
			onClose();
		} catch {
			notify.error("Failed to insert image. Please try again.");
		}
	};
 
	return (
		<div className="space-y-4">
			<div className="flex gap-2">
				<Input
					value={search}
					onChange={(e) => setSearch(e.target.value)}
					placeholder="Search all media..."
					onKeyDown={(e) => e.key === "Enter" && handleSearch()}
				/>
				<Button onClick={handleSearch} disabled={isLoading}>
					<Search className="h-4 w-4 mr-2" />
					Search
				</Button>
			</div>
 
			<div className="flex items-center gap-2">
				<label htmlFor="pro-filter" className="text-sm text-foreground-muted whitespace-nowrap">
					Filter by pro:
				</label>
				<select
					id="pro-filter"
					className="text-sm border border-default rounded px-2 py-1 bg-background flex-1"
					value={selectedProId ?? ""}
					onChange={(e) => handleProFilter(e.target.value)}
				>
					<option value="">All pros</option>
					{prosData.map((pro) => (
						<option key={pro.id} value={pro.id}>
							{pro.businessName}
						</option>
					))}
				</select>
			</div>
 
			{isLoading ? (
				<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 media...</p>
				</div>
			) : items.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>
						{debouncedSearch
							? `No results for "${debouncedSearch}"`
							: "No media found."}
					</p>
				</div>
			) : (
				<>
					<div
						data-testid="all-pros-media-grid"
						className="grid grid-cols-3 gap-4"
					>
						{items.map((item) => (
							<button
								type="button"
								key={item.id}
								data-testid="all-pros-media-item"
								disabled={addMediaMutation.isPending}
								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 disabled:opacity-50"
								onClick={() => handlePickItem(item)}
							>
								<img
									src={getImageUrl(item.storageKey)}
									alt={item.altText ?? ""}
									className="w-full h-full object-cover"
									loading="lazy"
								/>
								<div className="absolute inset-0 bg-black/0 group-hover:bg-black/40 transition-colors flex items-center justify-center">
									<span className="opacity-0 group-hover:opacity-100 transition-opacity bg-white text-black text-xs font-medium px-2 py-1 rounded">
										Insert
									</span>
								</div>
								{item.altText && (
									<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">{item.altText}</p>
									</div>
								)}
							</button>
						))}
					</div>
 
					{meta && meta.totalPages > 1 && (
						<div className="flex items-center justify-between mt-4">
							<p className="text-sm text-foreground-muted">
								Page {meta.page} of {meta.totalPages}
							</p>
							<div className="flex gap-2">
								<Button
									onClick={() => setPage((p) => Math.max(1, p - 1))}
									disabled={!meta.hasPrev}
									variant="outline"
									size="sm"
								>
									Previous
								</Button>
								<Button
									onClick={() => setPage((p) => p + 1)}
									disabled={!meta.hasNext}
									variant="outline"
									size="sm"
								>
									Next
								</Button>
							</div>
						</div>
					)}
				</>
			)}
		</div>
	);
}