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 | 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 3x 1x 1x 2x 34x 2x 2x 2x 2x 2x 2x 1x 1x 1x 34x 34x 2x 1x 1x 1x 1x 23x 6x 1x 2x | import { useState } from "react";
import { notify } from "../../lib/notify";
import { Search, CheckCircle2 } from "lucide-react";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { proApi } from "../../lib/api";
import type {
PexelsPhoto,
PexelsSearchResult,
} from "../../lib/api/blog-images";
import { usePexelsSearch } from "../../hooks/queries/useBlogImageQueries";
import { useAdminPexelsSearch } from "../../hooks/queries/useAdminBlogImageQueries";
import { useAddPexelsImage } from "../../hooks/mutations/useBlogImageMutations";
import { useAdminAddPexelsImage } from "../../hooks/mutations/useAdminBlogImageMutations";
interface PexelsImageTabProps {
proId?: string;
blogId: string;
mode?: "pro" | "admin";
onInsertImage: (image: { url: string; alt: string }) => void;
onClose: () => void;
}
export function PexelsImageTab({
proId,
blogId,
mode = "pro",
onInsertImage,
onClose,
}: PexelsImageTabProps) {
const [query, setQuery] = useState("");
const [page, setPage] = useState(1);
const [selectedId, setSelectedId] = useState<number | null>(null);
const [altText, setAltText] = useState("");
// Call both hooks unconditionally (Rules of Hooks). Each is gated via enabled.
const proSearch = usePexelsSearch(
mode === "pro" ? (proId ?? null) : null,
query,
page,
);
const adminSearch = useAdminPexelsSearch(mode === "admin" ? query : "", page);
const addProPexelsMutation = useAddPexelsImage(proId ?? "", blogId);
const addAdminPexelsMutation = useAdminAddPexelsImage(blogId);
// Select the active data/state based on mode
const activeSearch = mode === "admin" ? adminSearch : proSearch;
const { data: pexelsData, isLoading, error } = activeSearch;
const result: PexelsSearchResult = (pexelsData as
| PexelsSearchResult
| null
| undefined) ?? {
photos: [],
page: 1,
per_page: 20,
total_results: 0,
};
const photos = result.photos;
const totalResults = result.total_results;
const perPage = result.per_page;
const hasNextPage = result.next_page !== undefined;
const handleSearch = () => {
if (query.trim().length === 0) {
notify.error("Please enter a search query");
return;
}
setPage(1);
};
const handleConfirm = async () => {
const photo = photos.find((p: PexelsPhoto) => p.id === selectedId);
Iif (!photo) return;
try {
const input = {
pexelsId: photo.id.toString(),
url: photo.src.large,
width: photo.width,
height: photo.height,
altText,
photographer: photo.photographer,
photographerUrl: photo.photographer_url,
};
Iif (mode === "admin") {
const added = await addAdminPexelsMutation.mutateAsync(input);
if (!added) return;
onInsertImage({
url: proApi.getBlogImageUrl(added),
alt: added.altText ?? altText,
});
} else {
const addedImage = await addProPexelsMutation.mutateAsync(input);
onInsertImage({
url: proApi.getBlogImageUrl(addedImage),
alt: addedImage.altText,
});
}
onClose();
} catch {
notify.error("Failed to add Pexels image. Please try again.");
}
};
const isPending =
mode === "admin"
? addAdminPexelsMutation.isPending
: addProPexelsMutation.isPending;
return (
<div className="space-y-4">
<div className="flex gap-2">
<Input
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Search for stock photos..."
onKeyDown={(e) => e.key === "Enter" && handleSearch()}
/>
<Button onClick={handleSearch} disabled={isLoading}>
<Search className="h-4 w-4 mr-2" />
Search
</Button>
</div>
{selectedId && (
<div className="p-4 bg-info-light border border-info rounded-lg">
<p className="text-sm font-medium mb-2">
Selected Photo - Add Alt Text:
</p>
<div className="flex gap-2">
<Input
value={altText}
onChange={(e) => setAltText(e.target.value)}
placeholder="Alt text (optional, recommended for SEO)"
/>
<Button onClick={handleConfirm} disabled={isPending} size="sm">
{isPending ? "Adding..." : "Add & Insert"}
</Button>
<Button
onClick={() => {
setSelectedId(null);
setAltText("");
}}
variant="outline"
size="sm"
>
Cancel
</Button>
</div>
</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">
Searching Pexels...
</p>
</div>
) : error ? (
<div className="text-center py-12 text-notification-error-text">
<p>Failed to search stock photos.</p>
<p className="text-sm mt-1">
Please check the API configuration or try again later.
</p>
</div>
) : photos.length > 0 ? (
<>
<div className="grid grid-cols-3 gap-4">
{photos.map((photo: PexelsPhoto) => (
<button
type="button"
key={photo.id}
className={`group relative aspect-video bg-background-subtle rounded-lg overflow-hidden border-2 cursor-pointer transition-all ${
selectedId === photo.id
? "border-primary-500 ring-2 ring-primary-200"
: "border-default hover:border-primary-300"
}`}
onClick={() => setSelectedId(photo.id)}
>
<img
src={photo.src.medium}
alt={photo.alt || "Pexels photo"}
className="w-full h-full object-cover"
loading="lazy"
/>
{selectedId === photo.id && (
<div className="absolute top-2 right-2 bg-primary-500 text-foreground-inverse rounded-full p-1">
<CheckCircle2 className="h-5 w-5" />
</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">
by {photo.photographer}
</p>
</div>
</button>
))}
</div>
<div className="flex items-center justify-between mt-4">
<p className="text-sm text-foreground-muted">
Page {result.page} of {Math.ceil(totalResults / perPage)}
</p>
<div className="flex gap-2">
<Button
onClick={() => setPage((prev) => Math.max(1, prev - 1))}
disabled={page === 1}
variant="outline"
size="sm"
>
Previous
</Button>
<Button
onClick={() => setPage((prev) => prev + 1)}
disabled={!hasNextPage}
variant="outline"
size="sm"
>
Next
</Button>
</div>
</div>
</>
) : query ? (
<div className="text-center py-12 text-foreground-muted">
<Search className="h-12 w-12 mx-auto mb-3 text-foreground-subtle" />
<p>No results found for "{query}"</p>
<p className="text-sm mt-1">Try a different search term.</p>
</div>
) : (
<div className="text-center py-12 text-foreground-muted">
<Search className="h-12 w-12 mx-auto mb-3 text-foreground-subtle" />
<p>Search for free stock photos from Pexels</p>
<p className="text-sm mt-1">
Enter keywords like "modern kitchen" or "interior design"
</p>
</div>
)}
</div>
);
}
|