-
Notifications
You must be signed in to change notification settings - Fork 1
feat(arcade): update components for arcade native #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5917c32
a7e4b83
2f03d9c
1118855
2cef01d
f5eba16
e3039c2
b739d1c
045d66d
239e00a
2e6703e
e5636e8
6afc678
58104bc
648dd74
598d07f
b6ce62b
602fd6b
45a4c12
bac7189
c574d66
954379b
e9c7fc4
a3e28c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| import { createContext, type PropsWithChildren } from "react"; | ||
| import arcadeMock from "./arcade.mock.json"; | ||
| /** | ||
| * Arcade Context - REAL DATA from Torii | ||
| * Provides games and editions data | ||
| */ | ||
| import { createContext, type PropsWithChildren, useMemo, useRef, useEffect, useState } from "react"; | ||
| import { useGames, type Game as BaseGame } from "../../../../../../hooks/useGames"; | ||
|
|
||
| const CHAIN_ID = "SN_MAIN"; | ||
|
|
||
|
|
@@ -8,22 +12,98 @@ export interface ProjectProps { | |
| project: string; | ||
| } | ||
|
|
||
| const initialState = { | ||
| export type Game = BaseGame & { id: number }; | ||
|
|
||
| export interface Edition { | ||
| id: string; | ||
| gameId: number; | ||
| config?: { | ||
| project?: string; | ||
| }; | ||
| } | ||
|
|
||
| const emptyState = { | ||
| chainId: CHAIN_ID, | ||
| provider: {}, | ||
| pins: {}, | ||
| follows: {}, | ||
| accesses: [], | ||
| games: arcadeMock.games, | ||
| editions: arcadeMock.editions, | ||
| games: [], | ||
| gamesList: [], // Lightweight list for UI | ||
| editions: [], | ||
| chains: [], | ||
| clients: {}, | ||
| player: undefined, | ||
| setPlayer: () => {}, | ||
| version: 0, | ||
| }; | ||
|
|
||
| export const ArcadeContext = createContext(initialState); | ||
| export const ArcadeContext = createContext(emptyState); | ||
|
|
||
| export function ArcadeProvider(props: PropsWithChildren) { | ||
| return <ArcadeContext.Provider value={initialState} {...props} />; | ||
| let baseGames: BaseGame[] = []; | ||
|
|
||
| try { | ||
| const result = useGames(); | ||
| baseGames = result.games; | ||
| } catch (err) { | ||
| console.error('❌ ArcadeProvider: Failed to load games', err); | ||
| baseGames = []; | ||
| } | ||
|
|
||
| // Use refs for stable data + version counter for updates | ||
| const gamesRef = useRef<Game[]>([]); | ||
| const editionsRef = useRef<Edition[]>([]); | ||
| // Create a lightweight games list with only simple values for UI | ||
| const gamesListRef = useRef<Array<{id: number, name: string, icon?: string, color?: string}>>([]); | ||
| const [version, setVersion] = useState(0); | ||
|
|
||
| // Update refs and version when games count changes | ||
| useEffect(() => { | ||
| if (baseGames.length !== gamesRef.current.length) { | ||
| console.log('🎮 ArcadeProvider: Updating games -', baseGames.length, 'games'); | ||
|
|
||
| // Convert games | ||
| const newGames = baseGames.map((game, index) => ({ | ||
| ...game, | ||
| id: parseInt(game.id) || index, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Game ID zero incorrectly replaced by array indexThe expression |
||
| })); | ||
|
|
||
| // Create lightweight list for UI (only simple values!) | ||
| const newGamesList = newGames.map(g => ({ | ||
| id: g.id, | ||
| name: g.name, | ||
| icon: g.properties?.icon || undefined, | ||
| color: g.color || undefined, | ||
| })); | ||
|
|
||
| // Create editions | ||
| const newEditions = newGames.map((game) => ({ | ||
| id: game.id.toString(), | ||
| gameId: game.id, | ||
| config: { | ||
| project: `arcade-${game.name.toLowerCase().replace(/\s+/g, '-')}`, | ||
| }, | ||
| })); | ||
|
|
||
| gamesRef.current = newGames; | ||
| gamesListRef.current = newGamesList; | ||
| editionsRef.current = newEditions; | ||
|
|
||
| // Increment version to trigger re-renders | ||
| setVersion(v => v + 1); | ||
| } | ||
| }, [baseGames.length]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: useEffect only updates games when count changesThe |
||
|
|
||
| const value = useMemo(() => ({ | ||
| ...emptyState, | ||
| games: gamesRef.current, | ||
| gamesList: gamesListRef.current, // Expose lightweight list | ||
| editions: editionsRef.current, | ||
| version, // Include version so consumers can depend on it | ||
| }), [version]); // Only depend on version number! | ||
|
|
||
| console.log('🎮 ArcadeProvider: Providing', gamesRef.current.length, 'real games (v', version, ')'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Debug console.log statements left in codeMultiple debug Additional Locations (1) |
||
|
|
||
| return <ArcadeContext.Provider value={value} {...props} />; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: React hook called inside try-catch block
The
useGames()hook is called inside a try-catch block, which violates React's rules of hooks. Hooks must be called unconditionally at the top level of a component - they cannot be wrapped in try-catch, conditionals, or loops. While this code will execute, the try-catch won't actually catch errors thrown by the hook during React's render cycle. IfuseGames()throws, it will propagate up and cause rendering issues. The error handling approach here gives a false sense of safety without actually providing it.