"use client"; import { useState, useEffect } from "react"; interface ShareMenuProps { targetType: "DECK" | "QUIZ" | "GROUP"; contentId: string; classSlug: string; } export function ShareMenu({ targetType, contentId, classSlug }: ShareMenuProps) { const [token, setToken] = useState(null); const [loading, setLoading] = useState(true); const [open, setOpen] = useState(false); const [copied, setCopied] = useState(false); const [isGroupShared, setIsGroupShared] = useState(false); const [groupName, setGroupName] = useState(null); useEffect(() => { if (!open) return; fetch(`/api/share?targetType=${targetType}&contentId=${contentId}`) .then(res => res.json()) .then(data => { setToken(data.token); setIsGroupShared(data.isGroupShared || false); setGroupName(data.groupName || null); }) .finally(() => setLoading(false)); }, [open, targetType, contentId]); async function handleToggle() { setLoading(true); try { const res = await fetch("/api/share", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ targetType, contentId }), }); const data = await res.json(); setToken(data.token); } finally { setLoading(false); } } async function handleCopy() { if (!token) return; const typeStr = isGroupShared ? "groups" : targetType === "GROUP" ? "groups" : targetType === "DECK" ? "flashcards" : "quizzes"; const itemQuery = isGroupShared ? `?itemId=${encodeURIComponent(contentId)}` : ""; const url = `${window.location.origin}/shared/${classSlug}/${typeStr}/${token}${itemQuery}`; await navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 2000); } return (
{open && ( <>
setOpen(false)} />

Public sharing

{loading ? (
) : (
Enable link sharing
{isGroupShared && (
This item is publicly shared via its group: {groupName}. Turn off group sharing to restrict access.
)} {(token || isGroupShared) && (

Anyone with the link can view and study this {targetType === "GROUP" ? "group" : (targetType === "DECK" ? "deck" : "quiz")}. No progress is saved.

)}
)}
)}
); }