All files / web/src/components/toys/euclid HecklerCallOverlay.tsx

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

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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
import { MiniWaveform, AnimatedDots, formatTime } from '@/lib/voice/PhoneCallOverlay'

export type HecklerPhase = 'watching' | 'ringing' | 'connecting' | 'active'

export function HecklerCallOverlay({
  phase,
  profileImage,
  lgProfileImage,
  characterName,
  isSpeaking,
  isThinking,
  timeRemaining,
  onAnswer,
  onDismiss,
  onHangUp,
}: {
  phase: HecklerPhase
  profileImage: string
  lgProfileImage: string
  characterName: string
  isSpeaking: boolean
  isThinking: boolean
  timeRemaining: number | null
  onAnswer: () => void
  onDismiss: () => void
  onHangUp: () => void
}) {
  const inCall = phase === 'connecting' || phase === 'active'

  // ── Position: bottom-center for watching/ringing, bottom-left for in-call ──
  const positionStyle: React.CSSProperties = inCall
    ? { bottom: 24, left: 24, transform: 'none' }
    : { bottom: 80, left: '50%', transform: 'translateX(-50%)' }

  // ── Layout: horizontal bar for watching/ringing, vertical card for in-call ──
  const layoutStyle: React.CSSProperties = inCall
    ? {
        flexDirection: 'column',
        alignItems: 'center',
        gap: 10,
        padding: '16px 20px',
        minWidth: 140,
        backdropFilter: 'blur(12px)',
      }
    : {
        flexDirection: 'row',
        alignItems: 'center',
        gap: 12,
        padding: phase === 'ringing' ? '12px 20px' : '8px 16px',
        minWidth: undefined,
        backdropFilter: undefined,
      }

  const glowColor = isSpeaking
    ? 'rgba(168, 85, 247, 0.7)'
    : isThinking
      ? 'rgba(168, 85, 247, 0.35)'
      : 'transparent'
  const glowShadow = isSpeaking
    ? `0 0 0 3px ${glowColor}, 0 0 20px ${glowColor}`
    : isThinking
      ? `0 0 0 2px ${glowColor}, 0 0 12px ${glowColor}`
      : '0 4px 16px rgba(0,0,0,0.3)'

  // Avatar size: 72px in-call, 40px ringing, 32px watching
  const avatarSize = inCall ? 72 : phase === 'ringing' ? 40 : 32

  return (
    <div
      data-element="heckler-call-overlay"
      data-phase={phase}
      style={{
        position: 'absolute',
        ...positionStyle,
        display: 'flex',
        ...layoutStyle,
        borderRadius: 16,
        background: phase === 'watching' ? 'rgba(15, 23, 42, 0.6)' : 'rgba(15, 23, 42, 0.92)',
        color: '#f8fafc',
        fontSize: 13,
        fontFamily: 'system-ui, sans-serif',
        boxShadow:
          phase === 'ringing'
            ? '0 8px 32px rgba(0,0,0,0.4), 0 0 0 2px rgba(78, 121, 167, 0.4)'
            : '0 8px 32px rgba(0,0,0,0.35)',
        zIndex: 20,
        transition: 'all 0.4s cubic-bezier(0.4, 0, 0.2, 1)',
        opacity: phase === 'watching' ? 0.7 : 1,
        animation: phase === 'ringing' ? 'heckler-ring 1s ease-in-out infinite' : undefined,
        pointerEvents: phase === 'watching' ? 'none' : 'auto',
      }}
      onPointerDown={inCall ? (e) => e.stopPropagation() : undefined}
    >
      {/* Avatar */}
      <div
        style={{
          width: avatarSize,
          height: avatarSize,
          borderRadius: '50%',
          overflow: 'hidden',
          flexShrink: 0,
          boxShadow: inCall ? glowShadow : undefined,
          transition: 'all 0.4s cubic-bezier(0.4, 0, 0.2, 1)',
          animation:
            inCall && isSpeaking
              ? 'hecklerSpeakingPulse 1.5s ease-in-out infinite'
              : inCall && isThinking
                ? 'hecklerSpeakingPulse 2.5s ease-in-out infinite'
                : undefined,
        }}
      >
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img
          src={inCall ? lgProfileImage : profileImage}
          alt={characterName}
          style={{
            width: '100%',
            height: '100%',
            objectFit: 'cover',
            display: 'block',
          }}
        />
      </div>

      {/* ── Watching phase ── */}
      {phase === 'watching' && (
        <span style={{ fontSize: 12, opacity: 0.8 }}>{characterName} is watching...</span>
      )}

      {/* ── Ringing phase ── */}
      {phase === 'ringing' && (
        <>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
            <span style={{ fontWeight: 600, fontSize: 14 }}>{characterName} is calling...</span>
            <span style={{ fontSize: 11, opacity: 0.7 }}>Incoming observation</span>
          </div>
          <div style={{ display: 'flex', gap: 8, marginLeft: 8 }}>
            <button
              data-action="answer-heckler"
              onClick={onAnswer}
              style={{
                border: 'none',
                borderRadius: 20,
                padding: '6px 16px',
                background: '#22c55e',
                color: '#fff',
                fontSize: 13,
                fontWeight: 600,
                cursor: 'pointer',
              }}
            >
              Answer
            </button>
            <button
              data-action="dismiss-heckler"
              onClick={onDismiss}
              style={{
                border: 'none',
                borderRadius: 20,
                padding: '6px 16px',
                background: '#ef4444',
                color: '#fff',
                fontSize: 13,
                fontWeight: 600,
                cursor: 'pointer',
              }}
            >
              Dismiss
            </button>
          </div>
        </>
      )}

      {/* ── Connecting phase (stalling TTS) ── */}
      {phase === 'connecting' && (
        <>
          <div
            style={{
              display: 'flex',
              alignItems: 'center',
              gap: 8,
              fontSize: 13,
              fontWeight: 500,
            }}
          >
            <span>{characterName}</span>
            <MiniWaveform isDark active={isSpeaking} />
          </div>
          <span style={{ opacity: 0.6, fontSize: 12 }}>
            Connecting
            <AnimatedDots />
          </span>
          <button
            data-action="end-heckler-call"
            onClick={onHangUp}
            onPointerDown={(e) => e.stopPropagation()}
            style={{
              width: '100%',
              border: 'none',
              borderRadius: 20,
              padding: '8px 0',
              background: 'rgba(239, 68, 68, 0.85)',
              color: '#fff',
              fontSize: 13,
              fontWeight: 600,
              cursor: 'pointer',
              transition: 'background 0.15s',
            }}
            onMouseEnter={(e) => {
              ;(e.target as HTMLButtonElement).style.background = 'rgba(239, 68, 68, 1)'
            }}
            onMouseLeave={(e) => {
              ;(e.target as HTMLButtonElement).style.background = 'rgba(239, 68, 68, 0.85)'
            }}
          >
            End Call
          </button>
        </>
      )}

      {/* ── Active phase ── */}
      {phase === 'active' && (
        <>
          <div
            style={{
              display: 'flex',
              alignItems: 'center',
              gap: 8,
              fontSize: 13,
              fontWeight: 500,
            }}
          >
            {isThinking ? (
              <span style={{ opacity: 0.7, fontSize: 12 }}>
                Consulting scrolls
                <AnimatedDots />
              </span>
            ) : (
              <>
                <span>{characterName}</span>
                <MiniWaveform isDark active={isSpeaking} />
                {timeRemaining != null && (
                  <span style={{ opacity: 0.6, fontSize: 12, fontVariantNumeric: 'tabular-nums' }}>
                    {formatTime(timeRemaining)}
                  </span>
                )}
              </>
            )}
          </div>
          <button
            data-action="end-heckler-call"
            onClick={onHangUp}
            onPointerDown={(e) => e.stopPropagation()}
            style={{
              width: '100%',
              border: 'none',
              borderRadius: 20,
              padding: '8px 0',
              background: 'rgba(239, 68, 68, 0.85)',
              color: '#fff',
              fontSize: 13,
              fontWeight: 600,
              cursor: 'pointer',
              transition: 'background 0.15s',
            }}
            onMouseEnter={(e) => {
              ;(e.target as HTMLButtonElement).style.background = 'rgba(239, 68, 68, 1)'
            }}
            onMouseLeave={(e) => {
              ;(e.target as HTMLButtonElement).style.background = 'rgba(239, 68, 68, 0.85)'
            }}
          >
            End Call
          </button>
        </>
      )}

      <style>{`
        @keyframes heckler-ring {
          0%, 100% { transform: translateX(-50%) scale(1); }
          10% { transform: translateX(-50%) scale(1.02) rotate(-1deg); }
          20% { transform: translateX(-50%) scale(1.02) rotate(1deg); }
          30% { transform: translateX(-50%) scale(1.02) rotate(-1deg); }
          40% { transform: translateX(-50%) scale(1); }
        }
        @keyframes hecklerSpeakingPulse {
          0%, 100% { box-shadow: ${glowShadow}; }
          50% { box-shadow: 0 0 0 ${isSpeaking ? 4 : 3}px ${glowColor}, 0 0 ${isSpeaking ? 28 : 16}px ${glowColor}; }
        }
      `}</style>
    </div>
  )
}