All files / web/src/components/nav HistoricalPlayersInvite.tsx

0% Statements 0/202
0% Branches 0/1
0% Functions 0/1
0% Lines 0/202

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                                                                                                                                                                                                                                                                                                                                                                                                                     
import { useState, useEffect } from 'react'
import { useRoomData } from '@/hooks/useRoomData'
import { useToast } from '@/components/common/ToastContext'

interface HistoricalMember {
  userId: string
  displayName: string
  firstJoinedAt: string
  lastSeenAt: string
  status: 'active' | 'banned' | 'kicked' | 'left'
  isCurrentlyInRoom: boolean
  isBanned: boolean
}

/**
 * Component to show historical players who are not currently in the room
 * with invite buttons to bring them back
 */
export function HistoricalPlayersInvite() {
  const { roomData } = useRoomData()
  const [historicalMembers, setHistoricalMembers] = useState<HistoricalMember[]>([])
  const [isLoading, setIsLoading] = useState(false)
  const [invitingUserId, setInvitingUserId] = useState<string | null>(null)
  const { showSuccess, showError } = useToast()

  // Fetch historical members
  useEffect(() => {
    if (!roomData?.id) return

    const loadHistoricalMembers = async () => {
      setIsLoading(true)
      try {
        const res = await fetch(`/api/arcade/rooms/${roomData.id}/history`)
        if (res.ok) {
          const data = await res.json()
          // Filter to only show members who are NOT currently in the room and NOT banned
          const notInRoom = (data.historicalMembers || []).filter(
            (m: HistoricalMember) => !m.isCurrentlyInRoom && !m.isBanned
          )
          setHistoricalMembers(notInRoom)
        }
      } catch (err) {
        console.error('Failed to load historical members:', err)
      } finally {
        setIsLoading(false)
      }
    }

    loadHistoricalMembers()
  }, [roomData?.id])

  const handleInvite = async (userId: string, displayName: string) => {
    if (!roomData?.id) return

    setInvitingUserId(userId)
    try {
      const res = await fetch(`/api/arcade/rooms/${roomData.id}/invite`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ userId, userName: displayName }),
      })

      if (!res.ok) {
        const errorData = await res.json()
        throw new Error(errorData.error || 'Failed to send invitation')
      }

      showSuccess(`Invitation sent to ${displayName}`)

      // Remove from list after inviting
      setHistoricalMembers((prev) => prev.filter((m) => m.userId !== userId))
    } catch (err) {
      showError(err instanceof Error ? err.message : 'Failed to send invitation')
    } finally {
      setInvitingUserId(null)
    }
  }

  if (!roomData?.id) {
    return null
  }

  if (isLoading) {
    return (
      <div
        style={{
          padding: '16px',
          textAlign: 'center',
          fontSize: '13px',
          color: '#6b7280',
        }}
      >
        Loading past players...
      </div>
    )
  }

  if (historicalMembers.length === 0) {
    return null
  }

  return (
    <div
      style={{
        display: 'flex',
        flexDirection: 'column',
        gap: '12px',
      }}
    >
      <div
        style={{
          fontSize: '12px',
          fontWeight: 700,
          color: '#6b7280',
          textTransform: 'uppercase',
          letterSpacing: '0.5px',
        }}
      >
        Past Players
      </div>

      <div
        style={{
          display: 'flex',
          flexDirection: 'column',
          gap: '8px',
        }}
      >
        {historicalMembers.map((member) => (
          <div
            key={member.userId}
            style={{
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'space-between',
              padding: '10px 12px',
              background: 'rgba(255, 255, 255, 0.5)',
              borderRadius: '8px',
              border: '1px solid rgba(139, 92, 246, 0.2)',
            }}
          >
            <div style={{ flex: 1 }}>
              <div
                style={{
                  fontSize: '14px',
                  fontWeight: 600,
                  color: '#1e293b',
                }}
              >
                {member.displayName}
              </div>
              <div
                style={{
                  fontSize: '11px',
                  color: '#64748b',
                  marginTop: '2px',
                }}
              >
                Last seen: {new Date(member.lastSeenAt).toLocaleDateString()}
              </div>
            </div>

            <button
              type="button"
              onClick={() => handleInvite(member.userId, member.displayName)}
              disabled={invitingUserId === member.userId}
              style={{
                padding: '6px 14px',
                fontSize: '12px',
                fontWeight: 700,
                borderRadius: '6px',
                border: 'none',
                background:
                  invitingUserId === member.userId
                    ? '#d1d5db'
                    : 'linear-gradient(135deg, #8b5cf6, #7c3aed)',
                color: 'white',
                cursor: invitingUserId === member.userId ? 'not-allowed' : 'pointer',
                transition: 'all 0.2s ease',
                opacity: invitingUserId === member.userId ? 0.6 : 1,
              }}
              onMouseEnter={(e) => {
                if (invitingUserId !== member.userId) {
                  e.currentTarget.style.background = 'linear-gradient(135deg, #7c3aed, #6d28d9)'
                  e.currentTarget.style.transform = 'scale(1.05)'
                }
              }}
              onMouseLeave={(e) => {
                if (invitingUserId !== member.userId) {
                  e.currentTarget.style.background = 'linear-gradient(135deg, #8b5cf6, #7c3aed)'
                  e.currentTarget.style.transform = 'scale(1)'
                }
              }}
            >
              {invitingUserId === member.userId ? 'Inviting...' : 'Invite'}
            </button>
          </div>
        ))}
      </div>
    </div>
  )
}