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

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
/**
 * 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() {
  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',
          })}
        >
          Creating your song...
        </span>
      </div>
    </PlayerShell>
  )
}

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 being generated in the background. Pulsing shimmer animation. */
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>
  ),
}