mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-12 01:46:08 +00:00
refactor(dashboard): centralize access guards and split interactive pages into client components
This commit is contained in:
@@ -0,0 +1,387 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { ChangeEvent, FormEvent, useState } from 'react';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import Image from 'next/image';
|
||||||
|
import { ArrowLeft, Bug, Image as ImageIcon, Loader2, MessageSquareQuote, X } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||||
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||||
|
|
||||||
|
type FeedbackCategory = 'BUG' | 'FEATURE' | 'OTHER';
|
||||||
|
type TabValue = 'feedback' | 'review';
|
||||||
|
|
||||||
|
interface FormStatus {
|
||||||
|
type: 'success' | 'error';
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function FeedbackPage() {
|
||||||
|
const [activeTab, setActiveTab] = useState<TabValue>('feedback');
|
||||||
|
const [status, setStatus] = useState<FormStatus | null>(null);
|
||||||
|
const [isSubmittingFeedback, setIsSubmittingFeedback] = useState(false);
|
||||||
|
const [isSubmittingReview, setIsSubmittingReview] = useState(false);
|
||||||
|
|
||||||
|
const [feedbackTitle, setFeedbackTitle] = useState('');
|
||||||
|
const [feedbackCategory, setFeedbackCategory] = useState<FeedbackCategory>('BUG');
|
||||||
|
const [feedbackMessage, setFeedbackMessage] = useState('');
|
||||||
|
const [feedbackScreenshotFiles, setFeedbackScreenshotFiles] = useState<File[]>([]);
|
||||||
|
const [feedbackScreenshotPreviewUrls, setFeedbackScreenshotPreviewUrls] = useState<string[]>([]);
|
||||||
|
|
||||||
|
const [reviewTitle, setReviewTitle] = useState('');
|
||||||
|
const [reviewMessage, setReviewMessage] = useState('');
|
||||||
|
const [reviewRating, setReviewRating] = useState('5');
|
||||||
|
const [allowShowcase, setAllowShowcase] = useState(false);
|
||||||
|
|
||||||
|
const uploadFeedbackScreenshot = async (file: File): Promise<string> => {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('image', file);
|
||||||
|
|
||||||
|
const response = await fetch('/api/feedback/upload', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(data.error || 'Failed to upload screenshot');
|
||||||
|
}
|
||||||
|
|
||||||
|
return data.data.url as string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleFeedbackScreenshotChange = (event: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const selectedFiles = Array.from(event.target.files ?? []);
|
||||||
|
if (selectedFiles.length === 0) return;
|
||||||
|
|
||||||
|
const remainingSlots = 5 - feedbackScreenshotFiles.length;
|
||||||
|
if (remainingSlots <= 0) {
|
||||||
|
setStatus({ type: 'error', message: 'You can upload up to 5 screenshots.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const allowedFiles = selectedFiles.slice(0, remainingSlots);
|
||||||
|
const nextFiles: File[] = [];
|
||||||
|
const nextPreviewUrls: string[] = [];
|
||||||
|
|
||||||
|
for (const file of allowedFiles) {
|
||||||
|
const isImage = ['image/jpeg', 'image/png', 'image/webp', 'image/gif'].includes(file.type);
|
||||||
|
if (!isImage) {
|
||||||
|
setStatus({ type: 'error', message: 'Unsupported screenshot format. Use JPG, PNG, WEBP, or GIF.' });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (file.size > 10 * 1024 * 1024) {
|
||||||
|
setStatus({ type: 'error', message: 'Each screenshot must be smaller than 10MB.' });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
nextFiles.push(file);
|
||||||
|
nextPreviewUrls.push(URL.createObjectURL(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextFiles.length > 0) {
|
||||||
|
setFeedbackScreenshotFiles((prev) => [...prev, ...nextFiles]);
|
||||||
|
setFeedbackScreenshotPreviewUrls((prev) => [...prev, ...nextPreviewUrls]);
|
||||||
|
}
|
||||||
|
event.target.value = '';
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeFeedbackScreenshot = (index: number) => {
|
||||||
|
const targetUrl = feedbackScreenshotPreviewUrls[index];
|
||||||
|
if (targetUrl) URL.revokeObjectURL(targetUrl);
|
||||||
|
setFeedbackScreenshotFiles((prev) => prev.filter((_, currentIndex) => currentIndex !== index));
|
||||||
|
setFeedbackScreenshotPreviewUrls((prev) => prev.filter((_, currentIndex) => currentIndex !== index));
|
||||||
|
};
|
||||||
|
|
||||||
|
const clearFeedbackScreenshots = () => {
|
||||||
|
feedbackScreenshotPreviewUrls.forEach((url) => URL.revokeObjectURL(url));
|
||||||
|
setFeedbackScreenshotFiles([]);
|
||||||
|
setFeedbackScreenshotPreviewUrls([]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleFeedbackSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
||||||
|
event.preventDefault();
|
||||||
|
setStatus(null);
|
||||||
|
setIsSubmittingFeedback(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const screenshotUrls = await Promise.all(
|
||||||
|
feedbackScreenshotFiles.map((file) => uploadFeedbackScreenshot(file))
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await fetch('/api/feedback', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
type: 'FEEDBACK',
|
||||||
|
category: feedbackCategory,
|
||||||
|
title: feedbackTitle,
|
||||||
|
message: feedbackMessage,
|
||||||
|
screenshotUrls,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
if (!response.ok) {
|
||||||
|
setStatus({ type: 'error', message: data.error || 'Failed to submit feedback' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setFeedbackTitle('');
|
||||||
|
setFeedbackCategory('BUG');
|
||||||
|
setFeedbackMessage('');
|
||||||
|
clearFeedbackScreenshots();
|
||||||
|
setStatus({ type: 'success', message: 'Feedback submitted. Thank you.' });
|
||||||
|
} catch {
|
||||||
|
setStatus({ type: 'error', message: 'Failed to submit feedback' });
|
||||||
|
} finally {
|
||||||
|
setIsSubmittingFeedback(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleReviewSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
||||||
|
event.preventDefault();
|
||||||
|
setStatus(null);
|
||||||
|
setIsSubmittingReview(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/feedback', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
type: 'REVIEW',
|
||||||
|
title: reviewTitle,
|
||||||
|
message: reviewMessage,
|
||||||
|
rating: Number(reviewRating),
|
||||||
|
allowShowcase,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
if (!response.ok) {
|
||||||
|
setStatus({ type: 'error', message: data.error || 'Failed to submit review' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setReviewTitle('');
|
||||||
|
setReviewMessage('');
|
||||||
|
setReviewRating('5');
|
||||||
|
setAllowShowcase(false);
|
||||||
|
setStatus({ type: 'success', message: 'Review submitted. Thank you for sharing your experience.' });
|
||||||
|
} catch {
|
||||||
|
setStatus({ type: 'error', message: 'Failed to submit review' });
|
||||||
|
} finally {
|
||||||
|
setIsSubmittingReview(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-[calc(100vh-4rem)] px-4 py-10">
|
||||||
|
<div className="mx-auto w-full max-w-3xl space-y-6">
|
||||||
|
<Link href="/dashboard" className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground">
|
||||||
|
<ArrowLeft className="mr-1 h-4 w-4" />
|
||||||
|
Back to Dashboard
|
||||||
|
</Link>
|
||||||
|
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="text-2xl">Feedback & Review</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Send product feedback, report bugs, or share a review we can feature on the landing page.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
{status && (
|
||||||
|
<div
|
||||||
|
className={`rounded-lg border px-3 py-2 text-sm ${
|
||||||
|
status.type === 'success'
|
||||||
|
? 'border-emerald-500/40 bg-emerald-500/10 text-emerald-700 dark:text-emerald-400'
|
||||||
|
: 'border-destructive/40 bg-destructive/10 text-destructive'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{status.message}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Tabs value={activeTab} onValueChange={(value) => setActiveTab(value as TabValue)} className="w-full">
|
||||||
|
<TabsList className="w-full">
|
||||||
|
<TabsTrigger value="feedback" className="gap-1.5">
|
||||||
|
<Bug className="h-3.5 w-3.5" />
|
||||||
|
Feedback
|
||||||
|
</TabsTrigger>
|
||||||
|
<TabsTrigger value="review" className="gap-1.5">
|
||||||
|
<MessageSquareQuote className="h-3.5 w-3.5" />
|
||||||
|
Review
|
||||||
|
</TabsTrigger>
|
||||||
|
</TabsList>
|
||||||
|
|
||||||
|
<TabsContent value="feedback">
|
||||||
|
<form className="space-y-4 pt-2" onSubmit={handleFeedbackSubmit}>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="feedback-title">Title</Label>
|
||||||
|
<Input
|
||||||
|
id="feedback-title"
|
||||||
|
value={feedbackTitle}
|
||||||
|
onChange={(event) => setFeedbackTitle(event.target.value)}
|
||||||
|
placeholder="Short summary"
|
||||||
|
minLength={3}
|
||||||
|
maxLength={120}
|
||||||
|
required
|
||||||
|
disabled={isSubmittingFeedback}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Category</Label>
|
||||||
|
<Select
|
||||||
|
value={feedbackCategory}
|
||||||
|
onValueChange={(value: FeedbackCategory) => setFeedbackCategory(value)}
|
||||||
|
disabled={isSubmittingFeedback}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="BUG">Bug report</SelectItem>
|
||||||
|
<SelectItem value="FEATURE">Feature request</SelectItem>
|
||||||
|
<SelectItem value="OTHER">Other feedback</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="feedback-message">Details</Label>
|
||||||
|
<Textarea
|
||||||
|
id="feedback-message"
|
||||||
|
value={feedbackMessage}
|
||||||
|
onChange={(event) => setFeedbackMessage(event.target.value)}
|
||||||
|
minLength={10}
|
||||||
|
maxLength={3000}
|
||||||
|
placeholder="Tell us what happened, what you expected, or what you want to see."
|
||||||
|
required
|
||||||
|
disabled={isSubmittingFeedback}
|
||||||
|
rows={6}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="feedback-screenshot">Screenshots (optional, up to 5)</Label>
|
||||||
|
<Input
|
||||||
|
id="feedback-screenshot"
|
||||||
|
type="file"
|
||||||
|
accept="image/jpeg,image/png,image/webp,image/gif"
|
||||||
|
multiple
|
||||||
|
onChange={handleFeedbackScreenshotChange}
|
||||||
|
disabled={isSubmittingFeedback || feedbackScreenshotFiles.length >= 5}
|
||||||
|
/>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{feedbackScreenshotFiles.length}/5 selected
|
||||||
|
</p>
|
||||||
|
{feedbackScreenshotPreviewUrls.length > 0 && (
|
||||||
|
<div className="grid gap-2 sm:grid-cols-2">
|
||||||
|
{feedbackScreenshotPreviewUrls.map((previewUrl, index) => (
|
||||||
|
<div key={`${previewUrl}-${index}`} className="rounded-md border p-2">
|
||||||
|
<Image
|
||||||
|
src={previewUrl}
|
||||||
|
alt={`Feedback screenshot preview ${index + 1}`}
|
||||||
|
width={640}
|
||||||
|
height={360}
|
||||||
|
className="max-h-48 w-full rounded-sm object-contain"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
className="mt-2"
|
||||||
|
onClick={() => removeFeedbackScreenshot(index)}
|
||||||
|
disabled={isSubmittingFeedback}
|
||||||
|
>
|
||||||
|
<X className="mr-1.5 h-3.5 w-3.5" />
|
||||||
|
Remove
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Button type="submit" disabled={isSubmittingFeedback}>
|
||||||
|
{isSubmittingFeedback ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : <ImageIcon className="mr-2 h-4 w-4" />}
|
||||||
|
Submit Feedback
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</TabsContent>
|
||||||
|
|
||||||
|
<TabsContent value="review">
|
||||||
|
<form className="space-y-4 pt-2" onSubmit={handleReviewSubmit}>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="review-title">Title</Label>
|
||||||
|
<Input
|
||||||
|
id="review-title"
|
||||||
|
value={reviewTitle}
|
||||||
|
onChange={(event) => setReviewTitle(event.target.value)}
|
||||||
|
placeholder="Your headline"
|
||||||
|
minLength={3}
|
||||||
|
maxLength={120}
|
||||||
|
required
|
||||||
|
disabled={isSubmittingReview}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Rating</Label>
|
||||||
|
<Select value={reviewRating} onValueChange={setReviewRating} disabled={isSubmittingReview}>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="5">5 - Excellent</SelectItem>
|
||||||
|
<SelectItem value="4">4 - Very good</SelectItem>
|
||||||
|
<SelectItem value="3">3 - Good</SelectItem>
|
||||||
|
<SelectItem value="2">2 - Needs improvement</SelectItem>
|
||||||
|
<SelectItem value="1">1 - Poor</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="review-message">Experience</Label>
|
||||||
|
<Textarea
|
||||||
|
id="review-message"
|
||||||
|
value={reviewMessage}
|
||||||
|
onChange={(event) => setReviewMessage(event.target.value)}
|
||||||
|
minLength={10}
|
||||||
|
maxLength={3000}
|
||||||
|
placeholder="What has your experience been like using OpenFrame?"
|
||||||
|
required
|
||||||
|
disabled={isSubmittingReview}
|
||||||
|
rows={6}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label className="flex items-start gap-2 rounded-md border p-3 text-sm">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
className="mt-0.5"
|
||||||
|
checked={allowShowcase}
|
||||||
|
onChange={(event) => setAllowShowcase(event.target.checked)}
|
||||||
|
disabled={isSubmittingReview}
|
||||||
|
/>
|
||||||
|
<span>I allow OpenFrame to potentially showcase this review on the landing page.</span>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<Button type="submit" disabled={isSubmittingReview}>
|
||||||
|
{isSubmittingReview ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : <MessageSquareQuote className="mr-2 h-4 w-4" />}
|
||||||
|
Submit Review
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</TabsContent>
|
||||||
|
</Tabs>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,387 +1,7 @@
|
|||||||
'use client';
|
import { requireAuthOrRedirect } from '@/lib/route-access';
|
||||||
|
import FeedbackPageClient from './feedback-page-client';
|
||||||
|
|
||||||
import { ChangeEvent, FormEvent, useState } from 'react';
|
export default async function FeedbackPage() {
|
||||||
import Link from 'next/link';
|
await requireAuthOrRedirect();
|
||||||
import Image from 'next/image';
|
return <FeedbackPageClient />;
|
||||||
import { ArrowLeft, Bug, Image as ImageIcon, Loader2, MessageSquareQuote, X } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
|
||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
|
||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
|
||||||
|
|
||||||
type FeedbackCategory = 'BUG' | 'FEATURE' | 'OTHER';
|
|
||||||
type TabValue = 'feedback' | 'review';
|
|
||||||
|
|
||||||
interface FormStatus {
|
|
||||||
type: 'success' | 'error';
|
|
||||||
message: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function FeedbackPage() {
|
|
||||||
const [activeTab, setActiveTab] = useState<TabValue>('feedback');
|
|
||||||
const [status, setStatus] = useState<FormStatus | null>(null);
|
|
||||||
const [isSubmittingFeedback, setIsSubmittingFeedback] = useState(false);
|
|
||||||
const [isSubmittingReview, setIsSubmittingReview] = useState(false);
|
|
||||||
|
|
||||||
const [feedbackTitle, setFeedbackTitle] = useState('');
|
|
||||||
const [feedbackCategory, setFeedbackCategory] = useState<FeedbackCategory>('BUG');
|
|
||||||
const [feedbackMessage, setFeedbackMessage] = useState('');
|
|
||||||
const [feedbackScreenshotFiles, setFeedbackScreenshotFiles] = useState<File[]>([]);
|
|
||||||
const [feedbackScreenshotPreviewUrls, setFeedbackScreenshotPreviewUrls] = useState<string[]>([]);
|
|
||||||
|
|
||||||
const [reviewTitle, setReviewTitle] = useState('');
|
|
||||||
const [reviewMessage, setReviewMessage] = useState('');
|
|
||||||
const [reviewRating, setReviewRating] = useState('5');
|
|
||||||
const [allowShowcase, setAllowShowcase] = useState(false);
|
|
||||||
|
|
||||||
const uploadFeedbackScreenshot = async (file: File): Promise<string> => {
|
|
||||||
const formData = new FormData();
|
|
||||||
formData.append('image', file);
|
|
||||||
|
|
||||||
const response = await fetch('/api/feedback/upload', {
|
|
||||||
method: 'POST',
|
|
||||||
body: formData,
|
|
||||||
});
|
|
||||||
const data = await response.json();
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(data.error || 'Failed to upload screenshot');
|
|
||||||
}
|
|
||||||
|
|
||||||
return data.data.url as string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleFeedbackScreenshotChange = (event: ChangeEvent<HTMLInputElement>) => {
|
|
||||||
const selectedFiles = Array.from(event.target.files ?? []);
|
|
||||||
if (selectedFiles.length === 0) return;
|
|
||||||
|
|
||||||
const remainingSlots = 5 - feedbackScreenshotFiles.length;
|
|
||||||
if (remainingSlots <= 0) {
|
|
||||||
setStatus({ type: 'error', message: 'You can upload up to 5 screenshots.' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const allowedFiles = selectedFiles.slice(0, remainingSlots);
|
|
||||||
const nextFiles: File[] = [];
|
|
||||||
const nextPreviewUrls: string[] = [];
|
|
||||||
|
|
||||||
for (const file of allowedFiles) {
|
|
||||||
const isImage = ['image/jpeg', 'image/png', 'image/webp', 'image/gif'].includes(file.type);
|
|
||||||
if (!isImage) {
|
|
||||||
setStatus({ type: 'error', message: 'Unsupported screenshot format. Use JPG, PNG, WEBP, or GIF.' });
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (file.size > 10 * 1024 * 1024) {
|
|
||||||
setStatus({ type: 'error', message: 'Each screenshot must be smaller than 10MB.' });
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
nextFiles.push(file);
|
|
||||||
nextPreviewUrls.push(URL.createObjectURL(file));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nextFiles.length > 0) {
|
|
||||||
setFeedbackScreenshotFiles((prev) => [...prev, ...nextFiles]);
|
|
||||||
setFeedbackScreenshotPreviewUrls((prev) => [...prev, ...nextPreviewUrls]);
|
|
||||||
}
|
|
||||||
event.target.value = '';
|
|
||||||
};
|
|
||||||
|
|
||||||
const removeFeedbackScreenshot = (index: number) => {
|
|
||||||
const targetUrl = feedbackScreenshotPreviewUrls[index];
|
|
||||||
if (targetUrl) URL.revokeObjectURL(targetUrl);
|
|
||||||
setFeedbackScreenshotFiles((prev) => prev.filter((_, currentIndex) => currentIndex !== index));
|
|
||||||
setFeedbackScreenshotPreviewUrls((prev) => prev.filter((_, currentIndex) => currentIndex !== index));
|
|
||||||
};
|
|
||||||
|
|
||||||
const clearFeedbackScreenshots = () => {
|
|
||||||
feedbackScreenshotPreviewUrls.forEach((url) => URL.revokeObjectURL(url));
|
|
||||||
setFeedbackScreenshotFiles([]);
|
|
||||||
setFeedbackScreenshotPreviewUrls([]);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleFeedbackSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
|
||||||
event.preventDefault();
|
|
||||||
setStatus(null);
|
|
||||||
setIsSubmittingFeedback(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const screenshotUrls = await Promise.all(
|
|
||||||
feedbackScreenshotFiles.map((file) => uploadFeedbackScreenshot(file))
|
|
||||||
);
|
|
||||||
|
|
||||||
const response = await fetch('/api/feedback', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
type: 'FEEDBACK',
|
|
||||||
category: feedbackCategory,
|
|
||||||
title: feedbackTitle,
|
|
||||||
message: feedbackMessage,
|
|
||||||
screenshotUrls,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
if (!response.ok) {
|
|
||||||
setStatus({ type: 'error', message: data.error || 'Failed to submit feedback' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setFeedbackTitle('');
|
|
||||||
setFeedbackCategory('BUG');
|
|
||||||
setFeedbackMessage('');
|
|
||||||
clearFeedbackScreenshots();
|
|
||||||
setStatus({ type: 'success', message: 'Feedback submitted. Thank you.' });
|
|
||||||
} catch {
|
|
||||||
setStatus({ type: 'error', message: 'Failed to submit feedback' });
|
|
||||||
} finally {
|
|
||||||
setIsSubmittingFeedback(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleReviewSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
|
||||||
event.preventDefault();
|
|
||||||
setStatus(null);
|
|
||||||
setIsSubmittingReview(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch('/api/feedback', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
type: 'REVIEW',
|
|
||||||
title: reviewTitle,
|
|
||||||
message: reviewMessage,
|
|
||||||
rating: Number(reviewRating),
|
|
||||||
allowShowcase,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
if (!response.ok) {
|
|
||||||
setStatus({ type: 'error', message: data.error || 'Failed to submit review' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setReviewTitle('');
|
|
||||||
setReviewMessage('');
|
|
||||||
setReviewRating('5');
|
|
||||||
setAllowShowcase(false);
|
|
||||||
setStatus({ type: 'success', message: 'Review submitted. Thank you for sharing your experience.' });
|
|
||||||
} catch {
|
|
||||||
setStatus({ type: 'error', message: 'Failed to submit review' });
|
|
||||||
} finally {
|
|
||||||
setIsSubmittingReview(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="min-h-[calc(100vh-4rem)] px-4 py-10">
|
|
||||||
<div className="mx-auto w-full max-w-3xl space-y-6">
|
|
||||||
<Link href="/dashboard" className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground">
|
|
||||||
<ArrowLeft className="mr-1 h-4 w-4" />
|
|
||||||
Back to Dashboard
|
|
||||||
</Link>
|
|
||||||
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle className="text-2xl">Feedback & Review</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Send product feedback, report bugs, or share a review we can feature on the landing page.
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
{status && (
|
|
||||||
<div
|
|
||||||
className={`rounded-lg border px-3 py-2 text-sm ${
|
|
||||||
status.type === 'success'
|
|
||||||
? 'border-emerald-500/40 bg-emerald-500/10 text-emerald-700 dark:text-emerald-400'
|
|
||||||
: 'border-destructive/40 bg-destructive/10 text-destructive'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{status.message}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<Tabs value={activeTab} onValueChange={(value) => setActiveTab(value as TabValue)} className="w-full">
|
|
||||||
<TabsList className="w-full">
|
|
||||||
<TabsTrigger value="feedback" className="gap-1.5">
|
|
||||||
<Bug className="h-3.5 w-3.5" />
|
|
||||||
Feedback
|
|
||||||
</TabsTrigger>
|
|
||||||
<TabsTrigger value="review" className="gap-1.5">
|
|
||||||
<MessageSquareQuote className="h-3.5 w-3.5" />
|
|
||||||
Review
|
|
||||||
</TabsTrigger>
|
|
||||||
</TabsList>
|
|
||||||
|
|
||||||
<TabsContent value="feedback">
|
|
||||||
<form className="space-y-4 pt-2" onSubmit={handleFeedbackSubmit}>
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="feedback-title">Title</Label>
|
|
||||||
<Input
|
|
||||||
id="feedback-title"
|
|
||||||
value={feedbackTitle}
|
|
||||||
onChange={(event) => setFeedbackTitle(event.target.value)}
|
|
||||||
placeholder="Short summary"
|
|
||||||
minLength={3}
|
|
||||||
maxLength={120}
|
|
||||||
required
|
|
||||||
disabled={isSubmittingFeedback}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label>Category</Label>
|
|
||||||
<Select
|
|
||||||
value={feedbackCategory}
|
|
||||||
onValueChange={(value: FeedbackCategory) => setFeedbackCategory(value)}
|
|
||||||
disabled={isSubmittingFeedback}
|
|
||||||
>
|
|
||||||
<SelectTrigger>
|
|
||||||
<SelectValue />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
<SelectItem value="BUG">Bug report</SelectItem>
|
|
||||||
<SelectItem value="FEATURE">Feature request</SelectItem>
|
|
||||||
<SelectItem value="OTHER">Other feedback</SelectItem>
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="feedback-message">Details</Label>
|
|
||||||
<Textarea
|
|
||||||
id="feedback-message"
|
|
||||||
value={feedbackMessage}
|
|
||||||
onChange={(event) => setFeedbackMessage(event.target.value)}
|
|
||||||
minLength={10}
|
|
||||||
maxLength={3000}
|
|
||||||
placeholder="Tell us what happened, what you expected, or what you want to see."
|
|
||||||
required
|
|
||||||
disabled={isSubmittingFeedback}
|
|
||||||
rows={6}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="feedback-screenshot">Screenshots (optional, up to 5)</Label>
|
|
||||||
<Input
|
|
||||||
id="feedback-screenshot"
|
|
||||||
type="file"
|
|
||||||
accept="image/jpeg,image/png,image/webp,image/gif"
|
|
||||||
multiple
|
|
||||||
onChange={handleFeedbackScreenshotChange}
|
|
||||||
disabled={isSubmittingFeedback || feedbackScreenshotFiles.length >= 5}
|
|
||||||
/>
|
|
||||||
<p className="text-xs text-muted-foreground">
|
|
||||||
{feedbackScreenshotFiles.length}/5 selected
|
|
||||||
</p>
|
|
||||||
{feedbackScreenshotPreviewUrls.length > 0 && (
|
|
||||||
<div className="grid gap-2 sm:grid-cols-2">
|
|
||||||
{feedbackScreenshotPreviewUrls.map((previewUrl, index) => (
|
|
||||||
<div key={`${previewUrl}-${index}`} className="rounded-md border p-2">
|
|
||||||
<Image
|
|
||||||
src={previewUrl}
|
|
||||||
alt={`Feedback screenshot preview ${index + 1}`}
|
|
||||||
width={640}
|
|
||||||
height={360}
|
|
||||||
className="max-h-48 w-full rounded-sm object-contain"
|
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
className="mt-2"
|
|
||||||
onClick={() => removeFeedbackScreenshot(index)}
|
|
||||||
disabled={isSubmittingFeedback}
|
|
||||||
>
|
|
||||||
<X className="mr-1.5 h-3.5 w-3.5" />
|
|
||||||
Remove
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Button type="submit" disabled={isSubmittingFeedback}>
|
|
||||||
{isSubmittingFeedback ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : <ImageIcon className="mr-2 h-4 w-4" />}
|
|
||||||
Submit Feedback
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
</TabsContent>
|
|
||||||
|
|
||||||
<TabsContent value="review">
|
|
||||||
<form className="space-y-4 pt-2" onSubmit={handleReviewSubmit}>
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="review-title">Title</Label>
|
|
||||||
<Input
|
|
||||||
id="review-title"
|
|
||||||
value={reviewTitle}
|
|
||||||
onChange={(event) => setReviewTitle(event.target.value)}
|
|
||||||
placeholder="Your headline"
|
|
||||||
minLength={3}
|
|
||||||
maxLength={120}
|
|
||||||
required
|
|
||||||
disabled={isSubmittingReview}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label>Rating</Label>
|
|
||||||
<Select value={reviewRating} onValueChange={setReviewRating} disabled={isSubmittingReview}>
|
|
||||||
<SelectTrigger>
|
|
||||||
<SelectValue />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
<SelectItem value="5">5 - Excellent</SelectItem>
|
|
||||||
<SelectItem value="4">4 - Very good</SelectItem>
|
|
||||||
<SelectItem value="3">3 - Good</SelectItem>
|
|
||||||
<SelectItem value="2">2 - Needs improvement</SelectItem>
|
|
||||||
<SelectItem value="1">1 - Poor</SelectItem>
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="review-message">Experience</Label>
|
|
||||||
<Textarea
|
|
||||||
id="review-message"
|
|
||||||
value={reviewMessage}
|
|
||||||
onChange={(event) => setReviewMessage(event.target.value)}
|
|
||||||
minLength={10}
|
|
||||||
maxLength={3000}
|
|
||||||
placeholder="What has your experience been like using OpenFrame?"
|
|
||||||
required
|
|
||||||
disabled={isSubmittingReview}
|
|
||||||
rows={6}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<label className="flex items-start gap-2 rounded-md border p-3 text-sm">
|
|
||||||
<input
|
|
||||||
type="checkbox"
|
|
||||||
className="mt-0.5"
|
|
||||||
checked={allowShowcase}
|
|
||||||
onChange={(event) => setAllowShowcase(event.target.checked)}
|
|
||||||
disabled={isSubmittingReview}
|
|
||||||
/>
|
|
||||||
<span>I allow OpenFrame to potentially showcase this review on the landing page.</span>
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<Button type="submit" disabled={isSubmittingReview}>
|
|
||||||
{isSubmittingReview ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : <MessageSquareQuote className="mr-2 h-4 w-4" />}
|
|
||||||
Submit Review
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
</TabsContent>
|
|
||||||
</Tabs>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useParams } from 'next/navigation';
|
|
||||||
import { MembersManagementPage } from '@/components/members-management-page';
|
import { MembersManagementPage } from '@/components/members-management-page';
|
||||||
|
import { requireProjectAccessOrRedirect } from '@/lib/route-access';
|
||||||
|
|
||||||
export default function ProjectMembersPage() {
|
interface ProjectMembersPageProps {
|
||||||
const params = useParams();
|
params: Promise<{ projectId: string }>;
|
||||||
const projectId = params.projectId as string;
|
}
|
||||||
|
|
||||||
|
export default async function ProjectMembersPage({ params }: ProjectMembersPageProps) {
|
||||||
|
const { projectId } = await params;
|
||||||
|
|
||||||
|
await requireProjectAccessOrRedirect({
|
||||||
|
projectId,
|
||||||
|
intent: 'manage',
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MembersManagementPage
|
<MembersManagementPage
|
||||||
@@ -20,7 +26,6 @@ export default function ProjectMembersPage() {
|
|||||||
<strong>Commentator</strong> - can view and comment only.
|
<strong>Commentator</strong> - can view and comment only.
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
forbiddenRedirect="/dashboard"
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,6 +93,9 @@ export default async function ProjectPage({ params, searchParams }: ProjectPageP
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isOwner && !isMember && !isPublic && !isWorkspaceMember) {
|
if (!isOwner && !isMember && !isPublic && !isWorkspaceMember) {
|
||||||
|
if (!session?.user?.id) {
|
||||||
|
redirect('/login');
|
||||||
|
}
|
||||||
redirect('/dashboard');
|
redirect('/dashboard');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,513 +1,17 @@
|
|||||||
'use client';
|
import { requireProjectAccessOrRedirect } from '@/lib/route-access';
|
||||||
|
import ProjectSettingsPageClient from './project-settings-page-client';
|
||||||
import { useState, useEffect } from 'react';
|
|
||||||
import { useRouter } from 'next/navigation';
|
|
||||||
import Link from 'next/link';
|
|
||||||
import { ArrowLeft, Loader2, Globe, Lock, UserPlus, Trash2, AlertTriangle, Settings, Save, Tag, Plus, X } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
|
||||||
import {
|
|
||||||
AlertDialog,
|
|
||||||
AlertDialogAction,
|
|
||||||
AlertDialogCancel,
|
|
||||||
AlertDialogContent,
|
|
||||||
AlertDialogDescription,
|
|
||||||
AlertDialogFooter,
|
|
||||||
AlertDialogHeader,
|
|
||||||
AlertDialogTitle,
|
|
||||||
AlertDialogTrigger,
|
|
||||||
} from '@/components/ui/alert-dialog';
|
|
||||||
|
|
||||||
type Visibility = 'PRIVATE' | 'INVITE' | 'PUBLIC';
|
|
||||||
|
|
||||||
const visibilityOptions: { value: Visibility; label: string; description: string; icon: React.ReactNode }[] = [
|
|
||||||
{
|
|
||||||
value: 'PRIVATE',
|
|
||||||
label: 'Private',
|
|
||||||
description: 'Only you can access this project',
|
|
||||||
icon: <Lock className="h-5 w-5" />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: 'INVITE',
|
|
||||||
label: 'Invite Only',
|
|
||||||
description: 'Share with specific people via email',
|
|
||||||
icon: <UserPlus className="h-5 w-5" />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: 'PUBLIC',
|
|
||||||
label: 'Public',
|
|
||||||
description: 'Anyone with the link can view',
|
|
||||||
icon: <Globe className="h-5 w-5" />,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
interface ProjectSettingsPageProps {
|
interface ProjectSettingsPageProps {
|
||||||
params: Promise<{ projectId: string }>;
|
params: Promise<{ projectId: string }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CommentTag {
|
export default async function ProjectSettingsPage({ params }: ProjectSettingsPageProps) {
|
||||||
id: string;
|
const { projectId } = await params;
|
||||||
name: string;
|
|
||||||
color: string;
|
|
||||||
position: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ProjectSettingsPage({ params }: ProjectSettingsPageProps) {
|
await requireProjectAccessOrRedirect({
|
||||||
const router = useRouter();
|
projectId,
|
||||||
const [projectId, setProjectId] = useState<string>('');
|
intent: 'manage',
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
});
|
||||||
const [isSaving, setIsSaving] = useState(false);
|
|
||||||
const [isDeleting, setIsDeleting] = useState(false);
|
|
||||||
const [error, setError] = useState('');
|
|
||||||
const [success, setSuccess] = useState('');
|
|
||||||
const [deleteConfirmation, setDeleteConfirmation] = useState('');
|
|
||||||
const [formData, setFormData] = useState({
|
|
||||||
name: '',
|
|
||||||
description: '',
|
|
||||||
visibility: 'PRIVATE' as Visibility,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Tag management state
|
return <ProjectSettingsPageClient projectId={projectId} />;
|
||||||
const [tags, setTags] = useState<CommentTag[]>([]);
|
|
||||||
const [newTagName, setNewTagName] = useState('');
|
|
||||||
const [newTagColor, setNewTagColor] = useState('#3B82F6');
|
|
||||||
const [isAddingTag, setIsAddingTag] = useState(false);
|
|
||||||
const [editingTagId, setEditingTagId] = useState<string | null>(null);
|
|
||||||
const [editTagName, setEditTagName] = useState('');
|
|
||||||
const [editTagColor, setEditTagColor] = useState('');
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
params.then(({ projectId: id }) => {
|
|
||||||
setProjectId(id);
|
|
||||||
// Fetch project data
|
|
||||||
fetch(`/api/projects/${id}`)
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((data) => {
|
|
||||||
if (data.error) {
|
|
||||||
setError(data.error);
|
|
||||||
} else {
|
|
||||||
const project = data.data;
|
|
||||||
setFormData({
|
|
||||||
name: project.name || '',
|
|
||||||
description: project.description || '',
|
|
||||||
visibility: project.visibility || 'PRIVATE',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(() => setError('Failed to load project'))
|
|
||||||
.finally(() => setIsLoading(false));
|
|
||||||
|
|
||||||
// Fetch tags
|
|
||||||
fetch(`/api/projects/${id}/tags`)
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((data) => {
|
|
||||||
if (Array.isArray(data.data)) {
|
|
||||||
setTags(data.data);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(() => { /* Silent fail - tags are optional */ });
|
|
||||||
});
|
|
||||||
}, [params]);
|
|
||||||
|
|
||||||
const handleSave = async (e: React.FormEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
setIsSaving(true);
|
|
||||||
setError('');
|
|
||||||
setSuccess('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/projects/${projectId}`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(formData),
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
setError(data.error || 'Failed to update project');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setSuccess('Project settings saved successfully');
|
|
||||||
setTimeout(() => setSuccess(''), 3000);
|
|
||||||
} catch {
|
|
||||||
setError('Something went wrong. Please try again.');
|
|
||||||
} finally {
|
|
||||||
setIsSaving(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleAddTag = async () => {
|
|
||||||
if (!newTagName.trim()) return;
|
|
||||||
setIsAddingTag(true);
|
|
||||||
try {
|
|
||||||
const res = await fetch(`/api/projects/${projectId}/tags`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ name: newTagName.trim(), color: newTagColor }),
|
|
||||||
});
|
|
||||||
if (res.ok) {
|
|
||||||
const data = await res.json();
|
|
||||||
const newTag = data.data;
|
|
||||||
setTags([...tags, newTag]);
|
|
||||||
setNewTagName('');
|
|
||||||
setNewTagColor('#3B82F6');
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Silent fail
|
|
||||||
} finally {
|
|
||||||
setIsAddingTag(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleUpdateTag = async (tagId: string) => {
|
|
||||||
if (!editTagName.trim()) return;
|
|
||||||
try {
|
|
||||||
const res = await fetch(`/api/projects/${projectId}/tags/${tagId}`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ name: editTagName.trim(), color: editTagColor }),
|
|
||||||
});
|
|
||||||
if (res.ok) {
|
|
||||||
const data = await res.json();
|
|
||||||
const updated = data.data;
|
|
||||||
setTags(tags.map((t) => (t.id === tagId ? updated : t)));
|
|
||||||
setEditingTagId(null);
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Silent fail
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleDeleteTag = async (tagId: string) => {
|
|
||||||
try {
|
|
||||||
const res = await fetch(`/api/projects/${projectId}/tags/${tagId}`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
});
|
|
||||||
if (res.ok) {
|
|
||||||
setTags(tags.filter((t) => t.id !== tagId));
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Silent fail
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleDelete = async () => {
|
|
||||||
if (deleteConfirmation !== formData.name) {
|
|
||||||
setError('Project name does not match');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsDeleting(true);
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/projects/${projectId}`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const data = await response.json();
|
|
||||||
setError(data.error || 'Failed to delete project');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
router.push('/dashboard');
|
|
||||||
} catch {
|
|
||||||
setError('Something went wrong. Please try again.');
|
|
||||||
} finally {
|
|
||||||
setIsDeleting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isLoading) {
|
|
||||||
return (
|
|
||||||
<div className="min-h-[calc(100vh-4rem)] flex items-center justify-center">
|
|
||||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
|
||||||
<div className="w-full max-w-xl">
|
|
||||||
<div className="mb-8">
|
|
||||||
<Link
|
|
||||||
href={`/projects/${projectId}`}
|
|
||||||
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4 mr-1" />
|
|
||||||
Back to Project
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-6">
|
|
||||||
{/* General Settings */}
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader className="text-center pb-2">
|
|
||||||
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
|
||||||
<Settings className="h-7 w-7 text-primary" />
|
|
||||||
</div>
|
|
||||||
<CardTitle className="text-2xl">Project Settings</CardTitle>
|
|
||||||
<CardDescription className="text-base">
|
|
||||||
Update your project details and access settings
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="pt-6">
|
|
||||||
<form onSubmit={handleSave} className="space-y-6">
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="name" className="text-sm font-medium">
|
|
||||||
Project Name
|
|
||||||
</Label>
|
|
||||||
<Input
|
|
||||||
id="name"
|
|
||||||
value={formData.name}
|
|
||||||
onChange={(e) => setFormData(prev => ({ ...prev, name: e.target.value }))}
|
|
||||||
required
|
|
||||||
disabled={isSaving}
|
|
||||||
className="h-11"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="description" className="text-sm font-medium">
|
|
||||||
Description
|
|
||||||
</Label>
|
|
||||||
<Textarea
|
|
||||||
id="description"
|
|
||||||
value={formData.description}
|
|
||||||
onChange={(e) => setFormData(prev => ({ ...prev, description: e.target.value }))}
|
|
||||||
rows={3}
|
|
||||||
disabled={isSaving}
|
|
||||||
className="resize-none"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-3">
|
|
||||||
<Label className="text-sm font-medium">Who can access?</Label>
|
|
||||||
<div className="grid gap-3">
|
|
||||||
{visibilityOptions.map((option) => (
|
|
||||||
<button
|
|
||||||
key={option.value}
|
|
||||||
type="button"
|
|
||||||
onClick={() => setFormData(prev => ({ ...prev, visibility: option.value }))}
|
|
||||||
disabled={isSaving}
|
|
||||||
className={`w-full flex items-center gap-4 p-4 rounded-xl border-2 text-left transition-all ${formData.visibility === option.value
|
|
||||||
? 'border-primary bg-primary/5 ring-1 ring-primary/20'
|
|
||||||
: 'border-border hover:border-border/80 hover:bg-accent/50'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<div className={`shrink-0 w-10 h-10 rounded-lg flex items-center justify-center ${formData.visibility === option.value
|
|
||||||
? 'bg-primary text-primary-foreground'
|
|
||||||
: 'bg-muted text-muted-foreground'
|
|
||||||
}`}>
|
|
||||||
{option.icon}
|
|
||||||
</div>
|
|
||||||
<div className="flex-1 min-w-0">
|
|
||||||
<div className="font-medium">{option.label}</div>
|
|
||||||
<div className="text-sm text-muted-foreground">
|
|
||||||
{option.description}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className={`shrink-0 w-5 h-5 rounded-full border-2 flex items-center justify-center ${formData.visibility === option.value
|
|
||||||
? 'border-primary bg-primary'
|
|
||||||
: 'border-muted-foreground/30'
|
|
||||||
}`}>
|
|
||||||
{formData.visibility === option.value && (
|
|
||||||
<div className="w-2 h-2 rounded-full bg-primary-foreground" />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{error && (
|
|
||||||
<div className="p-4 rounded-lg bg-destructive/10 border border-destructive/20 text-destructive text-sm">
|
|
||||||
{error}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{success && (
|
|
||||||
<div className="p-4 rounded-lg bg-green-500/10 border border-green-500/20 text-green-500 text-sm flex items-center gap-2">
|
|
||||||
<Save className="h-4 w-4" />
|
|
||||||
{success}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<Button type="submit" disabled={isSaving || !formData.name.trim()} className="h-11">
|
|
||||||
{isSaving && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
|
||||||
Save Changes
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* Comment Tags */}
|
|
||||||
<Card id="comment-tags" className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader className="pb-3">
|
|
||||||
<CardTitle className="text-lg flex items-center gap-2">
|
|
||||||
<Tag className="h-5 w-5" />
|
|
||||||
Comment Tags
|
|
||||||
</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Customize tags for categorizing comments on videos
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
{/* Existing tags */}
|
|
||||||
<div className="space-y-2">
|
|
||||||
{tags.map((tag) => (
|
|
||||||
<div key={tag.id} className="flex flex-wrap items-center gap-2 p-2 rounded-lg border bg-card">
|
|
||||||
{editingTagId === tag.id ? (
|
|
||||||
<>
|
|
||||||
<input
|
|
||||||
type="color"
|
|
||||||
value={editTagColor}
|
|
||||||
onChange={(e) => setEditTagColor(e.target.value)}
|
|
||||||
className="w-8 h-8 rounded cursor-pointer border-0"
|
|
||||||
/>
|
|
||||||
<Input
|
|
||||||
value={editTagName}
|
|
||||||
onChange={(e) => setEditTagName(e.target.value)}
|
|
||||||
className="flex-1 h-8"
|
|
||||||
onKeyDown={(e) => e.key === 'Enter' && handleUpdateTag(tag.id)}
|
|
||||||
/>
|
|
||||||
<Button size="sm" variant="ghost" onClick={() => handleUpdateTag(tag.id)}>
|
|
||||||
<Save className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
<Button size="sm" variant="ghost" onClick={() => setEditingTagId(null)}>
|
|
||||||
<X className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
<div
|
|
||||||
className="w-6 h-6 rounded-full shrink-0"
|
|
||||||
style={{ backgroundColor: tag.color }}
|
|
||||||
/>
|
|
||||||
<span className="flex-1 text-sm font-medium">{tag.name}</span>
|
|
||||||
<Button
|
|
||||||
size="sm"
|
|
||||||
variant="ghost"
|
|
||||||
onClick={() => {
|
|
||||||
setEditingTagId(tag.id);
|
|
||||||
setEditTagName(tag.name);
|
|
||||||
setEditTagColor(tag.color);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Edit
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
size="sm"
|
|
||||||
variant="ghost"
|
|
||||||
className="text-destructive hover:text-destructive"
|
|
||||||
onClick={() => handleDeleteTag(tag.id)}
|
|
||||||
>
|
|
||||||
<Trash2 className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Add new tag */}
|
|
||||||
<div className="flex flex-wrap items-center gap-2 pt-2 border-t">
|
|
||||||
<input
|
|
||||||
type="color"
|
|
||||||
value={newTagColor}
|
|
||||||
onChange={(e) => setNewTagColor(e.target.value)}
|
|
||||||
className="w-8 h-8 rounded cursor-pointer border-0"
|
|
||||||
/>
|
|
||||||
<Input
|
|
||||||
placeholder="New tag name..."
|
|
||||||
value={newTagName}
|
|
||||||
onChange={(e) => setNewTagName(e.target.value)}
|
|
||||||
className="flex-1 h-8"
|
|
||||||
onKeyDown={(e) => e.key === 'Enter' && handleAddTag()}
|
|
||||||
/>
|
|
||||||
<Button size="sm" onClick={handleAddTag} disabled={!newTagName.trim() || isAddingTag}>
|
|
||||||
{isAddingTag ? <Loader2 className="h-4 w-4 animate-spin" /> : <Plus className="h-4 w-4" />}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* Danger Zone */}
|
|
||||||
<Card className="border-destructive/30 shadow-lg">
|
|
||||||
<CardHeader className="pb-3">
|
|
||||||
<CardTitle className="text-lg text-destructive flex items-center gap-2">
|
|
||||||
<AlertTriangle className="h-5 w-5" />
|
|
||||||
Danger Zone
|
|
||||||
</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Irreversible actions that will permanently affect your project
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<div className="flex items-center justify-between p-4 rounded-xl border border-destructive/20 bg-destructive/5">
|
|
||||||
<div>
|
|
||||||
<h4 className="font-medium">Delete this project</h4>
|
|
||||||
<p className="text-sm text-muted-foreground">
|
|
||||||
This action cannot be undone
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<AlertDialog>
|
|
||||||
<AlertDialogTrigger asChild>
|
|
||||||
<Button variant="destructive" size="sm">
|
|
||||||
<Trash2 className="h-4 w-4 mr-2" />
|
|
||||||
Delete
|
|
||||||
</Button>
|
|
||||||
</AlertDialogTrigger>
|
|
||||||
<AlertDialogContent>
|
|
||||||
<AlertDialogHeader>
|
|
||||||
<AlertDialogTitle>Delete "{formData.name}"?</AlertDialogTitle>
|
|
||||||
<AlertDialogDescription asChild>
|
|
||||||
<div className="space-y-4">
|
|
||||||
<p>
|
|
||||||
This will permanently delete this project and all of its
|
|
||||||
videos, versions, and comments. This action cannot be undone.
|
|
||||||
</p>
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="delete-confirm">
|
|
||||||
Type <strong className="text-foreground">{formData.name}</strong> to confirm
|
|
||||||
</Label>
|
|
||||||
<Input
|
|
||||||
id="delete-confirm"
|
|
||||||
value={deleteConfirmation}
|
|
||||||
onChange={(e) => setDeleteConfirmation(e.target.value)}
|
|
||||||
placeholder="Project name"
|
|
||||||
className="h-11"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</AlertDialogDescription>
|
|
||||||
</AlertDialogHeader>
|
|
||||||
<AlertDialogFooter>
|
|
||||||
<AlertDialogCancel onClick={() => setDeleteConfirmation('')}>
|
|
||||||
Cancel
|
|
||||||
</AlertDialogCancel>
|
|
||||||
<AlertDialogAction
|
|
||||||
onClick={handleDelete}
|
|
||||||
disabled={deleteConfirmation !== formData.name || isDeleting}
|
|
||||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
|
||||||
>
|
|
||||||
{isDeleting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
|
||||||
Delete Project
|
|
||||||
</AlertDialogAction>
|
|
||||||
</AlertDialogFooter>
|
|
||||||
</AlertDialogContent>
|
|
||||||
</AlertDialog>
|
|
||||||
</div>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,509 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { ArrowLeft, Loader2, Globe, Lock, UserPlus, Trash2, AlertTriangle, Settings, Save, Tag, Plus, X } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogAction,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogHeader,
|
||||||
|
AlertDialogTitle,
|
||||||
|
AlertDialogTrigger,
|
||||||
|
} from '@/components/ui/alert-dialog';
|
||||||
|
|
||||||
|
type Visibility = 'PRIVATE' | 'INVITE' | 'PUBLIC';
|
||||||
|
|
||||||
|
const visibilityOptions: { value: Visibility; label: string; description: string; icon: React.ReactNode }[] = [
|
||||||
|
{
|
||||||
|
value: 'PRIVATE',
|
||||||
|
label: 'Private',
|
||||||
|
description: 'Only you can access this project',
|
||||||
|
icon: <Lock className="h-5 w-5" />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'INVITE',
|
||||||
|
label: 'Invite Only',
|
||||||
|
description: 'Share with specific people via email',
|
||||||
|
icon: <UserPlus className="h-5 w-5" />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'PUBLIC',
|
||||||
|
label: 'Public',
|
||||||
|
description: 'Anyone with the link can view',
|
||||||
|
icon: <Globe className="h-5 w-5" />,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
interface ProjectSettingsPageProps {
|
||||||
|
projectId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CommentTag {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
color: string;
|
||||||
|
position: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function ProjectSettingsPageClient({ projectId }: ProjectSettingsPageProps) {
|
||||||
|
const router = useRouter();
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [isSaving, setIsSaving] = useState(false);
|
||||||
|
const [isDeleting, setIsDeleting] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [success, setSuccess] = useState('');
|
||||||
|
const [deleteConfirmation, setDeleteConfirmation] = useState('');
|
||||||
|
const [formData, setFormData] = useState({
|
||||||
|
name: '',
|
||||||
|
description: '',
|
||||||
|
visibility: 'PRIVATE' as Visibility,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Tag management state
|
||||||
|
const [tags, setTags] = useState<CommentTag[]>([]);
|
||||||
|
const [newTagName, setNewTagName] = useState('');
|
||||||
|
const [newTagColor, setNewTagColor] = useState('#3B82F6');
|
||||||
|
const [isAddingTag, setIsAddingTag] = useState(false);
|
||||||
|
const [editingTagId, setEditingTagId] = useState<string | null>(null);
|
||||||
|
const [editTagName, setEditTagName] = useState('');
|
||||||
|
const [editTagColor, setEditTagColor] = useState('');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Fetch project data
|
||||||
|
fetch(`/api/projects/${projectId}`)
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
if (data.error) {
|
||||||
|
setError(data.error);
|
||||||
|
} else {
|
||||||
|
const project = data.data;
|
||||||
|
setFormData({
|
||||||
|
name: project.name || '',
|
||||||
|
description: project.description || '',
|
||||||
|
visibility: project.visibility || 'PRIVATE',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => setError('Failed to load project'))
|
||||||
|
.finally(() => setIsLoading(false));
|
||||||
|
|
||||||
|
// Fetch tags
|
||||||
|
fetch(`/api/projects/${projectId}/tags`)
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
if (Array.isArray(data.data)) {
|
||||||
|
setTags(data.data);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => { /* Silent fail - tags are optional */ });
|
||||||
|
}, [projectId]);
|
||||||
|
|
||||||
|
const handleSave = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setIsSaving(true);
|
||||||
|
setError('');
|
||||||
|
setSuccess('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/projects/${projectId}`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(formData),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
setError(data.error || 'Failed to update project');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setSuccess('Project settings saved successfully');
|
||||||
|
setTimeout(() => setSuccess(''), 3000);
|
||||||
|
} catch {
|
||||||
|
setError('Something went wrong. Please try again.');
|
||||||
|
} finally {
|
||||||
|
setIsSaving(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAddTag = async () => {
|
||||||
|
if (!newTagName.trim()) return;
|
||||||
|
setIsAddingTag(true);
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/projects/${projectId}/tags`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ name: newTagName.trim(), color: newTagColor }),
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
const newTag = data.data;
|
||||||
|
setTags([...tags, newTag]);
|
||||||
|
setNewTagName('');
|
||||||
|
setNewTagColor('#3B82F6');
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Silent fail
|
||||||
|
} finally {
|
||||||
|
setIsAddingTag(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdateTag = async (tagId: string) => {
|
||||||
|
if (!editTagName.trim()) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/projects/${projectId}/tags/${tagId}`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ name: editTagName.trim(), color: editTagColor }),
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
const updated = data.data;
|
||||||
|
setTags(tags.map((t) => (t.id === tagId ? updated : t)));
|
||||||
|
setEditingTagId(null);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Silent fail
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteTag = async (tagId: string) => {
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/projects/${projectId}/tags/${tagId}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
setTags(tags.filter((t) => t.id !== tagId));
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Silent fail
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDelete = async () => {
|
||||||
|
if (deleteConfirmation !== formData.name) {
|
||||||
|
setError('Project name does not match');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsDeleting(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/projects/${projectId}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
setError(data.error || 'Failed to delete project');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
router.push('/dashboard');
|
||||||
|
} catch {
|
||||||
|
setError('Something went wrong. Please try again.');
|
||||||
|
} finally {
|
||||||
|
setIsDeleting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isLoading) {
|
||||||
|
return (
|
||||||
|
<div className="min-h-[calc(100vh-4rem)] flex items-center justify-center">
|
||||||
|
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
||||||
|
<div className="w-full max-w-xl">
|
||||||
|
<div className="mb-8">
|
||||||
|
<Link
|
||||||
|
href={`/projects/${projectId}`}
|
||||||
|
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
||||||
|
>
|
||||||
|
<ArrowLeft className="h-4 w-4 mr-1" />
|
||||||
|
Back to Project
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-6">
|
||||||
|
{/* General Settings */}
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader className="text-center pb-2">
|
||||||
|
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
||||||
|
<Settings className="h-7 w-7 text-primary" />
|
||||||
|
</div>
|
||||||
|
<CardTitle className="text-2xl">Project Settings</CardTitle>
|
||||||
|
<CardDescription className="text-base">
|
||||||
|
Update your project details and access settings
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="pt-6">
|
||||||
|
<form onSubmit={handleSave} className="space-y-6">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="name" className="text-sm font-medium">
|
||||||
|
Project Name
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="name"
|
||||||
|
value={formData.name}
|
||||||
|
onChange={(e) => setFormData(prev => ({ ...prev, name: e.target.value }))}
|
||||||
|
required
|
||||||
|
disabled={isSaving}
|
||||||
|
className="h-11"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="description" className="text-sm font-medium">
|
||||||
|
Description
|
||||||
|
</Label>
|
||||||
|
<Textarea
|
||||||
|
id="description"
|
||||||
|
value={formData.description}
|
||||||
|
onChange={(e) => setFormData(prev => ({ ...prev, description: e.target.value }))}
|
||||||
|
rows={3}
|
||||||
|
disabled={isSaving}
|
||||||
|
className="resize-none"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-3">
|
||||||
|
<Label className="text-sm font-medium">Who can access?</Label>
|
||||||
|
<div className="grid gap-3">
|
||||||
|
{visibilityOptions.map((option) => (
|
||||||
|
<button
|
||||||
|
key={option.value}
|
||||||
|
type="button"
|
||||||
|
onClick={() => setFormData(prev => ({ ...prev, visibility: option.value }))}
|
||||||
|
disabled={isSaving}
|
||||||
|
className={`w-full flex items-center gap-4 p-4 rounded-xl border-2 text-left transition-all ${formData.visibility === option.value
|
||||||
|
? 'border-primary bg-primary/5 ring-1 ring-primary/20'
|
||||||
|
: 'border-border hover:border-border/80 hover:bg-accent/50'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<div className={`shrink-0 w-10 h-10 rounded-lg flex items-center justify-center ${formData.visibility === option.value
|
||||||
|
? 'bg-primary text-primary-foreground'
|
||||||
|
: 'bg-muted text-muted-foreground'
|
||||||
|
}`}>
|
||||||
|
{option.icon}
|
||||||
|
</div>
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<div className="font-medium">{option.label}</div>
|
||||||
|
<div className="text-sm text-muted-foreground">
|
||||||
|
{option.description}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className={`shrink-0 w-5 h-5 rounded-full border-2 flex items-center justify-center ${formData.visibility === option.value
|
||||||
|
? 'border-primary bg-primary'
|
||||||
|
: 'border-muted-foreground/30'
|
||||||
|
}`}>
|
||||||
|
{formData.visibility === option.value && (
|
||||||
|
<div className="w-2 h-2 rounded-full bg-primary-foreground" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="p-4 rounded-lg bg-destructive/10 border border-destructive/20 text-destructive text-sm">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{success && (
|
||||||
|
<div className="p-4 rounded-lg bg-green-500/10 border border-green-500/20 text-green-500 text-sm flex items-center gap-2">
|
||||||
|
<Save className="h-4 w-4" />
|
||||||
|
{success}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Button type="submit" disabled={isSaving || !formData.name.trim()} className="h-11">
|
||||||
|
{isSaving && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
||||||
|
Save Changes
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Comment Tags */}
|
||||||
|
<Card id="comment-tags" className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader className="pb-3">
|
||||||
|
<CardTitle className="text-lg flex items-center gap-2">
|
||||||
|
<Tag className="h-5 w-5" />
|
||||||
|
Comment Tags
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Customize tags for categorizing comments on videos
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
{/* Existing tags */}
|
||||||
|
<div className="space-y-2">
|
||||||
|
{tags.map((tag) => (
|
||||||
|
<div key={tag.id} className="flex flex-wrap items-center gap-2 p-2 rounded-lg border bg-card">
|
||||||
|
{editingTagId === tag.id ? (
|
||||||
|
<>
|
||||||
|
<input
|
||||||
|
type="color"
|
||||||
|
value={editTagColor}
|
||||||
|
onChange={(e) => setEditTagColor(e.target.value)}
|
||||||
|
className="w-8 h-8 rounded cursor-pointer border-0"
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
value={editTagName}
|
||||||
|
onChange={(e) => setEditTagName(e.target.value)}
|
||||||
|
className="flex-1 h-8"
|
||||||
|
onKeyDown={(e) => e.key === 'Enter' && handleUpdateTag(tag.id)}
|
||||||
|
/>
|
||||||
|
<Button size="sm" variant="ghost" onClick={() => handleUpdateTag(tag.id)}>
|
||||||
|
<Save className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
<Button size="sm" variant="ghost" onClick={() => setEditingTagId(null)}>
|
||||||
|
<X className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div
|
||||||
|
className="w-6 h-6 rounded-full shrink-0"
|
||||||
|
style={{ backgroundColor: tag.color }}
|
||||||
|
/>
|
||||||
|
<span className="flex-1 text-sm font-medium">{tag.name}</span>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() => {
|
||||||
|
setEditingTagId(tag.id);
|
||||||
|
setEditTagName(tag.name);
|
||||||
|
setEditTagColor(tag.color);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Edit
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="ghost"
|
||||||
|
className="text-destructive hover:text-destructive"
|
||||||
|
onClick={() => handleDeleteTag(tag.id)}
|
||||||
|
>
|
||||||
|
<Trash2 className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Add new tag */}
|
||||||
|
<div className="flex flex-wrap items-center gap-2 pt-2 border-t">
|
||||||
|
<input
|
||||||
|
type="color"
|
||||||
|
value={newTagColor}
|
||||||
|
onChange={(e) => setNewTagColor(e.target.value)}
|
||||||
|
className="w-8 h-8 rounded cursor-pointer border-0"
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
placeholder="New tag name..."
|
||||||
|
value={newTagName}
|
||||||
|
onChange={(e) => setNewTagName(e.target.value)}
|
||||||
|
className="flex-1 h-8"
|
||||||
|
onKeyDown={(e) => e.key === 'Enter' && handleAddTag()}
|
||||||
|
/>
|
||||||
|
<Button size="sm" onClick={handleAddTag} disabled={!newTagName.trim() || isAddingTag}>
|
||||||
|
{isAddingTag ? <Loader2 className="h-4 w-4 animate-spin" /> : <Plus className="h-4 w-4" />}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Danger Zone */}
|
||||||
|
<Card className="border-destructive/30 shadow-lg">
|
||||||
|
<CardHeader className="pb-3">
|
||||||
|
<CardTitle className="text-lg text-destructive flex items-center gap-2">
|
||||||
|
<AlertTriangle className="h-5 w-5" />
|
||||||
|
Danger Zone
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Irreversible actions that will permanently affect your project
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="flex items-center justify-between p-4 rounded-xl border border-destructive/20 bg-destructive/5">
|
||||||
|
<div>
|
||||||
|
<h4 className="font-medium">Delete this project</h4>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
This action cannot be undone
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<AlertDialog>
|
||||||
|
<AlertDialogTrigger asChild>
|
||||||
|
<Button variant="destructive" size="sm">
|
||||||
|
<Trash2 className="h-4 w-4 mr-2" />
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
</AlertDialogTrigger>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>Delete "{formData.name}"?</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription asChild>
|
||||||
|
<div className="space-y-4">
|
||||||
|
<p>
|
||||||
|
This will permanently delete this project and all of its
|
||||||
|
videos, versions, and comments. This action cannot be undone.
|
||||||
|
</p>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="delete-confirm">
|
||||||
|
Type <strong className="text-foreground">{formData.name}</strong> to confirm
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="delete-confirm"
|
||||||
|
value={deleteConfirmation}
|
||||||
|
onChange={(e) => setDeleteConfirmation(e.target.value)}
|
||||||
|
placeholder="Project name"
|
||||||
|
className="h-11"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</AlertDialogDescription>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel onClick={() => setDeleteConfirmation('')}>
|
||||||
|
Cancel
|
||||||
|
</AlertDialogCancel>
|
||||||
|
<AlertDialogAction
|
||||||
|
onClick={handleDelete}
|
||||||
|
disabled={deleteConfirmation !== formData.name || isDeleting}
|
||||||
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||||
|
>
|
||||||
|
{isDeleting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
||||||
|
Delete Project
|
||||||
|
</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,337 +1,17 @@
|
|||||||
'use client';
|
import { requireProjectAccessOrRedirect } from '@/lib/route-access';
|
||||||
|
import ProjectSharePageClient from './project-share-page-client';
|
||||||
import { useState, useEffect } from 'react';
|
|
||||||
import Link from 'next/link';
|
|
||||||
import { ArrowLeft, Copy, Check, Loader2, UserPlus, Share2, Globe, Lock, Mail, X } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Badge } from '@/components/ui/badge';
|
|
||||||
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
|
|
||||||
|
|
||||||
interface ProjectMember {
|
|
||||||
id: string;
|
|
||||||
role: string;
|
|
||||||
user: {
|
|
||||||
id: string;
|
|
||||||
name: string | null;
|
|
||||||
email: string | null;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ProjectSharePageProps {
|
interface ProjectSharePageProps {
|
||||||
params: Promise<{ projectId: string }>;
|
params: Promise<{ projectId: string }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ProjectSharePage({ params }: ProjectSharePageProps) {
|
export default async function ProjectSharePage({ params }: ProjectSharePageProps) {
|
||||||
const [projectId, setProjectId] = useState<string>('');
|
const { projectId } = await params;
|
||||||
const [projectName, setProjectName] = useState('');
|
|
||||||
const [projectVisibility, setProjectVisibility] = useState('');
|
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
|
||||||
const [members, setMembers] = useState<ProjectMember[]>([]);
|
|
||||||
const [copied, setCopied] = useState(false);
|
|
||||||
const [error, setError] = useState('');
|
|
||||||
const [inviteEmail, setInviteEmail] = useState('');
|
|
||||||
const [isInviting, setIsInviting] = useState(false);
|
|
||||||
const [inviteSuccess, setInviteSuccess] = useState('');
|
|
||||||
|
|
||||||
useEffect(() => {
|
await requireProjectAccessOrRedirect({
|
||||||
params.then(({ projectId: id }) => {
|
projectId,
|
||||||
setProjectId(id);
|
intent: 'manage',
|
||||||
fetch(`/api/projects/${id}`)
|
});
|
||||||
.then((res) => res.json())
|
|
||||||
.then((data) => {
|
|
||||||
if (data.error) {
|
|
||||||
setError(data.error);
|
|
||||||
} else {
|
|
||||||
const project = data.data;
|
|
||||||
setProjectName(project.name || '');
|
|
||||||
setProjectVisibility(project.visibility || 'PRIVATE');
|
|
||||||
setMembers(project.members || []);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(() => setError('Failed to load project'))
|
|
||||||
.finally(() => setIsLoading(false));
|
|
||||||
});
|
|
||||||
}, [params]);
|
|
||||||
|
|
||||||
const copyToClipboard = async (text: string) => {
|
return <ProjectSharePageClient projectId={projectId} />;
|
||||||
await navigator.clipboard.writeText(text);
|
|
||||||
setCopied(true);
|
|
||||||
setTimeout(() => setCopied(false), 2000);
|
|
||||||
};
|
|
||||||
|
|
||||||
const getDirectLink = () => {
|
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
return `${window.location.origin}/projects/${projectId}`;
|
|
||||||
}
|
|
||||||
return `/projects/${projectId}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleInvite = async (e: React.FormEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
if (!inviteEmail.trim()) return;
|
|
||||||
|
|
||||||
setIsInviting(true);
|
|
||||||
setError('');
|
|
||||||
setInviteSuccess('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
// TODO: Implement invite API
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 500));
|
|
||||||
setInviteSuccess(`Invitation sent to ${inviteEmail}`);
|
|
||||||
setInviteEmail('');
|
|
||||||
setTimeout(() => setInviteSuccess(''), 3000);
|
|
||||||
} catch {
|
|
||||||
setError('Failed to send invitation');
|
|
||||||
} finally {
|
|
||||||
setIsInviting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const VisibilityIcon = () => {
|
|
||||||
switch (projectVisibility) {
|
|
||||||
case 'PUBLIC':
|
|
||||||
return <Globe className="h-5 w-5" />;
|
|
||||||
case 'INVITE':
|
|
||||||
return <UserPlus className="h-5 w-5" />;
|
|
||||||
default:
|
|
||||||
return <Lock className="h-5 w-5" />;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const getVisibilityColor = () => {
|
|
||||||
switch (projectVisibility) {
|
|
||||||
case 'PUBLIC':
|
|
||||||
return 'bg-green-500/10 text-green-500';
|
|
||||||
case 'INVITE':
|
|
||||||
return 'bg-blue-500/10 text-blue-500';
|
|
||||||
default:
|
|
||||||
return 'bg-orange-500/10 text-orange-500';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const getVisibilityLabel = () => {
|
|
||||||
switch (projectVisibility) {
|
|
||||||
case 'PUBLIC':
|
|
||||||
return { title: 'Public', description: 'Anyone with the link can view this project' };
|
|
||||||
case 'INVITE':
|
|
||||||
return { title: 'Invite Only', description: 'Only people you invite can access' };
|
|
||||||
default:
|
|
||||||
return { title: 'Private', description: 'Only you can access this project' };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isLoading) {
|
|
||||||
return (
|
|
||||||
<div className="min-h-[calc(100vh-4rem)] flex items-center justify-center">
|
|
||||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const visibilityInfo = getVisibilityLabel();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
|
||||||
<div className="w-full max-w-xl">
|
|
||||||
<div className="mb-8">
|
|
||||||
<Link
|
|
||||||
href={`/projects/${projectId}`}
|
|
||||||
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4 mr-1" />
|
|
||||||
Back to Project
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-6">
|
|
||||||
{/* Header Card */}
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader className="text-center pb-2">
|
|
||||||
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
|
||||||
<Share2 className="h-7 w-7 text-primary" />
|
|
||||||
</div>
|
|
||||||
<CardTitle className="text-2xl">Share Project</CardTitle>
|
|
||||||
<CardDescription className="text-base">
|
|
||||||
Share "{projectName}" with your team or clients
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="pt-4">
|
|
||||||
{/* Visibility Status */}
|
|
||||||
<div className={`flex items-center gap-3 p-4 rounded-xl ${getVisibilityColor()}`}>
|
|
||||||
<div className="w-10 h-10 rounded-lg bg-current/10 flex items-center justify-center">
|
|
||||||
<VisibilityIcon />
|
|
||||||
</div>
|
|
||||||
<div className="flex-1">
|
|
||||||
<div className="font-medium">{visibilityInfo.title}</div>
|
|
||||||
<div className="text-sm opacity-80">{visibilityInfo.description}</div>
|
|
||||||
</div>
|
|
||||||
<Link href={`/projects/${projectId}/settings`}>
|
|
||||||
<Button variant="ghost" size="sm" className="text-current hover:bg-current/10">
|
|
||||||
Change
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* Invite People - Only show for INVITE visibility */}
|
|
||||||
{projectVisibility === 'INVITE' && (
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader className="pb-3">
|
|
||||||
<CardTitle className="text-lg flex items-center gap-2">
|
|
||||||
<Mail className="h-5 w-5 text-primary" />
|
|
||||||
Invite People
|
|
||||||
</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Send email invitations to specific people
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
<form onSubmit={handleInvite} className="flex gap-2">
|
|
||||||
<Input
|
|
||||||
type="email"
|
|
||||||
value={inviteEmail}
|
|
||||||
onChange={(e) => setInviteEmail(e.target.value)}
|
|
||||||
placeholder="[email protected]"
|
|
||||||
className="h-11 flex-1"
|
|
||||||
disabled={isInviting}
|
|
||||||
/>
|
|
||||||
<Button type="submit" disabled={isInviting || !inviteEmail.trim()} className="h-11">
|
|
||||||
{isInviting ? (
|
|
||||||
<Loader2 className="h-4 w-4 animate-spin" />
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
<UserPlus className="h-4 w-4 mr-2" />
|
|
||||||
Invite
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
{inviteSuccess && (
|
|
||||||
<div className="p-3 rounded-lg bg-green-500/10 border border-green-500/20 text-green-500 text-sm">
|
|
||||||
{inviteSuccess}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Current Members */}
|
|
||||||
{members.length > 0 && (
|
|
||||||
<div className="space-y-2 pt-2">
|
|
||||||
<Label className="text-sm text-muted-foreground">Project Members</Label>
|
|
||||||
<div className="space-y-2">
|
|
||||||
{members.map((member) => (
|
|
||||||
<div
|
|
||||||
key={member.id}
|
|
||||||
className="flex items-center justify-between p-3 rounded-xl border bg-card"
|
|
||||||
>
|
|
||||||
<div className="flex items-center gap-3">
|
|
||||||
<Avatar className="h-9 w-9">
|
|
||||||
<AvatarFallback className="text-xs">
|
|
||||||
{member.user.name?.charAt(0) || member.user.email?.charAt(0) || '?'}
|
|
||||||
</AvatarFallback>
|
|
||||||
</Avatar>
|
|
||||||
<div>
|
|
||||||
<div className="font-medium text-sm">
|
|
||||||
{member.user.name || 'Unknown'}
|
|
||||||
</div>
|
|
||||||
<div className="text-xs text-muted-foreground">
|
|
||||||
{member.user.email}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<Badge variant="secondary" className="text-xs capitalize">
|
|
||||||
{member.role.toLowerCase()}
|
|
||||||
</Badge>
|
|
||||||
<Button variant="ghost" size="icon" className="h-8 w-8 text-muted-foreground hover:text-destructive">
|
|
||||||
<X className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{members.length === 0 && (
|
|
||||||
<div className="text-center py-6 text-muted-foreground">
|
|
||||||
<UserPlus className="h-8 w-8 mx-auto mb-2 opacity-50" />
|
|
||||||
<p className="text-sm">No members yet</p>
|
|
||||||
<p className="text-xs opacity-70">Invite people to collaborate on this project</p>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Public Link - Only show for PUBLIC visibility */}
|
|
||||||
{projectVisibility === 'PUBLIC' && (
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader className="pb-3">
|
|
||||||
<CardTitle className="text-lg flex items-center gap-2">
|
|
||||||
<Globe className="h-5 w-5 text-primary" />
|
|
||||||
Public Link
|
|
||||||
</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Share this link with anyone
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<div className="flex gap-2">
|
|
||||||
<Input
|
|
||||||
value={getDirectLink()}
|
|
||||||
readOnly
|
|
||||||
className="font-mono text-sm h-11 bg-muted/50"
|
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
variant={copied ? 'default' : 'outline'}
|
|
||||||
size="icon"
|
|
||||||
className="h-11 w-11 shrink-0"
|
|
||||||
onClick={() => copyToClipboard(getDirectLink())}
|
|
||||||
>
|
|
||||||
{copied ? (
|
|
||||||
<Check className="h-4 w-4" />
|
|
||||||
) : (
|
|
||||||
<Copy className="h-4 w-4" />
|
|
||||||
)}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Private notice */}
|
|
||||||
{projectVisibility === 'PRIVATE' && (
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardContent className="py-8">
|
|
||||||
<div className="text-center">
|
|
||||||
<div className="w-16 h-16 rounded-full bg-muted/50 flex items-center justify-center mx-auto mb-4">
|
|
||||||
<Lock className="h-8 w-8 text-muted-foreground/50" />
|
|
||||||
</div>
|
|
||||||
<h3 className="font-medium mb-1">This project is private</h3>
|
|
||||||
<p className="text-sm text-muted-foreground mb-4">
|
|
||||||
Only you can access this project. Change visibility to share with others.
|
|
||||||
</p>
|
|
||||||
<Button asChild variant="outline">
|
|
||||||
<Link href={`/projects/${projectId}/settings`}>
|
|
||||||
Change Visibility
|
|
||||||
</Link>
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{error && (
|
|
||||||
<div className="p-4 rounded-lg bg-destructive/10 border border-destructive/20 text-destructive text-sm">
|
|
||||||
{error}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,333 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { ArrowLeft, Copy, Check, Loader2, UserPlus, Share2, Globe, Lock, Mail, X } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
|
||||||
|
|
||||||
|
interface ProjectMember {
|
||||||
|
id: string;
|
||||||
|
role: string;
|
||||||
|
user: {
|
||||||
|
id: string;
|
||||||
|
name: string | null;
|
||||||
|
email: string | null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ProjectSharePageProps {
|
||||||
|
projectId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function ProjectSharePageClient({ projectId }: ProjectSharePageProps) {
|
||||||
|
const [projectName, setProjectName] = useState('');
|
||||||
|
const [projectVisibility, setProjectVisibility] = useState('');
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [members, setMembers] = useState<ProjectMember[]>([]);
|
||||||
|
const [copied, setCopied] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [inviteEmail, setInviteEmail] = useState('');
|
||||||
|
const [isInviting, setIsInviting] = useState(false);
|
||||||
|
const [inviteSuccess, setInviteSuccess] = useState('');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetch(`/api/projects/${projectId}`)
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
if (data.error) {
|
||||||
|
setError(data.error);
|
||||||
|
} else {
|
||||||
|
const project = data.data;
|
||||||
|
setProjectName(project.name || '');
|
||||||
|
setProjectVisibility(project.visibility || 'PRIVATE');
|
||||||
|
setMembers(project.members || []);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => setError('Failed to load project'))
|
||||||
|
.finally(() => setIsLoading(false));
|
||||||
|
}, [projectId]);
|
||||||
|
|
||||||
|
const copyToClipboard = async (text: string) => {
|
||||||
|
await navigator.clipboard.writeText(text);
|
||||||
|
setCopied(true);
|
||||||
|
setTimeout(() => setCopied(false), 2000);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDirectLink = () => {
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
return `${window.location.origin}/projects/${projectId}`;
|
||||||
|
}
|
||||||
|
return `/projects/${projectId}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleInvite = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (!inviteEmail.trim()) return;
|
||||||
|
|
||||||
|
setIsInviting(true);
|
||||||
|
setError('');
|
||||||
|
setInviteSuccess('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// TODO: Implement invite API
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
setInviteSuccess(`Invitation sent to ${inviteEmail}`);
|
||||||
|
setInviteEmail('');
|
||||||
|
setTimeout(() => setInviteSuccess(''), 3000);
|
||||||
|
} catch {
|
||||||
|
setError('Failed to send invitation');
|
||||||
|
} finally {
|
||||||
|
setIsInviting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const VisibilityIcon = () => {
|
||||||
|
switch (projectVisibility) {
|
||||||
|
case 'PUBLIC':
|
||||||
|
return <Globe className="h-5 w-5" />;
|
||||||
|
case 'INVITE':
|
||||||
|
return <UserPlus className="h-5 w-5" />;
|
||||||
|
default:
|
||||||
|
return <Lock className="h-5 w-5" />;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getVisibilityColor = () => {
|
||||||
|
switch (projectVisibility) {
|
||||||
|
case 'PUBLIC':
|
||||||
|
return 'bg-green-500/10 text-green-500';
|
||||||
|
case 'INVITE':
|
||||||
|
return 'bg-blue-500/10 text-blue-500';
|
||||||
|
default:
|
||||||
|
return 'bg-orange-500/10 text-orange-500';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getVisibilityLabel = () => {
|
||||||
|
switch (projectVisibility) {
|
||||||
|
case 'PUBLIC':
|
||||||
|
return { title: 'Public', description: 'Anyone with the link can view this project' };
|
||||||
|
case 'INVITE':
|
||||||
|
return { title: 'Invite Only', description: 'Only people you invite can access' };
|
||||||
|
default:
|
||||||
|
return { title: 'Private', description: 'Only you can access this project' };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isLoading) {
|
||||||
|
return (
|
||||||
|
<div className="min-h-[calc(100vh-4rem)] flex items-center justify-center">
|
||||||
|
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const visibilityInfo = getVisibilityLabel();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
||||||
|
<div className="w-full max-w-xl">
|
||||||
|
<div className="mb-8">
|
||||||
|
<Link
|
||||||
|
href={`/projects/${projectId}`}
|
||||||
|
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
||||||
|
>
|
||||||
|
<ArrowLeft className="h-4 w-4 mr-1" />
|
||||||
|
Back to Project
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-6">
|
||||||
|
{/* Header Card */}
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader className="text-center pb-2">
|
||||||
|
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
||||||
|
<Share2 className="h-7 w-7 text-primary" />
|
||||||
|
</div>
|
||||||
|
<CardTitle className="text-2xl">Share Project</CardTitle>
|
||||||
|
<CardDescription className="text-base">
|
||||||
|
Share "{projectName}" with your team or clients
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="pt-4">
|
||||||
|
{/* Visibility Status */}
|
||||||
|
<div className={`flex items-center gap-3 p-4 rounded-xl ${getVisibilityColor()}`}>
|
||||||
|
<div className="w-10 h-10 rounded-lg bg-current/10 flex items-center justify-center">
|
||||||
|
<VisibilityIcon />
|
||||||
|
</div>
|
||||||
|
<div className="flex-1">
|
||||||
|
<div className="font-medium">{visibilityInfo.title}</div>
|
||||||
|
<div className="text-sm opacity-80">{visibilityInfo.description}</div>
|
||||||
|
</div>
|
||||||
|
<Link href={`/projects/${projectId}/settings`}>
|
||||||
|
<Button variant="ghost" size="sm" className="text-current hover:bg-current/10">
|
||||||
|
Change
|
||||||
|
</Button>
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Invite People - Only show for INVITE visibility */}
|
||||||
|
{projectVisibility === 'INVITE' && (
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader className="pb-3">
|
||||||
|
<CardTitle className="text-lg flex items-center gap-2">
|
||||||
|
<Mail className="h-5 w-5 text-primary" />
|
||||||
|
Invite People
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Send email invitations to specific people
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
<form onSubmit={handleInvite} className="flex gap-2">
|
||||||
|
<Input
|
||||||
|
type="email"
|
||||||
|
value={inviteEmail}
|
||||||
|
onChange={(e) => setInviteEmail(e.target.value)}
|
||||||
|
placeholder="[email protected]"
|
||||||
|
className="h-11 flex-1"
|
||||||
|
disabled={isInviting}
|
||||||
|
/>
|
||||||
|
<Button type="submit" disabled={isInviting || !inviteEmail.trim()} className="h-11">
|
||||||
|
{isInviting ? (
|
||||||
|
<Loader2 className="h-4 w-4 animate-spin" />
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<UserPlus className="h-4 w-4 mr-2" />
|
||||||
|
Invite
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
{inviteSuccess && (
|
||||||
|
<div className="p-3 rounded-lg bg-green-500/10 border border-green-500/20 text-green-500 text-sm">
|
||||||
|
{inviteSuccess}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Current Members */}
|
||||||
|
{members.length > 0 && (
|
||||||
|
<div className="space-y-2 pt-2">
|
||||||
|
<Label className="text-sm text-muted-foreground">Project Members</Label>
|
||||||
|
<div className="space-y-2">
|
||||||
|
{members.map((member) => (
|
||||||
|
<div
|
||||||
|
key={member.id}
|
||||||
|
className="flex items-center justify-between p-3 rounded-xl border bg-card"
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<Avatar className="h-9 w-9">
|
||||||
|
<AvatarFallback className="text-xs">
|
||||||
|
{member.user.name?.charAt(0) || member.user.email?.charAt(0) || '?'}
|
||||||
|
</AvatarFallback>
|
||||||
|
</Avatar>
|
||||||
|
<div>
|
||||||
|
<div className="font-medium text-sm">
|
||||||
|
{member.user.name || 'Unknown'}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-muted-foreground">
|
||||||
|
{member.user.email}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Badge variant="secondary" className="text-xs capitalize">
|
||||||
|
{member.role.toLowerCase()}
|
||||||
|
</Badge>
|
||||||
|
<Button variant="ghost" size="icon" className="h-8 w-8 text-muted-foreground hover:text-destructive">
|
||||||
|
<X className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{members.length === 0 && (
|
||||||
|
<div className="text-center py-6 text-muted-foreground">
|
||||||
|
<UserPlus className="h-8 w-8 mx-auto mb-2 opacity-50" />
|
||||||
|
<p className="text-sm">No members yet</p>
|
||||||
|
<p className="text-xs opacity-70">Invite people to collaborate on this project</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Public Link - Only show for PUBLIC visibility */}
|
||||||
|
{projectVisibility === 'PUBLIC' && (
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader className="pb-3">
|
||||||
|
<CardTitle className="text-lg flex items-center gap-2">
|
||||||
|
<Globe className="h-5 w-5 text-primary" />
|
||||||
|
Public Link
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Share this link with anyone
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Input
|
||||||
|
value={getDirectLink()}
|
||||||
|
readOnly
|
||||||
|
className="font-mono text-sm h-11 bg-muted/50"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant={copied ? 'default' : 'outline'}
|
||||||
|
size="icon"
|
||||||
|
className="h-11 w-11 shrink-0"
|
||||||
|
onClick={() => copyToClipboard(getDirectLink())}
|
||||||
|
>
|
||||||
|
{copied ? (
|
||||||
|
<Check className="h-4 w-4" />
|
||||||
|
) : (
|
||||||
|
<Copy className="h-4 w-4" />
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Private notice */}
|
||||||
|
{projectVisibility === 'PRIVATE' && (
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardContent className="py-8">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="w-16 h-16 rounded-full bg-muted/50 flex items-center justify-center mx-auto mb-4">
|
||||||
|
<Lock className="h-8 w-8 text-muted-foreground/50" />
|
||||||
|
</div>
|
||||||
|
<h3 className="font-medium mb-1">This project is private</h3>
|
||||||
|
<p className="text-sm text-muted-foreground mb-4">
|
||||||
|
Only you can access this project. Change visibility to share with others.
|
||||||
|
</p>
|
||||||
|
<Button asChild variant="outline">
|
||||||
|
<Link href={`/projects/${projectId}/settings`}>
|
||||||
|
Change Visibility
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="p-4 rounded-lg bg-destructive/10 border border-destructive/20 text-destructive text-sm">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
+1081
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,18 +1,22 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useParams } from 'next/navigation';
|
|
||||||
import { VideoPageContent } from '@/components/video-page-content';
|
import { VideoPageContent } from '@/components/video-page-content';
|
||||||
|
import { auth } from '@/lib/auth';
|
||||||
|
import { requireVideoProjectAccessOrRedirect } from '@/lib/route-access';
|
||||||
|
|
||||||
export default function VideoPage() {
|
interface VideoPageProps {
|
||||||
const params = useParams();
|
params: Promise<{ projectId: string; videoId: string }>;
|
||||||
const projectId = params.projectId as string;
|
}
|
||||||
const videoId = params.videoId as string;
|
|
||||||
|
|
||||||
return (
|
export default async function VideoPage({ params }: VideoPageProps) {
|
||||||
<VideoPageContent
|
const { projectId, videoId } = await params;
|
||||||
mode="dashboard"
|
const session = await auth();
|
||||||
videoId={videoId}
|
|
||||||
projectId={projectId}
|
await requireVideoProjectAccessOrRedirect({
|
||||||
/>
|
projectId,
|
||||||
);
|
videoId,
|
||||||
|
userId: session?.user?.id,
|
||||||
|
intent: 'view',
|
||||||
|
allowPublicView: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
return <VideoPageContent mode="dashboard" videoId={videoId} projectId={projectId} />;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,332 +1,18 @@
|
|||||||
'use client';
|
import { requireVideoProjectAccessOrRedirect } from '@/lib/route-access';
|
||||||
|
import VideoSharePageClient from './video-share-page-client';
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
import Link from 'next/link';
|
|
||||||
import { ArrowLeft, Check, Copy, Link2, Loader2, RefreshCcw, ShieldOff, Lock, ShieldCheck } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
|
|
||||||
type RouteParams = Promise<{ projectId: string; videoId: string }>;
|
|
||||||
|
|
||||||
interface VideoSharePageProps {
|
interface VideoSharePageProps {
|
||||||
params: RouteParams;
|
params: Promise<{ projectId: string; videoId: string }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ShareLinkData {
|
export default async function VideoSharePage({ params }: VideoSharePageProps) {
|
||||||
id: string;
|
const { projectId, videoId } = await params;
|
||||||
token: string;
|
|
||||||
allowGuests: boolean;
|
|
||||||
allowDownloads: boolean;
|
|
||||||
hasPassword: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ShareResponse {
|
await requireVideoProjectAccessOrRedirect({
|
||||||
data: {
|
projectId,
|
||||||
link: ShareLinkData | null;
|
videoId,
|
||||||
shareUrl: string | null;
|
intent: 'manage',
|
||||||
};
|
});
|
||||||
error?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function VideoSharePage({ params }: VideoSharePageProps) {
|
return <VideoSharePageClient projectId={projectId} videoId={videoId} />;
|
||||||
const [projectId, setProjectId] = useState('');
|
|
||||||
const [videoId, setVideoId] = useState('');
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [submitting, setSubmitting] = useState(false);
|
|
||||||
const [copied, setCopied] = useState(false);
|
|
||||||
const [error, setError] = useState('');
|
|
||||||
const [shareUrl, setShareUrl] = useState<string | null>(null);
|
|
||||||
const [hasPassword, setHasPassword] = useState(false);
|
|
||||||
const [password, setPassword] = useState('');
|
|
||||||
const [allowDownloads, setAllowDownloads] = useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
params.then(({ projectId: nextProjectId, videoId: nextVideoId }) => {
|
|
||||||
setProjectId(nextProjectId);
|
|
||||||
setVideoId(nextVideoId);
|
|
||||||
});
|
|
||||||
}, [params]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!projectId || !videoId) return;
|
|
||||||
|
|
||||||
async function loadShareLink() {
|
|
||||||
setLoading(true);
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, { cache: 'no-store' });
|
|
||||||
const payload = (await response.json()) as ShareResponse;
|
|
||||||
|
|
||||||
if (!response.ok || payload.error) {
|
|
||||||
setError(payload.error || 'Failed to load share link');
|
|
||||||
setShareUrl(null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setShareUrl(payload.data.shareUrl);
|
|
||||||
setHasPassword(!!payload.data.link?.hasPassword);
|
|
||||||
setAllowDownloads(!!payload.data.link?.allowDownloads);
|
|
||||||
} catch {
|
|
||||||
setError('Failed to load share link');
|
|
||||||
setShareUrl(null);
|
|
||||||
setHasPassword(false);
|
|
||||||
setAllowDownloads(false);
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loadShareLink();
|
|
||||||
}, [projectId, videoId]);
|
|
||||||
|
|
||||||
const copyLink = async () => {
|
|
||||||
if (!shareUrl) return;
|
|
||||||
await navigator.clipboard.writeText(shareUrl);
|
|
||||||
setCopied(true);
|
|
||||||
setTimeout(() => setCopied(false), 2000);
|
|
||||||
};
|
|
||||||
|
|
||||||
const createShareLink = async () => {
|
|
||||||
if (!projectId || !videoId) return;
|
|
||||||
|
|
||||||
setSubmitting(true);
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ allowGuests: true, allowDownloads }),
|
|
||||||
});
|
|
||||||
|
|
||||||
const payload = (await response.json()) as ShareResponse;
|
|
||||||
if (!response.ok || payload.error) {
|
|
||||||
setError(payload.error || 'Failed to create share link');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setShareUrl(payload.data.shareUrl);
|
|
||||||
setHasPassword(!!payload.data.link?.hasPassword);
|
|
||||||
setAllowDownloads(!!payload.data.link?.allowDownloads);
|
|
||||||
setPassword('');
|
|
||||||
} catch {
|
|
||||||
setError('Failed to create share link');
|
|
||||||
} finally {
|
|
||||||
setSubmitting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const revokeShareLink = async () => {
|
|
||||||
if (!projectId || !videoId) return;
|
|
||||||
|
|
||||||
setSubmitting(true);
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const payload = (await response.json().catch(() => null)) as { error?: string } | null;
|
|
||||||
setError(payload?.error || 'Failed to revoke share link');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setShareUrl(null);
|
|
||||||
setHasPassword(false);
|
|
||||||
setAllowDownloads(false);
|
|
||||||
setPassword('');
|
|
||||||
} catch {
|
|
||||||
setError('Failed to revoke share link');
|
|
||||||
} finally {
|
|
||||||
setSubmitting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateSecuritySettings = async (clearPassword = false) => {
|
|
||||||
if (!projectId || !videoId || !shareUrl) return;
|
|
||||||
|
|
||||||
setSubmitting(true);
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
...(clearPassword ? { clearPassword: true } : {}),
|
|
||||||
...(!clearPassword ? { password } : {}),
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const payload = (await response.json().catch(() => null)) as ShareResponse | { error?: string } | null;
|
|
||||||
if (!response.ok || ('error' in (payload || {}) && payload?.error)) {
|
|
||||||
setError((payload as { error?: string } | null)?.error || 'Failed to update link security');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = (payload as ShareResponse).data;
|
|
||||||
setShareUrl(data.shareUrl);
|
|
||||||
setHasPassword(!!data.link?.hasPassword);
|
|
||||||
setAllowDownloads(!!data.link?.allowDownloads);
|
|
||||||
setPassword('');
|
|
||||||
} catch {
|
|
||||||
setError('Failed to update link security');
|
|
||||||
} finally {
|
|
||||||
setSubmitting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateDownloadSetting = async (nextAllowDownloads: boolean) => {
|
|
||||||
if (!projectId || !videoId || !shareUrl) return;
|
|
||||||
|
|
||||||
setSubmitting(true);
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ allowDownloads: nextAllowDownloads }),
|
|
||||||
});
|
|
||||||
const payload = (await response.json().catch(() => null)) as ShareResponse | { error?: string } | null;
|
|
||||||
if (!response.ok || ('error' in (payload || {}) && payload?.error)) {
|
|
||||||
setError((payload as { error?: string } | null)?.error || 'Failed to update download setting');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const data = (payload as ShareResponse).data;
|
|
||||||
setShareUrl(data.shareUrl);
|
|
||||||
setAllowDownloads(!!data.link?.allowDownloads);
|
|
||||||
setHasPassword(!!data.link?.hasPassword);
|
|
||||||
} catch {
|
|
||||||
setError('Failed to update download setting');
|
|
||||||
} finally {
|
|
||||||
setSubmitting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
|
||||||
<div className="w-full max-w-xl space-y-6">
|
|
||||||
<Link
|
|
||||||
href={`/projects/${projectId}/videos/${videoId}`}
|
|
||||||
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4 mr-1" />
|
|
||||||
Back to Video
|
|
||||||
</Link>
|
|
||||||
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle className="text-2xl">Share Video For Review</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Create a private link so reviewers can watch and comment on this single video.
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
{loading ? (
|
|
||||||
<div className="flex items-center text-sm text-muted-foreground">
|
|
||||||
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
|
||||||
Loading link settings...
|
|
||||||
</div>
|
|
||||||
) : shareUrl ? (
|
|
||||||
<div className="space-y-3">
|
|
||||||
<div className="flex gap-2">
|
|
||||||
<Input value={shareUrl} readOnly className="font-mono text-sm h-11 bg-muted/50" />
|
|
||||||
<Button
|
|
||||||
variant={copied ? 'default' : 'outline'}
|
|
||||||
size="icon"
|
|
||||||
className="h-11 w-11 shrink-0"
|
|
||||||
onClick={copyLink}
|
|
||||||
>
|
|
||||||
{copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
<div className="flex gap-2">
|
|
||||||
<Button onClick={createShareLink} disabled={submitting} variant="outline">
|
|
||||||
{submitting ? <Loader2 className="h-4 w-4 mr-2 animate-spin" /> : <RefreshCcw className="h-4 w-4 mr-2" />}
|
|
||||||
Regenerate Link
|
|
||||||
</Button>
|
|
||||||
<Button onClick={revokeShareLink} disabled={submitting} variant="destructive">
|
|
||||||
{submitting ? <Loader2 className="h-4 w-4 mr-2 animate-spin" /> : <ShieldOff className="h-4 w-4 mr-2" />}
|
|
||||||
Revoke Link
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
<div className="rounded-lg border p-3 space-y-2">
|
|
||||||
<div>
|
|
||||||
<p className="text-sm font-medium">Video download</p>
|
|
||||||
<p className="text-xs text-muted-foreground">Allow viewers with this link to download</p>
|
|
||||||
</div>
|
|
||||||
<div className="flex gap-2">
|
|
||||||
<Button
|
|
||||||
variant={allowDownloads ? 'default' : 'outline'}
|
|
||||||
disabled={submitting || allowDownloads}
|
|
||||||
onClick={() => updateDownloadSetting(true)}
|
|
||||||
>
|
|
||||||
Allow Download
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
variant={!allowDownloads ? 'default' : 'outline'}
|
|
||||||
disabled={submitting || !allowDownloads}
|
|
||||||
onClick={() => updateDownloadSetting(false)}
|
|
||||||
>
|
|
||||||
Block Download
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="rounded-lg border p-3 space-y-2">
|
|
||||||
<div className="flex items-center gap-2 text-sm font-medium">
|
|
||||||
{hasPassword ? <ShieldCheck className="h-4 w-4 text-green-600" /> : <Lock className="h-4 w-4" />}
|
|
||||||
Link password
|
|
||||||
</div>
|
|
||||||
<div className="flex gap-2">
|
|
||||||
<Input
|
|
||||||
type="password"
|
|
||||||
placeholder={hasPassword ? 'Enter new password to replace current one' : 'Set a password'}
|
|
||||||
value={password}
|
|
||||||
onChange={(e) => setPassword(e.target.value)}
|
|
||||||
disabled={submitting}
|
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
onClick={() => updateSecuritySettings(false)}
|
|
||||||
disabled={submitting || !password.trim()}
|
|
||||||
variant="outline"
|
|
||||||
>
|
|
||||||
Save
|
|
||||||
</Button>
|
|
||||||
{hasPassword && (
|
|
||||||
<Button
|
|
||||||
onClick={() => updateSecuritySettings(true)}
|
|
||||||
disabled={submitting}
|
|
||||||
variant="outline"
|
|
||||||
>
|
|
||||||
Remove
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<Button onClick={createShareLink} disabled={submitting}>
|
|
||||||
{submitting ? <Loader2 className="h-4 w-4 mr-2 animate-spin" /> : <Link2 className="h-4 w-4 mr-2" />}
|
|
||||||
Create Review Link
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<p className="text-xs text-muted-foreground">
|
|
||||||
This link allows guests to leave comments without an account. You can optionally protect it with a password.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
{error && (
|
|
||||||
<p className="text-sm text-destructive">{error}</p>
|
|
||||||
)}
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
+322
@@ -0,0 +1,322 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { ArrowLeft, Check, Copy, Link2, Loader2, RefreshCcw, ShieldOff, Lock, ShieldCheck } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
|
||||||
|
interface VideoSharePageProps {
|
||||||
|
projectId: string;
|
||||||
|
videoId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ShareLinkData {
|
||||||
|
id: string;
|
||||||
|
token: string;
|
||||||
|
allowGuests: boolean;
|
||||||
|
allowDownloads: boolean;
|
||||||
|
hasPassword: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ShareResponse {
|
||||||
|
data: {
|
||||||
|
link: ShareLinkData | null;
|
||||||
|
shareUrl: string | null;
|
||||||
|
};
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function VideoSharePageClient({ projectId, videoId }: VideoSharePageProps) {
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [submitting, setSubmitting] = useState(false);
|
||||||
|
const [copied, setCopied] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [shareUrl, setShareUrl] = useState<string | null>(null);
|
||||||
|
const [hasPassword, setHasPassword] = useState(false);
|
||||||
|
const [password, setPassword] = useState('');
|
||||||
|
const [allowDownloads, setAllowDownloads] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!projectId || !videoId) return;
|
||||||
|
|
||||||
|
async function loadShareLink() {
|
||||||
|
setLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, { cache: 'no-store' });
|
||||||
|
const payload = (await response.json()) as ShareResponse;
|
||||||
|
|
||||||
|
if (!response.ok || payload.error) {
|
||||||
|
setError(payload.error || 'Failed to load share link');
|
||||||
|
setShareUrl(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setShareUrl(payload.data.shareUrl);
|
||||||
|
setHasPassword(!!payload.data.link?.hasPassword);
|
||||||
|
setAllowDownloads(!!payload.data.link?.allowDownloads);
|
||||||
|
} catch {
|
||||||
|
setError('Failed to load share link');
|
||||||
|
setShareUrl(null);
|
||||||
|
setHasPassword(false);
|
||||||
|
setAllowDownloads(false);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadShareLink();
|
||||||
|
}, [projectId, videoId]);
|
||||||
|
|
||||||
|
const copyLink = async () => {
|
||||||
|
if (!shareUrl) return;
|
||||||
|
await navigator.clipboard.writeText(shareUrl);
|
||||||
|
setCopied(true);
|
||||||
|
setTimeout(() => setCopied(false), 2000);
|
||||||
|
};
|
||||||
|
|
||||||
|
const createShareLink = async () => {
|
||||||
|
if (!projectId || !videoId) return;
|
||||||
|
|
||||||
|
setSubmitting(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ allowGuests: true, allowDownloads }),
|
||||||
|
});
|
||||||
|
|
||||||
|
const payload = (await response.json()) as ShareResponse;
|
||||||
|
if (!response.ok || payload.error) {
|
||||||
|
setError(payload.error || 'Failed to create share link');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setShareUrl(payload.data.shareUrl);
|
||||||
|
setHasPassword(!!payload.data.link?.hasPassword);
|
||||||
|
setAllowDownloads(!!payload.data.link?.allowDownloads);
|
||||||
|
setPassword('');
|
||||||
|
} catch {
|
||||||
|
setError('Failed to create share link');
|
||||||
|
} finally {
|
||||||
|
setSubmitting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const revokeShareLink = async () => {
|
||||||
|
if (!projectId || !videoId) return;
|
||||||
|
|
||||||
|
setSubmitting(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const payload = (await response.json().catch(() => null)) as { error?: string } | null;
|
||||||
|
setError(payload?.error || 'Failed to revoke share link');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setShareUrl(null);
|
||||||
|
setHasPassword(false);
|
||||||
|
setAllowDownloads(false);
|
||||||
|
setPassword('');
|
||||||
|
} catch {
|
||||||
|
setError('Failed to revoke share link');
|
||||||
|
} finally {
|
||||||
|
setSubmitting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateSecuritySettings = async (clearPassword = false) => {
|
||||||
|
if (!projectId || !videoId || !shareUrl) return;
|
||||||
|
|
||||||
|
setSubmitting(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
...(clearPassword ? { clearPassword: true } : {}),
|
||||||
|
...(!clearPassword ? { password } : {}),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const payload = (await response.json().catch(() => null)) as ShareResponse | { error?: string } | null;
|
||||||
|
if (!response.ok || ('error' in (payload || {}) && payload?.error)) {
|
||||||
|
setError((payload as { error?: string } | null)?.error || 'Failed to update link security');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = (payload as ShareResponse).data;
|
||||||
|
setShareUrl(data.shareUrl);
|
||||||
|
setHasPassword(!!data.link?.hasPassword);
|
||||||
|
setAllowDownloads(!!data.link?.allowDownloads);
|
||||||
|
setPassword('');
|
||||||
|
} catch {
|
||||||
|
setError('Failed to update link security');
|
||||||
|
} finally {
|
||||||
|
setSubmitting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateDownloadSetting = async (nextAllowDownloads: boolean) => {
|
||||||
|
if (!projectId || !videoId || !shareUrl) return;
|
||||||
|
|
||||||
|
setSubmitting(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/projects/${projectId}/videos/${videoId}/share`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ allowDownloads: nextAllowDownloads }),
|
||||||
|
});
|
||||||
|
const payload = (await response.json().catch(() => null)) as ShareResponse | { error?: string } | null;
|
||||||
|
if (!response.ok || ('error' in (payload || {}) && payload?.error)) {
|
||||||
|
setError((payload as { error?: string } | null)?.error || 'Failed to update download setting');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = (payload as ShareResponse).data;
|
||||||
|
setShareUrl(data.shareUrl);
|
||||||
|
setAllowDownloads(!!data.link?.allowDownloads);
|
||||||
|
setHasPassword(!!data.link?.hasPassword);
|
||||||
|
} catch {
|
||||||
|
setError('Failed to update download setting');
|
||||||
|
} finally {
|
||||||
|
setSubmitting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
||||||
|
<div className="w-full max-w-xl space-y-6">
|
||||||
|
<Link
|
||||||
|
href={`/projects/${projectId}/videos/${videoId}`}
|
||||||
|
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
||||||
|
>
|
||||||
|
<ArrowLeft className="h-4 w-4 mr-1" />
|
||||||
|
Back to Video
|
||||||
|
</Link>
|
||||||
|
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="text-2xl">Share Video For Review</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Create a private link so reviewers can watch and comment on this single video.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
{loading ? (
|
||||||
|
<div className="flex items-center text-sm text-muted-foreground">
|
||||||
|
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
||||||
|
Loading link settings...
|
||||||
|
</div>
|
||||||
|
) : shareUrl ? (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Input value={shareUrl} readOnly className="font-mono text-sm h-11 bg-muted/50" />
|
||||||
|
<Button
|
||||||
|
variant={copied ? 'default' : 'outline'}
|
||||||
|
size="icon"
|
||||||
|
className="h-11 w-11 shrink-0"
|
||||||
|
onClick={copyLink}
|
||||||
|
>
|
||||||
|
{copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Button onClick={createShareLink} disabled={submitting} variant="outline">
|
||||||
|
{submitting ? <Loader2 className="h-4 w-4 mr-2 animate-spin" /> : <RefreshCcw className="h-4 w-4 mr-2" />}
|
||||||
|
Regenerate Link
|
||||||
|
</Button>
|
||||||
|
<Button onClick={revokeShareLink} disabled={submitting} variant="destructive">
|
||||||
|
{submitting ? <Loader2 className="h-4 w-4 mr-2 animate-spin" /> : <ShieldOff className="h-4 w-4 mr-2" />}
|
||||||
|
Revoke Link
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="rounded-lg border p-3 space-y-2">
|
||||||
|
<div>
|
||||||
|
<p className="text-sm font-medium">Video download</p>
|
||||||
|
<p className="text-xs text-muted-foreground">Allow viewers with this link to download</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Button
|
||||||
|
variant={allowDownloads ? 'default' : 'outline'}
|
||||||
|
disabled={submitting || allowDownloads}
|
||||||
|
onClick={() => updateDownloadSetting(true)}
|
||||||
|
>
|
||||||
|
Allow Download
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant={!allowDownloads ? 'default' : 'outline'}
|
||||||
|
disabled={submitting || !allowDownloads}
|
||||||
|
onClick={() => updateDownloadSetting(false)}
|
||||||
|
>
|
||||||
|
Block Download
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-lg border p-3 space-y-2">
|
||||||
|
<div className="flex items-center gap-2 text-sm font-medium">
|
||||||
|
{hasPassword ? <ShieldCheck className="h-4 w-4 text-green-600" /> : <Lock className="h-4 w-4" />}
|
||||||
|
Link password
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Input
|
||||||
|
type="password"
|
||||||
|
placeholder={hasPassword ? 'Enter new password to replace current one' : 'Set a password'}
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
disabled={submitting}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
onClick={() => updateSecuritySettings(false)}
|
||||||
|
disabled={submitting || !password.trim()}
|
||||||
|
variant="outline"
|
||||||
|
>
|
||||||
|
Save
|
||||||
|
</Button>
|
||||||
|
{hasPassword && (
|
||||||
|
<Button
|
||||||
|
onClick={() => updateSecuritySettings(true)}
|
||||||
|
disabled={submitting}
|
||||||
|
variant="outline"
|
||||||
|
>
|
||||||
|
Remove
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<Button onClick={createShareLink} disabled={submitting}>
|
||||||
|
{submitting ? <Loader2 className="h-4 w-4 mr-2 animate-spin" /> : <Link2 className="h-4 w-4 mr-2" />}
|
||||||
|
Create Review Link
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
This link allows guests to leave comments without an account. You can optionally protect it with a password.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<p className="text-sm text-destructive">{error}</p>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,531 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect, useRef, useCallback } from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import Image from 'next/image';
|
||||||
|
import { ArrowLeft, Loader2, Link as LinkIcon, AlertCircle, CheckCircle2, UploadCloud, FileVideo } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||||
|
import { parseVideoUrl, fetchVideoMetadata, getThumbnailUrl, type VideoSource } from '@/lib/video-providers';
|
||||||
|
import * as tus from 'tus-js-client';
|
||||||
|
|
||||||
|
export default function NewVideoPageClient({ projectId }: { projectId: string }) {
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [isFetchingMeta, setIsFetchingMeta] = useState(false);
|
||||||
|
|
||||||
|
// URL Mode State
|
||||||
|
const [videoUrl, setVideoUrl] = useState('');
|
||||||
|
const [videoSource, setVideoSource] = useState<VideoSource | null>(null);
|
||||||
|
const [urlError, setUrlError] = useState('');
|
||||||
|
|
||||||
|
// Upload Mode State
|
||||||
|
const [uploadMode, setUploadMode] = useState<'url' | 'file'>('url');
|
||||||
|
const [selectedFile, setSelectedFile] = useState<File | null>(null);
|
||||||
|
const [uploadProgress, setUploadProgress] = useState(0);
|
||||||
|
const [uploadStatus, setUploadStatus] = useState('');
|
||||||
|
const [pendingBunnyVideoId, setPendingBunnyVideoId] = useState<string | null>(null);
|
||||||
|
const [pendingBunnyUploadToken, setPendingBunnyUploadToken] = useState<string | null>(null);
|
||||||
|
const pendingBunnyVideoIdRef = useRef<string | null>(null);
|
||||||
|
const pendingBunnyUploadTokenRef = useRef<string | null>(null);
|
||||||
|
const activeTusUploadRef = useRef<tus.Upload | null>(null);
|
||||||
|
|
||||||
|
const [submitError, setSubmitError] = useState('');
|
||||||
|
const [formData, setFormData] = useState({
|
||||||
|
title: '',
|
||||||
|
description: '',
|
||||||
|
});
|
||||||
|
const isUploadingFile = isLoading && uploadMode === 'file';
|
||||||
|
const leaveWarningMessage = 'A video upload is in progress. Leaving this page will interrupt it. Do you want to leave?';
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
pendingBunnyVideoIdRef.current = pendingBunnyVideoId;
|
||||||
|
}, [pendingBunnyVideoId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
pendingBunnyUploadTokenRef.current = pendingBunnyUploadToken;
|
||||||
|
}, [pendingBunnyUploadToken]);
|
||||||
|
|
||||||
|
const cleanupPendingBunnyVideo = useCallback(async (videoId: string, uploadToken: string, keepalive = false) => {
|
||||||
|
try {
|
||||||
|
await fetch(`/api/projects/${projectId}/videos/bunny-init`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ videoId, uploadToken }),
|
||||||
|
keepalive,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to cleanup pending Bunny upload:', error);
|
||||||
|
} finally {
|
||||||
|
if (pendingBunnyVideoIdRef.current === videoId) {
|
||||||
|
pendingBunnyVideoIdRef.current = null;
|
||||||
|
setPendingBunnyVideoId(null);
|
||||||
|
}
|
||||||
|
if (pendingBunnyUploadTokenRef.current === uploadToken) {
|
||||||
|
pendingBunnyUploadTokenRef.current = null;
|
||||||
|
setPendingBunnyUploadToken(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [projectId]);
|
||||||
|
|
||||||
|
const abortAndCleanupPendingUpload = useCallback((keepalive = false) => {
|
||||||
|
const pendingVideoId = pendingBunnyVideoIdRef.current;
|
||||||
|
const pendingUploadToken = pendingBunnyUploadTokenRef.current;
|
||||||
|
if (!pendingVideoId || !pendingUploadToken) return;
|
||||||
|
|
||||||
|
if (activeTusUploadRef.current) {
|
||||||
|
try {
|
||||||
|
activeTusUploadRef.current.abort(true);
|
||||||
|
} catch {
|
||||||
|
// Ignore abort failures; we'll still attempt cleanup.
|
||||||
|
} finally {
|
||||||
|
activeTusUploadRef.current = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanupPendingBunnyVideo(pendingVideoId, pendingUploadToken, keepalive);
|
||||||
|
}, [cleanupPendingBunnyVideo]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isUploadingFile) return;
|
||||||
|
|
||||||
|
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
|
||||||
|
event.preventDefault();
|
||||||
|
event.returnValue = '';
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePageHide = () => {
|
||||||
|
abortAndCleanupPendingUpload(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePopState = () => {
|
||||||
|
const shouldLeave = window.confirm(leaveWarningMessage);
|
||||||
|
if (!shouldLeave) {
|
||||||
|
window.history.pushState(null, '', window.location.href);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
abortAndCleanupPendingUpload(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.history.pushState(null, '', window.location.href);
|
||||||
|
window.addEventListener('beforeunload', handleBeforeUnload);
|
||||||
|
window.addEventListener('pagehide', handlePageHide);
|
||||||
|
window.addEventListener('popstate', handlePopState);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('beforeunload', handleBeforeUnload);
|
||||||
|
window.removeEventListener('pagehide', handlePageHide);
|
||||||
|
window.removeEventListener('popstate', handlePopState);
|
||||||
|
};
|
||||||
|
}, [abortAndCleanupPendingUpload, isUploadingFile]);
|
||||||
|
|
||||||
|
// Auto-fetch metadata when a valid video source is detected
|
||||||
|
useEffect(() => {
|
||||||
|
if (!videoSource) return;
|
||||||
|
|
||||||
|
let cancelled = false;
|
||||||
|
setIsFetchingMeta(true);
|
||||||
|
|
||||||
|
fetchVideoMetadata(videoSource).then((meta) => {
|
||||||
|
if (cancelled || !meta) {
|
||||||
|
setIsFetchingMeta(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!formData.title) {
|
||||||
|
setFormData((prev) => ({ ...prev, title: meta.title }));
|
||||||
|
}
|
||||||
|
setVideoSource((prev) => (prev ? { ...prev, metadata: meta } : prev));
|
||||||
|
setIsFetchingMeta(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [videoSource?.videoId, videoSource?.providerId]);
|
||||||
|
|
||||||
|
const handleUrlChange = (url: string) => {
|
||||||
|
setVideoUrl(url);
|
||||||
|
setUrlError('');
|
||||||
|
setSubmitError('');
|
||||||
|
|
||||||
|
if (!url.trim()) {
|
||||||
|
setVideoSource(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const source = parseVideoUrl(url);
|
||||||
|
if (source) {
|
||||||
|
setVideoSource(source);
|
||||||
|
} else {
|
||||||
|
setVideoSource(null);
|
||||||
|
if (url.length > 10) {
|
||||||
|
setUrlError('Could not recognize this video URL. Currently supported: YouTube, Vimeo');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const file = e.target.files?.[0];
|
||||||
|
if (file) {
|
||||||
|
if (!file.type.startsWith('video/')) {
|
||||||
|
setSubmitError('Please select a valid video file.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setSelectedFile(file);
|
||||||
|
setSubmitError('');
|
||||||
|
if (!formData.title) {
|
||||||
|
// Strip extension from filename for default title
|
||||||
|
const nameWithoutExt = file.name.replace(/\.[^/.]+$/, '');
|
||||||
|
setFormData((prev) => ({ ...prev, title: nameWithoutExt }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const uploadToBunny = async (
|
||||||
|
file: File
|
||||||
|
): Promise<{ videoId: string; libraryId: string; providerId: string; url: string; uploadToken: string }> => {
|
||||||
|
// 1. Initialize Bunny Stream upload (creates video & gets signature)
|
||||||
|
setUploadStatus('Initializing upload...');
|
||||||
|
const initRes = await fetch(`/api/projects/${projectId}/videos/bunny-init`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ title: formData.title || file.name })
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!initRes.ok) {
|
||||||
|
const data = await initRes.json();
|
||||||
|
throw new Error(data.error || 'Failed to initialize upload');
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data: { videoId, libraryId, signature, expirationTime, uploadToken } } = await initRes.json();
|
||||||
|
setPendingBunnyVideoId(videoId);
|
||||||
|
setPendingBunnyUploadToken(uploadToken);
|
||||||
|
pendingBunnyVideoIdRef.current = videoId;
|
||||||
|
pendingBunnyUploadTokenRef.current = uploadToken;
|
||||||
|
|
||||||
|
// 2. Upload via TUS
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
setUploadStatus('Uploading video...');
|
||||||
|
const upload = new tus.Upload(file, {
|
||||||
|
endpoint: 'https://video.bunnycdn.com/tusupload',
|
||||||
|
retryDelays: [0, 3000, 5000, 10000, 20000],
|
||||||
|
headers: {
|
||||||
|
AuthorizationSignature: signature,
|
||||||
|
AuthorizationExpire: expirationTime.toString(),
|
||||||
|
VideoId: videoId,
|
||||||
|
LibraryId: libraryId,
|
||||||
|
},
|
||||||
|
metadata: {
|
||||||
|
filetype: file.type,
|
||||||
|
title: formData.title || file.name,
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
activeTusUploadRef.current = null;
|
||||||
|
reject(new Error('Upload failed: ' + error.message));
|
||||||
|
},
|
||||||
|
onProgress: (bytesUploaded, bytesTotal) => {
|
||||||
|
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(1);
|
||||||
|
setUploadProgress(Number(percentage));
|
||||||
|
setUploadStatus(`Uploading... ${percentage}%`);
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
activeTusUploadRef.current = null;
|
||||||
|
setUploadStatus('Processing video...');
|
||||||
|
resolve({
|
||||||
|
videoId,
|
||||||
|
libraryId,
|
||||||
|
providerId: 'bunny',
|
||||||
|
url: `https://iframe.mediadelivery.net/embed/${libraryId}/${videoId}`,
|
||||||
|
uploadToken,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
activeTusUploadRef.current = upload;
|
||||||
|
upload.start();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
setSubmitError('');
|
||||||
|
setUploadStatus('');
|
||||||
|
setUploadProgress(0);
|
||||||
|
|
||||||
|
try {
|
||||||
|
let uploadedBunnyVideoId: string | null = null;
|
||||||
|
let uploadedBunnyUploadToken: string | null = null;
|
||||||
|
let finalTitle = formData.title.trim();
|
||||||
|
const finalDescription = formData.description.trim() || null;
|
||||||
|
let finalVideoUrl = '';
|
||||||
|
let finalProviderId = '';
|
||||||
|
let finalVideoId = '';
|
||||||
|
let finalThumbnailUrl: string | null = null;
|
||||||
|
let finalDuration: number | null = null;
|
||||||
|
|
||||||
|
if (uploadMode === 'url') {
|
||||||
|
if (!videoSource) {
|
||||||
|
setUrlError('Please enter a valid video URL');
|
||||||
|
setIsLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
finalTitle = finalTitle || videoSource.metadata?.title || 'Untitled Video';
|
||||||
|
finalVideoUrl = videoSource.originalUrl;
|
||||||
|
finalProviderId = videoSource.providerId;
|
||||||
|
finalVideoId = videoSource.videoId;
|
||||||
|
finalThumbnailUrl = getThumbnailUrl(videoSource, 'large');
|
||||||
|
finalDuration = videoSource.metadata?.duration || null;
|
||||||
|
} else {
|
||||||
|
if (!selectedFile) {
|
||||||
|
setSubmitError('Please select a video file to upload');
|
||||||
|
setIsLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
finalTitle = finalTitle || selectedFile.name;
|
||||||
|
|
||||||
|
// Handle TUS Upload
|
||||||
|
const bunnyData = await uploadToBunny(selectedFile);
|
||||||
|
uploadedBunnyVideoId = bunnyData.videoId;
|
||||||
|
uploadedBunnyUploadToken = bunnyData.uploadToken;
|
||||||
|
|
||||||
|
finalVideoUrl = bunnyData.url;
|
||||||
|
finalProviderId = bunnyData.providerId;
|
||||||
|
finalVideoId = bunnyData.videoId;
|
||||||
|
// Bunny will generate thumbnails automatically after processing.
|
||||||
|
// We'll just provide the standard CDN thumbnail URL format as fallback.
|
||||||
|
finalThumbnailUrl = `https://vz-thumbnail.b-cdn.net/${bunnyData.videoId}/thumbnail.jpg`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final POST to our database
|
||||||
|
const response = await fetch(`/api/projects/${projectId}/videos`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
title: finalTitle,
|
||||||
|
description: finalDescription,
|
||||||
|
videoUrl: finalVideoUrl,
|
||||||
|
providerId: finalProviderId,
|
||||||
|
videoId: finalVideoId,
|
||||||
|
thumbnailUrl: finalThumbnailUrl,
|
||||||
|
duration: finalDuration,
|
||||||
|
uploadToken: uploadedBunnyUploadToken,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
setSubmitError(data.error || 'Failed to add video');
|
||||||
|
if (uploadedBunnyVideoId && uploadedBunnyUploadToken) {
|
||||||
|
await cleanupPendingBunnyVideo(uploadedBunnyVideoId, uploadedBunnyUploadToken);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pendingBunnyVideoIdRef.current = null;
|
||||||
|
pendingBunnyUploadTokenRef.current = null;
|
||||||
|
setPendingBunnyVideoId(null);
|
||||||
|
setPendingBunnyUploadToken(null);
|
||||||
|
router.push(`/projects/${projectId}`);
|
||||||
|
} catch (error: unknown) {
|
||||||
|
console.error('Failed to add video:', error);
|
||||||
|
setSubmitError(error instanceof Error ? error.message : 'An unexpected error occurred');
|
||||||
|
if (pendingBunnyVideoIdRef.current && pendingBunnyUploadTokenRef.current) {
|
||||||
|
await cleanupPendingBunnyVideo(pendingBunnyVideoIdRef.current, pendingBunnyUploadTokenRef.current);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
activeTusUploadRef.current = null;
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const thumbnailUrl = videoSource ? getThumbnailUrl(videoSource, 'large') : null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="container max-w-2xl mx-auto py-8">
|
||||||
|
<div className="mb-6">
|
||||||
|
<Link
|
||||||
|
href={`/projects/${projectId}`}
|
||||||
|
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
||||||
|
onClick={(event) => {
|
||||||
|
if (!isUploadingFile) return;
|
||||||
|
const shouldLeave = window.confirm(leaveWarningMessage);
|
||||||
|
if (!shouldLeave) {
|
||||||
|
event.preventDefault();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
abortAndCleanupPendingUpload(true);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ArrowLeft className="h-4 w-4 mr-1" />
|
||||||
|
Back to Project
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Add Video</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Paste a video link or upload a file directly to add it to your project. Currently supports YouTube.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<Tabs value={uploadMode} onValueChange={(v) => !isLoading && setUploadMode(v as 'url' | 'file')} className="mb-6">
|
||||||
|
<TabsList className="grid w-full grid-cols-2">
|
||||||
|
<TabsTrigger value="url" disabled={isLoading}>Paste URL</TabsTrigger>
|
||||||
|
<TabsTrigger value="file" disabled={isLoading}>Direct Upload</TabsTrigger>
|
||||||
|
</TabsList>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
<form onSubmit={handleSubmit} className="space-y-6">
|
||||||
|
|
||||||
|
{uploadMode === 'url' ? (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="url">Video URL</Label>
|
||||||
|
<div className="relative">
|
||||||
|
<LinkIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
||||||
|
<Input
|
||||||
|
id="url"
|
||||||
|
placeholder="https://youtube.com/watch?v=..."
|
||||||
|
value={videoUrl}
|
||||||
|
onChange={(e) => handleUrlChange(e.target.value)}
|
||||||
|
className="pl-10"
|
||||||
|
required
|
||||||
|
disabled={isLoading}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{urlError && (
|
||||||
|
<p className="text-sm text-destructive flex items-center gap-1">
|
||||||
|
<AlertCircle className="h-4 w-4" />
|
||||||
|
{urlError}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{videoSource && (
|
||||||
|
<p className="text-sm text-green-600 flex items-center gap-1">
|
||||||
|
<CheckCircle2 className="h-4 w-4" />
|
||||||
|
{videoSource.providerId.charAt(0).toUpperCase() + videoSource.providerId.slice(1)} video detected
|
||||||
|
{isFetchingMeta && ' — fetching metadata...'}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="file">Video File</Label>
|
||||||
|
<div className="flex items-center justify-center w-full">
|
||||||
|
<label htmlFor="file" className={`flex flex-col items-center justify-center w-full h-40 border-2 border-dashed rounded-lg cursor-pointer bg-muted/30 hover:bg-muted/50 transition-colors ${selectedFile ? 'border-primary' : 'border-border'}`}>
|
||||||
|
<div className="flex flex-col items-center justify-center pt-5 pb-6">
|
||||||
|
{selectedFile ? (
|
||||||
|
<>
|
||||||
|
<FileVideo className="w-10 h-10 mb-3 text-primary" />
|
||||||
|
<p className="mb-2 text-sm text-foreground font-medium">{selectedFile.name}</p>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{(selectedFile.size / (1024 * 1024)).toFixed(2)} MB
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<UploadCloud className="w-10 h-10 mb-3 text-muted-foreground" />
|
||||||
|
<p className="mb-2 text-sm text-muted-foreground">
|
||||||
|
<span className="font-semibold">Click to upload</span> or drag and drop
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-muted-foreground">MP4, WebM, or OGG</p>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<input id="file" type="file" accept="video/*" className="hidden" onChange={handleFileChange} disabled={isLoading} />
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Video Preview (Only for URL mode) */}
|
||||||
|
{uploadMode === 'url' && thumbnailUrl && videoSource && (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Preview</Label>
|
||||||
|
<div className="relative aspect-video rounded-lg overflow-hidden bg-muted">
|
||||||
|
<Image
|
||||||
|
src={thumbnailUrl}
|
||||||
|
alt="Video thumbnail"
|
||||||
|
fill
|
||||||
|
sizes="(max-width: 768px) 100vw, 600px"
|
||||||
|
className="object-cover"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Title */}
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="title">Title</Label>
|
||||||
|
<Input
|
||||||
|
id="title"
|
||||||
|
placeholder={isFetchingMeta ? 'Fetching title...' : 'Video title (will auto-fill from video if empty)'}
|
||||||
|
value={formData.title}
|
||||||
|
onChange={(e) => setFormData((prev) => ({ ...prev, title: e.target.value }))}
|
||||||
|
disabled={isLoading}
|
||||||
|
/>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
Leave empty to use the original video title
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Description */}
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="description">Description (optional)</Label>
|
||||||
|
<Textarea
|
||||||
|
id="description"
|
||||||
|
placeholder="Add context about this video..."
|
||||||
|
value={formData.description}
|
||||||
|
onChange={(e) => setFormData((prev) => ({ ...prev, description: e.target.value }))}
|
||||||
|
rows={3}
|
||||||
|
disabled={isLoading}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{submitError && (
|
||||||
|
<p className="text-sm text-destructive flex items-center gap-1">
|
||||||
|
<AlertCircle className="h-4 w-4" />
|
||||||
|
{submitError}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{uploadStatus && (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<p className="text-sm text-muted-foreground">{uploadStatus}</p>
|
||||||
|
{uploadProgress > 0 && uploadProgress < 100 && (
|
||||||
|
<div className="w-full bg-secondary rounded-full h-2">
|
||||||
|
<div className="bg-primary h-2 rounded-full transition-all" style={{ width: `${uploadProgress}%` }}></div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{isUploadingFile && (
|
||||||
|
<p className="text-xs text-amber-500">
|
||||||
|
Do not close, refresh, or navigate away while the upload is in progress.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="flex flex-wrap gap-3">
|
||||||
|
<Button type="submit" disabled={isLoading || (uploadMode === 'url' && !videoSource) || (uploadMode === 'file' && !selectedFile)}>
|
||||||
|
{isLoading && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
||||||
|
Add Video
|
||||||
|
</Button>
|
||||||
|
<Button type="button" variant="outline" onClick={() => router.back()} disabled={isLoading}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,533 +1,17 @@
|
|||||||
'use client';
|
import { requireProjectAccessOrRedirect } from '@/lib/route-access';
|
||||||
|
import NewVideoPageClient from './new-video-page-client';
|
||||||
|
|
||||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
interface NewVideoPageProps {
|
||||||
import { useRouter, useParams } from 'next/navigation';
|
params: Promise<{ projectId: string }>;
|
||||||
import Link from 'next/link';
|
}
|
||||||
import Image from 'next/image';
|
|
||||||
import { ArrowLeft, Loader2, Link as LinkIcon, AlertCircle, CheckCircle2, UploadCloud, FileVideo } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
|
||||||
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
|
||||||
import { parseVideoUrl, fetchVideoMetadata, getThumbnailUrl, type VideoSource } from '@/lib/video-providers';
|
|
||||||
import * as tus from 'tus-js-client';
|
|
||||||
|
|
||||||
export default function NewVideoPage() {
|
export default async function NewVideoPage({ params }: NewVideoPageProps) {
|
||||||
const router = useRouter();
|
const { projectId } = await params;
|
||||||
const params = useParams();
|
|
||||||
const projectId = params.projectId as string;
|
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
await requireProjectAccessOrRedirect({
|
||||||
const [isFetchingMeta, setIsFetchingMeta] = useState(false);
|
projectId,
|
||||||
|
intent: 'manage',
|
||||||
// URL Mode State
|
|
||||||
const [videoUrl, setVideoUrl] = useState('');
|
|
||||||
const [videoSource, setVideoSource] = useState<VideoSource | null>(null);
|
|
||||||
const [urlError, setUrlError] = useState('');
|
|
||||||
|
|
||||||
// Upload Mode State
|
|
||||||
const [uploadMode, setUploadMode] = useState<'url' | 'file'>('url');
|
|
||||||
const [selectedFile, setSelectedFile] = useState<File | null>(null);
|
|
||||||
const [uploadProgress, setUploadProgress] = useState(0);
|
|
||||||
const [uploadStatus, setUploadStatus] = useState('');
|
|
||||||
const [pendingBunnyVideoId, setPendingBunnyVideoId] = useState<string | null>(null);
|
|
||||||
const [pendingBunnyUploadToken, setPendingBunnyUploadToken] = useState<string | null>(null);
|
|
||||||
const pendingBunnyVideoIdRef = useRef<string | null>(null);
|
|
||||||
const pendingBunnyUploadTokenRef = useRef<string | null>(null);
|
|
||||||
const activeTusUploadRef = useRef<tus.Upload | null>(null);
|
|
||||||
|
|
||||||
const [submitError, setSubmitError] = useState('');
|
|
||||||
const [formData, setFormData] = useState({
|
|
||||||
title: '',
|
|
||||||
description: '',
|
|
||||||
});
|
});
|
||||||
const isUploadingFile = isLoading && uploadMode === 'file';
|
|
||||||
const leaveWarningMessage = 'A video upload is in progress. Leaving this page will interrupt it. Do you want to leave?';
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
pendingBunnyVideoIdRef.current = pendingBunnyVideoId;
|
|
||||||
}, [pendingBunnyVideoId]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
pendingBunnyUploadTokenRef.current = pendingBunnyUploadToken;
|
|
||||||
}, [pendingBunnyUploadToken]);
|
|
||||||
|
|
||||||
const cleanupPendingBunnyVideo = useCallback(async (videoId: string, uploadToken: string, keepalive = false) => {
|
|
||||||
try {
|
|
||||||
await fetch(`/api/projects/${projectId}/videos/bunny-init`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ videoId, uploadToken }),
|
|
||||||
keepalive,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to cleanup pending Bunny upload:', error);
|
|
||||||
} finally {
|
|
||||||
if (pendingBunnyVideoIdRef.current === videoId) {
|
|
||||||
pendingBunnyVideoIdRef.current = null;
|
|
||||||
setPendingBunnyVideoId(null);
|
|
||||||
}
|
|
||||||
if (pendingBunnyUploadTokenRef.current === uploadToken) {
|
|
||||||
pendingBunnyUploadTokenRef.current = null;
|
|
||||||
setPendingBunnyUploadToken(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [projectId]);
|
|
||||||
|
|
||||||
const abortAndCleanupPendingUpload = useCallback((keepalive = false) => {
|
|
||||||
const pendingVideoId = pendingBunnyVideoIdRef.current;
|
|
||||||
const pendingUploadToken = pendingBunnyUploadTokenRef.current;
|
|
||||||
if (!pendingVideoId || !pendingUploadToken) return;
|
|
||||||
|
|
||||||
if (activeTusUploadRef.current) {
|
|
||||||
try {
|
|
||||||
activeTusUploadRef.current.abort(true);
|
|
||||||
} catch {
|
|
||||||
// Ignore abort failures; we'll still attempt cleanup.
|
|
||||||
} finally {
|
|
||||||
activeTusUploadRef.current = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void cleanupPendingBunnyVideo(pendingVideoId, pendingUploadToken, keepalive);
|
|
||||||
}, [cleanupPendingBunnyVideo]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!isUploadingFile) return;
|
|
||||||
|
|
||||||
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
|
|
||||||
event.preventDefault();
|
|
||||||
event.returnValue = '';
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlePageHide = () => {
|
|
||||||
abortAndCleanupPendingUpload(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlePopState = () => {
|
|
||||||
const shouldLeave = window.confirm(leaveWarningMessage);
|
|
||||||
if (!shouldLeave) {
|
|
||||||
window.history.pushState(null, '', window.location.href);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
abortAndCleanupPendingUpload(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
window.history.pushState(null, '', window.location.href);
|
|
||||||
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
||||||
window.addEventListener('pagehide', handlePageHide);
|
|
||||||
window.addEventListener('popstate', handlePopState);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener('beforeunload', handleBeforeUnload);
|
|
||||||
window.removeEventListener('pagehide', handlePageHide);
|
|
||||||
window.removeEventListener('popstate', handlePopState);
|
|
||||||
};
|
|
||||||
}, [abortAndCleanupPendingUpload, isUploadingFile]);
|
|
||||||
|
|
||||||
// Auto-fetch metadata when a valid video source is detected
|
|
||||||
useEffect(() => {
|
|
||||||
if (!videoSource) return;
|
|
||||||
|
|
||||||
let cancelled = false;
|
|
||||||
setIsFetchingMeta(true);
|
|
||||||
|
|
||||||
fetchVideoMetadata(videoSource).then((meta) => {
|
|
||||||
if (cancelled || !meta) {
|
|
||||||
setIsFetchingMeta(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!formData.title) {
|
|
||||||
setFormData((prev) => ({ ...prev, title: meta.title }));
|
|
||||||
}
|
|
||||||
setVideoSource((prev) => (prev ? { ...prev, metadata: meta } : prev));
|
|
||||||
setIsFetchingMeta(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
cancelled = true;
|
|
||||||
};
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [videoSource?.videoId, videoSource?.providerId]);
|
|
||||||
|
|
||||||
const handleUrlChange = (url: string) => {
|
|
||||||
setVideoUrl(url);
|
|
||||||
setUrlError('');
|
|
||||||
setSubmitError('');
|
|
||||||
|
|
||||||
if (!url.trim()) {
|
|
||||||
setVideoSource(null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const source = parseVideoUrl(url);
|
|
||||||
if (source) {
|
|
||||||
setVideoSource(source);
|
|
||||||
} else {
|
|
||||||
setVideoSource(null);
|
|
||||||
if (url.length > 10) {
|
|
||||||
setUrlError('Could not recognize this video URL. Currently supported: YouTube, Vimeo');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
||||||
const file = e.target.files?.[0];
|
|
||||||
if (file) {
|
|
||||||
if (!file.type.startsWith('video/')) {
|
|
||||||
setSubmitError('Please select a valid video file.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setSelectedFile(file);
|
|
||||||
setSubmitError('');
|
|
||||||
if (!formData.title) {
|
|
||||||
// Strip extension from filename for default title
|
|
||||||
const nameWithoutExt = file.name.replace(/\.[^/.]+$/, '');
|
|
||||||
setFormData((prev) => ({ ...prev, title: nameWithoutExt }));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const uploadToBunny = async (
|
|
||||||
file: File
|
|
||||||
): Promise<{ videoId: string; libraryId: string; providerId: string; url: string; uploadToken: string }> => {
|
|
||||||
// 1. Initialize Bunny Stream upload (creates video & gets signature)
|
|
||||||
setUploadStatus('Initializing upload...');
|
|
||||||
const initRes = await fetch(`/api/projects/${projectId}/videos/bunny-init`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ title: formData.title || file.name })
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!initRes.ok) {
|
|
||||||
const data = await initRes.json();
|
|
||||||
throw new Error(data.error || 'Failed to initialize upload');
|
|
||||||
}
|
|
||||||
|
|
||||||
const { data: { videoId, libraryId, signature, expirationTime, uploadToken } } = await initRes.json();
|
|
||||||
setPendingBunnyVideoId(videoId);
|
|
||||||
setPendingBunnyUploadToken(uploadToken);
|
|
||||||
pendingBunnyVideoIdRef.current = videoId;
|
|
||||||
pendingBunnyUploadTokenRef.current = uploadToken;
|
|
||||||
|
|
||||||
// 2. Upload via TUS
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
setUploadStatus('Uploading video...');
|
|
||||||
const upload = new tus.Upload(file, {
|
|
||||||
endpoint: 'https://video.bunnycdn.com/tusupload',
|
|
||||||
retryDelays: [0, 3000, 5000, 10000, 20000],
|
|
||||||
headers: {
|
|
||||||
AuthorizationSignature: signature,
|
|
||||||
AuthorizationExpire: expirationTime.toString(),
|
|
||||||
VideoId: videoId,
|
|
||||||
LibraryId: libraryId,
|
|
||||||
},
|
|
||||||
metadata: {
|
|
||||||
filetype: file.type,
|
|
||||||
title: formData.title || file.name,
|
|
||||||
},
|
|
||||||
onError: (error) => {
|
|
||||||
activeTusUploadRef.current = null;
|
|
||||||
reject(new Error('Upload failed: ' + error.message));
|
|
||||||
},
|
|
||||||
onProgress: (bytesUploaded, bytesTotal) => {
|
|
||||||
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(1);
|
|
||||||
setUploadProgress(Number(percentage));
|
|
||||||
setUploadStatus(`Uploading... ${percentage}%`);
|
|
||||||
},
|
|
||||||
onSuccess: () => {
|
|
||||||
activeTusUploadRef.current = null;
|
|
||||||
setUploadStatus('Processing video...');
|
|
||||||
resolve({
|
|
||||||
videoId,
|
|
||||||
libraryId,
|
|
||||||
providerId: 'bunny',
|
|
||||||
url: `https://iframe.mediadelivery.net/embed/${libraryId}/${videoId}`,
|
|
||||||
uploadToken,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
});
|
|
||||||
activeTusUploadRef.current = upload;
|
|
||||||
upload.start();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
setIsLoading(true);
|
|
||||||
setSubmitError('');
|
|
||||||
setUploadStatus('');
|
|
||||||
setUploadProgress(0);
|
|
||||||
|
|
||||||
try {
|
|
||||||
let uploadedBunnyVideoId: string | null = null;
|
|
||||||
let uploadedBunnyUploadToken: string | null = null;
|
|
||||||
let finalTitle = formData.title.trim();
|
|
||||||
const finalDescription = formData.description.trim() || null;
|
|
||||||
let finalVideoUrl = '';
|
|
||||||
let finalProviderId = '';
|
|
||||||
let finalVideoId = '';
|
|
||||||
let finalThumbnailUrl: string | null = null;
|
|
||||||
let finalDuration: number | null = null;
|
|
||||||
|
|
||||||
if (uploadMode === 'url') {
|
|
||||||
if (!videoSource) {
|
|
||||||
setUrlError('Please enter a valid video URL');
|
|
||||||
setIsLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
finalTitle = finalTitle || videoSource.metadata?.title || 'Untitled Video';
|
|
||||||
finalVideoUrl = videoSource.originalUrl;
|
|
||||||
finalProviderId = videoSource.providerId;
|
|
||||||
finalVideoId = videoSource.videoId;
|
|
||||||
finalThumbnailUrl = getThumbnailUrl(videoSource, 'large');
|
|
||||||
finalDuration = videoSource.metadata?.duration || null;
|
|
||||||
} else {
|
|
||||||
if (!selectedFile) {
|
|
||||||
setSubmitError('Please select a video file to upload');
|
|
||||||
setIsLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
finalTitle = finalTitle || selectedFile.name;
|
|
||||||
|
|
||||||
// Handle TUS Upload
|
|
||||||
const bunnyData = await uploadToBunny(selectedFile);
|
|
||||||
uploadedBunnyVideoId = bunnyData.videoId;
|
|
||||||
uploadedBunnyUploadToken = bunnyData.uploadToken;
|
|
||||||
|
|
||||||
finalVideoUrl = bunnyData.url;
|
|
||||||
finalProviderId = bunnyData.providerId;
|
|
||||||
finalVideoId = bunnyData.videoId;
|
|
||||||
// Bunny will generate thumbnails automatically after processing.
|
|
||||||
// We'll just provide the standard CDN thumbnail URL format as fallback.
|
|
||||||
finalThumbnailUrl = `https://vz-thumbnail.b-cdn.net/${bunnyData.videoId}/thumbnail.jpg`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Final POST to our database
|
|
||||||
const response = await fetch(`/api/projects/${projectId}/videos`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
title: finalTitle,
|
|
||||||
description: finalDescription,
|
|
||||||
videoUrl: finalVideoUrl,
|
|
||||||
providerId: finalProviderId,
|
|
||||||
videoId: finalVideoId,
|
|
||||||
thumbnailUrl: finalThumbnailUrl,
|
|
||||||
duration: finalDuration,
|
|
||||||
uploadToken: uploadedBunnyUploadToken,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const data = await response.json();
|
|
||||||
setSubmitError(data.error || 'Failed to add video');
|
|
||||||
if (uploadedBunnyVideoId && uploadedBunnyUploadToken) {
|
|
||||||
await cleanupPendingBunnyVideo(uploadedBunnyVideoId, uploadedBunnyUploadToken);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pendingBunnyVideoIdRef.current = null;
|
|
||||||
pendingBunnyUploadTokenRef.current = null;
|
|
||||||
setPendingBunnyVideoId(null);
|
|
||||||
setPendingBunnyUploadToken(null);
|
|
||||||
router.push(`/projects/${projectId}`);
|
|
||||||
} catch (error: unknown) {
|
|
||||||
console.error('Failed to add video:', error);
|
|
||||||
setSubmitError(error instanceof Error ? error.message : 'An unexpected error occurred');
|
|
||||||
if (pendingBunnyVideoIdRef.current && pendingBunnyUploadTokenRef.current) {
|
|
||||||
await cleanupPendingBunnyVideo(pendingBunnyVideoIdRef.current, pendingBunnyUploadTokenRef.current);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
activeTusUploadRef.current = null;
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const thumbnailUrl = videoSource ? getThumbnailUrl(videoSource, 'large') : null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="container max-w-2xl mx-auto py-8">
|
|
||||||
<div className="mb-6">
|
|
||||||
<Link
|
|
||||||
href={`/projects/${projectId}`}
|
|
||||||
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
||||||
onClick={(event) => {
|
|
||||||
if (!isUploadingFile) return;
|
|
||||||
const shouldLeave = window.confirm(leaveWarningMessage);
|
|
||||||
if (!shouldLeave) {
|
|
||||||
event.preventDefault();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
abortAndCleanupPendingUpload(true);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4 mr-1" />
|
|
||||||
Back to Project
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Card>
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle>Add Video</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Paste a video link or upload a file directly to add it to your project. Currently supports YouTube.
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<Tabs value={uploadMode} onValueChange={(v) => !isLoading && setUploadMode(v as 'url' | 'file')} className="mb-6">
|
|
||||||
<TabsList className="grid w-full grid-cols-2">
|
|
||||||
<TabsTrigger value="url" disabled={isLoading}>Paste URL</TabsTrigger>
|
|
||||||
<TabsTrigger value="file" disabled={isLoading}>Direct Upload</TabsTrigger>
|
|
||||||
</TabsList>
|
|
||||||
</Tabs>
|
|
||||||
|
|
||||||
<form onSubmit={handleSubmit} className="space-y-6">
|
|
||||||
|
|
||||||
{uploadMode === 'url' ? (
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="url">Video URL</Label>
|
|
||||||
<div className="relative">
|
|
||||||
<LinkIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
|
||||||
<Input
|
|
||||||
id="url"
|
|
||||||
placeholder="https://youtube.com/watch?v=..."
|
|
||||||
value={videoUrl}
|
|
||||||
onChange={(e) => handleUrlChange(e.target.value)}
|
|
||||||
className="pl-10"
|
|
||||||
required
|
|
||||||
disabled={isLoading}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{urlError && (
|
|
||||||
<p className="text-sm text-destructive flex items-center gap-1">
|
|
||||||
<AlertCircle className="h-4 w-4" />
|
|
||||||
{urlError}
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{videoSource && (
|
|
||||||
<p className="text-sm text-green-600 flex items-center gap-1">
|
|
||||||
<CheckCircle2 className="h-4 w-4" />
|
|
||||||
{videoSource.providerId.charAt(0).toUpperCase() + videoSource.providerId.slice(1)} video detected
|
|
||||||
{isFetchingMeta && ' — fetching metadata...'}
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="file">Video File</Label>
|
|
||||||
<div className="flex items-center justify-center w-full">
|
|
||||||
<label htmlFor="file" className={`flex flex-col items-center justify-center w-full h-40 border-2 border-dashed rounded-lg cursor-pointer bg-muted/30 hover:bg-muted/50 transition-colors ${selectedFile ? 'border-primary' : 'border-border'}`}>
|
|
||||||
<div className="flex flex-col items-center justify-center pt-5 pb-6">
|
|
||||||
{selectedFile ? (
|
|
||||||
<>
|
|
||||||
<FileVideo className="w-10 h-10 mb-3 text-primary" />
|
|
||||||
<p className="mb-2 text-sm text-foreground font-medium">{selectedFile.name}</p>
|
|
||||||
<p className="text-xs text-muted-foreground">
|
|
||||||
{(selectedFile.size / (1024 * 1024)).toFixed(2)} MB
|
|
||||||
</p>
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
<UploadCloud className="w-10 h-10 mb-3 text-muted-foreground" />
|
|
||||||
<p className="mb-2 text-sm text-muted-foreground">
|
|
||||||
<span className="font-semibold">Click to upload</span> or drag and drop
|
|
||||||
</p>
|
|
||||||
<p className="text-xs text-muted-foreground">MP4, WebM, or OGG</p>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
<input id="file" type="file" accept="video/*" className="hidden" onChange={handleFileChange} disabled={isLoading} />
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Video Preview (Only for URL mode) */}
|
|
||||||
{uploadMode === 'url' && thumbnailUrl && videoSource && (
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label>Preview</Label>
|
|
||||||
<div className="relative aspect-video rounded-lg overflow-hidden bg-muted">
|
|
||||||
<Image
|
|
||||||
src={thumbnailUrl}
|
|
||||||
alt="Video thumbnail"
|
|
||||||
fill
|
|
||||||
sizes="(max-width: 768px) 100vw, 600px"
|
|
||||||
className="object-cover"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Title */}
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="title">Title</Label>
|
|
||||||
<Input
|
|
||||||
id="title"
|
|
||||||
placeholder={isFetchingMeta ? 'Fetching title...' : 'Video title (will auto-fill from video if empty)'}
|
|
||||||
value={formData.title}
|
|
||||||
onChange={(e) => setFormData((prev) => ({ ...prev, title: e.target.value }))}
|
|
||||||
disabled={isLoading}
|
|
||||||
/>
|
|
||||||
<p className="text-xs text-muted-foreground">
|
|
||||||
Leave empty to use the original video title
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Description */}
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="description">Description (optional)</Label>
|
|
||||||
<Textarea
|
|
||||||
id="description"
|
|
||||||
placeholder="Add context about this video..."
|
|
||||||
value={formData.description}
|
|
||||||
onChange={(e) => setFormData((prev) => ({ ...prev, description: e.target.value }))}
|
|
||||||
rows={3}
|
|
||||||
disabled={isLoading}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{submitError && (
|
|
||||||
<p className="text-sm text-destructive flex items-center gap-1">
|
|
||||||
<AlertCircle className="h-4 w-4" />
|
|
||||||
{submitError}
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{uploadStatus && (
|
|
||||||
<div className="space-y-2">
|
|
||||||
<p className="text-sm text-muted-foreground">{uploadStatus}</p>
|
|
||||||
{uploadProgress > 0 && uploadProgress < 100 && (
|
|
||||||
<div className="w-full bg-secondary rounded-full h-2">
|
|
||||||
<div className="bg-primary h-2 rounded-full transition-all" style={{ width: `${uploadProgress}%` }}></div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{isUploadingFile && (
|
|
||||||
<p className="text-xs text-amber-500">
|
|
||||||
Do not close, refresh, or navigate away while the upload is in progress.
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<div className="flex flex-wrap gap-3">
|
return <NewVideoPageClient projectId={projectId} />;
|
||||||
<Button type="submit" disabled={isLoading || (uploadMode === 'url' && !videoSource) || (uploadMode === 'file' && !selectedFile)}>
|
|
||||||
{isLoading && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
|
||||||
Add Video
|
|
||||||
</Button>
|
|
||||||
<Button type="button" variant="outline" onClick={() => router.back()} disabled={isLoading}>
|
|
||||||
Cancel
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,282 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { ArrowLeft, Loader2, Globe, Lock, UserPlus, FolderPlus, Building2 } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from '@/components/ui/select';
|
||||||
|
|
||||||
|
type Visibility = 'PRIVATE' | 'INVITE' | 'PUBLIC';
|
||||||
|
|
||||||
|
interface Workspace {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const visibilityOptions: { value: Visibility; label: string; description: string; icon: React.ReactNode }[] = [
|
||||||
|
{
|
||||||
|
value: 'PRIVATE',
|
||||||
|
label: 'Private',
|
||||||
|
description: 'Only workspace members and project members can access',
|
||||||
|
icon: <Lock className="h-5 w-5" />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'INVITE',
|
||||||
|
label: 'Invite Only',
|
||||||
|
description: 'Share with specific people via email',
|
||||||
|
icon: <UserPlus className="h-5 w-5" />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'PUBLIC',
|
||||||
|
label: 'Public',
|
||||||
|
description: 'Anyone with the link can view',
|
||||||
|
icon: <Globe className="h-5 w-5" />,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export default function NewProjectPage() {
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const preselectedWorkspace = searchParams.get('workspace');
|
||||||
|
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [workspaces, setWorkspaces] = useState<Workspace[]>([]);
|
||||||
|
const [isLoadingWorkspaces, setIsLoadingWorkspaces] = useState(true);
|
||||||
|
const [formData, setFormData] = useState({
|
||||||
|
name: '',
|
||||||
|
description: '',
|
||||||
|
visibility: 'PRIVATE' as Visibility,
|
||||||
|
workspaceId: preselectedWorkspace || '',
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
async function fetchWorkspaces() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/workspaces');
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
const workspacesData = data.data.workspaces || [];
|
||||||
|
setWorkspaces(workspacesData);
|
||||||
|
// Auto-select if only one workspace and none preselected
|
||||||
|
if (!preselectedWorkspace && workspacesData.length === 1) {
|
||||||
|
setFormData(prev => ({ ...prev, workspaceId: workspacesData[0].id }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
} finally {
|
||||||
|
setIsLoadingWorkspaces(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fetchWorkspaces();
|
||||||
|
}, [preselectedWorkspace]);
|
||||||
|
|
||||||
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (!formData.workspaceId) {
|
||||||
|
setError('Please select a workspace');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setIsLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/projects', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(formData),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
setError(data.error || 'Failed to create project');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
router.push(`/projects/${data.data.id}`);
|
||||||
|
} catch {
|
||||||
|
setError('Something went wrong. Please try again.');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
||||||
|
<div className="w-full max-w-xl">
|
||||||
|
<div className="mb-8">
|
||||||
|
<Link
|
||||||
|
href="/dashboard"
|
||||||
|
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
||||||
|
>
|
||||||
|
<ArrowLeft className="h-4 w-4 mr-1" />
|
||||||
|
Back to Projects
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader className="text-center pb-2">
|
||||||
|
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
||||||
|
<FolderPlus className="h-7 w-7 text-primary" />
|
||||||
|
</div>
|
||||||
|
<CardTitle className="text-2xl">Create New Project</CardTitle>
|
||||||
|
<CardDescription className="text-base">
|
||||||
|
Set up a new project to organize your videos and collect feedback
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="pt-6">
|
||||||
|
<form onSubmit={handleSubmit} className="space-y-6">
|
||||||
|
{/* Workspace selector */}
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label className="text-sm font-medium">Workspace</Label>
|
||||||
|
{isLoadingWorkspaces ? (
|
||||||
|
<div className="flex items-center gap-2 text-sm text-muted-foreground py-2">
|
||||||
|
<Loader2 className="h-4 w-4 animate-spin" />
|
||||||
|
Loading workspaces...
|
||||||
|
</div>
|
||||||
|
) : workspaces.length === 0 ? (
|
||||||
|
<div className="rounded-lg border border-dashed p-4 text-center">
|
||||||
|
<Building2 className="h-8 w-8 mx-auto text-muted-foreground mb-2" />
|
||||||
|
<p className="text-sm text-muted-foreground mb-2">
|
||||||
|
You need a workspace first. Every project belongs to a workspace.
|
||||||
|
</p>
|
||||||
|
<Button asChild size="sm" variant="outline">
|
||||||
|
<Link href="/workspaces/new">Create Workspace</Link>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<Select
|
||||||
|
value={formData.workspaceId}
|
||||||
|
onValueChange={(v) => setFormData(prev => ({ ...prev, workspaceId: v }))}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="h-11">
|
||||||
|
<SelectValue placeholder="Select a workspace" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{workspaces.map((ws) => (
|
||||||
|
<SelectItem key={ws.id} value={ws.id}>
|
||||||
|
<span className="flex items-center gap-2">
|
||||||
|
<Building2 className="h-4 w-4" />
|
||||||
|
{ws.name}
|
||||||
|
</span>
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="name" className="text-sm font-medium">
|
||||||
|
Project Name
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="name"
|
||||||
|
placeholder="e.g. Product Demo Q1"
|
||||||
|
value={formData.name}
|
||||||
|
onChange={(e) => setFormData(prev => ({ ...prev, name: e.target.value }))}
|
||||||
|
required
|
||||||
|
disabled={isLoading}
|
||||||
|
className="h-11"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="description" className="text-sm font-medium">
|
||||||
|
Description
|
||||||
|
<span className="text-muted-foreground font-normal ml-1">(optional)</span>
|
||||||
|
</Label>
|
||||||
|
<Textarea
|
||||||
|
id="description"
|
||||||
|
placeholder="Brief description of what this project is about..."
|
||||||
|
value={formData.description}
|
||||||
|
onChange={(e) => setFormData(prev => ({ ...prev, description: e.target.value }))}
|
||||||
|
rows={3}
|
||||||
|
disabled={isLoading}
|
||||||
|
className="resize-none"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-3">
|
||||||
|
<Label className="text-sm font-medium">Who can access?</Label>
|
||||||
|
<div className="grid gap-3">
|
||||||
|
{visibilityOptions.map((option) => (
|
||||||
|
<button
|
||||||
|
key={option.value}
|
||||||
|
type="button"
|
||||||
|
onClick={() => setFormData(prev => ({ ...prev, visibility: option.value }))}
|
||||||
|
disabled={isLoading}
|
||||||
|
className={`w-full flex items-center gap-4 p-4 rounded-xl border-2 text-left transition-all ${formData.visibility === option.value
|
||||||
|
? 'border-primary bg-primary/5 ring-1 ring-primary/20'
|
||||||
|
: 'border-border hover:border-border/80 hover:bg-accent/50'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<div className={`shrink-0 w-10 h-10 rounded-lg flex items-center justify-center ${formData.visibility === option.value
|
||||||
|
? 'bg-primary text-primary-foreground'
|
||||||
|
: 'bg-muted text-muted-foreground'
|
||||||
|
}`}>
|
||||||
|
{option.icon}
|
||||||
|
</div>
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<div className="font-medium">{option.label}</div>
|
||||||
|
<div className="text-sm text-muted-foreground">
|
||||||
|
{option.description}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className={`shrink-0 w-5 h-5 rounded-full border-2 flex items-center justify-center ${formData.visibility === option.value
|
||||||
|
? 'border-primary bg-primary'
|
||||||
|
: 'border-muted-foreground/30'
|
||||||
|
}`}>
|
||||||
|
{formData.visibility === option.value && (
|
||||||
|
<div className="w-2 h-2 rounded-full bg-primary-foreground" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="p-4 rounded-lg bg-destructive/10 border border-destructive/20 text-destructive text-sm">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="flex gap-3 pt-4">
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
disabled={isLoading || !formData.name.trim() || !formData.workspaceId}
|
||||||
|
className="flex-1 h-11"
|
||||||
|
>
|
||||||
|
{isLoading && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
||||||
|
Create Project
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => router.back()}
|
||||||
|
className="h-11 px-6"
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,282 +1,7 @@
|
|||||||
'use client';
|
import { requireAuthOrRedirect } from '@/lib/route-access';
|
||||||
|
import NewProjectPageClient from './new-project-page-client';
|
||||||
|
|
||||||
import { useState, useEffect } from 'react';
|
export default async function NewProjectPage() {
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
await requireAuthOrRedirect();
|
||||||
import Link from 'next/link';
|
return <NewProjectPageClient />;
|
||||||
import { ArrowLeft, Loader2, Globe, Lock, UserPlus, FolderPlus, Building2 } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
|
||||||
import {
|
|
||||||
Select,
|
|
||||||
SelectContent,
|
|
||||||
SelectItem,
|
|
||||||
SelectTrigger,
|
|
||||||
SelectValue,
|
|
||||||
} from '@/components/ui/select';
|
|
||||||
|
|
||||||
type Visibility = 'PRIVATE' | 'INVITE' | 'PUBLIC';
|
|
||||||
|
|
||||||
interface Workspace {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const visibilityOptions: { value: Visibility; label: string; description: string; icon: React.ReactNode }[] = [
|
|
||||||
{
|
|
||||||
value: 'PRIVATE',
|
|
||||||
label: 'Private',
|
|
||||||
description: 'Only workspace members and project members can access',
|
|
||||||
icon: <Lock className="h-5 w-5" />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: 'INVITE',
|
|
||||||
label: 'Invite Only',
|
|
||||||
description: 'Share with specific people via email',
|
|
||||||
icon: <UserPlus className="h-5 w-5" />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: 'PUBLIC',
|
|
||||||
label: 'Public',
|
|
||||||
description: 'Anyone with the link can view',
|
|
||||||
icon: <Globe className="h-5 w-5" />,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export default function NewProjectPage() {
|
|
||||||
const router = useRouter();
|
|
||||||
const searchParams = useSearchParams();
|
|
||||||
const preselectedWorkspace = searchParams.get('workspace');
|
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
const [error, setError] = useState('');
|
|
||||||
const [workspaces, setWorkspaces] = useState<Workspace[]>([]);
|
|
||||||
const [isLoadingWorkspaces, setIsLoadingWorkspaces] = useState(true);
|
|
||||||
const [formData, setFormData] = useState({
|
|
||||||
name: '',
|
|
||||||
description: '',
|
|
||||||
visibility: 'PRIVATE' as Visibility,
|
|
||||||
workspaceId: preselectedWorkspace || '',
|
|
||||||
});
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
async function fetchWorkspaces() {
|
|
||||||
try {
|
|
||||||
const res = await fetch('/api/workspaces');
|
|
||||||
if (res.ok) {
|
|
||||||
const data = await res.json();
|
|
||||||
const workspacesData = data.data.workspaces || [];
|
|
||||||
setWorkspaces(workspacesData);
|
|
||||||
// Auto-select if only one workspace and none preselected
|
|
||||||
if (!preselectedWorkspace && workspacesData.length === 1) {
|
|
||||||
setFormData(prev => ({ ...prev, workspaceId: workspacesData[0].id }));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
} finally {
|
|
||||||
setIsLoadingWorkspaces(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fetchWorkspaces();
|
|
||||||
}, [preselectedWorkspace]);
|
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
if (!formData.workspaceId) {
|
|
||||||
setError('Please select a workspace');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setIsLoading(true);
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch('/api/projects', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(formData),
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
setError(data.error || 'Failed to create project');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
router.push(`/projects/${data.data.id}`);
|
|
||||||
} catch {
|
|
||||||
setError('Something went wrong. Please try again.');
|
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
|
||||||
<div className="w-full max-w-xl">
|
|
||||||
<div className="mb-8">
|
|
||||||
<Link
|
|
||||||
href="/dashboard"
|
|
||||||
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4 mr-1" />
|
|
||||||
Back to Projects
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader className="text-center pb-2">
|
|
||||||
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
|
||||||
<FolderPlus className="h-7 w-7 text-primary" />
|
|
||||||
</div>
|
|
||||||
<CardTitle className="text-2xl">Create New Project</CardTitle>
|
|
||||||
<CardDescription className="text-base">
|
|
||||||
Set up a new project to organize your videos and collect feedback
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="pt-6">
|
|
||||||
<form onSubmit={handleSubmit} className="space-y-6">
|
|
||||||
{/* Workspace selector */}
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label className="text-sm font-medium">Workspace</Label>
|
|
||||||
{isLoadingWorkspaces ? (
|
|
||||||
<div className="flex items-center gap-2 text-sm text-muted-foreground py-2">
|
|
||||||
<Loader2 className="h-4 w-4 animate-spin" />
|
|
||||||
Loading workspaces...
|
|
||||||
</div>
|
|
||||||
) : workspaces.length === 0 ? (
|
|
||||||
<div className="rounded-lg border border-dashed p-4 text-center">
|
|
||||||
<Building2 className="h-8 w-8 mx-auto text-muted-foreground mb-2" />
|
|
||||||
<p className="text-sm text-muted-foreground mb-2">
|
|
||||||
You need a workspace first. Every project belongs to a workspace.
|
|
||||||
</p>
|
|
||||||
<Button asChild size="sm" variant="outline">
|
|
||||||
<Link href="/workspaces/new">Create Workspace</Link>
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<Select
|
|
||||||
value={formData.workspaceId}
|
|
||||||
onValueChange={(v) => setFormData(prev => ({ ...prev, workspaceId: v }))}
|
|
||||||
>
|
|
||||||
<SelectTrigger className="h-11">
|
|
||||||
<SelectValue placeholder="Select a workspace" />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
{workspaces.map((ws) => (
|
|
||||||
<SelectItem key={ws.id} value={ws.id}>
|
|
||||||
<span className="flex items-center gap-2">
|
|
||||||
<Building2 className="h-4 w-4" />
|
|
||||||
{ws.name}
|
|
||||||
</span>
|
|
||||||
</SelectItem>
|
|
||||||
))}
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="name" className="text-sm font-medium">
|
|
||||||
Project Name
|
|
||||||
</Label>
|
|
||||||
<Input
|
|
||||||
id="name"
|
|
||||||
placeholder="e.g. Product Demo Q1"
|
|
||||||
value={formData.name}
|
|
||||||
onChange={(e) => setFormData(prev => ({ ...prev, name: e.target.value }))}
|
|
||||||
required
|
|
||||||
disabled={isLoading}
|
|
||||||
className="h-11"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="description" className="text-sm font-medium">
|
|
||||||
Description
|
|
||||||
<span className="text-muted-foreground font-normal ml-1">(optional)</span>
|
|
||||||
</Label>
|
|
||||||
<Textarea
|
|
||||||
id="description"
|
|
||||||
placeholder="Brief description of what this project is about..."
|
|
||||||
value={formData.description}
|
|
||||||
onChange={(e) => setFormData(prev => ({ ...prev, description: e.target.value }))}
|
|
||||||
rows={3}
|
|
||||||
disabled={isLoading}
|
|
||||||
className="resize-none"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-3">
|
|
||||||
<Label className="text-sm font-medium">Who can access?</Label>
|
|
||||||
<div className="grid gap-3">
|
|
||||||
{visibilityOptions.map((option) => (
|
|
||||||
<button
|
|
||||||
key={option.value}
|
|
||||||
type="button"
|
|
||||||
onClick={() => setFormData(prev => ({ ...prev, visibility: option.value }))}
|
|
||||||
disabled={isLoading}
|
|
||||||
className={`w-full flex items-center gap-4 p-4 rounded-xl border-2 text-left transition-all ${formData.visibility === option.value
|
|
||||||
? 'border-primary bg-primary/5 ring-1 ring-primary/20'
|
|
||||||
: 'border-border hover:border-border/80 hover:bg-accent/50'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<div className={`shrink-0 w-10 h-10 rounded-lg flex items-center justify-center ${formData.visibility === option.value
|
|
||||||
? 'bg-primary text-primary-foreground'
|
|
||||||
: 'bg-muted text-muted-foreground'
|
|
||||||
}`}>
|
|
||||||
{option.icon}
|
|
||||||
</div>
|
|
||||||
<div className="flex-1 min-w-0">
|
|
||||||
<div className="font-medium">{option.label}</div>
|
|
||||||
<div className="text-sm text-muted-foreground">
|
|
||||||
{option.description}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className={`shrink-0 w-5 h-5 rounded-full border-2 flex items-center justify-center ${formData.visibility === option.value
|
|
||||||
? 'border-primary bg-primary'
|
|
||||||
: 'border-muted-foreground/30'
|
|
||||||
}`}>
|
|
||||||
{formData.visibility === option.value && (
|
|
||||||
<div className="w-2 h-2 rounded-full bg-primary-foreground" />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{error && (
|
|
||||||
<div className="p-4 rounded-lg bg-destructive/10 border border-destructive/20 text-destructive text-sm">
|
|
||||||
{error}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<div className="flex gap-3 pt-4">
|
|
||||||
<Button
|
|
||||||
type="submit"
|
|
||||||
disabled={isLoading || !formData.name.trim() || !formData.workspaceId}
|
|
||||||
className="flex-1 h-11"
|
|
||||||
>
|
|
||||||
{isLoading && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
|
||||||
Create Project
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
variant="outline"
|
|
||||||
onClick={() => router.back()}
|
|
||||||
className="h-11 px-6"
|
|
||||||
>
|
|
||||||
Cancel
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,513 +1,7 @@
|
|||||||
'use client';
|
import { requireAuthOrRedirect } from '@/lib/route-access';
|
||||||
|
import SettingsPageClient from './settings-page-client';
|
||||||
|
|
||||||
import { useState, useEffect, useCallback } from 'react';
|
export default async function SettingsPage() {
|
||||||
import { Bell, Send, Mail, CheckCircle2, AlertCircle, Loader2, Globe } from 'lucide-react';
|
await requireAuthOrRedirect();
|
||||||
import { Button } from '@/components/ui/button';
|
return <SettingsPageClient />;
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Separator } from '@/components/ui/separator';
|
|
||||||
import { Skeleton } from '@/components/ui/skeleton';
|
|
||||||
import { Badge } from '@/components/ui/badge';
|
|
||||||
import {
|
|
||||||
Select,
|
|
||||||
SelectContent,
|
|
||||||
SelectGroup,
|
|
||||||
SelectItem,
|
|
||||||
SelectLabel,
|
|
||||||
SelectTrigger,
|
|
||||||
SelectValue,
|
|
||||||
} from '@/components/ui/select';
|
|
||||||
import { cn } from '@/lib/utils';
|
|
||||||
|
|
||||||
interface NotificationSettings {
|
|
||||||
telegramBotToken: string | null;
|
|
||||||
telegramChatId: string | null;
|
|
||||||
telegramEnabled: boolean;
|
|
||||||
emailEnabled: boolean;
|
|
||||||
onNewVideo: boolean;
|
|
||||||
onNewVersion: boolean;
|
|
||||||
onNewComment: boolean;
|
|
||||||
onNewReply: boolean;
|
|
||||||
onApprovalEvents: boolean;
|
|
||||||
timezone: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
function ToggleButton({
|
|
||||||
enabled,
|
|
||||||
onToggle,
|
|
||||||
label,
|
|
||||||
description,
|
|
||||||
}: {
|
|
||||||
enabled: boolean;
|
|
||||||
onToggle: () => void;
|
|
||||||
label: string;
|
|
||||||
description?: string;
|
|
||||||
}) {
|
|
||||||
return (
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={onToggle}
|
|
||||||
className={cn(
|
|
||||||
'flex items-center justify-between w-full p-3 rounded-lg border transition-colors text-left',
|
|
||||||
enabled
|
|
||||||
? 'border-primary/50 bg-primary/5'
|
|
||||||
: 'border-border hover:bg-accent/50'
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<div className="flex-1 min-w-0 pr-4">
|
|
||||||
<span className="text-sm font-medium">{label}</span>
|
|
||||||
{description && (
|
|
||||||
<p className="text-xs text-muted-foreground mt-0.5">{description}</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
className={cn(
|
|
||||||
'w-10 h-6 shrink-0 rounded-full relative transition-colors',
|
|
||||||
enabled ? 'bg-primary' : 'bg-muted'
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
className={cn(
|
|
||||||
'absolute top-1 w-4 h-4 rounded-full bg-white transition-transform',
|
|
||||||
enabled ? 'translate-x-5' : 'translate-x-1'
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</button>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function SettingsPage() {
|
|
||||||
const [settings, setSettings] = useState<NotificationSettings>({
|
|
||||||
telegramBotToken: null,
|
|
||||||
telegramChatId: null,
|
|
||||||
telegramEnabled: false,
|
|
||||||
emailEnabled: false,
|
|
||||||
onNewVideo: true,
|
|
||||||
onNewVersion: true,
|
|
||||||
onNewComment: true,
|
|
||||||
onNewReply: true,
|
|
||||||
onApprovalEvents: true,
|
|
||||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC',
|
|
||||||
});
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [saving, setSaving] = useState(false);
|
|
||||||
const [testing, setTesting] = useState<string | null>(null);
|
|
||||||
const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null);
|
|
||||||
|
|
||||||
// Form state for Telegram fields (separate from saved settings for editing)
|
|
||||||
const [telegramToken, setTelegramToken] = useState('');
|
|
||||||
const [telegramChatId, setTelegramChatId] = useState('');
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
async function fetchSettings() {
|
|
||||||
try {
|
|
||||||
const res = await fetch('/api/settings/notifications');
|
|
||||||
if (res.ok) {
|
|
||||||
const data = await res.json();
|
|
||||||
setSettings(data.data);
|
|
||||||
setTelegramToken(data.data.telegramBotToken || '');
|
|
||||||
setTelegramChatId(data.data.telegramChatId || '');
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
console.error('Failed to fetch notification settings');
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fetchSettings();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const showMessage = useCallback((type: 'success' | 'error', text: string) => {
|
|
||||||
setMessage({ type, text });
|
|
||||||
setTimeout(() => setMessage(null), 4000);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleSave = useCallback(async () => {
|
|
||||||
setSaving(true);
|
|
||||||
try {
|
|
||||||
const res = await fetch('/api/settings/notifications', {
|
|
||||||
method: 'PUT',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
...settings,
|
|
||||||
telegramBotToken: telegramToken || null,
|
|
||||||
telegramChatId: telegramChatId || null,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
const data = await res.json();
|
|
||||||
setSettings(data.data);
|
|
||||||
showMessage('success', 'Settings saved');
|
|
||||||
} else {
|
|
||||||
const data = await res.json();
|
|
||||||
showMessage('error', data.error || 'Failed to save');
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
showMessage('error', 'Failed to save settings');
|
|
||||||
} finally {
|
|
||||||
setSaving(false);
|
|
||||||
}
|
|
||||||
}, [settings, telegramToken, telegramChatId, showMessage]);
|
|
||||||
|
|
||||||
const handleTest = useCallback(
|
|
||||||
async (channel: 'telegram' | 'email') => {
|
|
||||||
setTesting(channel);
|
|
||||||
try {
|
|
||||||
const res = await fetch('/api/settings/notifications', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
channel,
|
|
||||||
telegramBotToken: telegramToken,
|
|
||||||
telegramChatId,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
const data = await res.json();
|
|
||||||
if (res.ok) {
|
|
||||||
showMessage('success', data.data.message);
|
|
||||||
} else {
|
|
||||||
showMessage('error', data.error || 'Test failed');
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
showMessage('error', 'Test failed');
|
|
||||||
} finally {
|
|
||||||
setTesting(null);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[telegramToken, telegramChatId, showMessage]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (loading) {
|
|
||||||
return (
|
|
||||||
<div className="max-w-2xl mx-auto py-8 px-4 space-y-6">
|
|
||||||
<div>
|
|
||||||
<Skeleton className="h-9 w-56" />
|
|
||||||
<Skeleton className="h-4 w-80 mt-2" />
|
|
||||||
</div>
|
|
||||||
{Array.from({ length: 4 }).map((_, i) => (
|
|
||||||
<Card key={i}>
|
|
||||||
<CardHeader>
|
|
||||||
<Skeleton className="h-5 w-40" />
|
|
||||||
<Skeleton className="h-4 w-64 mt-1" />
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
{Array.from({ length: 3 }).map((_, j) => (
|
|
||||||
<div key={j} className="flex items-center justify-between">
|
|
||||||
<div className="space-y-1">
|
|
||||||
<Skeleton className="h-4 w-32" />
|
|
||||||
<Skeleton className="h-3 w-48" />
|
|
||||||
</div>
|
|
||||||
<Skeleton className="h-5 w-10 rounded-full" />
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
))}
|
|
||||||
<div className="flex justify-end">
|
|
||||||
<Skeleton className="h-10 w-32 rounded-md" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="max-w-2xl mx-auto py-8 px-4">
|
|
||||||
<div className="mb-8">
|
|
||||||
<h1 className="text-2xl font-bold tracking-tight">Settings</h1>
|
|
||||||
<p className="text-muted-foreground mt-1">
|
|
||||||
Manage your notification preferences
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Status message */}
|
|
||||||
{message && (
|
|
||||||
<div
|
|
||||||
className={cn(
|
|
||||||
'flex items-center gap-2 p-3 rounded-lg mb-6 text-sm',
|
|
||||||
message.type === 'success'
|
|
||||||
? 'bg-green-500/10 text-green-700 dark:text-green-400'
|
|
||||||
: 'bg-destructive/10 text-destructive'
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{message.type === 'success' ? (
|
|
||||||
<CheckCircle2 className="h-4 w-4 shrink-0" />
|
|
||||||
) : (
|
|
||||||
<AlertCircle className="h-4 w-4 shrink-0" />
|
|
||||||
)}
|
|
||||||
{message.text}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Event Subscriptions */}
|
|
||||||
<Card className="mb-6">
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle className="flex items-center gap-2">
|
|
||||||
<Bell className="h-5 w-5" />
|
|
||||||
Notification Events
|
|
||||||
</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Choose which events trigger notifications
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-2">
|
|
||||||
<ToggleButton
|
|
||||||
enabled={settings.onNewVideo}
|
|
||||||
onToggle={() =>
|
|
||||||
setSettings((s) => ({ ...s, onNewVideo: !s.onNewVideo }))
|
|
||||||
}
|
|
||||||
label="New Video Added"
|
|
||||||
description="When a new video is added to one of your projects"
|
|
||||||
/>
|
|
||||||
<ToggleButton
|
|
||||||
enabled={settings.onNewVersion}
|
|
||||||
onToggle={() =>
|
|
||||||
setSettings((s) => ({ ...s, onNewVersion: !s.onNewVersion }))
|
|
||||||
}
|
|
||||||
label="New Version Added"
|
|
||||||
description="When a new version is added to an existing video"
|
|
||||||
/>
|
|
||||||
<ToggleButton
|
|
||||||
enabled={settings.onNewComment}
|
|
||||||
onToggle={() =>
|
|
||||||
setSettings((s) => ({ ...s, onNewComment: !s.onNewComment }))
|
|
||||||
}
|
|
||||||
label="New Comment"
|
|
||||||
description="When someone leaves a comment on your videos"
|
|
||||||
/>
|
|
||||||
<ToggleButton
|
|
||||||
enabled={settings.onNewReply}
|
|
||||||
onToggle={() =>
|
|
||||||
setSettings((s) => ({ ...s, onNewReply: !s.onNewReply }))
|
|
||||||
}
|
|
||||||
label="New Reply"
|
|
||||||
description="When someone replies to a comment thread"
|
|
||||||
/>
|
|
||||||
<ToggleButton
|
|
||||||
enabled={settings.onApprovalEvents}
|
|
||||||
onToggle={() =>
|
|
||||||
setSettings((s) => ({ ...s, onApprovalEvents: !s.onApprovalEvents }))
|
|
||||||
}
|
|
||||||
label="Approval Workflow"
|
|
||||||
description="When approval requests are created, responded to, or finalized"
|
|
||||||
/>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* Telegram */}
|
|
||||||
<Card className="mb-6">
|
|
||||||
<CardHeader>
|
|
||||||
<div className="flex items-center justify-between">
|
|
||||||
<CardTitle className="flex items-center gap-2">
|
|
||||||
<Send className="h-5 w-5" />
|
|
||||||
Telegram
|
|
||||||
</CardTitle>
|
|
||||||
<Badge variant={settings.telegramEnabled ? 'default' : 'secondary'}>
|
|
||||||
{settings.telegramEnabled ? 'Enabled' : 'Disabled'}
|
|
||||||
</Badge>
|
|
||||||
</div>
|
|
||||||
<CardDescription>
|
|
||||||
Get instant notifications via a Telegram bot
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
<div className="space-y-3">
|
|
||||||
<div>
|
|
||||||
<Label htmlFor="telegram-token">Bot Token</Label>
|
|
||||||
<Input
|
|
||||||
id="telegram-token"
|
|
||||||
type="password"
|
|
||||||
placeholder="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
|
|
||||||
value={telegramToken}
|
|
||||||
onChange={(e) => setTelegramToken(e.target.value)}
|
|
||||||
className="mt-1 font-mono text-sm"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<Label htmlFor="telegram-chat-id">Chat ID</Label>
|
|
||||||
<Input
|
|
||||||
id="telegram-chat-id"
|
|
||||||
placeholder="-1001234567890"
|
|
||||||
value={telegramChatId}
|
|
||||||
onChange={(e) => setTelegramChatId(e.target.value)}
|
|
||||||
className="mt-1 font-mono text-sm"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<ToggleButton
|
|
||||||
enabled={settings.telegramEnabled}
|
|
||||||
onToggle={() =>
|
|
||||||
setSettings((s) => ({ ...s, telegramEnabled: !s.telegramEnabled }))
|
|
||||||
}
|
|
||||||
label="Enable Telegram notifications"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
onClick={() => handleTest('telegram')}
|
|
||||||
disabled={!telegramToken || !telegramChatId || testing === 'telegram'}
|
|
||||||
>
|
|
||||||
{testing === 'telegram' ? (
|
|
||||||
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
|
||||||
) : (
|
|
||||||
<Send className="h-4 w-4 mr-2" />
|
|
||||||
)}
|
|
||||||
Send Test Message
|
|
||||||
</Button>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* Email */}
|
|
||||||
<Card className="mb-6">
|
|
||||||
<CardHeader>
|
|
||||||
<div className="flex items-center justify-between">
|
|
||||||
<CardTitle className="flex items-center gap-2">
|
|
||||||
<Mail className="h-5 w-5" />
|
|
||||||
Email
|
|
||||||
</CardTitle>
|
|
||||||
<Badge variant={settings.emailEnabled ? 'default' : 'secondary'}>
|
|
||||||
{settings.emailEnabled ? 'Enabled' : 'Disabled'}
|
|
||||||
</Badge>
|
|
||||||
</div>
|
|
||||||
<CardDescription>
|
|
||||||
Receive notification emails to your account email address
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
<ToggleButton
|
|
||||||
enabled={settings.emailEnabled}
|
|
||||||
onToggle={() =>
|
|
||||||
setSettings((s) => ({ ...s, emailEnabled: !s.emailEnabled }))
|
|
||||||
}
|
|
||||||
label="Enable email notifications"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
onClick={() => handleTest('email')}
|
|
||||||
disabled={!settings.emailEnabled || testing === 'email'}
|
|
||||||
>
|
|
||||||
{testing === 'email' ? (
|
|
||||||
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
|
||||||
) : (
|
|
||||||
<Mail className="h-4 w-4 mr-2" />
|
|
||||||
)}
|
|
||||||
Send Test Email
|
|
||||||
</Button>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* Timezone */}
|
|
||||||
<Card className="mb-6">
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle className="flex items-center gap-2">
|
|
||||||
<Globe className="h-5 w-5" />
|
|
||||||
Timezone
|
|
||||||
</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Timestamps in notifications will use this timezone
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<Select
|
|
||||||
value={settings.timezone}
|
|
||||||
onValueChange={(value) =>
|
|
||||||
setSettings((s) => ({ ...s, timezone: value }))
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<SelectTrigger className="w-full">
|
|
||||||
<SelectValue placeholder="Select timezone" />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
<SelectGroup>
|
|
||||||
<SelectLabel>Americas</SelectLabel>
|
|
||||||
<SelectItem value="America/New_York">Eastern Time (New York)</SelectItem>
|
|
||||||
<SelectItem value="America/Chicago">Central Time (Chicago)</SelectItem>
|
|
||||||
<SelectItem value="America/Denver">Mountain Time (Denver)</SelectItem>
|
|
||||||
<SelectItem value="America/Los_Angeles">Pacific Time (Los Angeles)</SelectItem>
|
|
||||||
<SelectItem value="America/Anchorage">Alaska (Anchorage)</SelectItem>
|
|
||||||
<SelectItem value="Pacific/Honolulu">Hawaii (Honolulu)</SelectItem>
|
|
||||||
<SelectItem value="America/Toronto">Toronto</SelectItem>
|
|
||||||
<SelectItem value="America/Vancouver">Vancouver</SelectItem>
|
|
||||||
<SelectItem value="America/Mexico_City">Mexico City</SelectItem>
|
|
||||||
<SelectItem value="America/Sao_Paulo">São Paulo</SelectItem>
|
|
||||||
<SelectItem value="America/Argentina/Buenos_Aires">Buenos Aires</SelectItem>
|
|
||||||
<SelectItem value="America/Bogota">Bogotá</SelectItem>
|
|
||||||
</SelectGroup>
|
|
||||||
<SelectGroup>
|
|
||||||
<SelectLabel>Europe</SelectLabel>
|
|
||||||
<SelectItem value="Europe/London">London (GMT/BST)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Paris">Paris (CET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Berlin">Berlin (CET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Amsterdam">Amsterdam (CET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Madrid">Madrid (CET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Rome">Rome (CET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Zurich">Zurich (CET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Stockholm">Stockholm (CET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Helsinki">Helsinki (EET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Athens">Athens (EET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Istanbul">Istanbul (TRT)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Moscow">Moscow (MSK)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Kiev">Kyiv (EET)</SelectItem>
|
|
||||||
<SelectItem value="Europe/Warsaw">Warsaw (CET)</SelectItem>
|
|
||||||
</SelectGroup>
|
|
||||||
<SelectGroup>
|
|
||||||
<SelectLabel>Asia & Pacific</SelectLabel>
|
|
||||||
<SelectItem value="Asia/Dubai">Dubai (GST)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Kolkata">India (IST)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Bangkok">Bangkok (ICT)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Singapore">Singapore (SGT)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Hong_Kong">Hong Kong (HKT)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Shanghai">Shanghai (CST)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Tokyo">Tokyo (JST)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Seoul">Seoul (KST)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Taipei">Taipei (CST)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Jakarta">Jakarta (WIB)</SelectItem>
|
|
||||||
<SelectItem value="Australia/Sydney">Sydney (AEST)</SelectItem>
|
|
||||||
<SelectItem value="Australia/Melbourne">Melbourne (AEST)</SelectItem>
|
|
||||||
<SelectItem value="Australia/Perth">Perth (AWST)</SelectItem>
|
|
||||||
<SelectItem value="Pacific/Auckland">Auckland (NZST)</SelectItem>
|
|
||||||
</SelectGroup>
|
|
||||||
<SelectGroup>
|
|
||||||
<SelectLabel>Africa & Middle East</SelectLabel>
|
|
||||||
<SelectItem value="Africa/Cairo">Cairo (EET)</SelectItem>
|
|
||||||
<SelectItem value="Africa/Lagos">Lagos (WAT)</SelectItem>
|
|
||||||
<SelectItem value="Africa/Johannesburg">Johannesburg (SAST)</SelectItem>
|
|
||||||
<SelectItem value="Africa/Nairobi">Nairobi (EAT)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Riyadh">Riyadh (AST)</SelectItem>
|
|
||||||
<SelectItem value="Asia/Tehran">Tehran (IRST)</SelectItem>
|
|
||||||
</SelectGroup>
|
|
||||||
<SelectGroup>
|
|
||||||
<SelectLabel>Other</SelectLabel>
|
|
||||||
<SelectItem value="UTC">UTC</SelectItem>
|
|
||||||
</SelectGroup>
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
<Separator className="my-6" />
|
|
||||||
|
|
||||||
{/* Save button */}
|
|
||||||
<div className="flex justify-end">
|
|
||||||
<Button onClick={handleSave} disabled={saving}>
|
|
||||||
{saving ? (
|
|
||||||
<>
|
|
||||||
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
|
||||||
Saving...
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
'Save Settings'
|
|
||||||
)}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,513 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect, useCallback } from 'react';
|
||||||
|
import { Bell, Send, Mail, CheckCircle2, AlertCircle, Loader2, Globe } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Separator } from '@/components/ui/separator';
|
||||||
|
import { Skeleton } from '@/components/ui/skeleton';
|
||||||
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectGroup,
|
||||||
|
SelectItem,
|
||||||
|
SelectLabel,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from '@/components/ui/select';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
|
interface NotificationSettings {
|
||||||
|
telegramBotToken: string | null;
|
||||||
|
telegramChatId: string | null;
|
||||||
|
telegramEnabled: boolean;
|
||||||
|
emailEnabled: boolean;
|
||||||
|
onNewVideo: boolean;
|
||||||
|
onNewVersion: boolean;
|
||||||
|
onNewComment: boolean;
|
||||||
|
onNewReply: boolean;
|
||||||
|
onApprovalEvents: boolean;
|
||||||
|
timezone: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ToggleButton({
|
||||||
|
enabled,
|
||||||
|
onToggle,
|
||||||
|
label,
|
||||||
|
description,
|
||||||
|
}: {
|
||||||
|
enabled: boolean;
|
||||||
|
onToggle: () => void;
|
||||||
|
label: string;
|
||||||
|
description?: string;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onToggle}
|
||||||
|
className={cn(
|
||||||
|
'flex items-center justify-between w-full p-3 rounded-lg border transition-colors text-left',
|
||||||
|
enabled
|
||||||
|
? 'border-primary/50 bg-primary/5'
|
||||||
|
: 'border-border hover:bg-accent/50'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="flex-1 min-w-0 pr-4">
|
||||||
|
<span className="text-sm font-medium">{label}</span>
|
||||||
|
{description && (
|
||||||
|
<p className="text-xs text-muted-foreground mt-0.5">{description}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
'w-10 h-6 shrink-0 rounded-full relative transition-colors',
|
||||||
|
enabled ? 'bg-primary' : 'bg-muted'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
'absolute top-1 w-4 h-4 rounded-full bg-white transition-transform',
|
||||||
|
enabled ? 'translate-x-5' : 'translate-x-1'
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function SettingsPage() {
|
||||||
|
const [settings, setSettings] = useState<NotificationSettings>({
|
||||||
|
telegramBotToken: null,
|
||||||
|
telegramChatId: null,
|
||||||
|
telegramEnabled: false,
|
||||||
|
emailEnabled: false,
|
||||||
|
onNewVideo: true,
|
||||||
|
onNewVersion: true,
|
||||||
|
onNewComment: true,
|
||||||
|
onNewReply: true,
|
||||||
|
onApprovalEvents: true,
|
||||||
|
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC',
|
||||||
|
});
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [testing, setTesting] = useState<string | null>(null);
|
||||||
|
const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null);
|
||||||
|
|
||||||
|
// Form state for Telegram fields (separate from saved settings for editing)
|
||||||
|
const [telegramToken, setTelegramToken] = useState('');
|
||||||
|
const [telegramChatId, setTelegramChatId] = useState('');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
async function fetchSettings() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/settings/notifications');
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
setSettings(data.data);
|
||||||
|
setTelegramToken(data.data.telegramBotToken || '');
|
||||||
|
setTelegramChatId(data.data.telegramChatId || '');
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
console.error('Failed to fetch notification settings');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fetchSettings();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const showMessage = useCallback((type: 'success' | 'error', text: string) => {
|
||||||
|
setMessage({ type, text });
|
||||||
|
setTimeout(() => setMessage(null), 4000);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleSave = useCallback(async () => {
|
||||||
|
setSaving(true);
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/settings/notifications', {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
...settings,
|
||||||
|
telegramBotToken: telegramToken || null,
|
||||||
|
telegramChatId: telegramChatId || null,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
setSettings(data.data);
|
||||||
|
showMessage('success', 'Settings saved');
|
||||||
|
} else {
|
||||||
|
const data = await res.json();
|
||||||
|
showMessage('error', data.error || 'Failed to save');
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
showMessage('error', 'Failed to save settings');
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
}, [settings, telegramToken, telegramChatId, showMessage]);
|
||||||
|
|
||||||
|
const handleTest = useCallback(
|
||||||
|
async (channel: 'telegram' | 'email') => {
|
||||||
|
setTesting(channel);
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/settings/notifications', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
channel,
|
||||||
|
telegramBotToken: telegramToken,
|
||||||
|
telegramChatId,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
if (res.ok) {
|
||||||
|
showMessage('success', data.data.message);
|
||||||
|
} else {
|
||||||
|
showMessage('error', data.error || 'Test failed');
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
showMessage('error', 'Test failed');
|
||||||
|
} finally {
|
||||||
|
setTesting(null);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[telegramToken, telegramChatId, showMessage]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
return (
|
||||||
|
<div className="max-w-2xl mx-auto py-8 px-4 space-y-6">
|
||||||
|
<div>
|
||||||
|
<Skeleton className="h-9 w-56" />
|
||||||
|
<Skeleton className="h-4 w-80 mt-2" />
|
||||||
|
</div>
|
||||||
|
{Array.from({ length: 4 }).map((_, i) => (
|
||||||
|
<Card key={i}>
|
||||||
|
<CardHeader>
|
||||||
|
<Skeleton className="h-5 w-40" />
|
||||||
|
<Skeleton className="h-4 w-64 mt-1" />
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
{Array.from({ length: 3 }).map((_, j) => (
|
||||||
|
<div key={j} className="flex items-center justify-between">
|
||||||
|
<div className="space-y-1">
|
||||||
|
<Skeleton className="h-4 w-32" />
|
||||||
|
<Skeleton className="h-3 w-48" />
|
||||||
|
</div>
|
||||||
|
<Skeleton className="h-5 w-10 rounded-full" />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
))}
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<Skeleton className="h-10 w-32 rounded-md" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="max-w-2xl mx-auto py-8 px-4">
|
||||||
|
<div className="mb-8">
|
||||||
|
<h1 className="text-2xl font-bold tracking-tight">Settings</h1>
|
||||||
|
<p className="text-muted-foreground mt-1">
|
||||||
|
Manage your notification preferences
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Status message */}
|
||||||
|
{message && (
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
'flex items-center gap-2 p-3 rounded-lg mb-6 text-sm',
|
||||||
|
message.type === 'success'
|
||||||
|
? 'bg-green-500/10 text-green-700 dark:text-green-400'
|
||||||
|
: 'bg-destructive/10 text-destructive'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{message.type === 'success' ? (
|
||||||
|
<CheckCircle2 className="h-4 w-4 shrink-0" />
|
||||||
|
) : (
|
||||||
|
<AlertCircle className="h-4 w-4 shrink-0" />
|
||||||
|
)}
|
||||||
|
{message.text}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Event Subscriptions */}
|
||||||
|
<Card className="mb-6">
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="flex items-center gap-2">
|
||||||
|
<Bell className="h-5 w-5" />
|
||||||
|
Notification Events
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Choose which events trigger notifications
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-2">
|
||||||
|
<ToggleButton
|
||||||
|
enabled={settings.onNewVideo}
|
||||||
|
onToggle={() =>
|
||||||
|
setSettings((s) => ({ ...s, onNewVideo: !s.onNewVideo }))
|
||||||
|
}
|
||||||
|
label="New Video Added"
|
||||||
|
description="When a new video is added to one of your projects"
|
||||||
|
/>
|
||||||
|
<ToggleButton
|
||||||
|
enabled={settings.onNewVersion}
|
||||||
|
onToggle={() =>
|
||||||
|
setSettings((s) => ({ ...s, onNewVersion: !s.onNewVersion }))
|
||||||
|
}
|
||||||
|
label="New Version Added"
|
||||||
|
description="When a new version is added to an existing video"
|
||||||
|
/>
|
||||||
|
<ToggleButton
|
||||||
|
enabled={settings.onNewComment}
|
||||||
|
onToggle={() =>
|
||||||
|
setSettings((s) => ({ ...s, onNewComment: !s.onNewComment }))
|
||||||
|
}
|
||||||
|
label="New Comment"
|
||||||
|
description="When someone leaves a comment on your videos"
|
||||||
|
/>
|
||||||
|
<ToggleButton
|
||||||
|
enabled={settings.onNewReply}
|
||||||
|
onToggle={() =>
|
||||||
|
setSettings((s) => ({ ...s, onNewReply: !s.onNewReply }))
|
||||||
|
}
|
||||||
|
label="New Reply"
|
||||||
|
description="When someone replies to a comment thread"
|
||||||
|
/>
|
||||||
|
<ToggleButton
|
||||||
|
enabled={settings.onApprovalEvents}
|
||||||
|
onToggle={() =>
|
||||||
|
setSettings((s) => ({ ...s, onApprovalEvents: !s.onApprovalEvents }))
|
||||||
|
}
|
||||||
|
label="Approval Workflow"
|
||||||
|
description="When approval requests are created, responded to, or finalized"
|
||||||
|
/>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Telegram */}
|
||||||
|
<Card className="mb-6">
|
||||||
|
<CardHeader>
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<CardTitle className="flex items-center gap-2">
|
||||||
|
<Send className="h-5 w-5" />
|
||||||
|
Telegram
|
||||||
|
</CardTitle>
|
||||||
|
<Badge variant={settings.telegramEnabled ? 'default' : 'secondary'}>
|
||||||
|
{settings.telegramEnabled ? 'Enabled' : 'Disabled'}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
<CardDescription>
|
||||||
|
Get instant notifications via a Telegram bot
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div>
|
||||||
|
<Label htmlFor="telegram-token">Bot Token</Label>
|
||||||
|
<Input
|
||||||
|
id="telegram-token"
|
||||||
|
type="password"
|
||||||
|
placeholder="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
|
||||||
|
value={telegramToken}
|
||||||
|
onChange={(e) => setTelegramToken(e.target.value)}
|
||||||
|
className="mt-1 font-mono text-sm"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Label htmlFor="telegram-chat-id">Chat ID</Label>
|
||||||
|
<Input
|
||||||
|
id="telegram-chat-id"
|
||||||
|
placeholder="-1001234567890"
|
||||||
|
value={telegramChatId}
|
||||||
|
onChange={(e) => setTelegramChatId(e.target.value)}
|
||||||
|
className="mt-1 font-mono text-sm"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<ToggleButton
|
||||||
|
enabled={settings.telegramEnabled}
|
||||||
|
onToggle={() =>
|
||||||
|
setSettings((s) => ({ ...s, telegramEnabled: !s.telegramEnabled }))
|
||||||
|
}
|
||||||
|
label="Enable Telegram notifications"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => handleTest('telegram')}
|
||||||
|
disabled={!telegramToken || !telegramChatId || testing === 'telegram'}
|
||||||
|
>
|
||||||
|
{testing === 'telegram' ? (
|
||||||
|
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
||||||
|
) : (
|
||||||
|
<Send className="h-4 w-4 mr-2" />
|
||||||
|
)}
|
||||||
|
Send Test Message
|
||||||
|
</Button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Email */}
|
||||||
|
<Card className="mb-6">
|
||||||
|
<CardHeader>
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<CardTitle className="flex items-center gap-2">
|
||||||
|
<Mail className="h-5 w-5" />
|
||||||
|
Email
|
||||||
|
</CardTitle>
|
||||||
|
<Badge variant={settings.emailEnabled ? 'default' : 'secondary'}>
|
||||||
|
{settings.emailEnabled ? 'Enabled' : 'Disabled'}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
<CardDescription>
|
||||||
|
Receive notification emails to your account email address
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
<ToggleButton
|
||||||
|
enabled={settings.emailEnabled}
|
||||||
|
onToggle={() =>
|
||||||
|
setSettings((s) => ({ ...s, emailEnabled: !s.emailEnabled }))
|
||||||
|
}
|
||||||
|
label="Enable email notifications"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => handleTest('email')}
|
||||||
|
disabled={!settings.emailEnabled || testing === 'email'}
|
||||||
|
>
|
||||||
|
{testing === 'email' ? (
|
||||||
|
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
||||||
|
) : (
|
||||||
|
<Mail className="h-4 w-4 mr-2" />
|
||||||
|
)}
|
||||||
|
Send Test Email
|
||||||
|
</Button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Timezone */}
|
||||||
|
<Card className="mb-6">
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="flex items-center gap-2">
|
||||||
|
<Globe className="h-5 w-5" />
|
||||||
|
Timezone
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Timestamps in notifications will use this timezone
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<Select
|
||||||
|
value={settings.timezone}
|
||||||
|
onValueChange={(value) =>
|
||||||
|
setSettings((s) => ({ ...s, timezone: value }))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="w-full">
|
||||||
|
<SelectValue placeholder="Select timezone" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectGroup>
|
||||||
|
<SelectLabel>Americas</SelectLabel>
|
||||||
|
<SelectItem value="America/New_York">Eastern Time (New York)</SelectItem>
|
||||||
|
<SelectItem value="America/Chicago">Central Time (Chicago)</SelectItem>
|
||||||
|
<SelectItem value="America/Denver">Mountain Time (Denver)</SelectItem>
|
||||||
|
<SelectItem value="America/Los_Angeles">Pacific Time (Los Angeles)</SelectItem>
|
||||||
|
<SelectItem value="America/Anchorage">Alaska (Anchorage)</SelectItem>
|
||||||
|
<SelectItem value="Pacific/Honolulu">Hawaii (Honolulu)</SelectItem>
|
||||||
|
<SelectItem value="America/Toronto">Toronto</SelectItem>
|
||||||
|
<SelectItem value="America/Vancouver">Vancouver</SelectItem>
|
||||||
|
<SelectItem value="America/Mexico_City">Mexico City</SelectItem>
|
||||||
|
<SelectItem value="America/Sao_Paulo">São Paulo</SelectItem>
|
||||||
|
<SelectItem value="America/Argentina/Buenos_Aires">Buenos Aires</SelectItem>
|
||||||
|
<SelectItem value="America/Bogota">Bogotá</SelectItem>
|
||||||
|
</SelectGroup>
|
||||||
|
<SelectGroup>
|
||||||
|
<SelectLabel>Europe</SelectLabel>
|
||||||
|
<SelectItem value="Europe/London">London (GMT/BST)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Paris">Paris (CET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Berlin">Berlin (CET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Amsterdam">Amsterdam (CET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Madrid">Madrid (CET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Rome">Rome (CET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Zurich">Zurich (CET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Stockholm">Stockholm (CET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Helsinki">Helsinki (EET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Athens">Athens (EET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Istanbul">Istanbul (TRT)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Moscow">Moscow (MSK)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Kiev">Kyiv (EET)</SelectItem>
|
||||||
|
<SelectItem value="Europe/Warsaw">Warsaw (CET)</SelectItem>
|
||||||
|
</SelectGroup>
|
||||||
|
<SelectGroup>
|
||||||
|
<SelectLabel>Asia & Pacific</SelectLabel>
|
||||||
|
<SelectItem value="Asia/Dubai">Dubai (GST)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Kolkata">India (IST)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Bangkok">Bangkok (ICT)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Singapore">Singapore (SGT)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Hong_Kong">Hong Kong (HKT)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Shanghai">Shanghai (CST)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Tokyo">Tokyo (JST)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Seoul">Seoul (KST)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Taipei">Taipei (CST)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Jakarta">Jakarta (WIB)</SelectItem>
|
||||||
|
<SelectItem value="Australia/Sydney">Sydney (AEST)</SelectItem>
|
||||||
|
<SelectItem value="Australia/Melbourne">Melbourne (AEST)</SelectItem>
|
||||||
|
<SelectItem value="Australia/Perth">Perth (AWST)</SelectItem>
|
||||||
|
<SelectItem value="Pacific/Auckland">Auckland (NZST)</SelectItem>
|
||||||
|
</SelectGroup>
|
||||||
|
<SelectGroup>
|
||||||
|
<SelectLabel>Africa & Middle East</SelectLabel>
|
||||||
|
<SelectItem value="Africa/Cairo">Cairo (EET)</SelectItem>
|
||||||
|
<SelectItem value="Africa/Lagos">Lagos (WAT)</SelectItem>
|
||||||
|
<SelectItem value="Africa/Johannesburg">Johannesburg (SAST)</SelectItem>
|
||||||
|
<SelectItem value="Africa/Nairobi">Nairobi (EAT)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Riyadh">Riyadh (AST)</SelectItem>
|
||||||
|
<SelectItem value="Asia/Tehran">Tehran (IRST)</SelectItem>
|
||||||
|
</SelectGroup>
|
||||||
|
<SelectGroup>
|
||||||
|
<SelectLabel>Other</SelectLabel>
|
||||||
|
<SelectItem value="UTC">UTC</SelectItem>
|
||||||
|
</SelectGroup>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<Separator className="my-6" />
|
||||||
|
|
||||||
|
{/* Save button */}
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<Button onClick={handleSave} disabled={saving}>
|
||||||
|
{saving ? (
|
||||||
|
<>
|
||||||
|
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
||||||
|
Saving...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
'Save Settings'
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,11 +1,17 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useParams } from 'next/navigation';
|
|
||||||
import { MembersManagementPage } from '@/components/members-management-page';
|
import { MembersManagementPage } from '@/components/members-management-page';
|
||||||
|
import { requireWorkspaceAccessOrRedirect } from '@/lib/route-access';
|
||||||
|
|
||||||
export default function WorkspaceMembersPage() {
|
interface WorkspaceMembersPageProps {
|
||||||
const params = useParams();
|
params: Promise<{ workspaceId: string }>;
|
||||||
const workspaceId = params.workspaceId as string;
|
}
|
||||||
|
|
||||||
|
export default async function WorkspaceMembersPage({ params }: WorkspaceMembersPageProps) {
|
||||||
|
const { workspaceId } = await params;
|
||||||
|
|
||||||
|
await requireWorkspaceAccessOrRedirect({
|
||||||
|
workspaceId,
|
||||||
|
intent: 'manage',
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MembersManagementPage
|
<MembersManagementPage
|
||||||
@@ -15,7 +21,6 @@ export default function WorkspaceMembersPage() {
|
|||||||
title="Members"
|
title="Members"
|
||||||
subtitle="Manage who has access to this workspace and all its projects"
|
subtitle="Manage who has access to this workspace and all its projects"
|
||||||
membersDescription="Admins can manage projects and members. Commentators can view and comment only."
|
membersDescription="Admins can manage projects and members. Commentators can view and comment only."
|
||||||
forbiddenRedirect="/workspaces"
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ export default async function WorkspacePage({ params, searchParams }: WorkspaceP
|
|||||||
const isAdmin = isOwner || membership?.role === 'ADMIN';
|
const isAdmin = isOwner || membership?.role === 'ADMIN';
|
||||||
|
|
||||||
if (!isOwner && !isMember) {
|
if (!isOwner && !isMember) {
|
||||||
redirect('/workspaces');
|
redirect('/dashboard');
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalPages = Math.ceil(workspace._count.projects / pageSize);
|
const totalPages = Math.ceil(workspace._count.projects / pageSize);
|
||||||
|
|||||||
+181
@@ -0,0 +1,181 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState } from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { ArrowLeft, Loader2, Globe, Lock, UserPlus, FolderPlus } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
|
||||||
|
type Visibility = 'PRIVATE' | 'INVITE' | 'PUBLIC';
|
||||||
|
|
||||||
|
const visibilityOptions: { value: Visibility; label: string; description: string; icon: React.ReactNode }[] = [
|
||||||
|
{
|
||||||
|
value: 'PRIVATE',
|
||||||
|
label: 'Private',
|
||||||
|
description: 'Only workspace members and project members can access',
|
||||||
|
icon: <Lock className="h-5 w-5" />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'INVITE',
|
||||||
|
label: 'Invite Only',
|
||||||
|
description: 'Share with specific people via email',
|
||||||
|
icon: <UserPlus className="h-5 w-5" />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'PUBLIC',
|
||||||
|
label: 'Public',
|
||||||
|
description: 'Anyone with the link can view',
|
||||||
|
icon: <Globe className="h-5 w-5" />,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export default function NewWorkspaceProjectPageClient({ workspaceId }: { workspaceId: string }) {
|
||||||
|
const router = useRouter();
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [formData, setFormData] = useState({
|
||||||
|
name: '',
|
||||||
|
description: '',
|
||||||
|
visibility: 'PRIVATE' as Visibility,
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setIsLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/projects', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ ...formData, workspaceId }),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
setError(data.error || 'Failed to create project');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
router.push(`/projects/${data.data.id}`);
|
||||||
|
} catch {
|
||||||
|
setError('Something went wrong. Please try again.');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
||||||
|
<div className="w-full max-w-xl">
|
||||||
|
<div className="mb-8">
|
||||||
|
<Link
|
||||||
|
href={`/workspaces/${workspaceId}`}
|
||||||
|
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
||||||
|
>
|
||||||
|
<ArrowLeft className="h-4 w-4 mr-1" />
|
||||||
|
Back to Workspace
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader className="text-center pb-2">
|
||||||
|
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
||||||
|
<FolderPlus className="h-7 w-7 text-primary" />
|
||||||
|
</div>
|
||||||
|
<CardTitle className="text-2xl">Create Project in Workspace</CardTitle>
|
||||||
|
<CardDescription className="text-base">
|
||||||
|
This project will be accessible to all workspace members
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="pt-6">
|
||||||
|
<form onSubmit={handleSubmit} className="space-y-6">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="name" className="text-sm font-medium">
|
||||||
|
Project Name
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="name"
|
||||||
|
placeholder="e.g., Product Launch Video"
|
||||||
|
value={formData.name}
|
||||||
|
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
||||||
|
required
|
||||||
|
disabled={isLoading}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="description" className="text-sm font-medium">
|
||||||
|
Description{' '}
|
||||||
|
<span className="text-muted-foreground font-normal">(optional)</span>
|
||||||
|
</Label>
|
||||||
|
<Textarea
|
||||||
|
id="description"
|
||||||
|
placeholder="What is this project about?"
|
||||||
|
value={formData.description}
|
||||||
|
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
||||||
|
rows={3}
|
||||||
|
disabled={isLoading}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-3">
|
||||||
|
<Label className="text-sm font-medium">Visibility</Label>
|
||||||
|
<div className="grid gap-2">
|
||||||
|
{visibilityOptions.map((option) => (
|
||||||
|
<label
|
||||||
|
key={option.value}
|
||||||
|
className={`flex items-start gap-3 rounded-lg border p-3 cursor-pointer transition-colors ${
|
||||||
|
formData.visibility === option.value
|
||||||
|
? 'border-primary bg-primary/5'
|
||||||
|
: 'hover:bg-accent/50'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="visibility"
|
||||||
|
value={option.value}
|
||||||
|
checked={formData.visibility === option.value}
|
||||||
|
onChange={(e) =>
|
||||||
|
setFormData({ ...formData, visibility: e.target.value as Visibility })
|
||||||
|
}
|
||||||
|
className="sr-only"
|
||||||
|
/>
|
||||||
|
<div className="mt-0.5 text-muted-foreground">{option.icon}</div>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm font-medium">{option.label}</p>
|
||||||
|
<p className="text-xs text-muted-foreground">{option.description}</p>
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Button type="submit" className="w-full" disabled={isLoading}>
|
||||||
|
{isLoading ? (
|
||||||
|
<>
|
||||||
|
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||||||
|
Creating...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
'Create Project'
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,182 +1,17 @@
|
|||||||
'use client';
|
import { requireWorkspaceAccessOrRedirect } from '@/lib/route-access';
|
||||||
|
import NewWorkspaceProjectPageClient from './new-workspace-project-page-client';
|
||||||
|
|
||||||
import { useState, use } from 'react';
|
interface NewWorkspaceProjectPageProps {
|
||||||
import { useRouter } from 'next/navigation';
|
params: Promise<{ workspaceId: string }>;
|
||||||
import Link from 'next/link';
|
}
|
||||||
import { ArrowLeft, Loader2, Globe, Lock, UserPlus, FolderPlus } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
|
||||||
|
|
||||||
type Visibility = 'PRIVATE' | 'INVITE' | 'PUBLIC';
|
export default async function NewWorkspaceProjectPage({ params }: NewWorkspaceProjectPageProps) {
|
||||||
|
const { workspaceId } = await params;
|
||||||
|
|
||||||
const visibilityOptions: { value: Visibility; label: string; description: string; icon: React.ReactNode }[] = [
|
await requireWorkspaceAccessOrRedirect({
|
||||||
{
|
workspaceId,
|
||||||
value: 'PRIVATE',
|
intent: 'manage',
|
||||||
label: 'Private',
|
|
||||||
description: 'Only workspace members and project members can access',
|
|
||||||
icon: <Lock className="h-5 w-5" />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: 'INVITE',
|
|
||||||
label: 'Invite Only',
|
|
||||||
description: 'Share with specific people via email',
|
|
||||||
icon: <UserPlus className="h-5 w-5" />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: 'PUBLIC',
|
|
||||||
label: 'Public',
|
|
||||||
description: 'Anyone with the link can view',
|
|
||||||
icon: <Globe className="h-5 w-5" />,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export default function NewWorkspaceProjectPage({ params }: { params: Promise<{ workspaceId: string }> }) {
|
|
||||||
const { workspaceId } = use(params);
|
|
||||||
const router = useRouter();
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
const [error, setError] = useState('');
|
|
||||||
const [formData, setFormData] = useState({
|
|
||||||
name: '',
|
|
||||||
description: '',
|
|
||||||
visibility: 'PRIVATE' as Visibility,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
return <NewWorkspaceProjectPageClient workspaceId={workspaceId} />;
|
||||||
e.preventDefault();
|
|
||||||
setIsLoading(true);
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch('/api/projects', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ ...formData, workspaceId }),
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
setError(data.error || 'Failed to create project');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
router.push(`/projects/${data.data.id}`);
|
|
||||||
} catch {
|
|
||||||
setError('Something went wrong. Please try again.');
|
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
|
||||||
<div className="w-full max-w-xl">
|
|
||||||
<div className="mb-8">
|
|
||||||
<Link
|
|
||||||
href={`/workspaces/${workspaceId}`}
|
|
||||||
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4 mr-1" />
|
|
||||||
Back to Workspace
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader className="text-center pb-2">
|
|
||||||
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
|
||||||
<FolderPlus className="h-7 w-7 text-primary" />
|
|
||||||
</div>
|
|
||||||
<CardTitle className="text-2xl">Create Project in Workspace</CardTitle>
|
|
||||||
<CardDescription className="text-base">
|
|
||||||
This project will be accessible to all workspace members
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="pt-6">
|
|
||||||
<form onSubmit={handleSubmit} className="space-y-6">
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="name" className="text-sm font-medium">
|
|
||||||
Project Name
|
|
||||||
</Label>
|
|
||||||
<Input
|
|
||||||
id="name"
|
|
||||||
placeholder="e.g., Product Launch Video"
|
|
||||||
value={formData.name}
|
|
||||||
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
|
||||||
required
|
|
||||||
disabled={isLoading}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="description" className="text-sm font-medium">
|
|
||||||
Description{' '}
|
|
||||||
<span className="text-muted-foreground font-normal">(optional)</span>
|
|
||||||
</Label>
|
|
||||||
<Textarea
|
|
||||||
id="description"
|
|
||||||
placeholder="What is this project about?"
|
|
||||||
value={formData.description}
|
|
||||||
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
|
||||||
rows={3}
|
|
||||||
disabled={isLoading}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-3">
|
|
||||||
<Label className="text-sm font-medium">Visibility</Label>
|
|
||||||
<div className="grid gap-2">
|
|
||||||
{visibilityOptions.map((option) => (
|
|
||||||
<label
|
|
||||||
key={option.value}
|
|
||||||
className={`flex items-start gap-3 rounded-lg border p-3 cursor-pointer transition-colors ${
|
|
||||||
formData.visibility === option.value
|
|
||||||
? 'border-primary bg-primary/5'
|
|
||||||
: 'hover:bg-accent/50'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<input
|
|
||||||
type="radio"
|
|
||||||
name="visibility"
|
|
||||||
value={option.value}
|
|
||||||
checked={formData.visibility === option.value}
|
|
||||||
onChange={(e) =>
|
|
||||||
setFormData({ ...formData, visibility: e.target.value as Visibility })
|
|
||||||
}
|
|
||||||
className="sr-only"
|
|
||||||
/>
|
|
||||||
<div className="mt-0.5 text-muted-foreground">{option.icon}</div>
|
|
||||||
<div>
|
|
||||||
<p className="text-sm font-medium">{option.label}</p>
|
|
||||||
<p className="text-xs text-muted-foreground">{option.description}</p>
|
|
||||||
</div>
|
|
||||||
</label>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{error && (
|
|
||||||
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
|
|
||||||
{error}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<Button type="submit" className="w-full" disabled={isLoading}>
|
|
||||||
{isLoading ? (
|
|
||||||
<>
|
|
||||||
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
|
||||||
Creating...
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
'Create Project'
|
|
||||||
)}
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,268 +1,17 @@
|
|||||||
'use client';
|
import { requireWorkspaceAccessOrRedirect } from '@/lib/route-access';
|
||||||
|
import WorkspaceSettingsPageClient from './workspace-settings-page-client';
|
||||||
|
|
||||||
import { useState, useEffect, useCallback } from 'react';
|
interface WorkspaceSettingsPageProps {
|
||||||
import { useParams, useRouter } from 'next/navigation';
|
params: Promise<{ workspaceId: string }>;
|
||||||
import Link from 'next/link';
|
|
||||||
import { ArrowLeft, Loader2, Building2, Trash2 } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
|
||||||
import { Separator } from '@/components/ui/separator';
|
|
||||||
import {
|
|
||||||
AlertDialog,
|
|
||||||
AlertDialogAction,
|
|
||||||
AlertDialogCancel,
|
|
||||||
AlertDialogContent,
|
|
||||||
AlertDialogDescription,
|
|
||||||
AlertDialogFooter,
|
|
||||||
AlertDialogHeader,
|
|
||||||
AlertDialogTitle,
|
|
||||||
AlertDialogTrigger,
|
|
||||||
} from '@/components/ui/alert-dialog';
|
|
||||||
|
|
||||||
interface WorkspaceData {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
description: string | null;
|
|
||||||
slug: string;
|
|
||||||
ownerId: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function WorkspaceSettingsPage() {
|
export default async function WorkspaceSettingsPage({ params }: WorkspaceSettingsPageProps) {
|
||||||
const params = useParams();
|
const { workspaceId } = await params;
|
||||||
const router = useRouter();
|
|
||||||
const workspaceId = params.workspaceId as string;
|
|
||||||
|
|
||||||
const [workspace, setWorkspace] = useState<WorkspaceData | null>(null);
|
await requireWorkspaceAccessOrRedirect({
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
workspaceId,
|
||||||
const [isSaving, setIsSaving] = useState(false);
|
intent: 'manage',
|
||||||
const [isDeleting, setIsDeleting] = useState(false);
|
});
|
||||||
const [error, setError] = useState('');
|
|
||||||
const [success, setSuccess] = useState('');
|
|
||||||
const [formData, setFormData] = useState({ name: '', description: '' });
|
|
||||||
const [deleteConfirmation, setDeleteConfirmation] = useState('');
|
|
||||||
|
|
||||||
const fetchWorkspace = useCallback(async () => {
|
return <WorkspaceSettingsPageClient workspaceId={workspaceId} />;
|
||||||
try {
|
|
||||||
const res = await fetch(`/api/workspaces/${workspaceId}`);
|
|
||||||
if (!res.ok) {
|
|
||||||
router.push('/workspaces');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const data = await res.json();
|
|
||||||
const workspace = data.data;
|
|
||||||
setWorkspace(workspace);
|
|
||||||
setFormData({
|
|
||||||
name: workspace.name,
|
|
||||||
description: workspace.description || '',
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
setError('Failed to load workspace');
|
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
}, [workspaceId, router]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
fetchWorkspace();
|
|
||||||
}, [fetchWorkspace]);
|
|
||||||
|
|
||||||
const handleSave = async (e: React.FormEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
setIsSaving(true);
|
|
||||||
setError('');
|
|
||||||
setSuccess('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(`/api/workspaces/${workspaceId}`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(formData),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
const data = await res.json();
|
|
||||||
setError(data.error || 'Failed to update workspace');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setSuccess('Workspace updated successfully');
|
|
||||||
} catch {
|
|
||||||
setError('Something went wrong');
|
|
||||||
} finally {
|
|
||||||
setIsSaving(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleDelete = async () => {
|
|
||||||
if (!workspace) return;
|
|
||||||
if (deleteConfirmation !== workspace.name) return;
|
|
||||||
|
|
||||||
setIsDeleting(true);
|
|
||||||
try {
|
|
||||||
const res = await fetch(`/api/workspaces/${workspaceId}`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
const data = await res.json();
|
|
||||||
setError(data.error || 'Failed to delete workspace');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
router.push('/workspaces');
|
|
||||||
} catch {
|
|
||||||
setError('Failed to delete workspace');
|
|
||||||
} finally {
|
|
||||||
setIsDeleting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isLoading) {
|
|
||||||
return (
|
|
||||||
<div className="flex items-center justify-center min-h-[50vh]">
|
|
||||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!workspace) return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="px-6 lg:px-8 py-8 w-full max-w-2xl mx-auto">
|
|
||||||
<div className="mb-6">
|
|
||||||
<Link
|
|
||||||
href={`/workspaces/${workspaceId}`}
|
|
||||||
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4 mr-1" />
|
|
||||||
Back to Workspace
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="mb-8">
|
|
||||||
<h1 className="text-3xl font-bold tracking-tight">Workspace Settings</h1>
|
|
||||||
<p className="text-muted-foreground mt-1">
|
|
||||||
Manage workspace configuration
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Card className="mb-8">
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle className="flex items-center gap-2">
|
|
||||||
<Building2 className="h-5 w-5" />
|
|
||||||
General
|
|
||||||
</CardTitle>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<form onSubmit={handleSave} className="space-y-4">
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="name">Workspace Name</Label>
|
|
||||||
<Input
|
|
||||||
id="name"
|
|
||||||
value={formData.name}
|
|
||||||
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
|
||||||
required
|
|
||||||
disabled={isSaving}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="description">Description</Label>
|
|
||||||
<Textarea
|
|
||||||
id="description"
|
|
||||||
value={formData.description}
|
|
||||||
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
|
||||||
rows={3}
|
|
||||||
disabled={isSaving}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{error && (
|
|
||||||
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
|
|
||||||
{error}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{success && (
|
|
||||||
<div className="rounded-md bg-green-500/10 p-3 text-sm text-green-700 dark:text-green-400">
|
|
||||||
{success}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<Button type="submit" disabled={isSaving}>
|
|
||||||
{isSaving ? (
|
|
||||||
<>
|
|
||||||
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
|
||||||
Saving...
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
'Save Changes'
|
|
||||||
)}
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
<Separator className="my-8" />
|
|
||||||
|
|
||||||
{/* Danger Zone */}
|
|
||||||
<Card className="border-destructive/50">
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle className="text-destructive">Danger Zone</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Irreversible actions. Proceed with caution.
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<AlertDialog>
|
|
||||||
<AlertDialogTrigger asChild>
|
|
||||||
<Button variant="destructive">
|
|
||||||
<Trash2 className="h-4 w-4 mr-2" />
|
|
||||||
Delete Workspace
|
|
||||||
</Button>
|
|
||||||
</AlertDialogTrigger>
|
|
||||||
<AlertDialogContent>
|
|
||||||
<AlertDialogHeader>
|
|
||||||
<AlertDialogTitle>Delete "{workspace.name}"?</AlertDialogTitle>
|
|
||||||
<AlertDialogDescription asChild>
|
|
||||||
<div className="space-y-4">
|
|
||||||
<p>
|
|
||||||
This will permanently delete this workspace and everything inside it
|
|
||||||
(projects, videos, comments, images, and voice notes). This action cannot be undone.
|
|
||||||
</p>
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="delete-workspace-confirm">
|
|
||||||
Type <strong className="text-foreground">{workspace.name}</strong> to confirm
|
|
||||||
</Label>
|
|
||||||
<Input
|
|
||||||
id="delete-workspace-confirm"
|
|
||||||
value={deleteConfirmation}
|
|
||||||
onChange={(e) => setDeleteConfirmation(e.target.value)}
|
|
||||||
placeholder="Workspace name"
|
|
||||||
className="h-11"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</AlertDialogDescription>
|
|
||||||
</AlertDialogHeader>
|
|
||||||
<AlertDialogFooter>
|
|
||||||
<AlertDialogCancel onClick={() => setDeleteConfirmation('')}>
|
|
||||||
Cancel
|
|
||||||
</AlertDialogCancel>
|
|
||||||
<AlertDialogAction
|
|
||||||
onClick={handleDelete}
|
|
||||||
disabled={deleteConfirmation !== workspace.name || isDeleting}
|
|
||||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
|
||||||
>
|
|
||||||
{isDeleting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
|
||||||
Delete Workspace
|
|
||||||
</AlertDialogAction>
|
|
||||||
</AlertDialogFooter>
|
|
||||||
</AlertDialogContent>
|
|
||||||
</AlertDialog>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,266 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect, useCallback } from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { ArrowLeft, Loader2, Building2, Trash2 } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import { Separator } from '@/components/ui/separator';
|
||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogAction,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogHeader,
|
||||||
|
AlertDialogTitle,
|
||||||
|
AlertDialogTrigger,
|
||||||
|
} from '@/components/ui/alert-dialog';
|
||||||
|
|
||||||
|
interface WorkspaceData {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
description: string | null;
|
||||||
|
slug: string;
|
||||||
|
ownerId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function WorkspaceSettingsPageClient({ workspaceId }: { workspaceId: string }) {
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const [workspace, setWorkspace] = useState<WorkspaceData | null>(null);
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [isSaving, setIsSaving] = useState(false);
|
||||||
|
const [isDeleting, setIsDeleting] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [success, setSuccess] = useState('');
|
||||||
|
const [formData, setFormData] = useState({ name: '', description: '' });
|
||||||
|
const [deleteConfirmation, setDeleteConfirmation] = useState('');
|
||||||
|
|
||||||
|
const fetchWorkspace = useCallback(async () => {
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/workspaces/${workspaceId}`);
|
||||||
|
if (!res.ok) {
|
||||||
|
router.push('/dashboard');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = await res.json();
|
||||||
|
const workspace = data.data;
|
||||||
|
setWorkspace(workspace);
|
||||||
|
setFormData({
|
||||||
|
name: workspace.name,
|
||||||
|
description: workspace.description || '',
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
setError('Failed to load workspace');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
}, [workspaceId, router]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchWorkspace();
|
||||||
|
}, [fetchWorkspace]);
|
||||||
|
|
||||||
|
const handleSave = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setIsSaving(true);
|
||||||
|
setError('');
|
||||||
|
setSuccess('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/workspaces/${workspaceId}`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(formData),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
setError(data.error || 'Failed to update workspace');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setSuccess('Workspace updated successfully');
|
||||||
|
} catch {
|
||||||
|
setError('Something went wrong');
|
||||||
|
} finally {
|
||||||
|
setIsSaving(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDelete = async () => {
|
||||||
|
if (!workspace) return;
|
||||||
|
if (deleteConfirmation !== workspace.name) return;
|
||||||
|
|
||||||
|
setIsDeleting(true);
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/workspaces/${workspaceId}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
setError(data.error || 'Failed to delete workspace');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
router.push('/workspaces');
|
||||||
|
} catch {
|
||||||
|
setError('Failed to delete workspace');
|
||||||
|
} finally {
|
||||||
|
setIsDeleting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isLoading) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-center min-h-[50vh]">
|
||||||
|
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!workspace) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="px-6 lg:px-8 py-8 w-full max-w-2xl mx-auto">
|
||||||
|
<div className="mb-6">
|
||||||
|
<Link
|
||||||
|
href={`/workspaces/${workspaceId}`}
|
||||||
|
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
||||||
|
>
|
||||||
|
<ArrowLeft className="h-4 w-4 mr-1" />
|
||||||
|
Back to Workspace
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-8">
|
||||||
|
<h1 className="text-3xl font-bold tracking-tight">Workspace Settings</h1>
|
||||||
|
<p className="text-muted-foreground mt-1">
|
||||||
|
Manage workspace configuration
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Card className="mb-8">
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="flex items-center gap-2">
|
||||||
|
<Building2 className="h-5 w-5" />
|
||||||
|
General
|
||||||
|
</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<form onSubmit={handleSave} className="space-y-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="name">Workspace Name</Label>
|
||||||
|
<Input
|
||||||
|
id="name"
|
||||||
|
value={formData.name}
|
||||||
|
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
||||||
|
required
|
||||||
|
disabled={isSaving}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="description">Description</Label>
|
||||||
|
<Textarea
|
||||||
|
id="description"
|
||||||
|
value={formData.description}
|
||||||
|
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
||||||
|
rows={3}
|
||||||
|
disabled={isSaving}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{success && (
|
||||||
|
<div className="rounded-md bg-green-500/10 p-3 text-sm text-green-700 dark:text-green-400">
|
||||||
|
{success}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Button type="submit" disabled={isSaving}>
|
||||||
|
{isSaving ? (
|
||||||
|
<>
|
||||||
|
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||||||
|
Saving...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
'Save Changes'
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<Separator className="my-8" />
|
||||||
|
|
||||||
|
{/* Danger Zone */}
|
||||||
|
<Card className="border-destructive/50">
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle className="text-destructive">Danger Zone</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Irreversible actions. Proceed with caution.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<AlertDialog>
|
||||||
|
<AlertDialogTrigger asChild>
|
||||||
|
<Button variant="destructive">
|
||||||
|
<Trash2 className="h-4 w-4 mr-2" />
|
||||||
|
Delete Workspace
|
||||||
|
</Button>
|
||||||
|
</AlertDialogTrigger>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>Delete "{workspace.name}"?</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription asChild>
|
||||||
|
<div className="space-y-4">
|
||||||
|
<p>
|
||||||
|
This will permanently delete this workspace and everything inside it
|
||||||
|
(projects, videos, comments, images, and voice notes). This action cannot be undone.
|
||||||
|
</p>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="delete-workspace-confirm">
|
||||||
|
Type <strong className="text-foreground">{workspace.name}</strong> to confirm
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="delete-workspace-confirm"
|
||||||
|
value={deleteConfirmation}
|
||||||
|
onChange={(e) => setDeleteConfirmation(e.target.value)}
|
||||||
|
placeholder="Workspace name"
|
||||||
|
className="h-11"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</AlertDialogDescription>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel onClick={() => setDeleteConfirmation('')}>
|
||||||
|
Cancel
|
||||||
|
</AlertDialogCancel>
|
||||||
|
<AlertDialogAction
|
||||||
|
onClick={handleDelete}
|
||||||
|
disabled={deleteConfirmation !== workspace.name || isDeleting}
|
||||||
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||||
|
>
|
||||||
|
{isDeleting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
||||||
|
Delete Workspace
|
||||||
|
</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState } from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { ArrowLeft, Loader2, Building2 } from 'lucide-react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
|
||||||
|
export default function NewWorkspacePage() {
|
||||||
|
const router = useRouter();
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [formData, setFormData] = useState({
|
||||||
|
name: '',
|
||||||
|
description: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setIsLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/workspaces', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(formData),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
setError(data.error || 'Failed to create workspace');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
router.push(`/workspaces/${data.data.id}`);
|
||||||
|
} catch {
|
||||||
|
setError('Something went wrong. Please try again.');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
||||||
|
<div className="w-full max-w-xl">
|
||||||
|
<div className="mb-8">
|
||||||
|
<Link
|
||||||
|
href="/workspaces"
|
||||||
|
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
||||||
|
>
|
||||||
|
<ArrowLeft className="h-4 w-4 mr-1" />
|
||||||
|
Back to Workspaces
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Card className="border-border/50 shadow-lg">
|
||||||
|
<CardHeader className="text-center pb-2">
|
||||||
|
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
||||||
|
<Building2 className="h-7 w-7 text-primary" />
|
||||||
|
</div>
|
||||||
|
<CardTitle className="text-2xl">Create New Workspace</CardTitle>
|
||||||
|
<CardDescription className="text-base">
|
||||||
|
Set up a workspace to organize projects and invite your team
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="pt-6">
|
||||||
|
<form onSubmit={handleSubmit} className="space-y-6">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="name" className="text-sm font-medium">
|
||||||
|
Workspace Name
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="name"
|
||||||
|
placeholder="e.g., My Studio"
|
||||||
|
value={formData.name}
|
||||||
|
onChange={(e) =>
|
||||||
|
setFormData({ ...formData, name: e.target.value })
|
||||||
|
}
|
||||||
|
required
|
||||||
|
disabled={isLoading}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="description" className="text-sm font-medium">
|
||||||
|
Description{' '}
|
||||||
|
<span className="text-muted-foreground font-normal">(optional)</span>
|
||||||
|
</Label>
|
||||||
|
<Textarea
|
||||||
|
id="description"
|
||||||
|
placeholder="What is this workspace for?"
|
||||||
|
value={formData.description}
|
||||||
|
onChange={(e) =>
|
||||||
|
setFormData({ ...formData, description: e.target.value })
|
||||||
|
}
|
||||||
|
rows={3}
|
||||||
|
disabled={isLoading}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Button type="submit" className="w-full" disabled={isLoading}>
|
||||||
|
{isLoading ? (
|
||||||
|
<>
|
||||||
|
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||||||
|
Creating...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
'Create Workspace'
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,129 +1,7 @@
|
|||||||
'use client';
|
import { requireAuthOrRedirect } from '@/lib/route-access';
|
||||||
|
import NewWorkspacePageClient from './new-workspace-page-client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
export default async function NewWorkspacePage() {
|
||||||
import { useRouter } from 'next/navigation';
|
await requireAuthOrRedirect();
|
||||||
import Link from 'next/link';
|
return <NewWorkspacePageClient />;
|
||||||
import { ArrowLeft, Loader2, Building2 } from 'lucide-react';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
|
||||||
|
|
||||||
export default function NewWorkspacePage() {
|
|
||||||
const router = useRouter();
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
const [error, setError] = useState('');
|
|
||||||
const [formData, setFormData] = useState({
|
|
||||||
name: '',
|
|
||||||
description: '',
|
|
||||||
});
|
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
setIsLoading(true);
|
|
||||||
setError('');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch('/api/workspaces', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(formData),
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
setError(data.error || 'Failed to create workspace');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
router.push(`/workspaces/${data.data.id}`);
|
|
||||||
} catch {
|
|
||||||
setError('Something went wrong. Please try again.');
|
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
|
|
||||||
<div className="w-full max-w-xl">
|
|
||||||
<div className="mb-8">
|
|
||||||
<Link
|
|
||||||
href="/workspaces"
|
|
||||||
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4 mr-1" />
|
|
||||||
Back to Workspaces
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Card className="border-border/50 shadow-lg">
|
|
||||||
<CardHeader className="text-center pb-2">
|
|
||||||
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
|
|
||||||
<Building2 className="h-7 w-7 text-primary" />
|
|
||||||
</div>
|
|
||||||
<CardTitle className="text-2xl">Create New Workspace</CardTitle>
|
|
||||||
<CardDescription className="text-base">
|
|
||||||
Set up a workspace to organize projects and invite your team
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="pt-6">
|
|
||||||
<form onSubmit={handleSubmit} className="space-y-6">
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="name" className="text-sm font-medium">
|
|
||||||
Workspace Name
|
|
||||||
</Label>
|
|
||||||
<Input
|
|
||||||
id="name"
|
|
||||||
placeholder="e.g., My Studio"
|
|
||||||
value={formData.name}
|
|
||||||
onChange={(e) =>
|
|
||||||
setFormData({ ...formData, name: e.target.value })
|
|
||||||
}
|
|
||||||
required
|
|
||||||
disabled={isLoading}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="description" className="text-sm font-medium">
|
|
||||||
Description{' '}
|
|
||||||
<span className="text-muted-foreground font-normal">(optional)</span>
|
|
||||||
</Label>
|
|
||||||
<Textarea
|
|
||||||
id="description"
|
|
||||||
placeholder="What is this workspace for?"
|
|
||||||
value={formData.description}
|
|
||||||
onChange={(e) =>
|
|
||||||
setFormData({ ...formData, description: e.target.value })
|
|
||||||
}
|
|
||||||
rows={3}
|
|
||||||
disabled={isLoading}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{error && (
|
|
||||||
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
|
|
||||||
{error}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<Button type="submit" className="w-full" disabled={isLoading}>
|
|
||||||
{isLoading ? (
|
|
||||||
<>
|
|
||||||
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
|
||||||
Creating...
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
'Create Workspace'
|
|
||||||
)}
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ interface MembersManagementPageProps {
|
|||||||
title: string;
|
title: string;
|
||||||
subtitle: string;
|
subtitle: string;
|
||||||
membersDescription: ReactNode;
|
membersDescription: ReactNode;
|
||||||
forbiddenRedirect: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function MembersManagementPage({
|
export function MembersManagementPage({
|
||||||
@@ -78,7 +77,6 @@ export function MembersManagementPage({
|
|||||||
title,
|
title,
|
||||||
subtitle,
|
subtitle,
|
||||||
membersDescription,
|
membersDescription,
|
||||||
forbiddenRedirect,
|
|
||||||
}: MembersManagementPageProps) {
|
}: MembersManagementPageProps) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
@@ -99,7 +97,14 @@ export function MembersManagementPage({
|
|||||||
cache: 'no-store',
|
cache: 'no-store',
|
||||||
});
|
});
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
if (res.status === 403) router.push(forbiddenRedirect);
|
if (res.status === 401) {
|
||||||
|
router.push('/login');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (res.status === 403) {
|
||||||
|
router.push('/dashboard');
|
||||||
|
return;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
@@ -111,7 +116,7 @@ export function MembersManagementPage({
|
|||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}, [apiBasePath, forbiddenRedirect, router]);
|
}, [apiBasePath, router]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchMembers();
|
fetchMembers();
|
||||||
|
|||||||
@@ -0,0 +1,171 @@
|
|||||||
|
import { notFound, redirect } from 'next/navigation';
|
||||||
|
import { auth, checkProjectAccess, checkWorkspaceAccess } from '@/lib/auth';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
|
||||||
|
type AccessIntent = 'view' | 'manage';
|
||||||
|
|
||||||
|
const LOGIN_REDIRECT = '/login';
|
||||||
|
const FORBIDDEN_REDIRECT = '/dashboard';
|
||||||
|
|
||||||
|
function redirectForMissingAuth() {
|
||||||
|
redirect(LOGIN_REDIRECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
function redirectForForbidden() {
|
||||||
|
redirect(FORBIDDEN_REDIRECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensureGuestPolicy(options: { userId?: string; intent: AccessIntent; allowPublicView: boolean }) {
|
||||||
|
const { userId, intent, allowPublicView } = options;
|
||||||
|
if (userId) return;
|
||||||
|
|
||||||
|
if (intent !== 'view' || !allowPublicView) {
|
||||||
|
redirectForMissingAuth();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function assertProjectAccessOrRedirect(
|
||||||
|
project: { id: string; ownerId: string; workspaceId: string; visibility: string },
|
||||||
|
options: {
|
||||||
|
userId?: string;
|
||||||
|
intent: AccessIntent;
|
||||||
|
allowPublicView: boolean;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
const { userId, intent, allowPublicView } = options;
|
||||||
|
|
||||||
|
ensureGuestPolicy({ userId, intent, allowPublicView });
|
||||||
|
|
||||||
|
const access = await checkProjectAccess(project, userId, { intent });
|
||||||
|
|
||||||
|
if (!access.hasAccess) {
|
||||||
|
if (!userId) {
|
||||||
|
redirectForMissingAuth();
|
||||||
|
}
|
||||||
|
redirectForForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (intent === 'manage' && !access.canEdit) {
|
||||||
|
redirectForForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
return access;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function requireAuthOrRedirect() {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) {
|
||||||
|
redirectForMissingAuth();
|
||||||
|
}
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function requireWorkspaceAccessOrRedirect(options: {
|
||||||
|
workspaceId: string;
|
||||||
|
userId?: string;
|
||||||
|
intent?: AccessIntent;
|
||||||
|
}) {
|
||||||
|
const { workspaceId, userId, intent = 'view' } = options;
|
||||||
|
const resolvedUserId = userId ?? (await auth())?.user?.id;
|
||||||
|
|
||||||
|
if (!resolvedUserId) {
|
||||||
|
redirectForMissingAuth();
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspace = await db.workspace.findUnique({
|
||||||
|
where: { id: workspaceId },
|
||||||
|
select: { id: true, ownerId: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!workspace) {
|
||||||
|
notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
const access = await checkWorkspaceAccess(workspace, resolvedUserId);
|
||||||
|
|
||||||
|
if (!access.hasAccess) {
|
||||||
|
redirectForForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (intent === 'manage' && !access.canEdit) {
|
||||||
|
redirectForForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
return { workspace, access };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function requireProjectAccessOrRedirect(options: {
|
||||||
|
projectId: string;
|
||||||
|
userId?: string;
|
||||||
|
intent?: AccessIntent;
|
||||||
|
allowPublicView?: boolean;
|
||||||
|
}) {
|
||||||
|
const { projectId, userId, intent = 'view', allowPublicView = false } = options;
|
||||||
|
const resolvedUserId = userId ?? (await auth())?.user?.id;
|
||||||
|
|
||||||
|
// Fail closed before resource lookup when the route is not public.
|
||||||
|
if (!resolvedUserId && !allowPublicView) {
|
||||||
|
redirectForMissingAuth();
|
||||||
|
}
|
||||||
|
|
||||||
|
const project = await db.project.findUnique({
|
||||||
|
where: { id: projectId },
|
||||||
|
select: { id: true, ownerId: true, workspaceId: true, visibility: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!project) {
|
||||||
|
if (!resolvedUserId) {
|
||||||
|
redirectForMissingAuth();
|
||||||
|
}
|
||||||
|
notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
const access = await assertProjectAccessOrRedirect(project, {
|
||||||
|
userId: resolvedUserId,
|
||||||
|
intent,
|
||||||
|
allowPublicView,
|
||||||
|
});
|
||||||
|
|
||||||
|
return { project, access };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function requireVideoProjectAccessOrRedirect(options: {
|
||||||
|
projectId: string;
|
||||||
|
videoId: string;
|
||||||
|
userId?: string;
|
||||||
|
intent?: AccessIntent;
|
||||||
|
allowPublicView?: boolean;
|
||||||
|
}) {
|
||||||
|
const { projectId, videoId, userId, intent = 'view', allowPublicView = false } = options;
|
||||||
|
const resolvedUserId = userId ?? (await auth())?.user?.id;
|
||||||
|
|
||||||
|
// Fail closed before resource lookup when the route is not public.
|
||||||
|
if (!resolvedUserId && !allowPublicView) {
|
||||||
|
redirectForMissingAuth();
|
||||||
|
}
|
||||||
|
|
||||||
|
const video = await db.video.findFirst({
|
||||||
|
where: { id: videoId, projectId },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
project: {
|
||||||
|
select: { id: true, ownerId: true, workspaceId: true, visibility: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!video) {
|
||||||
|
if (!resolvedUserId) {
|
||||||
|
redirectForMissingAuth();
|
||||||
|
}
|
||||||
|
notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
const access = await assertProjectAccessOrRedirect(video.project, {
|
||||||
|
userId: resolvedUserId,
|
||||||
|
intent,
|
||||||
|
allowPublicView,
|
||||||
|
});
|
||||||
|
|
||||||
|
return { video, access, project: video.project };
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user