feat(arcade): add Change Game functionality for room hosts

Allows room hosts to return to game selection screen by clearing the
room's game selection. Adds useClearRoomGame hook and "Change Game"
menu item in room dropdown (only visible when a game is selected).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Thomas Hallock
2025-10-14 12:30:22 -05:00
parent f07b96d26e
commit ee39241e3c
2 changed files with 81 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'
import { useRouter } from 'next/navigation'
import { useEffect, useState } from 'react'
import { useLeaveRoom, useRoomData } from '@/hooks/useRoomData'
import { useClearRoomGame, useLeaveRoom, useRoomData } from '@/hooks/useRoomData'
import { useViewerId } from '@/hooks/useViewerId'
import { getRoomDisplayWithEmoji } from '@/utils/room-display'
import { CreateRoomModal } from './CreateRoomModal'
@@ -62,6 +62,7 @@ export function RoomInfo({
const { getRoomShareUrl, roomData } = useRoomData()
const { data: currentUserId } = useViewerId()
const { mutateAsync: leaveRoom } = useLeaveRoom()
const { mutate: clearRoomGame } = useClearRoomGame()
// Use room display utility for consistent naming
const displayName = joinCode
@@ -403,6 +404,43 @@ export function RoomInfo({
</DropdownMenu.Item>
)}
{/* Change Game - only show for host and only when a game is selected */}
{isCurrentUserCreator && roomId && roomData?.gameName && (
<DropdownMenu.Item
onSelect={() => {
if (roomId) {
clearRoomGame(roomId)
}
}}
style={{
display: 'flex',
alignItems: 'center',
gap: '10px',
padding: '10px 14px',
borderRadius: '8px',
border: 'none',
background: 'transparent',
color: 'rgba(209, 213, 219, 1)',
fontSize: '14px',
fontWeight: '500',
cursor: 'pointer',
outline: 'none',
transition: 'all 0.2s ease',
}}
onMouseEnter={(e) => {
e.currentTarget.style.background = 'rgba(236, 72, 153, 0.2)'
e.currentTarget.style.color = 'rgba(249, 168, 212, 1)'
}}
onMouseLeave={(e) => {
e.currentTarget.style.background = 'transparent'
e.currentTarget.style.color = 'rgba(209, 213, 219, 1)'
}}
>
<span style={{ fontSize: '16px' }}>🔄</span>
<span>Change Game</span>
</DropdownMenu.Item>
)}
{/* Moderation - only show for host */}
{isCurrentUserCreator && roomId && (
<DropdownMenu.Item

View File

@@ -604,3 +604,45 @@ export function useSetRoomGame() {
},
})
}
/**
* Clear/reset game for a room (host only)
*/
async function clearRoomGameApi(roomId: string): Promise<void> {
const response = await fetch(`/api/arcade/rooms/${roomId}/settings`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
gameName: null,
gameConfig: null,
}),
})
if (!response.ok) {
const errorData = await response.json()
throw new Error(errorData.error || 'Failed to clear room game')
}
}
/**
* Hook: Clear/reset game for a room (returns to game selection screen)
*/
export function useClearRoomGame() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: clearRoomGameApi,
onSuccess: () => {
// Update the cache to clear the game
queryClient.setQueryData<RoomData | null>(roomKeys.current(), (prev) => {
if (!prev) return null
return {
...prev,
gameName: null,
}
})
// Refetch to get the full updated room data
queryClient.invalidateQueries({ queryKey: roomKeys.current() })
},
})
}