UIPackage React Native
Search
Menu

Episode Detail

Editorial episode details with caller-owned playback, resume, progress, retry, and notes composition.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add episode-detail

Source files

Expand a file to read the source the installer copies into your app.

components/blocks/episode-detail/EpisodeDetail.tsx
import type { ReactNode } from 'react'
import { View, type ViewProps } from 'react-native'
import { Pause, Play, RotateCcw } from 'lucide-react-native'
import { Button } from '@/components/ui/button'
import { Icon } from '@/components/ui/icon'
import { Progress } from '@/components/ui/progress'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export interface EpisodePlayback {
  status: 'ready' | 'playing' | 'loading' | 'error'
  /** The caller-owned playback position and duration in seconds. */
  position?: number
  duration?: number
  errorMessage?: string
}

const defaultLabels = {
  about: 'About this episode',
  play: 'Play episode',
  playAgain: 'Play again',
  resume: (time: string) => `Resume at ${time}`,
  pause: 'Pause episode',
  loading: 'Loading episode…',
  error: 'This episode could not be played.',
  retry: 'Retry playback',
  progress: 'Playback progress',
  elapsed: (position: string, duration: string) => `${position} of ${duration}`,
}

export interface EpisodeDetailProps extends ViewProps {
  className?: string
  title: string
  show?: string
  /** A localized date, episode number, or runtime. */
  metadata?: string
  summary?: string
  /** A caller-provided player, cover, or other episode media. */
  media?: ReactNode
  playback?: EpisodePlayback
  /** Starts or resumes from the given time in seconds. */
  onPlay?: (position: number) => void
  onPause?: () => void
  onRetry?: () => void
  labels?: Partial<typeof defaultLabels>
}

function timestamp(seconds: number) {
  const minutes = Math.floor(seconds / 60)
  const tail = String(Math.floor(seconds % 60)).padStart(2, '0')
  return `${minutes}:${tail}`
}

export function EpisodeDetail({
  title,
  show,
  metadata,
  summary,
  media,
  playback,
  onPlay,
  onPause,
  onRetry,
  labels,
  children,
  className,
  ...props
}: EpisodeDetailProps) {
  const copy = { ...defaultLabels, ...labels }
  const rawDuration = playback?.duration ?? 0
  const duration = Number.isFinite(rawDuration) && rawDuration > 0 ? rawDuration : undefined
  const rawPosition = playback?.position ?? 0
  const position = Number.isFinite(rawPosition) ? Math.max(0, Math.min(rawPosition, duration ?? rawPosition)) : 0
  const ended = duration !== undefined && position >= duration
  const loading = playback?.status === 'loading'
  const failed = playback?.status === 'error'

  let actionLabel = ended ? copy.playAgain : position > 0 ? copy.resume(timestamp(position)) : copy.play
  let action = onPlay ? () => onPlay(ended ? 0 : position) : undefined
  let icon = ended ? RotateCcw : Play
  if (loading) actionLabel = copy.loading
  if (playback?.status === 'playing') {
    actionLabel = copy.pause
    action = onPause
    icon = Pause
  }
  if (failed) {
    actionLabel = copy.retry
    action = onRetry
    icon = RotateCcw
  }

  return (
    <View {...props} className={cn('gap-[20px]', className)}>
      <View className="gap-[4px]">
        {show ? (
          <Text size="sm" variant="muted">
            {show}
          </Text>
        ) : null}
        <Text size="title">{title}</Text>
        {metadata ? (
          <Text size="sm" variant="muted">
            {metadata}
          </Text>
        ) : null}
      </View>

      {media}

      {duration !== undefined || failed || action ? (
        <View className="gap-[8px]">
          {duration !== undefined && !failed ? (
            <>
              <Progress
                value={position}
                max={duration}
                accessibilityLabel={copy.progress}
                accessibilityValue={{
                  min: 0,
                  max: Math.ceil(duration),
                  now: Math.floor(position),
                  text: copy.elapsed(timestamp(position), timestamp(duration)),
                }}
              />
              <View className="flex-row flex-wrap justify-between gap-[8px]" aria-hidden>
                <Text size="xs" variant="muted" style={{ fontVariant: ['tabular-nums'] }}>
                  {timestamp(position)}
                </Text>
                <Text size="xs" variant="muted" style={{ fontVariant: ['tabular-nums'] }}>
                  {timestamp(duration)}
                </Text>
              </View>
            </>
          ) : null}
          {failed ? (
            <Text size="sm" className="text-destructive" accessibilityRole="alert">
              {playback.errorMessage ?? copy.error}
            </Text>
          ) : null}
          {action ? (
            <Button
              accessibilityLabel={actionLabel}
              loading={loading}
              onPress={() => {
                if (!loading) action()
              }}
            >
              {loading ? null : <Icon icon={icon} size={18} className="text-primary-foreground" />}
              {actionLabel}
            </Button>
          ) : null}
        </View>
      ) : null}

      {summary ? (
        <View className="gap-[4px]">
          <Text className="font-semibold">{copy.about}</Text>
          <Text variant="muted">{summary}</Text>
        </View>
      ) : null}
      {children}
    </View>
  )
}
components/blocks/episode-detail/index.ts
export { EpisodeDetail, type EpisodeDetailProps, type EpisodePlayback } from './EpisodeDetail'

Dependencies

Registry items

npm packages

  • lucide-react-native
Browse media and playback