Files
otunes/apps/mobile/app/(app)/(tabs)/search.tsx
T
joshuafhiggins 73c7283d4d Add Expo mobile app and extract shared Convex package
- Replace mobile placeholder with Expo Router app (tabs, playback, auth, library)
- Move Convex backend to @otunes/convex for web and mobile reuse
- Update web app imports and API routes to use shared package
2026-08-02 04:14:01 +00:00

128 lines
4.1 KiB
TypeScript

import { useQuery } from "convex/react"
import { useMemo, useState } from "react"
import { ScrollView, StyleSheet, View } from "react-native"
import { SafeAreaView } from "react-native-safe-area-context"
import { ContentCarousel } from "@/components/media-cards"
import { contentBottomInset, SongRow } from "@/components/player"
import { ScreenHeader } from "@/components/screen-header"
import { Input, LoadingView } from "@/components/ui/primitives"
import { usePlayback } from "@/contexts/playback"
import { api } from "@otunes/convex/_generated/api"
import type { BrowseSong } from "@otunes/convex/types"
import { spacing, useThemeColors } from "@/lib/theme"
const SEARCH_DEBOUNCE_MS = 300
export default function SearchScreen() {
const colors = useThemeColors()
const { playFromSong } = usePlayback()
const [query, setQuery] = useState("")
const [searchQuery, setSearchQuery] = useState("")
const [debounceId, setDebounceId] = useState<ReturnType<typeof setTimeout> | null>(null)
const hasQuery = searchQuery.length > 0
const results = useQuery(
api.library.searchLibrary,
hasQuery ? { query: searchQuery } : "skip"
)
const playlistResults = useQuery(
api.playlists.searchPlaylists,
hasQuery ? { query: searchQuery } : "skip"
)
const isSearching = hasQuery && results === undefined
function handleQueryChange(value: string) {
setQuery(value)
if (debounceId) clearTimeout(debounceId)
const id = setTimeout(() => setSearchQuery(value.trim()), SEARCH_DEBOUNCE_MS)
setDebounceId(id)
}
const hasLibraryResults = useMemo(
() =>
Boolean(
results &&
(results.topResults.length > 0 ||
results.artists.length > 0 ||
results.albums.length > 0 ||
results.songs.length > 0)
),
[results]
)
return (
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
<ScreenHeader title="Search" />
<View style={styles.searchBox}>
<Input
value={query}
onChangeText={handleQueryChange}
placeholder="Search songs, albums, artists..."
returnKeyType="search"
onSubmitEditing={() => setSearchQuery(query.trim())}
/>
</View>
{isSearching ? (
<LoadingView />
) : !hasQuery ? (
<View style={styles.empty}>
<Input editable={false} value="Search your library" />
</View>
) : (
<ScrollView contentContainerStyle={{ paddingBottom: contentBottomInset }}>
{!hasLibraryResults ? (
<View style={styles.empty}>
<Input editable={false} value={`No results for "${searchQuery}"`} />
</View>
) : null}
{results?.artists.length ? (
<View style={styles.section}>
<ContentCarousel title="Artists" content={results.artists} />
</View>
) : null}
{results?.albums.length ? (
<View style={styles.section}>
<ContentCarousel title="Albums" content={results.albums} />
</View>
) : null}
{results?.songs.length ? (
<View style={styles.section}>
{results.songs.map((song: BrowseSong) => (
<SongRow
key={song.id}
song={song}
onPress={() =>
void playFromSong(
song.id,
results.songs.map((s: BrowseSong) => s.id)
)
}
/>
))}
</View>
) : null}
{playlistResults && playlistResults.length > 0 ? (
<View style={styles.section}>
<ContentCarousel title="Playlists" content={playlistResults} />
</View>
) : null}
</ScrollView>
)}
</SafeAreaView>
)
}
const styles = StyleSheet.create({
safe: { flex: 1 },
searchBox: { paddingHorizontal: spacing.lg, paddingBottom: spacing.md },
empty: { padding: spacing.lg },
section: { paddingHorizontal: spacing.lg, paddingBottom: spacing.lg, gap: spacing.md },
})