"use client"; import { API_URL } from "@/config"; import { useNotifications } from "@/context/NotificationContext"; import { useSession } from "@/context/session"; import { useState } from "react"; interface Friend { id: number; friend_id: number; friend_name: string; } export default function InviteFriendsModal({ gameCode, label = "Freunde einladen" }: { gameCode: string; label?: string; }) { const { user } = useSession(); const { sendGameInvite } = useNotifications(); const [open, setOpen] = useState(false); const [friends, setFriends] = useState([]); const [loading, setLoading] = useState(false); const [sent, setSent] = useState>({}); async function openModal() { if (!user?.id || typeof user.id === "string") return; setOpen(true); setLoading(true); const res = await fetch(`${API_URL}/v1/friends`, { credentials: "include" }); if (res.ok) setFriends(await res.json()); setLoading(false); } function invite(friendId: number) { sendGameInvite(friendId, gameCode); setSent((prev) => ({ ...prev, [friendId]: true })); setTimeout(() => setSent((prev) => ({ ...prev, [friendId]: false })), 3000); } if (!user?.id || typeof user.id === "string") return null; return ( <> {open && (

{label}

{loading ? (
) : friends.length === 0 ? (

Keine Freunde gefunden. Füge zuerst Freunde hinzu.

) : (
{friends.map((f) => (
{f.friend_name}
))}
)}
setOpen(false)} />
)} ); }