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 | import { useCallback, useEffect, useRef, useState } from 'react' import { useCreateRoom, useRoomData } from '@/hooks/useRoomData' import { RoomShareButtons } from './RoomShareButtons' import { HistoricalPlayersInvite } from './HistoricalPlayersInvite' /** * Tab content for inviting players to a room * * Behavior: * - If user is already in a room: Shows share buttons immediately * - If user is NOT in a room: Lazily creates a room on mount, then shows share buttons * - Shows loading state during room creation * - Shows error state if room creation fails */ export function InvitePlayersTab() { const { roomData, isInRoom, getRoomShareUrl, isLoading: isRoomDataLoading } = useRoomData() const { mutateAsync: createRoom, isPending: isCreating } = useCreateRoom() const [error, setError] = useState<string | null>(null) const hasAttemptedCreation = useRef(false) // Lazy room creation: only create if not already in a room const createQuickRoom = useCallback(async () => { if (isRoomDataLoading || isInRoom || isCreating || hasAttemptedCreation.current) { return // Already in a room, loading, creating, or already attempted } hasAttemptedCreation.current = true setError(null) try { await createRoom({ name: 'Quick Room', gameName: 'matching', creatorName: 'Player', gameConfig: { difficulty: 6, gameType: 'abacus-numeral', turnTimer: 30, }, }) // Room will be automatically updated in cache by the mutation's onSuccess } catch (err) { console.error('[InvitePlayersTab] Failed to create room:', err) setError(err instanceof Error ? err.message : 'Failed to create room') hasAttemptedCreation.current = false // Allow retry on error } }, [isRoomDataLoading, isInRoom, isCreating, createRoom]) // Auto-create room on mount if not in one useEffect(() => { if (!isRoomDataLoading && !isInRoom && !isCreating && !error && !hasAttemptedCreation.current) { createQuickRoom() } }, [isRoomDataLoading, isInRoom, isCreating, error, createQuickRoom]) // Loading state - show loading only if we're truly loading and not in a room yet if ((isRoomDataLoading || isCreating) && !isInRoom && !error) { return ( <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: '24px 16px', gap: '12px', }} > <div style={{ width: '32px', height: '32px', border: '3px solid rgba(139, 92, 246, 0.3)', borderTop: '3px solid rgba(139, 92, 246, 1)', borderRadius: '50%', animation: 'spin 0.8s linear infinite', }} /> <div style={{ fontSize: '13px', color: '#6b7280', fontWeight: 500, }} > Creating room... </div> <style dangerouslySetInnerHTML={{ __html: ` @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } `, }} /> </div> ) } // Error state if (error) { return ( <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', padding: '20px 16px', gap: '12px', }} > <div style={{ fontSize: '32px' }}>⚠️</div> <div style={{ fontSize: '13px', color: '#ef4444', fontWeight: 500, textAlign: 'center', }} > {error} </div> <button type="button" onClick={() => { setError(null) createQuickRoom() }} style={{ padding: '8px 16px', background: 'linear-gradient(135deg, rgba(139, 92, 246, 0.2), rgba(139, 92, 246, 0.3))', border: '2px solid rgba(139, 92, 246, 0.4)', borderRadius: '8px', color: '#8b5cf6', fontSize: '13px', fontWeight: 600, cursor: 'pointer', transition: 'all 0.2s ease', }} onMouseEnter={(e) => { e.currentTarget.style.background = 'linear-gradient(135deg, rgba(139, 92, 246, 0.3), rgba(139, 92, 246, 0.4))' e.currentTarget.style.borderColor = 'rgba(139, 92, 246, 0.6)' }} onMouseLeave={(e) => { e.currentTarget.style.background = 'linear-gradient(135deg, rgba(139, 92, 246, 0.2), rgba(139, 92, 246, 0.3))' e.currentTarget.style.borderColor = 'rgba(139, 92, 246, 0.4)' }} > Try Again </button> </div> ) } // Success state: Show room share buttons if (isInRoom && roomData) { const shareUrl = getRoomShareUrl(roomData.code) return ( <div style={{ display: 'flex', flexDirection: 'column', padding: '12px 8px 8px 8px', gap: '8px', }} > <div style={{ fontSize: '11px', fontWeight: 600, color: '#6b7280', textAlign: 'center', marginBottom: '4px', }} > Share to invite players </div> <RoomShareButtons joinCode={roomData.code} shareUrl={shareUrl} /> {/* Historical players who can be invited back */} <div style={{ marginTop: '8px' }}> <HistoricalPlayersInvite /> </div> </div> ) } // Fallback (should not reach here) return null } |