Initial commit
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
|
||||
const schema = z.object({
|
||||
company: z.string().min(1, "Company name is required"),
|
||||
contactPerson: z.string().min(1, "Contact person is required"),
|
||||
email: z.string().email("Invalid email address"),
|
||||
phone: z.string().optional(),
|
||||
notes: z.string().optional(),
|
||||
});
|
||||
|
||||
type FormValues = z.infer<typeof schema>;
|
||||
|
||||
interface NewClientDialogProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function NewClientDialog({ children }: NewClientDialogProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const router = useRouter();
|
||||
const { toast } = useToast();
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
reset,
|
||||
formState: { errors },
|
||||
} = useForm<FormValues>({
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: { company: "", contactPerson: "", email: "", phone: "", notes: "" },
|
||||
});
|
||||
|
||||
const onSubmit = async (values: FormValues) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await fetch("/api/clients", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(values),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const error = await res.json().catch(() => ({}));
|
||||
throw new Error(error.error ?? "Failed to create client");
|
||||
}
|
||||
const data = await res.json();
|
||||
toast({ title: `Client "${data.company}" created` });
|
||||
setOpen(false);
|
||||
reset();
|
||||
router.refresh();
|
||||
} catch (e) {
|
||||
toast({
|
||||
title: "Failed to create client",
|
||||
description: e instanceof Error ? e.message : undefined,
|
||||
variant: "destructive",
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>{children}</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>New Client</DialogTitle>
|
||||
</DialogHeader>
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="company">Company *</Label>
|
||||
<Input id="company" placeholder="Acme Productions" {...register("company")} />
|
||||
{errors.company && <p className="text-xs text-red-400">{errors.company.message}</p>}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="contactPerson">Contact Person *</Label>
|
||||
<Input id="contactPerson" placeholder="Jane Smith" {...register("contactPerson")} />
|
||||
{errors.contactPerson && <p className="text-xs text-red-400">{errors.contactPerson.message}</p>}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="email">Email *</Label>
|
||||
<Input id="email" type="email" placeholder="jane@acme.com" {...register("email")} />
|
||||
{errors.email && <p className="text-xs text-red-400">{errors.email.message}</p>}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="phone">Phone</Label>
|
||||
<Input id="phone" type="tel" placeholder="+1 (555) 000-0000" {...register("phone")} />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="notes">Notes</Label>
|
||||
<Textarea id="notes" placeholder="Any additional information..." className="resize-none" rows={3} {...register("notes")} />
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button type="button" variant="outline" onClick={() => setOpen(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" disabled={loading}>
|
||||
{loading ? "Creating..." : "Create Client"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { ExternalLink, Copy, Check, Trash2, Clock } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
import { cn, formatRelativeDate } from "@/lib/utils";
|
||||
|
||||
interface ReviewSession {
|
||||
id: string;
|
||||
token: string;
|
||||
label: string | null;
|
||||
email: string | null;
|
||||
expiresAt: Date | string | null;
|
||||
accessCount: number;
|
||||
isActive: boolean;
|
||||
project: { name: string };
|
||||
}
|
||||
|
||||
interface ReviewSessionListProps {
|
||||
sessions: ReviewSession[];
|
||||
}
|
||||
|
||||
const APP_URL = process.env.NEXT_PUBLIC_APP_URL ?? "";
|
||||
|
||||
export function ReviewSessionList({ sessions }: ReviewSessionListProps) {
|
||||
const [copiedId, setCopiedId] = useState<string | null>(null);
|
||||
const [deletingId, setDeletingId] = useState<string | null>(null);
|
||||
const { toast } = useToast();
|
||||
const router = useRouter();
|
||||
|
||||
const handleCopy = async (token: string, id: string) => {
|
||||
const url = `${APP_URL}/client/${token}`;
|
||||
await navigator.clipboard.writeText(url);
|
||||
setCopiedId(id);
|
||||
setTimeout(() => setCopiedId(null), 2000);
|
||||
};
|
||||
|
||||
const handleDeactivate = async (id: string) => {
|
||||
setDeletingId(id);
|
||||
try {
|
||||
const res = await fetch(`/api/review-sessions?id=${id}`, { method: "DELETE" });
|
||||
if (!res.ok) throw new Error();
|
||||
toast({ title: "Review link deactivated" });
|
||||
router.refresh();
|
||||
} catch {
|
||||
toast({ title: "Failed to deactivate link", variant: "destructive" });
|
||||
} finally {
|
||||
setDeletingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
if (sessions.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2 className="text-base font-semibold text-white mb-3">
|
||||
Active Review Links ({sessions.length})
|
||||
</h2>
|
||||
<div className="space-y-2">
|
||||
{sessions.map((session) => {
|
||||
const portalUrl = `${APP_URL}/client/${session.token}`;
|
||||
const expired =
|
||||
session.expiresAt && new Date(session.expiresAt) < new Date();
|
||||
|
||||
return (
|
||||
<div
|
||||
key={session.id}
|
||||
className={cn(
|
||||
"flex items-center gap-4 p-4 rounded-xl border transition-all",
|
||||
expired
|
||||
? "border-zinc-800 bg-zinc-900/50 opacity-60"
|
||||
: "border-zinc-800 bg-zinc-900"
|
||||
)}
|
||||
>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<p className="font-medium text-white text-sm">{session.label || "Untitled Review"}</p>
|
||||
{expired && (
|
||||
<span className="text-xs px-1.5 py-0.5 rounded bg-red-500/10 text-red-400">
|
||||
Expired
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-3 mt-1">
|
||||
<span className="text-xs text-zinc-500">{session.project.name}</span>
|
||||
{session.email && (
|
||||
<>
|
||||
<span className="text-zinc-700">·</span>
|
||||
<span className="text-xs text-zinc-500">{session.email}</span>
|
||||
</>
|
||||
)}
|
||||
{session.expiresAt && (
|
||||
<>
|
||||
<span className="text-zinc-700">·</span>
|
||||
<span className="text-xs text-zinc-500 flex items-center gap-1">
|
||||
<Clock className="h-3 w-3" />
|
||||
{expired ? "Expired" : `Expires ${formatRelativeDate(session.expiresAt)}`}
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<p className="font-mono text-xs text-zinc-600 truncate mt-1">{portalUrl}</p>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-1.5 shrink-0">
|
||||
<span className="text-xs text-zinc-500 mr-2">
|
||||
{session.accessCount} view{session.accessCount !== 1 ? "s" : ""}
|
||||
</span>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="h-7 px-2.5 gap-1.5"
|
||||
onClick={() => handleCopy(session.token, session.id)}
|
||||
>
|
||||
{copiedId === session.id ? (
|
||||
<Check className="h-3.5 w-3.5 text-emerald-400" />
|
||||
) : (
|
||||
<Copy className="h-3.5 w-3.5" />
|
||||
)}
|
||||
{copiedId === session.id ? "Copied" : "Copy"}
|
||||
</Button>
|
||||
<a href={portalUrl} target="_blank" rel="noopener noreferrer">
|
||||
<Button size="sm" variant="outline" className="h-7 px-2">
|
||||
<ExternalLink className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
</a>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="h-7 px-2 text-red-400 hover:bg-red-500/10 border-red-500/20"
|
||||
onClick={() => handleDeactivate(session.id)}
|
||||
disabled={deletingId === session.id}
|
||||
>
|
||||
<Trash2 className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
import { Copy, Check, ExternalLink } from "lucide-react";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
|
||||
const schema = z.object({
|
||||
projectId: z.string().min(1, "Select a project"),
|
||||
label: z.string().min(1, "Label is required"),
|
||||
email: z.string().email("Invalid email"),
|
||||
expiresInDays: z.number().int().positive().default(30),
|
||||
});
|
||||
|
||||
type FormValues = z.infer<typeof schema>;
|
||||
|
||||
interface Project {
|
||||
id: string;
|
||||
name: string;
|
||||
code: string;
|
||||
}
|
||||
|
||||
interface ShareReviewDialogProps {
|
||||
children: React.ReactNode;
|
||||
clientId: string;
|
||||
clientEmail: string;
|
||||
projects: Project[];
|
||||
}
|
||||
|
||||
export function ShareReviewDialog({
|
||||
children,
|
||||
clientEmail,
|
||||
projects,
|
||||
}: ShareReviewDialogProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [portalUrl, setPortalUrl] = useState<string | null>(null);
|
||||
const [copied, setCopied] = useState(false);
|
||||
const { toast } = useToast();
|
||||
const router = useRouter();
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
watch,
|
||||
setValue,
|
||||
reset,
|
||||
formState: { errors },
|
||||
} = useForm<FormValues>({
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: {
|
||||
projectId: projects[0]?.id ?? "",
|
||||
label: "Review Round 1",
|
||||
email: clientEmail,
|
||||
expiresInDays: 30,
|
||||
},
|
||||
});
|
||||
|
||||
const handleCopy = async () => {
|
||||
if (!portalUrl) return;
|
||||
await navigator.clipboard.writeText(portalUrl);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
};
|
||||
|
||||
const handleReset = () => {
|
||||
setPortalUrl(null);
|
||||
reset({
|
||||
projectId: projects[0]?.id ?? "",
|
||||
label: "Review Round 1",
|
||||
email: clientEmail,
|
||||
expiresInDays: 30,
|
||||
});
|
||||
};
|
||||
|
||||
const onSubmit = async (values: FormValues) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await fetch("/api/review-sessions", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(values),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err.error ?? "Failed to create review link");
|
||||
}
|
||||
const data = await res.json();
|
||||
setPortalUrl(data.portalUrl);
|
||||
router.refresh();
|
||||
} catch (e) {
|
||||
toast({
|
||||
title: "Failed to create review link",
|
||||
description: e instanceof Error ? e.message : undefined,
|
||||
variant: "destructive",
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open={open}
|
||||
onOpenChange={(o) => {
|
||||
if (!o) { setPortalUrl(null); }
|
||||
setOpen(o);
|
||||
}}
|
||||
>
|
||||
<DialogTrigger asChild>{children}</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Share Review Link</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
{portalUrl ? (
|
||||
<div className="space-y-4">
|
||||
<p className="text-sm text-zinc-400">
|
||||
Your review link is ready. Copy it and share it with your client.
|
||||
</p>
|
||||
<div className="flex items-center gap-2 p-3 rounded-lg bg-zinc-800 border border-zinc-700">
|
||||
<ExternalLink className="h-4 w-4 text-zinc-500 shrink-0" />
|
||||
<span className="flex-1 text-sm font-mono text-zinc-300 truncate">{portalUrl}</span>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="h-7 px-2.5 gap-1.5 shrink-0"
|
||||
onClick={handleCopy}
|
||||
>
|
||||
{copied ? (
|
||||
<Check className="h-3.5 w-3.5 text-emerald-400" />
|
||||
) : (
|
||||
<Copy className="h-3.5 w-3.5" />
|
||||
)}
|
||||
{copied ? "Copied!" : "Copy"}
|
||||
</Button>
|
||||
</div>
|
||||
<DialogFooter className="flex gap-2">
|
||||
<Button variant="outline" onClick={handleReset}>
|
||||
Create Another
|
||||
</Button>
|
||||
<Button onClick={() => setOpen(false)}>Done</Button>
|
||||
</DialogFooter>
|
||||
</div>
|
||||
) : (
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="projectId">Project *</Label>
|
||||
<Select
|
||||
defaultValue={watch("projectId")}
|
||||
onValueChange={(v) => setValue("projectId", v)}
|
||||
>
|
||||
<SelectTrigger id="projectId">
|
||||
<SelectValue placeholder="Select project" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{projects.map((p) => (
|
||||
<SelectItem key={p.id} value={p.id}>
|
||||
{p.name} <span className="text-zinc-500 text-xs ml-1">({p.code})</span>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.projectId && <p className="text-xs text-red-400">{errors.projectId.message}</p>}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="label">Label *</Label>
|
||||
<Input id="label" placeholder="e.g. Review Round 1" {...register("label")} />
|
||||
{errors.label && <p className="text-xs text-red-400">{errors.label.message}</p>}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="email">Client Email *</Label>
|
||||
<Input id="email" type="email" {...register("email")} />
|
||||
{errors.email && <p className="text-xs text-red-400">{errors.email.message}</p>}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="expiresInDays">Expires In (days)</Label>
|
||||
<Select
|
||||
defaultValue={String(watch("expiresInDays"))}
|
||||
onValueChange={(v) => setValue("expiresInDays", Number(v))}
|
||||
>
|
||||
<SelectTrigger id="expiresInDays">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{[7, 14, 30, 60, 90].map((d) => (
|
||||
<SelectItem key={d} value={String(d)}>
|
||||
{d} days
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button type="button" variant="outline" onClick={() => setOpen(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" disabled={loading || projects.length === 0}>
|
||||
{loading ? "Generating..." : "Create Link"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user