"use client"; import { useCallback, useMemo, useState } from "react"; import { Share2, Link, Clock, ShieldCheck, Copy, CheckCircle, AlertCircle, } from "lucide-react"; import { trpc } from "@/lib/trpc"; import { useToast } from "@/lib/toast"; import { Button } from "@micprotocol/ui"; import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog"; import { Input } from "@micprotocol/ui"; import { SectionHeader } from "@micprotocol/ui"; import { ShareType } from "@micprotocol/shared-types"; interface DocumentShareDialogProps { documentId: string; documentTitle: string; workspaceId: string; open: boolean; onOpenChange: (open: boolean) => void; onShareCreated?: () => void; } export default function DocumentShareDialog({ documentId, documentTitle, workspaceId, open, onOpenChange, onShareCreated, }: DocumentShareDialogProps) { const [shareType, setShareType] = useState(ShareType.PUBLIC); const [accessCode, setAccessCode] = useState(""); const [expiresAt, setExpiresAt] = useState(""); const [maxDownloads, setMaxDownloads] = useState(""); const [isCreating, setIsCreating] = useState(false); const [shareUrl, setShareUrl] = useState(null); const { success, error } = useToast(); const createShare = trpc.documents.createShare.useMutation({ onSuccess: (data) => { setIsCreating(false); setShareUrl(`/documents/shared/${data.id}`); success("Share link created"); onShareCreated?.(); }, onError: (err) => { setIsCreating(false); error(err.message || "Failed to create share link"); }, }); const handleCreate = async () => { setIsCreating(true); try { await createShare.mutateAsync({ documentId, workspaceId, shareType, accessCode: accessCode || undefined, expiresAt: expiresAt || undefined, maxDownloads: maxDownloads ? parseInt(maxDownloads, 10) : undefined, }); } catch (err) { setIsCreating(false); error(err instanceof Error ? err.message : "Failed to create share"); } }; const handleCopy = () => { if (!shareUrl) return; navigator.clipboard.writeText(shareUrl); success("Share link copied to clipboard"); }; const typeLabels: Record = { [ShareType.PUBLIC]: { label: "Public", description: "Anyone with the link can access" }, [ShareType.PROTECTED]: { label: "Protected", description: "Requires an access code" }, [ShareType.PRIVATE]: { label: "Private", description: "Only workspace members" }, }; return ( Create Share Link Share "{documentTitle}" with controlled access
{Object.values(ShareType).map((type) => { const info = typeLabels[type]; const selected = shareType === type; return ( ); })}
{shareType === ShareType.PROTECTED && (
setAccessCode(e.target.value)} placeholder="Enter access code" className="input" />
)}
setExpiresAt(e.target.value)} className="input" />
setMaxDownloads(e.target.value)} placeholder="Unlimited" className="input" />
{shareUrl && (
Share link ready: {shareUrl}
)}
); }