Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 6,645 Bytes
f555806 |
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 |
'use client';
import { useEffect, useState, use, useMemo } from 'react';
import { LuImageOff, LuLoader, LuBan } from 'react-icons/lu';
import { FaChevronLeft } from 'react-icons/fa';
import DatasetImageCard from '@/components/DatasetImageCard';
import { Button } from '@headlessui/react';
import AddImagesModal, { openImagesModal } from '@/components/AddImagesModal';
import { TopBar, MainContent } from '@/components/layout';
import { apiClient } from '@/utils/api';
import FullscreenDropOverlay from '@/components/FullscreenDropOverlay';
import { useRouter } from 'next/navigation';
import { usingBrowserDb } from '@/utils/env';
import { hasUserDataset } from '@/utils/storage/datasetStorage';
import { useAuth } from '@/contexts/AuthContext';
import HFLoginButton from '@/components/HFLoginButton';
import Link from 'next/link';
export default function DatasetPage({ params }: { params: { datasetName: string } }) {
const [imgList, setImgList] = useState<{ img_path: string }[]>([]);
const usableParams = use(params as any) as { datasetName: string };
const datasetName = usableParams.datasetName;
const [status, setStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');
const router = useRouter();
const { status: authStatus } = useAuth();
const isAuthenticated = authStatus === 'authenticated';
const hasDatasetEntry = !usingBrowserDb || hasUserDataset(datasetName);
const allowAccess = hasDatasetEntry && isAuthenticated;
const refreshImageList = (dbName: string) => {
setStatus('loading');
console.log('Fetching images for dataset:', dbName);
apiClient
.post('/api/datasets/listImages', { datasetName: dbName })
.then((res: any) => {
const data = res.data;
console.log('Images:', data.images);
// sort
data.images.sort((a: { img_path: string }, b: { img_path: string }) => a.img_path.localeCompare(b.img_path));
setImgList(data.images);
setStatus('success');
})
.catch(error => {
console.error('Error fetching images:', error);
setStatus('error');
});
};
useEffect(() => {
if (!datasetName) {
return;
}
if (!isAuthenticated) {
return;
}
if (!hasDatasetEntry) {
setImgList([]);
setStatus('error');
router.replace('/datasets');
return;
}
refreshImageList(datasetName);
}, [datasetName, hasDatasetEntry, isAuthenticated, router]);
if (!allowAccess) {
return (
<>
<TopBar>
<div>
<Button className="text-gray-500 dark:text-gray-300 px-3 mt-1" onClick={() => history.back()}>
<FaChevronLeft />
</Button>
</div>
<div>
<h1 className="text-lg">Dataset: {datasetName}</h1>
</div>
<div className="flex-1"></div>
</TopBar>
<MainContent>
<div className="border border-gray-800 rounded-lg p-6 bg-gray-900 text-gray-400 text-sm flex flex-col gap-4">
<p>You need to sign in with Hugging Face or provide a valid token to view this dataset.</p>
<div className="flex items-center gap-3">
<HFLoginButton size="sm" />
<Link href="/settings" className="text-xs text-blue-400 hover:text-blue-300">
Manage authentication in Settings
</Link>
</div>
</div>
</MainContent>
</>
);
}
const PageInfoContent = useMemo(() => {
let icon = null;
let text = '';
let subtitle = '';
let showIt = false;
let bgColor = '';
let textColor = '';
let iconColor = '';
if (status == 'loading') {
icon = <LuLoader className="animate-spin w-8 h-8" />;
text = 'Loading Images';
subtitle = 'Please wait while we fetch your dataset images...';
showIt = true;
bgColor = 'bg-gray-50 dark:bg-gray-800/50';
textColor = 'text-gray-900 dark:text-gray-100';
iconColor = 'text-gray-500 dark:text-gray-400';
}
if (status == 'error') {
icon = <LuBan className="w-8 h-8" />;
text = 'Error Loading Images';
subtitle = 'There was a problem fetching the images. Please try refreshing the page.';
showIt = true;
bgColor = 'bg-red-50 dark:bg-red-950/20';
textColor = 'text-red-900 dark:text-red-100';
iconColor = 'text-red-600 dark:text-red-400';
}
if (status == 'success' && imgList.length === 0) {
icon = <LuImageOff className="w-8 h-8" />;
text = 'No Images Found';
subtitle = 'This dataset is empty. Click "Add Images" to get started.';
showIt = true;
bgColor = 'bg-gray-50 dark:bg-gray-800/50';
textColor = 'text-gray-900 dark:text-gray-100';
iconColor = 'text-gray-500 dark:text-gray-400';
}
if (!showIt) return null;
return (
<div
className={`mt-10 flex flex-col items-center justify-center py-16 px-8 rounded-xl border-2 border-gray-700 border-dashed ${bgColor} ${textColor} mx-auto max-w-md text-center`}
>
<div className={`${iconColor} mb-4`}>{icon}</div>
<h3 className="text-lg font-semibold mb-2">{text}</h3>
<p className="text-sm opacity-75 leading-relaxed">{subtitle}</p>
</div>
);
}, [status, imgList.length]);
return (
<>
{/* Fixed top bar */}
<TopBar>
<div>
<Button className="text-gray-500 dark:text-gray-300 px-3 mt-1" onClick={() => history.back()}>
<FaChevronLeft />
</Button>
</div>
<div>
<h1 className="text-lg">Dataset: {datasetName}</h1>
</div>
<div className="flex-1"></div>
<div>
<Button
className="text-gray-200 bg-slate-600 px-3 py-1 rounded-md"
onClick={() => openImagesModal(datasetName, () => refreshImageList(datasetName))}
>
Add Images
</Button>
</div>
</TopBar>
<MainContent>
{PageInfoContent}
{status === 'success' && imgList.length > 0 && (
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
{imgList.map(img => (
<DatasetImageCard
key={img.img_path}
alt="image"
imageUrl={img.img_path}
onDelete={() => refreshImageList(datasetName)}
/>
))}
</div>
)}
</MainContent>
<AddImagesModal />
<FullscreenDropOverlay
datasetName={datasetName}
onComplete={() => refreshImageList(datasetName)}
/>
</>
);
}
|