All files / web/src/arcade-games/know-your-world/music MusicDebugPanel.tsx

0% Statements 0/192
0% Branches 0/1
0% Functions 0/1
0% Lines 0/192

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193                                                                                                                                                                                                                                                                                                                                                                                                 
/**
 * Music Debug Panel
 *
 * Shows the current Strudel pattern code being played,
 * similar to the Strudel REPL view.
 */

'use client'

import { useState } from 'react'
import { css } from '@styled/css'
import { useMusic } from './MusicContext'

interface MusicDebugPanelProps {
  /** Whether the panel starts expanded */
  defaultExpanded?: boolean
}

export function MusicDebugPanel({ defaultExpanded = false }: MusicDebugPanelProps) {
  const music = useMusic()
  const [isExpanded, setIsExpanded] = useState(defaultExpanded)

  if (!music.isPlaying && !music.currentPattern) {
    return null
  }

  return (
    <div
      data-component="music-debug-panel"
      className={css({
        position: 'fixed',
        bottom: '10px',
        right: '10px',
        zIndex: 9999,
        bg: 'gray.900',
        border: '1px solid',
        borderColor: 'gray.700',
        rounded: 'lg',
        shadow: 'xl',
        overflow: 'hidden',
        maxWidth: isExpanded ? '600px' : '200px',
        transition: 'all 0.2s',
      })}
    >
      {/* Header */}
      <button
        onClick={() => setIsExpanded(!isExpanded)}
        data-action="toggle-debug-panel"
        className={css({
          display: 'flex',
          alignItems: 'center',
          gap: '2',
          width: '100%',
          paddingY: '2',
          paddingX: '3',
          bg: 'gray.800',
          color: 'gray.100',
          fontSize: 'sm',
          fontWeight: 'medium',
          cursor: 'pointer',
          border: 'none',
          _hover: { bg: 'gray.700' },
        })}
      >
        <span>{music.isPlaying ? '🎵' : '⏸️'}</span>
        <span>Music Debug</span>
        <span className={css({ ml: 'auto', color: 'gray.400' })}>{isExpanded ? '▼' : '▲'}</span>
      </button>

      {/* Content */}
      {isExpanded && (
        <div className={css({ padding: '3' })}>
          {/* Status */}
          <div
            className={css({
              display: 'flex',
              gap: '3',
              mb: '3',
              fontSize: 'xs',
              color: 'gray.400',
            })}
          >
            <span>
              Status:{' '}
              <span
                className={css({
                  color: music.isPlaying ? 'green.400' : 'yellow.400',
                })}
              >
                {music.isPlaying ? 'Playing' : 'Paused'}
              </span>
            </span>
            <span>
              Preset: <span className={css({ color: 'blue.400' })}>{music.currentPresetId}</span>
            </span>
            <span>
              Vol:{' '}
              <span className={css({ color: 'purple.400' })}>
                {Math.round(music.volume * 100)}%
              </span>
            </span>
          </div>

          {/* Pattern Code */}
          <div
            className={css({
              bg: 'gray.950',
              rounded: 'md',
              padding: '3',
              maxHeight: '300px',
              overflow: 'auto',
            })}
          >
            <pre
              className={css({
                fontFamily: 'mono',
                fontSize: 'xs',
                lineHeight: '1.5',
                color: 'green.300',
                whiteSpace: 'pre-wrap',
                wordBreak: 'break-word',
                margin: 0,
              })}
            >
              {formatPattern(music.currentPattern)}
            </pre>
          </div>

          {/* Copy button */}
          <button
            onClick={() => {
              navigator.clipboard.writeText(music.currentPattern)
            }}
            data-action="copy-pattern"
            className={css({
              mt: '2',
              paddingY: '1',
              paddingX: '2',
              fontSize: 'xs',
              bg: 'gray.700',
              color: 'gray.300',
              rounded: 'sm',
              border: 'none',
              cursor: 'pointer',
              _hover: { bg: 'gray.600' },
            })}
          >
            📋 Copy to clipboard
          </button>

          {/* Hint for Strudel REPL */}
          <p
            className={css({
              mt: '2',
              fontSize: 'xs',
              color: 'gray.500',
            })}
          >
            Paste into{' '}
            <a
              href="https://strudel.cc/"
              target="_blank"
              rel="noopener noreferrer"
              className={css({
                color: 'blue.400',
                textDecoration: 'underline',
              })}
            >
              strudel.cc
            </a>{' '}
            to visualize with pianoroll
          </p>
        </div>
      )}
    </div>
  )
}

/**
 * Format the pattern for better readability
 */
function formatPattern(pattern: string): string {
  if (!pattern) return '// No pattern loaded'

  // Add some basic formatting
  return pattern
    .replace(/\)\./g, ')\n  .')
    .replace(/,\s*n\(/g, ',\n  n(')
    .replace(/,\s*s\(/g, ',\n  s(')
    .replace(/stack\(\s*/g, 'stack(\n  ')
    .replace(/\)\s*\)/g, ')\n)')
}