File size: 12,585 Bytes
35765b5 | 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | import { useState, useEffect } from 'react';
import { useUser } from '../context/UserContext';
import { useProject } from '../context/ProjectContext';
import { api } from '../api/client';
import type { Project } from '../types';
type Mode = 'select' | 'create' | 'join';
export function ProjectSelectionPage() {
const { user, logout } = useUser();
const { setCurrentProject } = useProject();
const [mode, setMode] = useState<Mode>('select');
const [existingProjects, setExistingProjects] = useState<Project[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState('');
// Create form state
const [projectName, setProjectName] = useState('');
const [projectDescription, setProjectDescription] = useState('');
// Join form state
const [joinProjectId, setJoinProjectId] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
// Fetch existing projects on mount
useEffect(() => {
if (!user) return;
const fetchProjects = async () => {
try {
const response = await api.listProjects(user.id);
setExistingProjects(response.projects || []);
} catch (err) {
console.error('Failed to fetch projects:', err);
} finally {
setIsLoading(false);
}
};
fetchProjects();
}, [user]);
const handleSelectProject = (project: Project) => {
setCurrentProject(project);
};
const handleCreateProject = async (e: React.FormEvent) => {
e.preventDefault();
setError('');
if (!projectName.trim()) {
setError('Project name is required');
return;
}
if (!user) return;
setIsSubmitting(true);
try {
// Check if project name is available (since name = id)
const availability = await api.checkProjectAvailability(projectName.trim());
if (!availability.available) {
setError('Project name already taken. Try a different name.');
setIsSubmitting(false);
return;
}
const project = await api.createProject({
name: projectName.trim(),
description: projectDescription.trim(),
userId: user.id,
});
setCurrentProject({ ...project, role: 'owner' });
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to create project');
} finally {
setIsSubmitting(false);
}
};
const handleJoinProject = async (e: React.FormEvent) => {
e.preventDefault();
setError('');
if (!joinProjectId.trim()) {
setError('Project ID is required');
return;
}
if (!user) return;
setIsSubmitting(true);
try {
const result = await api.joinProject(joinProjectId.trim(), user.id);
// Fetch the full project details
const projects = await api.listProjects(user.id);
const joinedProject = projects.projects?.find(p => p.id === result.project_id);
if (joinedProject) {
setCurrentProject(joinedProject);
} else {
// Fallback if project not found in list
setCurrentProject({
id: result.project_id,
name: result.project_id,
description: '',
created_at: new Date().toISOString(),
role: result.role,
});
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to join project');
} finally {
setIsSubmitting(false);
}
};
if (!user) return null;
return (
<div className="min-h-screen bg-gradient-to-br from-slate-900 via-purple-900 to-slate-900">
{/* Header */}
<header className="bg-white/5 backdrop-blur-lg border-b border-white/10">
<div className="max-w-4xl mx-auto px-4 py-4 flex items-center justify-between">
<h1 className="text-xl font-bold text-white">Project Memory</h1>
<div className="flex items-center gap-4">
<span className="text-purple-300 text-sm">{user.firstName} ({user.id})</span>
<button
onClick={logout}
className="px-3 py-1.5 bg-white/10 hover:bg-white/20 text-white rounded-lg transition-all text-sm"
>
Logout
</button>
</div>
</div>
</header>
{/* Main Content */}
<main className="max-w-4xl mx-auto px-4 py-8">
<div className="text-center mb-8">
<h2 className="text-3xl font-bold text-white mb-2">Select a Project</h2>
<p className="text-purple-300">Choose an existing project or start fresh</p>
</div>
{/* Error Display */}
{error && (
<div className="mb-6 p-4 bg-red-500/20 border border-red-500/50 rounded-lg text-red-200 text-center">
{error}
</div>
)}
{/* Loading State */}
{isLoading ? (
<div className="text-center text-purple-300 py-12">Loading projects...</div>
) : (
<>
{/* Existing Projects */}
{existingProjects.length > 0 && mode === 'select' && (
<div className="mb-8">
<h3 className="text-lg font-semibold text-white mb-4">Your Projects</h3>
<div className="grid gap-4 md:grid-cols-2">
{existingProjects.map((project) => (
<button
key={project.id}
onClick={() => handleSelectProject(project)}
className="text-left p-4 bg-white/10 hover:bg-white/20 border border-white/20 hover:border-purple-500/50 rounded-xl transition-all group"
>
<div className="flex items-start justify-between">
<div>
<h4 className="text-white font-medium group-hover:text-purple-300 transition-colors">
{project.name}
</h4>
<p className="text-purple-300/70 text-sm mt-1 line-clamp-2">
{project.description || 'No description'}
</p>
</div>
<span className="text-xs px-2 py-1 bg-purple-500/20 text-purple-300 rounded">
{project.role}
</span>
</div>
</button>
))}
</div>
</div>
)}
{/* Action Buttons (when in select mode) */}
{mode === 'select' && (
<div className="grid gap-4 md:grid-cols-2">
<button
onClick={() => setMode('create')}
className="p-6 bg-purple-600/20 hover:bg-purple-600/30 border-2 border-dashed border-purple-500/50 hover:border-purple-500 rounded-xl transition-all text-center"
>
<div className="text-4xl mb-2">+</div>
<h4 className="text-white font-medium">Create New Project</h4>
<p className="text-purple-300/70 text-sm mt-1">Start a new team project</p>
</button>
<button
onClick={() => setMode('join')}
className="p-6 bg-white/5 hover:bg-white/10 border-2 border-dashed border-white/20 hover:border-white/40 rounded-xl transition-all text-center"
>
<div className="text-4xl mb-2">→</div>
<h4 className="text-white font-medium">Join Existing Project</h4>
<p className="text-purple-300/70 text-sm mt-1">Enter a project ID to join</p>
</button>
</div>
)}
{/* Create Project Form */}
{mode === 'create' && (
<div className="bg-white/10 backdrop-blur-lg rounded-2xl p-8 border border-white/20">
<div className="flex items-center justify-between mb-6">
<h3 className="text-xl font-semibold text-white">Create New Project</h3>
<button
onClick={() => { setMode('select'); setError(''); }}
className="text-purple-300 hover:text-white text-sm"
>
← Back
</button>
</div>
<form onSubmit={handleCreateProject} className="space-y-4">
<div>
<label htmlFor="projectName" className="block text-sm font-medium text-purple-200 mb-1">
Project Name (also used as ID)
</label>
<input
id="projectName"
type="text"
value={projectName}
onChange={(e) => setProjectName(e.target.value)}
placeholder="e.g., fastgate"
className="w-full px-4 py-3 bg-white/5 border border-white/10 rounded-lg text-white placeholder-white/40 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent"
disabled={isSubmitting}
/>
<p className="text-purple-300/50 text-xs mt-1">Must be unique. This will be the project ID.</p>
</div>
<div>
<label htmlFor="projectDescription" className="block text-sm font-medium text-purple-200 mb-1">
Description
</label>
<textarea
id="projectDescription"
value={projectDescription}
onChange={(e) => setProjectDescription(e.target.value)}
placeholder="What is this project about?"
rows={3}
className="w-full px-4 py-3 bg-white/5 border border-white/10 rounded-lg text-white placeholder-white/40 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent resize-none"
disabled={isSubmitting}
/>
</div>
<button
type="submit"
disabled={isSubmitting}
className="w-full py-3 px-4 bg-purple-600 hover:bg-purple-700 disabled:bg-purple-800 disabled:cursor-not-allowed text-white font-medium rounded-lg transition-all"
>
{isSubmitting ? 'Creating...' : 'Create Project'}
</button>
</form>
</div>
)}
{/* Join Project Form */}
{mode === 'join' && (
<div className="bg-white/10 backdrop-blur-lg rounded-2xl p-8 border border-white/20">
<div className="flex items-center justify-between mb-6">
<h3 className="text-xl font-semibold text-white">Join Existing Project</h3>
<button
onClick={() => { setMode('select'); setError(''); }}
className="text-purple-300 hover:text-white text-sm"
>
← Back
</button>
</div>
<form onSubmit={handleJoinProject} className="space-y-4">
<div>
<label htmlFor="joinProjectId" className="block text-sm font-medium text-purple-200 mb-1">
Project ID
</label>
<input
id="joinProjectId"
type="text"
value={joinProjectId}
onChange={(e) => setJoinProjectId(e.target.value)}
placeholder="Enter the project ID"
className="w-full px-4 py-3 bg-white/5 border border-white/10 rounded-lg text-white placeholder-white/40 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent"
disabled={isSubmitting}
/>
<p className="text-purple-300/50 text-xs mt-1">Ask your team lead for the project ID</p>
</div>
<button
type="submit"
disabled={isSubmitting}
className="w-full py-3 px-4 bg-purple-600 hover:bg-purple-700 disabled:bg-purple-800 disabled:cursor-not-allowed text-white font-medium rounded-lg transition-all"
>
{isSubmitting ? 'Joining...' : 'Join Project'}
</button>
</form>
</div>
)}
</>
)}
</main>
</div>
);
}
|