Spaces:
Sleeping
Sleeping
File size: 13,464 Bytes
5463318 38ee1b6 5463318 38ee1b6 5463318 38ee1b6 5463318 | 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 246 247 248 249 250 | import React, { useState, useEffect, useRef } from 'react';
import { useLocation } from 'react-router-dom';
import { motion, AnimatePresence } from 'framer-motion';
import { useGenerate } from '../GenerateContext';
import { FileText, Tag, Presentation, Code, FileSpreadsheet, Search, Filter, CheckCircle2, FileQuestion, UploadCloud, Loader2 } from 'lucide-react';
import { cn } from '../../../../src/utils/cn';
import { Button } from '../../../components/ui/Button';
import { supabase } from '../../../utils/supabaseClient';
import { API_BASE_URL } from '../../../utils/api';
export const SelectMaterialStep = () => {
const { materials, setMaterials, setStep } = useGenerate();
const [search, setSearch] = useState('');
const [uploadedFiles, setUploadedFiles] = useState([]);
const [isUploading, setIsUploading] = useState(false);
const fileInputRef = useRef(null);
const location = useLocation();
useEffect(() => {
const fetchMaterials = async () => {
try {
const { data: { session } } = await supabase.auth.getSession();
if (!session) return;
const res = await fetch(`${API_BASE_URL}/api/materials`, {
method: "GET",
headers: { 'Authorization': `Bearer ${session.access_token}` }
});
if (res.ok) {
const data = await res.json();
const formatted = data.map(m => ({
id: m.storage_path,
name: m.file_name,
type: m.file_name.split('.').pop() || 'file',
tags: ['Uploaded', 'Dataset'],
date: new Date(m.created_at).toLocaleDateString(),
raw: m
}));
setUploadedFiles(formatted);
// Auto-select if passed from Navigation State
if (location.state?.uploadedFileId && materials.length === 0) {
setMaterials([location.state.uploadedFileId]);
}
}
} catch (error) {
console.error("Error fetching materials:", error);
}
};
fetchMaterials();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const toggleMaterial = (id) => {
if (materials.includes(id)) {
setMaterials(materials.filter(m => m !== id));
} else {
setMaterials([...materials, id]);
}
};
const toggleAll = () => {
const allMaterials = [...uploadedFiles];
if (materials.length === allMaterials.length) {
setMaterials([]);
} else {
setMaterials(allMaterials.map(m => m.id));
}
};
const handleFileUpload = async (e) => {
const file = e.target.files[0];
if (!file) return;
setIsUploading(true);
const formData = new FormData();
formData.append('file', file);
try {
const { data: { session } } = await supabase.auth.getSession();
const res = await fetch(`${API_BASE_URL}/upload`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${session?.access_token || ''}` },
body: formData
});
const data = await res.json();
if (data.file_id) {
const newFile = {
id: data.file_id,
name: file.name,
type: file.name.split('.').pop() || 'file',
tags: ['Uploaded', 'Target'],
date: 'Just now'
};
setUploadedFiles(prev => [newFile, ...prev]);
if (!materials.includes(data.file_id)) {
setMaterials(prev => [...prev, data.file_id]);
}
}
} catch (err) {
console.error("Upload failed", err);
} finally {
setIsUploading(false);
if (fileInputRef.current) fileInputRef.current.value = '';
}
};
const getIcon = (type) => {
switch (type) {
case 'pdf': return <FileText className="text-red-500" />;
case 'doc': return <FileText className="text-blue-500" />;
case 'presentation': return <Presentation className="text-orange-500" />;
case 'code': return <Code className="text-emerald-500" />;
default: return <FileQuestion className="text-slate-500" />;
}
};
const allAvailableMaterials = [...uploadedFiles];
const filtered = allAvailableMaterials.filter(m => m.name.toLowerCase().includes(search.toLowerCase()));
return (
<motion.div
initial={{ opacity: 0, x: 20 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: -20 }}
className="w-full flex flex-col bg-white dark:bg-[#111827] shadow-[0_8px_30px_rgb(0,0,0,0.04)] dark:shadow-[0_8px_30px_rgba(0,0,0,0.4)] rounded-2xl border border-slate-100 dark:border-slate-800 p-6 sm:p-8"
>
<div className="mb-6">
<h2 className="text-2xl font-bold text-slate-900 dark:text-white mb-2">Select Study Materials</h2>
<p className="text-slate-500 font-medium">Choose the source documents to generate AI questions from.</p>
</div>
<div className="flex flex-col sm:flex-row gap-4 mb-6">
<div className="relative flex-1">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 text-slate-400 w-5 h-5" />
<input
type="text"
placeholder="Search materials by name or tags..."
className="w-full pl-10 pr-4 py-2.5 rounded-xl border border-slate-200 dark:border-slate-700 bg-slate-50 dark:bg-[#0F172A] text-slate-900 dark:text-white focus:ring-2 focus:ring-[#4F7CFF] focus:border-transparent outline-none transition-all"
value={search}
onChange={e => setSearch(e.target.value)}
/>
</div>
<button className="flex items-center gap-2 px-4 py-2.5 rounded-xl border border-slate-200 dark:border-slate-700 text-slate-600 dark:text-slate-300 hover:bg-slate-50 dark:hover:bg-slate-800 transition-colors font-medium">
<Filter className="w-5 h-5" />
Filter
</button>
<input type="file" ref={fileInputRef} className="hidden" onChange={handleFileUpload} />
<button
onClick={() => fileInputRef.current?.click()}
disabled={isUploading}
className="flex items-center justify-center gap-2 px-6 py-2.5 rounded-xl bg-gradient-to-r from-[#8B5CF6] to-[#4F7CFF] text-white font-bold hover:shadow-lg transition-all disabled:opacity-50"
>
{isUploading ? <Loader2 className="w-5 h-5 animate-spin" /> : <UploadCloud className="w-5 h-5" />}
Upload New
</button>
</div>
<div className="flex items-center justify-between mb-4 px-2">
<label className="flex items-center gap-2 cursor-pointer group">
<div className="relative flex items-center justify-center">
<input type="checkbox" className="peer sr-only" checked={materials.length === allAvailableMaterials.length && allAvailableMaterials.length > 0} onChange={toggleAll} />
<div className="w-5 h-5 rounded border-2 border-slate-300 dark:border-slate-600 peer-checked:bg-[#4F7CFF] peer-checked:border-[#4F7CFF] flex items-center justify-center transition-colors">
<CheckCircle2 size={14} className="text-white opacity-0 peer-checked:opacity-100 transition-opacity" strokeWidth={3} />
</div>
</div>
<span className="text-sm font-semibold text-slate-600 dark:text-slate-300 group-hover:text-slate-900 dark:group-hover:text-white transition-colors">Select All Materials</span>
</label>
<span className="text-xs font-semibold px-2 py-1 bg-blue-50 dark:bg-[#4F7CFF]/10 text-[#4F7CFF] rounded-full">
{materials.length} Selected
</span>
</div>
<div className="space-y-3 pb-8">
<AnimatePresence>
{filtered.map((mat) => {
const isSelected = materials.includes(mat.id);
return (
<motion.div
key={mat.id}
layout
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
onClick={() => toggleMaterial(mat.id)}
className={cn(
"flex items-center gap-4 p-4 rounded-xl cursor-pointer transition-all duration-300 border hover:shadow-md group",
isSelected
? "bg-blue-50/50 dark:bg-[#4F7CFF]/5 border-[#4F7CFF] shadow-[0_4px_15px_rgba(79,124,255,0.1)]"
: "bg-white dark:bg-slate-800/50 border-slate-200 dark:border-slate-700 hover:border-slate-300 dark:hover:border-slate-600"
)}
>
<div className="flex-shrink-0 relative">
<input type="checkbox" className="peer sr-only" checked={isSelected} readOnly />
<div className="w-5 h-5 rounded border-2 border-slate-300 dark:border-slate-600 peer-checked:bg-[#4F7CFF] peer-checked:border-[#4F7CFF] flex items-center justify-center transition-colors">
<CheckCircle2 size={14} className="text-white opacity-0 peer-checked:opacity-100 transition-opacity" strokeWidth={3} />
</div>
</div>
<div className={cn("p-2 rounded-lg bg-slate-50 dark:bg-slate-800 transition-colors", isSelected && "bg-white dark:bg-[#0F172A]")}>
{getIcon(mat.type)}
</div>
<div className="flex-1 min-w-0">
<h3 className={cn("text-sm font-bold truncate transition-colors", isSelected ? "text-[#4F7CFF]" : "text-slate-900 dark:text-white")}>
{mat.name}
</h3>
<p className="text-xs text-slate-500 flex items-center gap-2 mt-1">
<span>Added {mat.date}</span>
<span className="w-1 h-1 rounded-full bg-slate-300 dark:bg-slate-600" />
<span className="text-xs text-slate-500 uppercase">{mat.type}</span>
</p>
</div>
<div className="flex gap-2">
{mat.tags.map(tag => (
<span key={tag} className={cn(
"hidden sm:flex items-center gap-1 text-[10px] uppercase tracking-wider font-bold px-2 py-1 rounded border",
isSelected
? "bg-[#4F7CFF]/10 text-[#4F7CFF] border-[#4F7CFF]/20"
: "bg-slate-50 dark:bg-slate-800 text-slate-500 border-slate-200 dark:border-slate-700"
)}>
<Tag size={10} /> {tag}
</span>
))}
</div>
</motion.div>
);
})}
</AnimatePresence>
{filtered.length === 0 && (
<div className="text-center py-12 text-slate-500">
<Search className="w-12 h-12 mx-auto mb-4 text-slate-300 dark:text-slate-700" />
<p>No materials found matching your search.</p>
</div>
)}
</div>
<div className="mt-auto pt-6 border-t border-slate-200 dark:border-slate-800 flex justify-end">
<Button
onClick={() => setStep(2)}
disabled={materials.length === 0}
className="px-8 py-2.5 bg-gradient-to-r from-[#8B5CF6] to-[#4F7CFF] hover:shadow-[0_0_20px_rgba(79,124,255,0.4)] text-white font-bold rounded-xl transition-all shadow-lg hover:scale-[1.02] disabled:opacity-50 disabled:scale-100 disabled:hover:shadow-none"
>
Continue to Configuration
</Button>
</div>
</motion.div>
);
};
|