mud_server.admin_gradio.ui.state

UI state builders for Gradio interface.

This module provides functions that build Gradio UI state updates based on application state. It encapsulates the complex tuple structures returned by various UI operations.

By separating UI state management from API logic, we: - Make API functions testable without Gradio dependencies - Centralize UI update patterns for consistency - Make it easier to modify the UI layout without changing API code

State Building Pattern:

UI state builders return tuples in a fixed order matching the Gradio component structure. Each builder documents its return tuple structure.

Functions

build_logged_in_state(session_state, message[, ...])

Build UI state for successful login.

build_logged_out_state(session_state, message)

Build UI state for logged out user.

build_login_failed_state(session_state, error_message)

Build UI state for failed login attempt.

is_admin_role(role)

Check if a role has admin access.

update_session_state(session_state, session_id, ...[, ...])

Update session state with login information.

clear_session_state(session_state)

Clear session state on logout.

Module Contents

mud_server.admin_gradio.ui.state.build_logged_in_state(session_state, message, has_admin_access=False)[source]

Build UI state for successful login.

When a user logs in successfully, we: - Clear password fields for security - Show game-related tabs - Show admin tabs if user has admin/superuser role - Keep login tab visible to show success message

Parameters:
  • session_state (dict) – Updated session state dictionary

  • message (str) – Success message to display

  • has_admin_access (bool) – Whether user has admin or superuser role

Returns:

Tuple of (session_state, message, clear_username, clear_password,

login_tab, register_tab, game_tab, settings_tab, db_tab, ollama_tab, help_tab)

Return type:

tuple

mud_server.admin_gradio.ui.state.build_logged_out_state(session_state, message)[source]

Build UI state for logged out user.

When a user is logged out (or logout fails), we: - Clear sensitive fields - Hide all game and admin tabs - Show only login and register tabs

Parameters:
  • session_state (dict) – Updated session state dictionary

  • message (str) – Message to display (success or error)

Returns:

Tuple of (session_state, message, blank, login_tab, register_tab,

game_tab, settings_tab, db_tab, ollama_tab, help_tab)

Return type:

tuple

mud_server.admin_gradio.ui.state.build_login_failed_state(session_state, error_message)[source]

Build UI state for failed login attempt.

When login fails, we: - Clear password field for security (but preserve username) - Keep login/register tabs visible - Keep all other tabs hidden - Display error message

Parameters:
  • session_state (dict) – Current session state (unchanged)

  • error_message (str) – Error message to display

Returns:

Tuple of (session_state, error_message, preserve_username, clear_password,

login_tab, register_tab, game_tab, settings_tab, db_tab, ollama_tab, help_tab)

Return type:

tuple

mud_server.admin_gradio.ui.state.is_admin_role(role)[source]

Check if a role has admin access.

Parameters:

role (str) – User role string

Returns:

True if role is admin or superuser, False otherwise

Return type:

bool

Examples

>>> is_admin_role("admin")
True
>>> is_admin_role("superuser")
True
>>> is_admin_role("player")
False
>>> is_admin_role("worldbuilder")
False
mud_server.admin_gradio.ui.state.update_session_state(session_state, session_id, username, role, logged_in=True)[source]

Update session state with login information.

This is a pure function that creates a new session state dictionary with updated values.

Parameters:
  • session_state (dict) – Current session state dictionary

  • session_id (str) – New session ID from backend

  • username (str) – Username that logged in

  • role (str) – User’s role (player/worldbuilder/admin/superuser)

  • logged_in (bool) – Login status (default: True)

Returns:

Updated session state dictionary

Return type:

dict

Examples

>>> state = {}
>>> new_state = update_session_state(state, "abc123", "alice", "player")
>>> new_state["session_id"]
'abc123'
>>> new_state["username"]
'alice'
>>> new_state["logged_in"]
True
mud_server.admin_gradio.ui.state.clear_session_state(session_state)[source]

Clear session state on logout.

This is a pure function that resets all session fields to None/False.

Parameters:

session_state (dict) – Current session state dictionary

Returns:

Cleared session state dictionary

Return type:

dict

Examples

>>> state = {"session_id": "abc", "username": "alice", "role": "player", "logged_in": True}
>>> cleared = clear_session_state(state)
>>> cleared["logged_in"]
False
>>> cleared["session_id"] is None
True