Skip to main content

Frontend Context Providers

Context modules live in src/contexts and are responsible for cross-page state and socket lifecycle glue. Global providers are composed in _app.tsx. Game-only providers are created inside the game room page.

Provider composition

  • App shell: SocketProvider -> PartyProvider -> MatchmakingProvider -> FriendshipProvider
  • Game room only: GameStateProvider -> GameTestCasesProvider

SocketContextsrc/contexts/SocketContext.tsx

Purpose

  • Owns the singleton Socket.IO connection for the client.

State

FieldTypeDescription
socketSocket | undefinedLive Socket.IO client instance, set after the session loads.
setSocketDispatch<SetStateAction<Socket | undefined>>Manual override; used only internally.

Behavior

  • Waits for authClient.useSession() to return a user ID before creating the socket.
  • Connects using io({ autoConnect: true }) and emits register on connect.
  • Listens for error events and forwards messages through showErrorNotification.
  • Disconnects on unmount and clears socket state.

MatchmakingContextsrc/contexts/MatchmakingContext.tsx

Purpose

  • Shared state for queueing, difficulty selection, and match resolution.

State

FieldTypeDescription
status"idle" | "queued" | "matched" | "error"Queue state for the current user.
gameTypeGameTypeTWOPLAYER or FOURPLAYER.
difficultyProblemDifficultySelected difficulty for matchmaking.
gameIdstring | undefinedSet once a match is found or a room is created.

Socket events

  • Emits register when the socket is available and a session exists.
  • Listens for:
    • matchFound -> redirects to /game/[gameId] and sets status.
    • queueStatus -> updates status or marks errors.
    • receiveQueueSelection -> syncs game type/difficulty for party guests.
    • partySearchUpdate -> syncs queue state for party guests.
    • createdRoomFromHost -> redirects guests to the host-created room.

PartyContextsrc/contexts/PartyContext.tsx

Purpose

  • Tracks party membership, invites, and party join codes.

State

FieldTypeDescription
partyMemberPartyMember | nullParty guest (for the owner) when a party is formed.
joinedPartyPartyMember | nullThe party owner record when the current user is the guest.
pendingInvitePartyInvite | nullIncoming party invite currently awaiting response.
partyCodestring | nullOwner's party join code.

Behavior

  • On session load, fetches /api/party and hydrates the initial state.
  • Listens for:
    • partyMemberJoined -> fills the guest slot for the owner.
    • partyInviteReceived -> sets pendingInvite and shows a notification with accept/decline actions.
    • partyMemberLeft -> clears the guest slot on the owner view.
    • joinedPartyLeft -> clears joinedParty when the guest is removed.
  • Accept/decline actions emit partyInviteAccept / partyInviteDecline and clear local state.

FriendshipContextsrc/contexts/FriendshipContext.tsx

Purpose

  • Stores friends, friend requests, and the user's friend code.

State

FieldTypeDescription
friendsFriend[]Current friend list.
friendRequestsFriendRequest[]All friend requests (incoming + outgoing).
incomingRequestsFriendRequest[]Derived: direction === "incoming".
outgoingRequestsFriendRequest[]Derived: direction === "outgoing".
friendCodestring | nullUser's invite code for adding friends.

Behavior

  • On session load, fetches /api/friends and hydrates friends + requests.
  • Listens for:
    • friendRequestReceived -> adds an incoming request and opens an accept/decline notification.
    • friendRequestAccepted -> moves a request into friends.
    • friendRequestDeclined -> removes the request locally.
    • friendDeleted -> removes a friend from the list.

GameStateContextsrc/contexts/GameStateContext.tsx

Purpose

  • Game-room scoped state: team identity, game identity, and shared code buffer.

State

FieldTypeDescription
teamIdstring | undefinedTeam room ID once selected.
gameIdstring | undefinedGame room ID (URL param).
gameTypeGameType | undefinedCurrent game mode.
codestring | undefinedShared editor contents; defaults to a waiting placeholder.

GameTestCasesContextsrc/contexts/GameTestCasesContext.tsx

Purpose

  • Game-room scoped test cases and parameter definitions used by testers.

State

FieldTypeDescription
parametersParameterType[]Input/output parameter schema for each test.
casesTestableCase[]Active test cases. Defaults to DEFAULT_TEST_CASES.

Helpers

FunctionDescription
addCase(testCase)Appends a new test case.
removeCase(id)Removes a test case by ID.
updateCase(testCase)Replaces the test case with the same ID.

Defaults

  • Parameters start as a, b, and output result (output parameter).
  • DEFAULT_TEST_CASES contains a single test with inputs 2 and 3 expecting 5.