"use client"; import { useState } from "react"; import { Share2, Download, Eye, Link2, Copy, Check, Folder } from "lucide-react"; import toast from "react-hot-toast"; import Modal from "../ui/Modal"; import Button from "../ui/Button"; export interface ShareTarget { type: "file" | "folder"; id: string; name: string; } interface ShareModalProps { isOpen: boolean; onClose: () => void; target: ShareTarget; } export async function copyToClipboard(text: string): Promise { try { await navigator.clipboard.writeText(text); return true; } catch { const textarea = document.createElement("textarea"); textarea.value = text; textarea.style.position = "fixed"; textarea.style.opacity = "0"; document.body.appendChild(textarea); textarea.select(); try { document.execCommand("copy"); return true; } catch { return false; } finally { document.body.removeChild(textarea); } } } /** Création d'un lien de partage public (fichier ou dossier complet). */ export default function ShareModal({ isOpen, onClose, target }: ShareModalProps) { const [password, setPassword] = useState(""); const [expiry, setExpiry] = useState(""); const [mode, setMode] = useState<"DOWNLOAD" | "PREVIEW">("DOWNLOAD"); const [loading, setLoading] = useState(false); const [link, setLink] = useState(null); const [copied, setCopied] = useState(false); const close = () => { setLink(null); setPassword(""); setExpiry(""); onClose(); }; const create = async () => { setLoading(true); try { const res = await fetch("/api/shares", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ [target.type === "folder" ? "folderId" : "fileId"]: target.id, password: password || undefined, expiresAt: expiry ? new Date(expiry).toISOString() : undefined, mode, }), }); const data = await res.json().catch(() => ({})); if (res.ok) { const url = `${window.location.origin}/shared/${data.token}`; setLink(url); const ok = await copyToClipboard(url); toast.success(ok ? "Lien copié dans le presse-papiers" : "Lien de partage créé"); } else { toast.error(data.error || "Erreur lors du partage"); } } catch (err) { toast.error("Erreur réseau : " + (err as Error).message); } finally { setLoading(false); } }; const isFolder = target.type === "folder"; return ( {link ? (
Lien de partage créé
(e.target as HTMLInputElement).select()} />
{isFolder && (

Les visiteurs verront le contenu du dossier et de ses sous-dossiers (fichiers ajoutés plus tard inclus) et pourront tout télécharger en ZIP.

)}
) : (
{([["DOWNLOAD", "Téléchargement", Download], ["PREVIEW", "Aperçu seulement", Eye]] as const).map(([v, label, Icon]) => ( ))}
setPassword(e.target.value)} placeholder="Laisser vide pour aucun mot de passe" className="field" autoComplete="new-password" />
setExpiry(e.target.value)} className="field" min={new Date(Date.now() + 60000).toISOString().slice(0, 16)} />
)}
); }