"use client" import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Spinner } from "@/components/ui/spinner"; import { useMutation, useQuery } from "convex/react"; import { CheckIcon, UserPlusIcon, XIcon } from "lucide-react"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import { api } from "../../../../convex/_generated/api"; interface FriendRequestModalProps { open: boolean; onOpenChange: (open: boolean) => void; } export default function FriendRequestModal({ open, onOpenChange, }: FriendRequestModalProps) { const getFriendRequests = useQuery(api.auth.getFriendRequests); const sendFriendRequest = useMutation(api.auth.sendFriendRequest); const answerFriendRequest = useMutation(api.auth.answerFriendRequest); const [username, setUsername] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [activeTab, setActiveTab] = useState<"send" | "pending" | "sent">("send"); const [pendingRequests, setPendingRequests] = useState([]); const [sentRequests, setSentRequests] = useState([]); useEffect(() => { if (getFriendRequests) { if (!getFriendRequests || getFriendRequests.length === 0) { console.debug("[FriendRequestModal] > Such a sad day, no friend requests found") setPendingRequests([]); setSentRequests([]); return; } console.debug("[FriendRequestModal] > This guy is important, look at him with his big friend request list (¬.¬) :", getFriendRequests); setPendingRequests(getFriendRequests.filter((request: any) => request.method === "receive")); setSentRequests(getFriendRequests.filter((request: any) => request.method === "send")); } }, [getFriendRequests]); const handleSendRequest = async () => { if (!username.trim()) return; setIsLoading(true); setError(null); try { await sendFriendRequest({ username: username, }); toast.success("Friend request sent successfully"); setUsername(""); } catch (err) { setError(err instanceof Error ? err.message : "Failed to send friend request"); } finally { setIsLoading(false); } }; const handleAccept = async (requestId: string) => { setIsLoading(true); try { await answerFriendRequest({ requestId: requestId, answer: "accept", }); } catch (err) { setError(err instanceof Error ? err.message : "Failed to accept request"); } finally { setIsLoading(false); } }; const handleDecline = async (requestId: string) => { setIsLoading(true); try { await answerFriendRequest({ requestId: requestId, answer: "decline", }); } catch (err) { setError(err instanceof Error ? err.message : "Failed to decline request"); } finally { setIsLoading(false); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !isLoading) { handleSendRequest(); } }; const formatTimeAgo = (timestamp: number) => { const seconds = Math.floor((Date.now() - timestamp) / 1000); if (seconds < 60) return "just now"; const minutes = Math.floor(seconds / 60); if (minutes < 60) return `${minutes}m ago`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours}h ago`; const days = Math.floor(hours / 24); return `${days}d ago`; }; return ( Friend Requests Send, accept, or manage your friend requests. {/* Tabs */}
{/* Content */}
{activeTab === "send" && (
setUsername(e.target.value)} onKeyDown={handleKeyDown} disabled={isLoading} /> {error && (

{error}

)}
)} {activeTab === "pending" && (
{pendingRequests.length === 0 ? (

No pending friend requests

) : ( pendingRequests.map((request) => (
{request.username.charAt(0).toUpperCase()}
{request.username} {formatTimeAgo(request.createdAt)}
)) )}
)} {activeTab === "sent" && (
{sentRequests.length === 0 ? (

No sent friend requests

) : ( sentRequests.map((request) => (
{request.username.charAt(0).toUpperCase()}
{request.username} Sent {formatTimeAgo(request.createdAt)}
Pending...
)) )}
)}
); }