import { useState } from 'react'; import { api } from '../lib/api.ts'; interface InviteModalProps { serverId: string; serverName: string; onClose: () => void; } interface InviteResult { code: string; server_name: string; max_uses: number | null; use_count: number; expires_at: string | null; } export function InviteModal({ serverId, serverName, onClose }: InviteModalProps) { const [expiresHours, setExpiresHours] = useState(24); const [maxUses, setMaxUses] = useState(''); const [invite, setInvite] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [copied, setCopied] = useState(false); const createInvite = async () => { setLoading(true); setError(null); try { const payload: Record = { expires_hours: expiresHours, }; if (maxUses !== '') { payload.max_uses = maxUses; } const result = await api.post(`/servers/${serverId}/invites`, payload); setInvite(result); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to create invite'); } finally { setLoading(false); } }; const inviteUrl = invite ? `${window.location.origin}/invites/${invite.code}` : ''; const copyLink = () => { if (inviteUrl) { navigator.clipboard.writeText(inviteUrl); setCopied(true); setTimeout(() => setCopied(false), 2000); } }; return (
e.stopPropagation()}>
INVITE TO {serverName.toUpperCase()}
{!invite ? (
setMaxUses(e.target.value === '' ? '' : Number(e.target.value))} placeholder="unlimited" min={1} className="w-full px-2 py-1 bg-gb-bg-s text-gb-fg text-xs border-none outline-none" />
{error && (
{error}
)}
) : (
Share this link:
{invite.expires_at && (
Expires: {new Date(invite.expires_at).toLocaleString()}
)} {invite.max_uses && (
Max uses: {invite.max_uses}
)}
)}
); }