Spaces:
Sleeping
Sleeping
Upload 121 files
Browse files- src/components/FavoritesView.tsx +156 -10
- src/components/ImageLightbox.tsx +17 -1
src/components/FavoritesView.tsx
CHANGED
|
@@ -1,13 +1,106 @@
|
|
| 1 |
import { useState, useEffect } from 'react';
|
| 2 |
-
import { Heart, Trash2 } from 'lucide-react';
|
| 3 |
import { getFavorites, removeFavorite, FavoriteImage } from '../utils/favorites';
|
|
|
|
| 4 |
import { ImageLoader } from './ImageLoader';
|
| 5 |
import { ImageLightbox } from './ImageLightbox';
|
| 6 |
|
| 7 |
export function FavoritesView() {
|
| 8 |
const [favorites, setFavorites] = useState<FavoriteImage[]>([]);
|
|
|
|
| 9 |
const [lightboxImages, setLightboxImages] = useState<string[]>([]);
|
| 10 |
const [lightboxIndex, setLightboxIndex] = useState(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
const sortFavorites = (favs: FavoriteImage[]) => {
|
| 13 |
return favs.sort((a, b) => {
|
|
@@ -29,6 +122,10 @@ export function FavoritesView() {
|
|
| 29 |
setFavorites(sortFavorites(getFavorites()));
|
| 30 |
};
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
if (favorites.length === 0) {
|
| 33 |
return (
|
| 34 |
<div className="flex flex-col items-center justify-center py-20 animate-in fade-in duration-300">
|
|
@@ -41,24 +138,70 @@ export function FavoritesView() {
|
|
| 41 |
|
| 42 |
return (
|
| 43 |
<div className="max-w-[1800px] mx-auto p-2 sm:p-4 animate-in fade-in duration-300">
|
| 44 |
-
<div className="flex items-center gap-
|
| 45 |
-
<
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
</div>
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
key={fav.id}
|
| 54 |
className="masonry-item group relative bg-black rounded-lg shadow-lg overflow-hidden cursor-pointer"
|
| 55 |
onClick={() => {
|
| 56 |
-
setLightboxImages(
|
| 57 |
setLightboxIndex(index);
|
| 58 |
}}
|
| 59 |
>
|
| 60 |
<ImageLoader
|
| 61 |
-
src={fav.pageUrl}
|
| 62 |
alt={`Favorito de ${fav.mangaTitle}`}
|
| 63 |
aspectRatio="auto"
|
| 64 |
objectFit="contain"
|
|
@@ -86,12 +229,15 @@ export function FavoritesView() {
|
|
| 86 |
</div>
|
| 87 |
))}
|
| 88 |
</div>
|
|
|
|
| 89 |
|
| 90 |
{lightboxImages.length > 0 && (
|
| 91 |
<ImageLightbox
|
| 92 |
images={lightboxImages}
|
| 93 |
initialIndex={lightboxIndex}
|
| 94 |
onClose={() => setLightboxImages([])}
|
|
|
|
|
|
|
| 95 |
/>
|
| 96 |
)}
|
| 97 |
</div>
|
|
|
|
| 1 |
import { useState, useEffect } from 'react';
|
| 2 |
+
import { Heart, Trash2, Loader2, Sparkles, Search } from 'lucide-react';
|
| 3 |
import { getFavorites, removeFavorite, FavoriteImage } from '../utils/favorites';
|
| 4 |
+
import { getMangaById } from '../services/manga';
|
| 5 |
import { ImageLoader } from './ImageLoader';
|
| 6 |
import { ImageLightbox } from './ImageLightbox';
|
| 7 |
|
| 8 |
export function FavoritesView() {
|
| 9 |
const [favorites, setFavorites] = useState<FavoriteImage[]>([]);
|
| 10 |
+
const [searchQuery, setSearchQuery] = useState('');
|
| 11 |
const [lightboxImages, setLightboxImages] = useState<string[]>([]);
|
| 12 |
const [lightboxIndex, setLightboxIndex] = useState(0);
|
| 13 |
+
const [hdUrls, setHdUrls] = useState<Record<string, string>>({}); // Key is fav.id
|
| 14 |
+
const [isLoadingAllHD, setIsLoadingAllHD] = useState(false);
|
| 15 |
+
|
| 16 |
+
const isAllHDLoaded = favorites.length > 0 && Object.keys(hdUrls).length === favorites.length;
|
| 17 |
+
|
| 18 |
+
const handleToggleAllHD = async () => {
|
| 19 |
+
if (Object.keys(hdUrls).length > 0) {
|
| 20 |
+
// Toggle OFF all
|
| 21 |
+
setHdUrls({});
|
| 22 |
+
if (lightboxImages.length > 0) {
|
| 23 |
+
setLightboxImages(favorites.map(f => f.pageUrl));
|
| 24 |
+
}
|
| 25 |
+
return;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// Toggle ON all
|
| 29 |
+
setIsLoadingAllHD(true);
|
| 30 |
+
const newHdUrls = { ...hdUrls };
|
| 31 |
+
|
| 32 |
+
// Agrupar por mangaId para no hacer peticiones repetidas al mismo c贸mic
|
| 33 |
+
const mangaGroups: Record<string, FavoriteImage[]> = {};
|
| 34 |
+
for (const fav of favorites) {
|
| 35 |
+
if (!newHdUrls[fav.id]) {
|
| 36 |
+
if (!mangaGroups[fav.mangaId]) mangaGroups[fav.mangaId] = [];
|
| 37 |
+
mangaGroups[fav.mangaId].push(fav);
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
try {
|
| 42 |
+
for (const mangaId of Object.keys(mangaGroups)) {
|
| 43 |
+
const manga = await getMangaById(mangaId);
|
| 44 |
+
if (manga && manga.chapters?.[0]?.original_pages) {
|
| 45 |
+
for (const fav of mangaGroups[mangaId]) {
|
| 46 |
+
const hdUrl = manga.chapters[0].original_pages[fav.pageIndex];
|
| 47 |
+
if (hdUrl) {
|
| 48 |
+
newHdUrls[fav.id] = hdUrl;
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
setHdUrls(newHdUrls);
|
| 54 |
+
|
| 55 |
+
// Si el lightbox est谩 abierto, actualizarlo
|
| 56 |
+
if (lightboxImages.length > 0) {
|
| 57 |
+
setLightboxImages(favorites.map(f => newHdUrls[f.id] || f.pageUrl));
|
| 58 |
+
}
|
| 59 |
+
} catch (error) {
|
| 60 |
+
console.error("Error loading all HD:", error);
|
| 61 |
+
} finally {
|
| 62 |
+
setIsLoadingAllHD(false);
|
| 63 |
+
}
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
const handleRequestHD = async (index: number) => {
|
| 67 |
+
const fav = favorites[index];
|
| 68 |
+
if (!fav) return;
|
| 69 |
+
|
| 70 |
+
// Si ya est谩 en HD, lo desactivamos (Toggle OFF)
|
| 71 |
+
if (hdUrls[fav.id]) {
|
| 72 |
+
setHdUrls(prev => {
|
| 73 |
+
const newHdUrls = { ...prev };
|
| 74 |
+
delete newHdUrls[fav.id];
|
| 75 |
+
return newHdUrls;
|
| 76 |
+
});
|
| 77 |
+
setLightboxImages(prev => {
|
| 78 |
+
const newImages = [...prev];
|
| 79 |
+
newImages[index] = fav.pageUrl;
|
| 80 |
+
return newImages;
|
| 81 |
+
});
|
| 82 |
+
return;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
try {
|
| 86 |
+
const manga = await getMangaById(fav.mangaId);
|
| 87 |
+
const hdUrl = manga?.chapters?.[0]?.original_pages?.[fav.pageIndex];
|
| 88 |
+
|
| 89 |
+
if (hdUrl) {
|
| 90 |
+
setHdUrls(prev => ({ ...prev, [fav.id]: hdUrl }));
|
| 91 |
+
// Actualizamos las im谩genes del lightbox para que el visor actualice la fuente
|
| 92 |
+
setLightboxImages(prev => {
|
| 93 |
+
const newImages = [...prev];
|
| 94 |
+
newImages[index] = hdUrl;
|
| 95 |
+
return newImages;
|
| 96 |
+
});
|
| 97 |
+
} else {
|
| 98 |
+
console.warn('No hay versi贸n HD para esta p谩gina');
|
| 99 |
+
}
|
| 100 |
+
} catch (e) {
|
| 101 |
+
console.error("Error cargando versi贸n HD", e);
|
| 102 |
+
}
|
| 103 |
+
};
|
| 104 |
|
| 105 |
const sortFavorites = (favs: FavoriteImage[]) => {
|
| 106 |
return favs.sort((a, b) => {
|
|
|
|
| 122 |
setFavorites(sortFavorites(getFavorites()));
|
| 123 |
};
|
| 124 |
|
| 125 |
+
const filteredFavorites = favorites.filter(fav =>
|
| 126 |
+
fav.mangaTitle.toLowerCase().includes(searchQuery.toLowerCase())
|
| 127 |
+
);
|
| 128 |
+
|
| 129 |
if (favorites.length === 0) {
|
| 130 |
return (
|
| 131 |
<div className="flex flex-col items-center justify-center py-20 animate-in fade-in duration-300">
|
|
|
|
| 138 |
|
| 139 |
return (
|
| 140 |
<div className="max-w-[1800px] mx-auto p-2 sm:p-4 animate-in fade-in duration-300">
|
| 141 |
+
<div className="flex items-center justify-between gap-4 mb-6 flex-wrap">
|
| 142 |
+
<div className="flex items-center gap-2">
|
| 143 |
+
<Heart className="w-5 h-5 sm:w-6 sm:h-6 text-[#ff4b4b] fill-[#ff4b4b]" />
|
| 144 |
+
<h2 className="text-lg sm:text-xl font-bold text-white">Tus P谩ginas Favoritas</h2>
|
| 145 |
+
<span className="text-gray-400 text-xs sm:text-sm ml-1">({favorites.length})</span>
|
| 146 |
+
</div>
|
| 147 |
+
|
| 148 |
+
{/* Search Bar */}
|
| 149 |
+
{favorites.length > 0 && (
|
| 150 |
+
<div className="w-full sm:w-auto flex-1 max-w-md order-3 sm:order-none mt-2 sm:mt-0">
|
| 151 |
+
<div className="relative">
|
| 152 |
+
<Search className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-gray-400" />
|
| 153 |
+
<input
|
| 154 |
+
type="text"
|
| 155 |
+
value={searchQuery}
|
| 156 |
+
onChange={(e) => setSearchQuery(e.target.value)}
|
| 157 |
+
placeholder="Buscar por t铆tulo de c贸mic..."
|
| 158 |
+
className="w-full bg-[#1e1e1e] border border-gray-700/50 rounded-lg pl-9 pr-4 py-2 text-sm text-white placeholder:text-gray-500 focus:outline-none focus:border-[#bb86fc] transition-colors"
|
| 159 |
+
/>
|
| 160 |
+
</div>
|
| 161 |
+
</div>
|
| 162 |
+
)}
|
| 163 |
+
|
| 164 |
+
{favorites.length > 0 && (
|
| 165 |
+
<button
|
| 166 |
+
onClick={handleToggleAllHD}
|
| 167 |
+
disabled={isLoadingAllHD}
|
| 168 |
+
className={`flex items-center justify-center gap-2 px-3 sm:px-4 py-1.5 sm:py-2 rounded-lg font-bold text-sm transition-all duration-300 shadow-lg whitespace-nowrap
|
| 169 |
+
${Object.keys(hdUrls).length > 0
|
| 170 |
+
? 'bg-gradient-to-r from-[#bb86fc] to-[#9965f4] text-white shadow-[#bb86fc]/20 hover:shadow-[#bb86fc]/40'
|
| 171 |
+
: 'bg-white/10 text-gray-300 hover:bg-white/20 hover:text-white'
|
| 172 |
+
}
|
| 173 |
+
${isLoadingAllHD ? 'opacity-70 cursor-not-allowed' : ''}
|
| 174 |
+
`}
|
| 175 |
+
>
|
| 176 |
+
{isLoadingAllHD ? (
|
| 177 |
+
<Loader2 className="w-4 h-4 animate-spin shrink-0" />
|
| 178 |
+
) : (
|
| 179 |
+
<Sparkles className="w-4 h-4 shrink-0" />
|
| 180 |
+
)}
|
| 181 |
+
<span className="hidden sm:inline">Modo Full HD</span>
|
| 182 |
+
<span className="sm:hidden">Full HD</span>
|
| 183 |
+
</button>
|
| 184 |
+
)}
|
| 185 |
</div>
|
| 186 |
|
| 187 |
+
{filteredFavorites.length === 0 && favorites.length > 0 ? (
|
| 188 |
+
<div className="flex flex-col items-center justify-center py-20 text-gray-400">
|
| 189 |
+
<Search className="w-12 h-12 mb-4 opacity-20" />
|
| 190 |
+
<p>No se encontraron p谩ginas para "{searchQuery}"</p>
|
| 191 |
+
</div>
|
| 192 |
+
) : (
|
| 193 |
+
<div className="masonry-container">
|
| 194 |
+
{filteredFavorites.map((fav, index) => (
|
| 195 |
+
<div
|
| 196 |
key={fav.id}
|
| 197 |
className="masonry-item group relative bg-black rounded-lg shadow-lg overflow-hidden cursor-pointer"
|
| 198 |
onClick={() => {
|
| 199 |
+
setLightboxImages(filteredFavorites.map(f => hdUrls[f.id] || f.pageUrl));
|
| 200 |
setLightboxIndex(index);
|
| 201 |
}}
|
| 202 |
>
|
| 203 |
<ImageLoader
|
| 204 |
+
src={hdUrls[fav.id] || fav.pageUrl}
|
| 205 |
alt={`Favorito de ${fav.mangaTitle}`}
|
| 206 |
aspectRatio="auto"
|
| 207 |
objectFit="contain"
|
|
|
|
| 229 |
</div>
|
| 230 |
))}
|
| 231 |
</div>
|
| 232 |
+
)}
|
| 233 |
|
| 234 |
{lightboxImages.length > 0 && (
|
| 235 |
<ImageLightbox
|
| 236 |
images={lightboxImages}
|
| 237 |
initialIndex={lightboxIndex}
|
| 238 |
onClose={() => setLightboxImages([])}
|
| 239 |
+
onRequestHD={handleRequestHD}
|
| 240 |
+
isHDLoaded={(idx) => !!hdUrls[filteredFavorites[idx]?.id]}
|
| 241 |
/>
|
| 242 |
)}
|
| 243 |
</div>
|
src/components/ImageLightbox.tsx
CHANGED
|
@@ -9,9 +9,11 @@ interface ImageLightboxProps {
|
|
| 9 |
onClose: () => void;
|
| 10 |
mangaId?: string;
|
| 11 |
mangaTitle?: string;
|
|
|
|
|
|
|
| 12 |
}
|
| 13 |
|
| 14 |
-
export function ImageLightbox({ images, initialIndex, onClose, mangaId, mangaTitle }: ImageLightboxProps) {
|
| 15 |
const [currentIndex, setCurrentIndex] = useState(initialIndex);
|
| 16 |
const [isBookMode, setIsBookMode] = useState(false);
|
| 17 |
const [favRefresh, setFavRefresh] = useState(0);
|
|
@@ -336,6 +338,20 @@ export function ImageLightbox({ images, initialIndex, onClose, mangaId, mangaTit
|
|
| 336 |
</button>
|
| 337 |
<div className="w-px h-4 bg-white/20 mx-1"></div>
|
| 338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
<button onClick={() => setScale(p => {
|
| 340 |
const newS = Math.max(p - 0.5, 1);
|
| 341 |
setPosition(prev => clampPosition(prev, newS));
|
|
|
|
| 9 |
onClose: () => void;
|
| 10 |
mangaId?: string;
|
| 11 |
mangaTitle?: string;
|
| 12 |
+
onRequestHD?: (index: number) => void;
|
| 13 |
+
isHDLoaded?: (index: number) => boolean;
|
| 14 |
}
|
| 15 |
|
| 16 |
+
export function ImageLightbox({ images, initialIndex, onClose, mangaId, mangaTitle, onRequestHD, isHDLoaded }: ImageLightboxProps) {
|
| 17 |
const [currentIndex, setCurrentIndex] = useState(initialIndex);
|
| 18 |
const [isBookMode, setIsBookMode] = useState(false);
|
| 19 |
const [favRefresh, setFavRefresh] = useState(0);
|
|
|
|
| 338 |
</button>
|
| 339 |
<div className="w-px h-4 bg-white/20 mx-1"></div>
|
| 340 |
|
| 341 |
+
{/* HD Toggle */}
|
| 342 |
+
{onRequestHD && (
|
| 343 |
+
<>
|
| 344 |
+
<button
|
| 345 |
+
onClick={() => onRequestHD(currentIndex)}
|
| 346 |
+
className={`p-2 transition-colors font-bold text-sm ${isHDLoaded?.(currentIndex) ? 'text-[#bb86fc]' : 'text-white hover:text-[#bb86fc]'}`}
|
| 347 |
+
title="Cargar versi贸n en alta calidad (HD)"
|
| 348 |
+
>
|
| 349 |
+
HD
|
| 350 |
+
</button>
|
| 351 |
+
<div className="w-px h-4 bg-white/20 mx-1"></div>
|
| 352 |
+
</>
|
| 353 |
+
)}
|
| 354 |
+
|
| 355 |
<button onClick={() => setScale(p => {
|
| 356 |
const newS = Math.max(p - 0.5, 1);
|
| 357 |
setPosition(prev => clampPosition(prev, newS));
|