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 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 7x 7x 7x 7x 7x 9x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 9x 1x 1x 9x 9x 1x 1x 1x 1x 1x 1x 9x 8x 8x 9x 9x 21x 21x 21x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 9x 9x 9x 9x 21x 10x 10x 10x 10x 10x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 8x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 5x 5x 21x 21x 21x 21x 21x 21x 21x 21x 11x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 88x 88x 88x 88x 88x 88x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 11x 11x 21x 21x 21x 21x 21x 21x 21x | 'use client';
import { useState, useEffect, useRef } from 'react';
import { useRouter } from 'next/navigation';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import {
Loader2,
Plus,
Pencil,
Trash2,
ExternalLink,
Image as ImageIcon,
Video,
Github,
Globe,
FileText,
Upload,
X,
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Label } from '@/components/ui/label';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { createClient } from '@/lib/supabase/client';
import {
createSampleSchema,
type CreateSampleInput,
type SampleType,
} from '@/lib/validations/sample';
import { useToast } from '@/hooks/use-toast';
import type { Sample } from '@/types/database';
const SAMPLE_TYPE_LABELS: Record<string, string> = {
image: 'Image',
video: 'Video',
github: 'GitHub',
website: 'Website',
document: 'Document',
dribbble: 'Dribbble',
behance: 'Behance',
url: 'URL',
pdf: 'PDF',
other: 'Other',
};
const SAMPLE_TYPES: { value: SampleType; label: string; icon: typeof ImageIcon }[] = [
{ value: 'image', label: 'Image', icon: ImageIcon },
{ value: 'video', label: 'Video', icon: Video },
{ value: 'github', label: 'GitHub', icon: Github },
{ value: 'dribbble', label: 'Dribbble', icon: Globe },
{ value: 'behance', label: 'Behance', icon: Globe },
{ value: 'url', label: 'URL', icon: ExternalLink },
{ value: 'pdf', label: 'PDF', icon: FileText },
{ value: 'other', label: 'Other', icon: Globe },
];
export default function SamplesPage() {
const [isLoading, setIsLoading] = useState(true);
const [samples, setSamples] = useState<Sample[]>([]);
const [profileId, setProfileId] = useState<string | null>(null);
const [userId, setUserId] = useState<string | null>(null);
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [editingSample, setEditingSample] = useState<Sample | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false);
const [deletingSampleId, setDeletingSampleId] = useState<string | null>(null);
const [isUploadingThumbnail, setIsUploadingThumbnail] = useState(false);
const [thumbnailPreview, setThumbnailPreview] = useState<string | null>(null);
const thumbnailInputRef = useRef<HTMLInputElement>(null);
const router = useRouter();
const { toast } = useToast();
const {
register,
handleSubmit,
reset,
setValue,
watch,
formState: { errors },
} = useForm<CreateSampleInput>({
resolver: zodResolver(createSampleSchema),
defaultValues: {
type: 'url',
title: '',
description: '',
url: '',
thumbnail_url: '',
display_order: 0,
},
});
const selectedType = watch('type');
useEffect(() => {
const fetchSamples = async () => {
try {
const supabase = createClient();
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
router.push('/login');
return;
}
const { data: profile } = await supabase
.from('profiles')
.select('id')
.eq('user_id', user.id)
.single();
if (!profile) {
router.push('/onboarding');
return;
}
setProfileId(profile.id);
setUserId(user.id);
const { data: samplesData, error } = await supabase
.from('samples')
.select('*')
.eq('profile_id', profile.id)
.order('display_order', { ascending: true });
if (error) {
throw error;
}
setSamples(samplesData || []);
} catch (error) {
console.error('Error fetching samples:', error);
toast({
variant: 'destructive',
title: 'Error',
description: 'Failed to load samples.',
});
} finally {
setIsLoading(false);
}
};
fetchSamples();
}, [router, toast]);
const handleThumbnailUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file || !userId) return;
// Validate file type
const validTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
if (!validTypes.includes(file.type)) {
toast({
variant: 'destructive',
title: 'Invalid File Type',
description: 'Please upload a JPEG, PNG, GIF, or WebP image.',
});
return;
}
// Validate file size (max 10MB)
if (file.size > 10 * 1024 * 1024) {
toast({
variant: 'destructive',
title: 'File Too Large',
description: 'Please upload an image smaller than 10MB.',
});
return;
}
setIsUploadingThumbnail(true);
try {
const supabase = createClient();
// Generate unique filename
const fileExt = file.name.split('.').pop();
const fileName = `${userId}/${Date.now()}-${Math.random().toString(36).substring(7)}.${fileExt}`;
// Upload to Supabase Storage (samples bucket)
const { error: uploadError } = await supabase.storage.from('samples').upload(fileName, file, {
cacheControl: '3600',
upsert: false,
});
if (uploadError) {
console.error('Storage upload error:', uploadError);
throw uploadError;
}
// Get public URL
const { data: urlData } = supabase.storage.from('samples').getPublicUrl(fileName);
const thumbnailUrl = urlData.publicUrl;
// Set the thumbnail URL in the form
setValue('thumbnail_url', thumbnailUrl);
setThumbnailPreview(thumbnailUrl);
toast({
title: 'Image Uploaded',
description: 'Thumbnail has been uploaded successfully.',
});
} catch (error) {
console.error('Error uploading thumbnail:', error);
toast({
variant: 'destructive',
title: 'Upload Failed',
description: 'Failed to upload thumbnail. Please try again.',
});
} finally {
setIsUploadingThumbnail(false);
// Reset file input
if (thumbnailInputRef.current) {
thumbnailInputRef.current.value = '';
}
}
};
const clearThumbnail = () => {
setValue('thumbnail_url', '');
setThumbnailPreview(null);
};
const openCreateDialog = () => {
setEditingSample(null);
setThumbnailPreview(null);
reset({
type: 'url',
title: '',
description: '',
url: '',
thumbnail_url: '',
display_order: samples.length,
});
setIsDialogOpen(true);
};
const openEditDialog = (sample: Sample) => {
setEditingSample(sample);
setThumbnailPreview(sample.thumbnail_url || null);
reset({
type: sample.type as SampleType,
title: sample.title,
description: sample.description || '',
url: sample.url,
thumbnail_url: sample.thumbnail_url || '',
display_order: sample.display_order,
});
setIsDialogOpen(true);
};
const onSubmit = async (data: CreateSampleInput) => {
if (!profileId) return;
setIsSubmitting(true);
try {
const supabase = createClient();
if (editingSample) {
// Update existing sample
const { error } = await supabase
.from('samples')
.update({
type: data.type,
title: data.title,
description: data.description || null,
url: data.url,
thumbnail_url: data.thumbnail_url || null,
display_order: data.display_order,
updated_at: new Date().toISOString(),
})
.eq('id', editingSample.id);
if (error) throw error;
setSamples((prev) =>
prev.map((s) =>
s.id === editingSample.id
? {
...s,
type: data.type,
title: data.title,
description: data.description || null,
url: data.url,
thumbnail_url: data.thumbnail_url || null,
display_order: data.display_order ?? 0,
}
: s
)
);
toast({
title: 'Sample Updated',
description: 'Your work sample has been updated.',
});
} else {
// Create new sample
const { data: newSample, error } = await supabase
.from('samples')
.insert({
profile_id: profileId,
type: data.type,
title: data.title,
description: data.description || null,
url: data.url,
thumbnail_url: data.thumbnail_url || null,
display_order: data.display_order,
})
.select()
.single();
if (error) throw error;
setSamples((prev) => [...prev, newSample]);
toast({
title: 'Sample Added',
description: 'Your work sample has been added.',
});
}
setIsDialogOpen(false);
} catch (error) {
console.error('Error saving sample:', error);
toast({
variant: 'destructive',
title: 'Error',
description: 'Failed to save sample. Please try again.',
});
} finally {
setIsSubmitting(false);
}
};
const deleteSample = async (sampleId: string) => {
setDeletingSampleId(sampleId);
try {
const supabase = createClient();
const { error } = await supabase.from('samples').delete().eq('id', sampleId);
if (error) throw error;
setSamples((prev) => prev.filter((s) => s.id !== sampleId));
toast({
title: 'Sample Deleted',
description: 'Your work sample has been removed.',
});
} catch (error) {
console.error('Error deleting sample:', error);
toast({
variant: 'destructive',
title: 'Error',
description: 'Failed to delete sample. Please try again.',
});
} finally {
setDeletingSampleId(null);
}
};
const getTypeIcon = (type: string) => {
const typeConfig = SAMPLE_TYPES.find((t) => t.value === type);
const Icon = typeConfig?.icon || Globe;
return <Icon className="h-4 w-4" />;
};
if (isLoading) {
return (
<div className="flex min-h-[400px] items-center justify-center">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
);
}
return (
<div className="space-y-6">
<div className="flex items-center justify-between">
<div>
<h1 className="text-3xl font-bold">Work Samples</h1>
<p className="text-muted-foreground">Showcase your best work to potential clients</p>
</div>
<Button onClick={openCreateDialog}>
<Plus className="mr-2 h-4 w-4" />
Add Sample
</Button>
</div>
{samples.length === 0 ? (
<Card>
<CardContent className="flex flex-col items-center justify-center py-12">
<ImageIcon className="mb-4 h-12 w-12 text-muted-foreground" />
<h3 className="mb-2 text-lg font-medium">No work samples yet</h3>
<p className="mb-4 text-center text-muted-foreground">
Add your first work sample to showcase your skills and projects.
</p>
<Button onClick={openCreateDialog}>
<Plus className="mr-2 h-4 w-4" />
Add Your First Sample
</Button>
</CardContent>
</Card>
) : (
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{samples.map((sample) => (
<Card key={sample.id} className="group relative">
<CardHeader className="pb-3">
<div className="flex items-start justify-between">
<div className="flex items-center gap-2">
{getTypeIcon(sample.type)}
<span className="text-xs uppercase text-muted-foreground">{sample.type}</span>
</div>
<div className="flex gap-1 opacity-0 transition-opacity group-hover:opacity-100">
<Button
variant="ghost"
size="icon"
className="h-8 w-8"
onClick={() => openEditDialog(sample)}
>
<Pencil className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
className="h-8 w-8 text-destructive hover:text-destructive"
onClick={() => deleteSample(sample.id)}
disabled={deletingSampleId === sample.id}
>
{deletingSampleId === sample.id ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<Trash2 className="h-4 w-4" />
)}
</Button>
</div>
</div>
<CardTitle className="text-lg">{sample.title}</CardTitle>
{sample.description && (
<CardDescription className="line-clamp-2">{sample.description}</CardDescription>
)}
</CardHeader>
<CardContent>
<a
href={sample.url}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-sm text-primary hover:underline"
>
View Sample
<ExternalLink className="h-3 w-3" />
</a>
</CardContent>
</Card>
))}
</div>
)}
{/* Add/Edit Sample Dialog */}
<Dialog
open={isDialogOpen}
onOpenChange={(open) => {
setIsDialogOpen(open);
if (!open) {
setThumbnailPreview(null);
}
}}
>
<DialogContent className="max-h-[90vh] overflow-y-auto sm:max-w-[500px]">
<DialogHeader>
<DialogTitle>{editingSample ? 'Edit Sample' : 'Add Work Sample'}</DialogTitle>
<DialogDescription>
{editingSample
? 'Update your work sample details.'
: 'Add a new work sample to showcase your skills.'}
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="type">Sample Type</Label>
<Select
value={selectedType}
onValueChange={(value) => setValue('type', value as SampleType)}
disabled={isSubmitting}
>
<SelectTrigger>
<SelectValue placeholder="Select type" />
</SelectTrigger>
<SelectContent>
{SAMPLE_TYPES.map((type) => (
<SelectItem key={type.value} value={type.value}>
<div className="flex items-center gap-2">
<type.icon className="h-4 w-4" />
{type.label}
</div>
</SelectItem>
))}
</SelectContent>
</Select>
{errors.type && <p className="text-sm text-destructive">{errors.type.message}</p>}
</div>
<div className="space-y-2">
<Label htmlFor="title">Title</Label>
<Input
id="title"
{...register('title')}
placeholder="My Awesome Project"
disabled={isSubmitting}
/>
{errors.title && <p className="text-sm text-destructive">{errors.title.message}</p>}
</div>
<div className="space-y-2">
<Label htmlFor="description">Description (optional)</Label>
<Textarea
id="description"
{...register('description')}
placeholder="Brief description of this work sample..."
rows={3}
disabled={isSubmitting}
/>
{errors.description && (
<p className="text-sm text-destructive">{errors.description.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="url">URL</Label>
<Input
id="url"
{...register('url')}
placeholder="https://example.com/project"
disabled={isSubmitting}
/>
{errors.url && <p className="text-sm text-destructive">{errors.url.message}</p>}
</div>
<div className="space-y-2">
<Label>Thumbnail (optional)</Label>
{thumbnailPreview ? (
<div className="relative w-full">
<div className="relative aspect-video w-full overflow-hidden rounded-lg border bg-muted">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={thumbnailPreview}
alt="Thumbnail preview"
className="h-full w-full object-cover"
/>
<button
type="button"
onClick={clearThumbnail}
className="absolute right-2 top-2 rounded-full bg-destructive p-1 text-destructive-foreground shadow-lg hover:bg-destructive/90"
disabled={isSubmitting}
>
<X className="h-4 w-4" />
</button>
</div>
</div>
) : (
<div className="space-y-2">
<div
onClick={() => thumbnailInputRef.current?.click()}
className="flex cursor-pointer flex-col items-center justify-center rounded-lg border-2 border-dashed border-muted-foreground/25 py-8 transition-colors hover:border-muted-foreground/50"
>
{isUploadingThumbnail ? (
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
) : (
<>
<Upload className="mb-2 h-8 w-8 text-muted-foreground" />
<p className="text-sm text-muted-foreground">Click to upload thumbnail</p>
<p className="text-xs text-muted-foreground">
JPEG, PNG, GIF, WebP (max 10MB)
</p>
</>
)}
</div>
<input
ref={thumbnailInputRef}
type="file"
accept="image/jpeg,image/png,image/gif,image/webp"
className="hidden"
onChange={handleThumbnailUpload}
disabled={isSubmitting || isUploadingThumbnail}
/>
<p className="text-xs text-muted-foreground">Or enter a URL:</p>
<Input
{...register('thumbnail_url')}
placeholder="https://example.com/thumbnail.jpg"
disabled={isSubmitting}
onChange={(e) => {
setValue('thumbnail_url', e.target.value);
if (e.target.value) {
setThumbnailPreview(e.target.value);
}
}}
/>
</div>
)}
{errors.thumbnail_url && (
<p className="text-sm text-destructive">{errors.thumbnail_url.message}</p>
)}
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setIsDialogOpen(false)}
disabled={isSubmitting}
>
Cancel
</Button>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Saving...
</>
) : editingSample ? (
'Update Sample'
) : (
'Add Sample'
)}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
</div>
);
}
|