UIPackage React Native
Search
Menu

Episode Feed

Virtualized series feed with played and unplayed filters, readable progress, and caller-owned episode destinations.

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-feed

Source files

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

components/blocks/episode-feed/EpisodeFeed.tsx
import { useMemo, useState, type ReactNode } from 'react'
import { FlatList, I18nManager, View, type ViewProps } from 'react-native'
import { ChevronLeft, ChevronRight, CircleCheck } from 'lucide-react-native'
import { Button } from '@/components/ui/button'
import { Icon } from '@/components/ui/icon'
import { ListItem, ListItemContent, ListItemDescription, ListItemTitle, ListSeparator } from '@/components/ui/list'
import { Progress } from '@/components/ui/progress'
import { SegmentedControl, SegmentedControlItem } from '@/components/ui/segmented-control'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

export type EpisodeFeedFilter = 'all' | 'unplayed' | 'played'

export interface EpisodeFeedEntry {
  id: string
  title: string
  metadata?: string
  summary?: string
  /** Persisted player state in seconds. Only the caller marks an episode played. */
  position?: number
  duration?: number
  played?: boolean
}

const percentFormat = new Intl.NumberFormat(undefined, { style: 'percent', maximumFractionDigits: 0 })
const defaultLabels = {
  filter: 'Filter episodes',
  all: 'All',
  unplayed: 'Unplayed',
  played: 'Played',
  inProgress: 'In progress',
  progress: (value: number) => `${percentFormat.format(value)} played`,
  empty: 'No episodes yet.',
  emptyUnplayed: 'No unplayed episodes.',
  emptyPlayed: 'No played episodes.',
  showAll: 'Show all episodes',
  count: (value: number) => `${value} ${value === 1 ? 'episode' : 'episodes'}`,
}

export interface EpisodeFeedProps<Entry extends EpisodeFeedEntry = EpisodeFeedEntry> extends Omit<
  ViewProps,
  'children'
> {
  className?: string
  title: string
  summary?: string
  episodes?: readonly Entry[]
  /** Optional content above the series title, inside the list's scroll region. */
  header?: ReactNode
  /** Owns the row layout when supplied. The feed continues to own filtering and virtualization. */
  renderEpisode?: (episode: Entry) => ReactNode
  filter?: EpisodeFeedFilter
  defaultFilter?: EpisodeFeedFilter
  onFilterChange?: (filter: EpisodeFeedFilter) => void
  onOpen?: (episode: Entry) => void
  labels?: Partial<typeof defaultLabels>
}

export function EpisodeFeed<Entry extends EpisodeFeedEntry>({
  title,
  summary,
  episodes = [],
  header,
  renderEpisode,
  filter,
  defaultFilter = 'all',
  onFilterChange,
  onOpen,
  labels,
  className,
  ...props
}: EpisodeFeedProps<Entry>) {
  const copy = { ...defaultLabels, ...labels }
  const [internalFilter, setInternalFilter] = useState(defaultFilter)
  const selected = filter ?? internalFilter
  const canFilter = filter === undefined || onFilterChange !== undefined
  const items = useMemo(
    () => (selected === 'all' ? episodes : episodes.filter((episode) => !!episode.played === (selected === 'played'))),
    [episodes, selected],
  )
  useAnnouncement(selected !== 'all' ? `${copy[selected]}. ${copy.count(items.length)}.` : undefined)

  function changeFilter(next: string) {
    if (!canFilter || next === selected || (next !== 'all' && next !== 'unplayed' && next !== 'played')) return
    if (filter === undefined) setInternalFilter(next)
    onFilterChange?.(next)
  }

  return (
    <View {...props} className={cn('min-h-0 flex-1 bg-background', className)}>
      <FlatList
        data={items}
        keyExtractor={(episode) => episode.id}
        ItemSeparatorComponent={ListSeparator}
        contentContainerStyle={{ paddingBottom: 16 }}
        ListHeaderComponent={
          <View className="gap-[12px] pb-[12px]">
            {header}
            <View className="gap-[4px]">
              <Text size="title" accessibilityRole="header">
                {title}
              </Text>
              {summary ? <Text variant="muted">{summary}</Text> : null}
            </View>
            <SegmentedControl
              accessibilityLabel={copy.filter}
              value={selected}
              onValueChange={changeFilter}
              disabled={!canFilter}
            >
              <SegmentedControlItem value="all">{copy.all}</SegmentedControlItem>
              <SegmentedControlItem value="unplayed">{copy.unplayed}</SegmentedControlItem>
              <SegmentedControlItem value="played">{copy.played}</SegmentedControlItem>
            </SegmentedControl>
          </View>
        }
        ListEmptyComponent={
          <View className="gap-[8px] rounded-lg border border-border bg-card p-[16px]">
            <Text variant="muted">
              {selected === 'played' ? copy.emptyPlayed : selected === 'unplayed' ? copy.emptyUnplayed : copy.empty}
            </Text>
            {episodes.length && selected !== 'all' && canFilter ? (
              <Button variant="outline" accessibilityLabel={copy.showAll} onPress={() => changeFilter('all')}>
                {copy.showAll}
              </Button>
            ) : null}
          </View>
        }
        renderItem={({ item }) => {
          if (renderEpisode) return renderEpisode(item)
          const position = item.position ?? 0
          const duration = item.duration ?? 0
          const started = Number.isFinite(position) && position > 0
          const progress = started && Number.isFinite(duration) && duration > 0 ? Math.min(1, position / duration) : 0
          const percentage = Math.round(progress * 100)
          const status = item.played
            ? copy.played
            : progress > 0
              ? copy.progress(progress)
              : started
                ? copy.inProgress
                : copy.unplayed

          return (
            <ListItem
              className="items-start px-0"
              onPress={onOpen ? () => onOpen(item) : undefined}
              accessibilityLabel={onOpen ? [item.title, item.metadata, status].filter(Boolean).join(', ') : undefined}
              accessibilityHint={onOpen ? item.summary : undefined}
            >
              <ListItemContent>
                <ListItemTitle>{item.title}</ListItemTitle>
                {item.metadata ? <ListItemDescription>{item.metadata}</ListItemDescription> : null}
                {item.summary ? <ListItemDescription>{item.summary}</ListItemDescription> : null}
                <View className="flex-row flex-wrap items-center gap-[6px]">
                  {item.played ? <Icon icon={CircleCheck} size={16} className="text-muted-foreground" /> : null}
                  <Text size="xs" variant="muted" style={{ fontVariant: ['tabular-nums'] }}>
                    {status}
                  </Text>
                </View>
                {!item.played && progress > 0 ? (
                  <Progress
                    value={percentage}
                    accessibilityLabel={status}
                    className="mt-[4px] max-w-[192px]"
                    aria-hidden={!!onOpen}
                  />
                ) : null}
              </ListItemContent>
              {onOpen ? (
                <Icon
                  icon={I18nManager.isRTL ? ChevronLeft : ChevronRight}
                  size={18}
                  className="mt-[4px] text-muted-foreground"
                />
              ) : null}
            </ListItem>
          )
        }}
      />
    </View>
  )
}
components/blocks/episode-feed/index.ts
export { EpisodeFeed, type EpisodeFeedEntry, type EpisodeFeedFilter, type EpisodeFeedProps } from './EpisodeFeed'

Dependencies

Registry items

npm packages

  • lucide-react-native
Browse media and playback