244 lines
6.4 KiB
TypeScript
244 lines
6.4 KiB
TypeScript
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react"
|
|
import {
|
|
Pressable,
|
|
ScrollView,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
type ScrollViewInstance,
|
|
} from "react-native"
|
|
|
|
import {
|
|
usePlaybackActions,
|
|
usePlaybackProgress,
|
|
usePlaybackQueue,
|
|
} from "@/contexts/playback"
|
|
import { useLyricsTime } from "@/hooks/use-lyrics"
|
|
import type { NowPlayingColors } from "@/lib/now-playing-colors"
|
|
import {
|
|
activeLineIndex,
|
|
type LyricLine,
|
|
type Lyrics,
|
|
} from "@otunes/convex/client/lyrics"
|
|
|
|
/** How long a manual scroll keeps the view from following the song. */
|
|
const MANUAL_SCROLL_HOLD_MS = 3000
|
|
/** Where the playing line sits, as a share of the view's height from the top. */
|
|
const ACTIVE_LINE_POSITION = 0.12
|
|
|
|
/**
|
|
* Lyrics in the player's own colors. Synced lines follow playback and seek
|
|
* when tapped; `null` lyrics show a short note instead.
|
|
*/
|
|
export function LyricsView({
|
|
lyrics,
|
|
colors,
|
|
fontSize = 30,
|
|
}: {
|
|
lyrics: Lyrics | null | undefined
|
|
colors: NowPlayingColors
|
|
fontSize?: number
|
|
}) {
|
|
if (lyrics === undefined) return null
|
|
if (lyrics === null) {
|
|
return (
|
|
<View style={styles.empty}>
|
|
<Text style={[styles.emptyText, { color: colors.secondaryForeground }]}>
|
|
No lyrics
|
|
</Text>
|
|
</View>
|
|
)
|
|
}
|
|
return <LyricsLines lyrics={lyrics} colors={colors} fontSize={fontSize} />
|
|
}
|
|
|
|
function LyricsLines({
|
|
lyrics,
|
|
colors,
|
|
fontSize,
|
|
}: {
|
|
lyrics: Lyrics
|
|
colors: NowPlayingColors
|
|
fontSize: number
|
|
}) {
|
|
const { currentTime } = usePlaybackProgress()
|
|
const { isPlaying } = usePlaybackQueue()
|
|
const { seekTo, setIsPlaying } = usePlaybackActions()
|
|
const time = useLyricsTime(lyrics, currentTime, isPlaying)
|
|
const active = activeLineIndex(lyrics, time)
|
|
const synced = lyrics.synced !== "none"
|
|
const scrollRef = useRef<ScrollViewInstance>(null)
|
|
const lineOffsets = useRef<number[]>([])
|
|
const [height, setHeight] = useState(0)
|
|
const manualScrollAt = useRef(0)
|
|
|
|
const scrollToLine = useCallback(
|
|
(index: number) => {
|
|
const y = lineOffsets.current[Math.max(index, 0)]
|
|
if (y === undefined || !synced || height === 0) return
|
|
if (Date.now() - manualScrollAt.current < MANUAL_SCROLL_HOLD_MS) return
|
|
scrollRef.current?.scrollTo({
|
|
y: Math.max(0, y - height * ACTIVE_LINE_POSITION),
|
|
animated: true,
|
|
})
|
|
},
|
|
[height, synced]
|
|
)
|
|
|
|
useEffect(() => {
|
|
scrollToLine(active)
|
|
}, [active, scrollToLine])
|
|
|
|
const activeRef = useRef(active)
|
|
useEffect(() => {
|
|
activeRef.current = active
|
|
}, [active])
|
|
const handleLineLayout = useCallback(
|
|
(index: number, y: number) => {
|
|
lineOffsets.current[index] = y
|
|
// Opening mid-song: jump once the playing line has been measured.
|
|
if (index === Math.max(activeRef.current, 0)) scrollToLine(index)
|
|
},
|
|
[scrollToLine]
|
|
)
|
|
|
|
const playFrom = useCallback(
|
|
(seconds: number) => {
|
|
seekTo(seconds)
|
|
setIsPlaying(true)
|
|
manualScrollAt.current = 0
|
|
},
|
|
[seekTo, setIsPlaying]
|
|
)
|
|
|
|
const lineStyle = useMemo(
|
|
() => ({ fontSize, lineHeight: Math.round(fontSize * 1.18) }),
|
|
[fontSize]
|
|
)
|
|
|
|
return (
|
|
<ScrollView
|
|
ref={scrollRef}
|
|
style={styles.fill}
|
|
contentContainerStyle={{
|
|
paddingTop: height * ACTIVE_LINE_POSITION,
|
|
paddingBottom: height * 0.7,
|
|
}}
|
|
showsVerticalScrollIndicator={false}
|
|
onLayout={(event) => setHeight(event.nativeEvent.layout.height)}
|
|
onScrollBeginDrag={() => {
|
|
manualScrollAt.current = Date.now()
|
|
}}
|
|
onMomentumScrollEnd={() => {
|
|
// Hold from when the scroll settles, not when the finger lifted.
|
|
if (manualScrollAt.current > 0) manualScrollAt.current = Date.now()
|
|
}}
|
|
>
|
|
{lyrics.lines.map((line, index) =>
|
|
synced ? (
|
|
<SyncedLine
|
|
key={index}
|
|
index={index}
|
|
line={line}
|
|
state={
|
|
index === active ? "active" : index < active ? "past" : "next"
|
|
}
|
|
time={index === active ? time : null}
|
|
colors={colors}
|
|
textStyle={lineStyle}
|
|
onSelect={playFrom}
|
|
onLayoutY={handleLineLayout}
|
|
/>
|
|
) : (
|
|
<Text
|
|
key={index}
|
|
style={[
|
|
styles.plainLine,
|
|
{
|
|
color: colors.foreground,
|
|
fontSize: fontSize * 0.75,
|
|
lineHeight: Math.round(fontSize * 0.75 * 1.25),
|
|
},
|
|
line.text === "" && styles.stanzaGap,
|
|
]}
|
|
>
|
|
{line.text}
|
|
</Text>
|
|
)
|
|
)}
|
|
</ScrollView>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* One timed line. Only the playing line gets the time, so the others skip
|
|
* re-rendering as the song plays.
|
|
*/
|
|
const SyncedLine = memo(function SyncedLine({
|
|
index,
|
|
line,
|
|
state,
|
|
time,
|
|
colors,
|
|
textStyle,
|
|
onSelect,
|
|
onLayoutY,
|
|
}: {
|
|
index: number
|
|
line: LyricLine
|
|
state: "past" | "active" | "next"
|
|
time: number | null
|
|
colors: NowPlayingColors
|
|
textStyle: { fontSize: number; lineHeight: number }
|
|
onSelect: (seconds: number) => void
|
|
onLayoutY: (index: number, y: number) => void
|
|
}) {
|
|
const active = state === "active"
|
|
return (
|
|
<Pressable
|
|
onPress={() => onSelect(line.start ?? 0)}
|
|
onLayout={(event) => onLayoutY(index, event.nativeEvent.layout.y)}
|
|
accessibilityRole="button"
|
|
accessibilityHint="Plays from this line"
|
|
style={styles.line}
|
|
>
|
|
<Text
|
|
style={[
|
|
styles.lineText,
|
|
textStyle,
|
|
{
|
|
color:
|
|
active && !line.words ? colors.foreground : colors.lyricsInactive,
|
|
},
|
|
]}
|
|
>
|
|
{active && line.words
|
|
? line.words.map((word, wordIndex) => (
|
|
<Text
|
|
key={wordIndex}
|
|
style={{
|
|
color:
|
|
time !== null && time >= word.start
|
|
? colors.foreground
|
|
: colors.lyricsUnsung,
|
|
}}
|
|
>
|
|
{word.text}
|
|
</Text>
|
|
))
|
|
: line.text}
|
|
</Text>
|
|
</Pressable>
|
|
)
|
|
})
|
|
|
|
const styles = StyleSheet.create({
|
|
fill: { flex: 1 },
|
|
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
|
|
emptyText: { fontSize: 17, fontWeight: "600" },
|
|
line: { paddingVertical: 9 },
|
|
lineText: { fontWeight: "800" },
|
|
plainLine: { fontWeight: "700", paddingVertical: 2 },
|
|
stanzaGap: { height: 18 },
|
|
})
|