All files / web/src/components/practice SessionSongPlayer.stories.tsx

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

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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
/**
 * Stories for SessionSongPlayer — renders each visual state directly
 * without needing the useSessionSong hook (which requires API access).
 *
 * We render the internal markup directly to show each state.
 */
import type { Meta, StoryObj } from '@storybook/react'
import { useState } from 'react'
import { css } from '../../../styled-system/css'

// ============================================================================
// Presentational wrappers that replicate the component's visual states
// without the hook dependency
// ============================================================================

function PlayerShell({ children }: { children: React.ReactNode }) {
  return (
    <div
      data-component="session-song-player"
      className={css({
        mx: 'auto',
        maxW: '480px',
        p: 4,
        borderRadius: 'xl',
        bg: 'purple.50',
        _dark: { bg: 'purple.900/30' },
        mb: 4,
      })}
    >
      {children}
    </div>
  )
}

function GeneratingState({
  copy = 'Creating your song...',
  reconnecting = false,
}: {
  copy?: string
  reconnecting?: boolean
}) {
  return (
    <PlayerShell>
      <div
        className={css({
          display: 'flex',
          alignItems: 'center',
          gap: 3,
          py: 2,
        })}
      >
        <div
          className={css({
            w: 8,
            h: 8,
            borderRadius: 'full',
            bg: 'purple.200',
            _dark: { bg: 'purple.700' },
            animation: 'pulse 1.5s ease-in-out infinite',
            flexShrink: 0,
          })}
        />
        <span
          className={css({
            fontSize: 'sm',
            color: 'purple.700',
            _dark: { color: 'purple.200' },
            fontWeight: 'medium',
          })}
        >
          {copy}
          {reconnecting && (
            <span
              className={css({
                ml: 2,
                fontSize: 'xs',
                color: 'purple.500',
                _dark: { color: 'purple.400' },
                fontStyle: 'italic',
              })}
            >
              · reconnecting…
            </span>
          )}
        </span>
      </div>
    </PlayerShell>
  )
}

function ConnectionLostState({ ownerCopy = false }: { ownerCopy?: boolean }) {
  return (
    <div
      data-element="connection-lost"
      className={css({
        mx: 'auto',
        maxW: '480px',
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'start',
        gap: 2,
        p: 4,
        borderRadius: 'xl',
        bg: 'amber.50',
        border: '1px solid',
        borderColor: 'amber.200',
        _dark: { bg: 'amber.900/30', borderColor: 'amber.700' },
      })}
    >
      <span
        className={css({
          fontSize: 'sm',
          color: 'amber.800',
          _dark: { color: 'amber.100' },
          fontWeight: 'medium',
        })}
      >
        {ownerCopy
          ? 'Lost the realtime connection to the server. The song is still generating in the background — reconnect to resume live updates.'
          : "Lost the live updates — your song is still cooking, but we won't see it the moment it's done."}
      </span>
      <button
        type="button"
        data-action="reconnect-session-song"
        onClick={() => {}}
        className={css({
          fontSize: 'xs',
          fontWeight: 'bold',
          color: 'amber.900',
          _dark: { color: 'amber.100' },
          textDecoration: 'underline',
          cursor: 'pointer',
          border: 'none',
          bg: 'transparent',
          p: 0,
        })}
      >
        Reconnect
      </button>
    </div>
  )
}

function TapToPlayState({ title }: { title: string }) {
  const [tapped, setTapped] = useState(false)

  if (tapped) {
    return <ReadyPlayerState title={title} simulatePlaying />
  }

  return (
    <PlayerShell>
      <button
        data-action="tap-to-play"
        onClick={() => setTapped(true)}
        className={css({
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center',
          gap: 2,
          w: '100%',
          py: 3,
          border: 'none',
          bg: 'transparent',
          cursor: 'pointer',
        })}
      >
        <div
          className={css({
            w: 16,
            h: 16,
            borderRadius: 'full',
            bg: 'purple.500',
            color: 'white',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            fontSize: '2xl',
            animation: 'pulse 1.5s ease-in-out infinite',
          })}
        >
          {'\u25B6'}
        </div>
        <span
          className={css({
            fontSize: 'md',
            fontWeight: 'bold',
            color: 'purple.700',
            _dark: { color: 'purple.200' },
          })}
        >
          Tap to play your song!
        </span>
        <span
          className={css({
            fontSize: 'sm',
            color: 'purple.500',
            _dark: { color: 'purple.300' },
          })}
        >
          {title}
        </span>
      </button>
    </PlayerShell>
  )
}

function ReadyPlayerState({
  title,
  simulatePlaying = false,
}: {
  title: string
  simulatePlaying?: boolean
}) {
  const [isPlaying, setIsPlaying] = useState(simulatePlaying)
  const progress = simulatePlaying ? 35 : 0
  const currentTime = simulatePlaying ? '0:16' : '0:00'
  const totalTime = '0:45'

  return (
    <PlayerShell>
      {/* Title */}
      <div
        className={css({
          fontSize: 'md',
          fontWeight: 'bold',
          color: 'purple.800',
          _dark: { color: 'purple.100' },
          mb: 3,
          textAlign: 'center',
        })}
      >
        {title}
      </div>

      {/* Play button + progress */}
      <div
        className={css({
          display: 'flex',
          alignItems: 'center',
          gap: 3,
        })}
      >
        <button
          data-action="toggle-play"
          onClick={() => setIsPlaying(!isPlaying)}
          className={css({
            w: 12,
            h: 12,
            borderRadius: 'full',
            bg: 'purple.500',
            color: 'white',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            fontSize: 'xl',
            cursor: 'pointer',
            flexShrink: 0,
            border: 'none',
            _hover: { bg: 'purple.600' },
            _active: { bg: 'purple.700' },
            transition: 'background 0.15s',
          })}
        >
          {isPlaying ? '\u23F8' : '\u25B6'}
        </button>

        <div className={css({ flex: 1, minW: 0 })}>
          <div
            data-element="progress-bar"
            className={css({
              h: 2,
              bg: 'purple.200',
              _dark: { bg: 'purple.700' },
              borderRadius: 'full',
              cursor: 'pointer',
              position: 'relative',
              mb: 1,
            })}
          >
            <div
              className={css({
                h: '100%',
                bg: 'purple.500',
                borderRadius: 'full',
                transition: 'width 0.1s linear',
              })}
              style={{ width: `${progress}%` }}
            />
          </div>

          <div
            className={css({
              display: 'flex',
              justifyContent: 'space-between',
              fontSize: 'xs',
              color: 'purple.500',
              _dark: { color: 'purple.300' },
            })}
          >
            <span>{currentTime}</span>
            <span>{totalTime}</span>
          </div>
        </div>
      </div>
    </PlayerShell>
  )
}

// ============================================================================
// Meta
// ============================================================================

const meta: Meta = {
  title: 'Practice/SessionSongPlayer',
  decorators: [
    (Story) => (
      <div
        className={css({
          padding: '2rem',
          maxWidth: '500px',
          margin: '0 auto',
        })}
      >
        <Story />
      </div>
    ),
  ],
  parameters: {
    layout: 'centered',
  },
}

export default meta
type Story = StoryObj

// ============================================================================
// Stories
// ============================================================================

/** Song is queued; worker hasn't picked it up yet. */
export const GeneratingPending: Story = {
  render: () => <GeneratingState copy="Getting ready to write your song..." />,
}

/** LLM is writing the lyrics + composition plan. */
export const GeneratingPrompt: Story = {
  render: () => <GeneratingState copy="Listening to how your session went..." />,
}

/** ElevenLabs is producing the audio. */
export const GeneratingMusic: Story = {
  render: () => <GeneratingState copy="Writing your celebration song 🎵" />,
}

/** Generation has been running longer than ~45s — soft reassurance copy. */
export const GeneratingLongRunning: Story = {
  render: () => (
    <GeneratingState copy="Still cooking — this one's taking a little longer, hang tight!" />
  ),
}

/** Generation has been running longer than ~3min — invite the user to come back. */
export const GeneratingVeryLongRunning: Story = {
  render: () => <GeneratingState copy="You can come back later — we'll have it ready for you" />,
}

/** Socket disconnected briefly; same shimmer with a subtle hint. */
export const GeneratingReconnecting: Story = {
  render: () => <GeneratingState copy="Writing your celebration song 🎵" reconnecting />,
}

/** Socket has been lost long enough to surface to the user — kid-facing copy. */
export const GeneratingConnectionLost: Story = {
  render: () => <ConnectionLostState />,
}

/** Same as above but with the account-owner / admin copy. */
export const GeneratingConnectionLostOwner: Story = {
  render: () => <ConnectionLostState ownerCopy />,
}

/**
 * Back-compat: matches the previous generic "Creating your song..." shimmer.
 * Used as a fallback when status doesn't match a known phase.
 */
export const Generating: Story = {
  render: () => <GeneratingState />,
}

/**
 * Song is ready but autoplay was blocked by the browser.
 * Large pulsing play button with "Tap to play your song!" prompt.
 * Click the button to transition to the normal player.
 */
export const AutoplayBlocked: Story = {
  render: () => <TapToPlayState title="Sonia Counts to Victory" />,
}

/** Normal player with title, play/pause, seekable progress bar. */
export const ReadyPaused: Story = {
  render: () => <ReadyPlayerState title="Abacus Adventures with Fern" />,
}

/** Player mid-playback showing progress. */
export const ReadyPlaying: Story = {
  render: () => <ReadyPlayerState title="Abacus Adventures with Fern" simulatePlaying />,
}

/**
 * No song / failed states render nothing.
 * Shown here with a placeholder to confirm emptiness.
 */
export const NoSongOrFailed: Story = {
  render: () => (
    <div className={css({ textAlign: 'center', color: 'gray.400', fontSize: 'sm' })}>
      (Component renders nothing when no song exists or generation failed)
    </div>
  ),
}