All files / web/src/utils room-display.ts

100% Statements 126/126
100% Branches 10/10
100% Functions 3/3
100% Lines 126/126

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 1271x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 5x 5x 5x 5x 5x 5x 5x 5x 5x 8x 8x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 3x 3x  
/**
 * Utility for displaying room names consistently across the codebase
 */
 
export interface RoomDisplayData {
  /**
   * The room's custom name if provided
   */
  name: string | null
  /**
   * The room's unique code (e.g., "ABC123")
   */
  code: string
  /**
   * The game type (optional, for emoji selection)
   */
  gameName?: string
}
 
export interface RoomDisplay {
  /**
   * Plain text representation - ALWAYS available
   * Use this for: document titles, logs, notifications, plaintext contexts
   */
  plaintext: string
 
  /**
   * Primary display text (without emoji)
   */
  primary: string
 
  /**
   * Secondary/subtitle text (optional)
   */
  secondary?: string
 
  /**
   * Emoji/icon for the room (optional)
   */
  emoji?: string
 
  /**
   * Whether the name was auto-generated (vs. custom)
   */
  isGenerated: boolean
}
 
const GAME_EMOJIS: Record<string, string> = {
  matching: '🃏',
  'memory-quiz': '🧠',
  'complement-race': '⚡',
}
 
const DEFAULT_EMOJI = '🎮'
 
/**
 * Get structured room display information
 *
 * @example
 * // Custom named room
 * const display = getRoomDisplay({ name: "Alice's Room", code: "ABC123" })
 * // => { plaintext: "Alice's Room", primary: "Alice's Room", secondary: "ABC123", emoji: undefined, isGenerated: false }
 *
 * @example
 * // Auto-generated (no name)
 * const display = getRoomDisplay({ name: null, code: "ABC123", gameName: "matching" })
 * // => { plaintext: "Room ABC123", primary: "ABC123", secondary: undefined, emoji: "🃏", isGenerated: true }
 */
export function getRoomDisplay(room: RoomDisplayData): RoomDisplay {
  if (room.name) {
    // Custom name provided
    return {
      plaintext: room.name,
      primary: room.name,
      secondary: room.code,
      emoji: undefined,
      isGenerated: false,
    }
  }
 
  // Auto-generate display
  const emoji = GAME_EMOJIS[room.gameName || ''] || DEFAULT_EMOJI
 
  return {
    plaintext: `Room ${room.code}`, // Always plaintext fallback
    primary: room.code,
    secondary: undefined,
    emoji,
    isGenerated: true,
  }
}
 
/**
 * Get plaintext room name (shorthand)
 * Use this when you just need a string representation
 *
 * @example
 * getRoomDisplayName({ name: "Alice's Room", code: "ABC123" })
 * // => "Alice's Room"
 *
 * @example
 * getRoomDisplayName({ name: null, code: "ABC123" })
 * // => "Room ABC123"
 */
export function getRoomDisplayName(room: RoomDisplayData): string {
  return getRoomDisplay(room).plaintext
}
 
/**
 * Get room display with emoji (for rich contexts)
 *
 * @example
 * getRoomDisplayWithEmoji({ name: "Alice's Room", code: "ABC123" })
 * // => "Alice's Room"
 *
 * @example
 * getRoomDisplayWithEmoji({ name: null, code: "ABC123", gameName: "matching" })
 * // => "🃏 ABC123"
 */
export function getRoomDisplayWithEmoji(room: RoomDisplayData): string {
  const display = getRoomDisplay(room)
  if (display.emoji) {
    return `${display.emoji} ${display.primary}`
  }
  return display.primary
}