1293 lines
35 KiB
TypeScript
1293 lines
35 KiB
TypeScript
import { atmosEnabled, dolbyAtmosSupported } from "@/lib/playback-capabilities"
|
|
import { Ionicons } from "@expo/vector-icons"
|
|
import { useAction, useMutation, useQuery } from "convex/react"
|
|
import { Image } from "expo-image"
|
|
import * as ImagePicker from "expo-image-picker"
|
|
import { useEffect, useMemo, useState } from "react"
|
|
import { router } from "expo-router"
|
|
import {
|
|
Alert,
|
|
Platform,
|
|
Pressable,
|
|
ScrollView,
|
|
StyleSheet,
|
|
Switch,
|
|
Text,
|
|
View,
|
|
} from "react-native"
|
|
import type { StyleProp, ViewStyle } from "react-native"
|
|
|
|
import { AudioFormatBadge } from "@/components/audio-format-badge"
|
|
import { contentBottomInset } from "@/components/player"
|
|
import { useDownloads, useDownloadsVersion } from "@/contexts/downloads"
|
|
import {
|
|
readRememberedDownloadQuality,
|
|
writeRememberedDownloadQuality,
|
|
} from "@/lib/downloads"
|
|
import {
|
|
DOWNLOAD_QUALITIES,
|
|
DOWNLOAD_QUALITY_DETAILS,
|
|
DOWNLOAD_QUALITY_LABELS,
|
|
formatBytes,
|
|
type DownloadQuality,
|
|
} from "@otunes/convex/download-quality"
|
|
import { Button, Input, LoadingView } from "@/components/ui/primitives"
|
|
import { useInvalidateAuthSession } from "@/contexts/convex-auth-session"
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import {
|
|
setArtworkColorsEnabled,
|
|
useArtworkColorsEnabled,
|
|
} from "@/lib/artwork-color-preference"
|
|
import { initialsFromName } from "@/lib/format"
|
|
import { useCachedQuery } from "@/hooks/use-cached-query"
|
|
import { clearCoverPaletteCache } from "@/hooks/use-cover-palette"
|
|
import { useProfileImageUrl } from "@/hooks/use-profile-image-url"
|
|
import { changeServer, signOut } from "@/lib/session"
|
|
import { radius, spacing, useThemeColors } from "@/lib/theme"
|
|
import { showError } from "@/lib/toast"
|
|
import type { PlaybackQuality } from "@otunes/convex/playback-stream"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
|
|
type Section = "profile" | "quality" | "downloads" | "library" | "users"
|
|
|
|
type CurrentUser = {
|
|
_id: Id<"users">
|
|
name: string
|
|
image?: string
|
|
email: string
|
|
isAdmin?: boolean
|
|
playbackQuality?: PlaybackQuality
|
|
preferAtmos?: boolean
|
|
}
|
|
|
|
type AdminUser = {
|
|
_id: Id<"users">
|
|
name: string
|
|
email: string
|
|
isAdmin?: boolean
|
|
}
|
|
|
|
type SectionConfig = {
|
|
id: Section
|
|
label: string
|
|
icon: React.ComponentProps<typeof Ionicons>["name"]
|
|
admin?: boolean
|
|
}
|
|
|
|
const QUALITY_OPTIONS: {
|
|
value: PlaybackQuality
|
|
label: string
|
|
description: string
|
|
}[] = [
|
|
{ value: "low", label: "Low", description: "AAC 64kb/s" },
|
|
{ value: "medium", label: "Medium", description: "AAC 128kb/s" },
|
|
{ value: "high", label: "High", description: "AAC 256kb/s" },
|
|
{ value: "lossless", label: "Lossless", description: "ALAC or FLAC" },
|
|
]
|
|
|
|
const SECTIONS: SectionConfig[] = [
|
|
{ id: "profile", label: "Profile", icon: "person-circle-outline" },
|
|
{ id: "quality", label: "Playback", icon: "volume-high-outline" },
|
|
{ id: "downloads", label: "Downloads", icon: "arrow-down-circle-outline" },
|
|
{ id: "library", label: "Library", icon: "library-outline", admin: true },
|
|
{ id: "users", label: "Users", icon: "people-outline", admin: true },
|
|
]
|
|
|
|
export default function SettingsScreen() {
|
|
const colors = useThemeColors()
|
|
const user = useCachedQuery(api.auth.currentUser)
|
|
const { invalidateSession } = useInvalidateAuthSession()
|
|
const { clearServer } = useServerConfig()
|
|
const [section, setSection] = useState<Section>("profile")
|
|
const allUsers = useQuery(
|
|
api.users.listUsers,
|
|
user?.isAdmin === true && section === "users" ? {} : "skip"
|
|
)
|
|
|
|
if (!user) {
|
|
return <LoadingView />
|
|
}
|
|
|
|
const isAdmin = user.isAdmin === true
|
|
const visibleSections = SECTIONS.filter((item) => !item.admin || isAdmin)
|
|
|
|
async function handleSignOut() {
|
|
try {
|
|
await signOut(invalidateSession)
|
|
} catch {
|
|
showError("Could not clear all local data.")
|
|
}
|
|
}
|
|
|
|
async function handleChangeServer() {
|
|
try {
|
|
await changeServer(invalidateSession, clearServer)
|
|
} catch {
|
|
showError("Could not clear all local data.")
|
|
}
|
|
}
|
|
|
|
return (
|
|
<ScrollView
|
|
style={{ flex: 1, backgroundColor: colors.background }}
|
|
contentContainerStyle={[
|
|
styles.container,
|
|
{ paddingBottom: contentBottomInset + spacing.xl },
|
|
]}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
>
|
|
<View style={[styles.pillGroup, { backgroundColor: colors.card }]}>
|
|
{visibleSections.map((item) => {
|
|
const selected = section === item.id
|
|
return (
|
|
<Pressable
|
|
key={item.id}
|
|
accessibilityLabel={item.label}
|
|
accessibilityRole="button"
|
|
accessibilityState={{ selected }}
|
|
onPress={() => setSection(item.id)}
|
|
style={[
|
|
styles.pill,
|
|
{ backgroundColor: selected ? colors.primary : "transparent" },
|
|
]}
|
|
>
|
|
<Ionicons
|
|
name={item.icon}
|
|
size={26}
|
|
color={
|
|
selected ? colors.primaryForeground : colors.mutedForeground
|
|
}
|
|
/>
|
|
</Pressable>
|
|
)
|
|
})}
|
|
</View>
|
|
|
|
{section === "profile" ? (
|
|
<>
|
|
<AppearanceSection />
|
|
<ProfileSection user={user} />
|
|
</>
|
|
) : null}
|
|
{section === "quality" ? <QualitySection user={user} /> : null}
|
|
{section === "downloads" ? <DownloadsSection /> : null}
|
|
{section === "library" && isAdmin ? <LibrarySection /> : null}
|
|
{section === "users" && isAdmin && allUsers ? (
|
|
<UsersSection users={allUsers} currentUserId={user._id} />
|
|
) : null}
|
|
|
|
{section === "profile" ? (
|
|
<SessionSection
|
|
onChangeServer={() => void handleChangeServer()}
|
|
onSignOut={() => void handleSignOut()}
|
|
/>
|
|
) : null}
|
|
</ScrollView>
|
|
)
|
|
}
|
|
|
|
function AppearanceSection() {
|
|
const enabled = useArtworkColorsEnabled()
|
|
const [saving, setSaving] = useState(false)
|
|
|
|
return (
|
|
<SettingsCard title="Appearance">
|
|
<SwitchRow
|
|
title="New artwork colors"
|
|
subtitle="Use the new color algorithm on this device."
|
|
value={enabled ?? false}
|
|
disabled={enabled === undefined || saving}
|
|
onValueChange={async (checked) => {
|
|
setSaving(true)
|
|
try {
|
|
await setArtworkColorsEnabled(checked)
|
|
clearCoverPaletteCache()
|
|
} catch {
|
|
showError("Could not save artwork colors preference.")
|
|
} finally {
|
|
setSaving(false)
|
|
}
|
|
}}
|
|
/>
|
|
</SettingsCard>
|
|
)
|
|
}
|
|
|
|
function ProfileSection({ user }: { user: CurrentUser }) {
|
|
const colors = useThemeColors()
|
|
const updateName = useMutation(api.users.updateProfileName)
|
|
const updateEmail = useMutation(api.users.updateProfileEmail)
|
|
const updateImage = useMutation(api.users.updateProfileImage)
|
|
const updatePassword = useAction(api.users.updateOwnPassword)
|
|
const [name, setName] = useState(user.name)
|
|
const [email, setEmail] = useState(user.email)
|
|
const [newPassword, setNewPassword] = useState("")
|
|
const [confirmPassword, setConfirmPassword] = useState("")
|
|
const [changingPassword, setChangingPassword] = useState(false)
|
|
|
|
async function pickImage() {
|
|
const result = await ImagePicker.launchImageLibraryAsync({
|
|
mediaTypes: ["images"],
|
|
quality: 0.8,
|
|
base64: true,
|
|
})
|
|
if (result.canceled || !result.assets[0]?.base64) return
|
|
|
|
const mime = result.assets[0].mimeType ?? "image/jpeg"
|
|
try {
|
|
await updateImage({
|
|
image: `data:${mime};base64,${result.assets[0].base64}`,
|
|
})
|
|
} catch (error) {
|
|
showError(
|
|
error instanceof Error ? error.message : "Failed to update photo"
|
|
)
|
|
}
|
|
}
|
|
|
|
async function removeImage() {
|
|
try {
|
|
await updateImage({ image: null })
|
|
} catch (error) {
|
|
showError(
|
|
error instanceof Error ? error.message : "Failed to remove photo"
|
|
)
|
|
}
|
|
}
|
|
|
|
async function handlePasswordChange() {
|
|
if (newPassword !== confirmPassword) {
|
|
showError("Passwords do not match")
|
|
return
|
|
}
|
|
|
|
setChangingPassword(true)
|
|
try {
|
|
await updatePassword({ newPassword })
|
|
setNewPassword("")
|
|
setConfirmPassword("")
|
|
} catch (error) {
|
|
showError(
|
|
error instanceof Error ? error.message : "Failed to update password"
|
|
)
|
|
} finally {
|
|
setChangingPassword(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<SettingsCard
|
|
title="Profile Photo"
|
|
subtitle="This is the avatar shown in the app header and shared spaces."
|
|
>
|
|
<View style={styles.photoRow}>
|
|
<ProfileAvatar user={user} size={76} />
|
|
<View style={styles.photoActions}>
|
|
<Button
|
|
title="Choose Photo"
|
|
variant="secondary"
|
|
icon="image-outline"
|
|
onPress={() => void pickImage()}
|
|
/>
|
|
{user.image ? (
|
|
<Button
|
|
title="Remove"
|
|
variant="ghost"
|
|
icon="trash-outline"
|
|
onPress={() => void removeImage()}
|
|
/>
|
|
) : null}
|
|
</View>
|
|
</View>
|
|
</SettingsCard>
|
|
|
|
<SettingsCard
|
|
title="Account Details"
|
|
subtitle="Keep your profile information up to date."
|
|
>
|
|
<Field label="Name">
|
|
<Input value={name} onChangeText={setName} placeholder="Name" />
|
|
</Field>
|
|
<Button
|
|
title="Save Name"
|
|
variant="secondary"
|
|
disabled={name.trim() === user.name}
|
|
onPress={() =>
|
|
void updateName({ name: name.trim() }).catch((error: Error) =>
|
|
showError(error.message)
|
|
)
|
|
}
|
|
/>
|
|
<View style={[styles.divider, { backgroundColor: colors.border }]} />
|
|
<Field label="Email">
|
|
<Input
|
|
value={email}
|
|
onChangeText={setEmail}
|
|
placeholder="Email"
|
|
autoCapitalize="none"
|
|
keyboardType="email-address"
|
|
/>
|
|
</Field>
|
|
<Button
|
|
title="Save Email"
|
|
variant="secondary"
|
|
disabled={email.trim() === user.email}
|
|
onPress={() =>
|
|
void updateEmail({ email: email.trim() }).catch((error: Error) =>
|
|
showError(error.message)
|
|
)
|
|
}
|
|
/>
|
|
</SettingsCard>
|
|
|
|
<SettingsCard
|
|
title="Password"
|
|
subtitle="Choose a new password for this account."
|
|
>
|
|
<Field label="New Password">
|
|
<Input
|
|
value={newPassword}
|
|
onChangeText={setNewPassword}
|
|
placeholder="New password"
|
|
secureTextEntry
|
|
/>
|
|
</Field>
|
|
<Field label="Confirm Password">
|
|
<Input
|
|
value={confirmPassword}
|
|
onChangeText={setConfirmPassword}
|
|
placeholder="Confirm password"
|
|
secureTextEntry
|
|
/>
|
|
</Field>
|
|
<Button
|
|
title="Update Password"
|
|
loading={changingPassword}
|
|
disabled={!newPassword || newPassword !== confirmPassword}
|
|
onPress={() => void handlePasswordChange()}
|
|
/>
|
|
</SettingsCard>
|
|
</>
|
|
)
|
|
}
|
|
|
|
function QualitySection({ user }: { user: CurrentUser }) {
|
|
const colors = useThemeColors()
|
|
const updateQuality = useMutation(api.users.updatePlaybackQuality)
|
|
const updateAtmos = useMutation(api.users.updatePreferAtmos)
|
|
const quality = user.playbackQuality ?? "medium"
|
|
|
|
return (
|
|
<>
|
|
<SettingsCard
|
|
title="Streaming Quality"
|
|
subtitle="Pick the default audio quality for playback."
|
|
>
|
|
{QUALITY_OPTIONS.map((option) => {
|
|
const selected = quality === option.value
|
|
return (
|
|
<Pressable
|
|
key={option.value}
|
|
accessibilityRole="button"
|
|
accessibilityState={{ selected }}
|
|
onPress={() =>
|
|
void updateQuality({ playbackQuality: option.value }).catch(
|
|
(error: Error) => showError(error.message)
|
|
)
|
|
}
|
|
style={[
|
|
styles.optionRow,
|
|
{
|
|
borderColor: selected ? colors.primary : colors.border,
|
|
backgroundColor: selected ? colors.muted : colors.card,
|
|
},
|
|
]}
|
|
>
|
|
<View style={styles.optionCopy}>
|
|
<Text
|
|
style={[styles.optionTitle, { color: colors.foreground }]}
|
|
>
|
|
{option.label}
|
|
</Text>
|
|
<Text style={{ color: colors.mutedForeground }}>
|
|
{option.description}
|
|
</Text>
|
|
</View>
|
|
{selected ? (
|
|
<Ionicons
|
|
name="checkmark-circle"
|
|
size={22}
|
|
color={colors.primary}
|
|
/>
|
|
) : null}
|
|
</Pressable>
|
|
)
|
|
})}
|
|
</SettingsCard>
|
|
|
|
<SettingsCard title="Spatial Audio">
|
|
<SwitchRow
|
|
title="Prefer Dolby Atmos"
|
|
subtitle={
|
|
dolbyAtmosSupported
|
|
? "Use Atmos versions when they are available."
|
|
: "This device does not support Dolby Atmos playback."
|
|
}
|
|
disabled={!dolbyAtmosSupported}
|
|
value={atmosEnabled(user)}
|
|
onValueChange={(checked) =>
|
|
void updateAtmos({ preferAtmos: checked }).catch((error: Error) =>
|
|
showError(error.message)
|
|
)
|
|
}
|
|
/>
|
|
</SettingsCard>
|
|
</>
|
|
)
|
|
}
|
|
|
|
/** Shades of the text color, faintest for the smallest files. */
|
|
const QUALITY_OPACITY: Record<DownloadQuality, number> = {
|
|
medium: 0.25,
|
|
high: 0.45,
|
|
lossless: 0.7,
|
|
atmos: 1,
|
|
}
|
|
|
|
function songCount(count: number) {
|
|
return count === 1 ? "1 song" : `${count} songs`
|
|
}
|
|
|
|
function QualityName({
|
|
quality,
|
|
color,
|
|
backgroundColor,
|
|
}: {
|
|
quality: DownloadQuality
|
|
color: string
|
|
backgroundColor: string
|
|
}) {
|
|
if (quality === "lossless" || quality === "atmos") {
|
|
return (
|
|
<AudioFormatBadge
|
|
format={quality}
|
|
color={color}
|
|
backgroundColor={backgroundColor}
|
|
/>
|
|
)
|
|
}
|
|
return (
|
|
<Text style={{ color, fontSize: 13, fontWeight: "500" }}>
|
|
{DOWNLOAD_QUALITY_LABELS[quality]}
|
|
</Text>
|
|
)
|
|
}
|
|
|
|
function confirm(title: string, onConfirm: () => void) {
|
|
Alert.alert(title, undefined, [
|
|
{ text: "Cancel", style: "cancel" },
|
|
{ text: "Remove", style: "destructive", onPress: onConfirm },
|
|
])
|
|
}
|
|
|
|
function RememberedQualityCard() {
|
|
const colors = useThemeColors()
|
|
// undefined until read; null means ask every time.
|
|
const [remembered, setRemembered] = useState<DownloadQuality | null>()
|
|
useEffect(() => {
|
|
void readRememberedDownloadQuality().then((quality) =>
|
|
setRemembered(quality ?? null)
|
|
)
|
|
}, [])
|
|
const options: {
|
|
value: DownloadQuality | null
|
|
label: string
|
|
detail: string
|
|
}[] = [
|
|
{ value: null, label: "Ask every time", detail: "Choose when downloading" },
|
|
...DOWNLOAD_QUALITIES.map((quality) => ({
|
|
value: quality,
|
|
label: DOWNLOAD_QUALITY_LABELS[quality],
|
|
detail: DOWNLOAD_QUALITY_DETAILS[quality],
|
|
})),
|
|
]
|
|
|
|
return (
|
|
<SettingsCard title="Download Quality">
|
|
{options.map((option) => {
|
|
const selected = remembered === option.value
|
|
return (
|
|
<Pressable
|
|
key={option.value ?? "ask"}
|
|
accessibilityRole="button"
|
|
accessibilityState={{ selected }}
|
|
onPress={() => {
|
|
setRemembered(option.value)
|
|
void writeRememberedDownloadQuality(option.value ?? undefined)
|
|
}}
|
|
style={[
|
|
styles.optionRow,
|
|
{
|
|
borderColor: selected ? colors.primary : colors.border,
|
|
backgroundColor: selected ? colors.muted : colors.card,
|
|
},
|
|
]}
|
|
>
|
|
<View style={styles.optionCopy}>
|
|
<Text style={[styles.optionTitle, { color: colors.foreground }]}>
|
|
{option.label}
|
|
</Text>
|
|
<Text style={{ color: colors.mutedForeground }}>
|
|
{option.detail}
|
|
</Text>
|
|
</View>
|
|
{selected ? (
|
|
<Ionicons
|
|
name="checkmark-circle"
|
|
size={22}
|
|
color={colors.primary}
|
|
/>
|
|
) : null}
|
|
</Pressable>
|
|
)
|
|
})}
|
|
</SettingsCard>
|
|
)
|
|
}
|
|
|
|
function DownloadsSection() {
|
|
const colors = useThemeColors()
|
|
const { queue } = useDownloads()
|
|
const version = useDownloadsVersion()
|
|
const breakdown = useMemo(
|
|
() => queue?.breakdown(),
|
|
// version invalidates the breakdown when downloads change.
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
[queue, version]
|
|
)
|
|
if (!queue || !breakdown) return null
|
|
const qualities = breakdown.byQuality.filter((entry) => entry.songs > 0)
|
|
|
|
return (
|
|
<>
|
|
<SettingsCard title="Storage">
|
|
<Text style={{ color: colors.foreground, fontSize: 15 }}>
|
|
{formatBytes(breakdown.bytes)} · {songCount(breakdown.songs)}
|
|
{breakdown.queued > 0 ? ` · ${breakdown.queued} downloading` : ""}
|
|
</Text>
|
|
{breakdown.songs > 0 ? (
|
|
<>
|
|
<View
|
|
style={[styles.storageBar, { backgroundColor: colors.muted }]}
|
|
>
|
|
{qualities.map((entry) => (
|
|
<View
|
|
key={entry.quality}
|
|
style={{
|
|
flex: entry.bytes,
|
|
backgroundColor: colors.foreground,
|
|
opacity: QUALITY_OPACITY[entry.quality],
|
|
}}
|
|
/>
|
|
))}
|
|
</View>
|
|
{qualities.map((entry) => (
|
|
<View key={entry.quality} style={styles.storageRow}>
|
|
<View
|
|
style={[
|
|
styles.storageDot,
|
|
{
|
|
backgroundColor: colors.foreground,
|
|
opacity: QUALITY_OPACITY[entry.quality],
|
|
},
|
|
]}
|
|
/>
|
|
<View style={{ flex: 1 }}>
|
|
<QualityName
|
|
quality={entry.quality}
|
|
color={colors.foreground}
|
|
backgroundColor={colors.card}
|
|
/>
|
|
</View>
|
|
<Text style={{ color: colors.mutedForeground, fontSize: 13 }}>
|
|
{songCount(entry.songs)}
|
|
</Text>
|
|
<Text
|
|
style={[styles.storageSize, { color: colors.foreground }]}
|
|
>
|
|
{formatBytes(entry.bytes)}
|
|
</Text>
|
|
</View>
|
|
))}
|
|
</>
|
|
) : null}
|
|
</SettingsCard>
|
|
|
|
<RememberedQualityCard />
|
|
|
|
{breakdown.collections.length > 0 ||
|
|
breakdown.otherSongs.songIds.length > 0 ? (
|
|
<SettingsCard title="Downloaded">
|
|
{breakdown.collections.map(
|
|
({ collection, bytes, status, fallbackCount }) => (
|
|
<View
|
|
key={`${collection.kind}:${collection.id}`}
|
|
style={styles.storageRow}
|
|
>
|
|
<View style={styles.optionCopy}>
|
|
<Text
|
|
style={{ color: colors.foreground, fontSize: 15 }}
|
|
numberOfLines={1}
|
|
>
|
|
{collection.title}
|
|
</Text>
|
|
<View style={styles.collectionMeta}>
|
|
<Text
|
|
style={{ color: colors.mutedForeground, fontSize: 13 }}
|
|
>
|
|
{collection.kind === "album" ? "Album" : "Playlist"} ·
|
|
</Text>
|
|
<QualityName
|
|
quality={collection.quality}
|
|
color={colors.mutedForeground}
|
|
backgroundColor={colors.card}
|
|
/>
|
|
{fallbackCount > 0 ? (
|
|
<Text
|
|
style={{ color: colors.mutedForeground, fontSize: 13 }}
|
|
>
|
|
({fallbackCount} in High)
|
|
</Text>
|
|
) : null}
|
|
{status.downloaded < status.total ? (
|
|
<Text
|
|
style={{ color: colors.mutedForeground, fontSize: 13 }}
|
|
>
|
|
· {status.downloaded} of {status.total}
|
|
</Text>
|
|
) : null}
|
|
</View>
|
|
</View>
|
|
<Text
|
|
style={[styles.storageSize, { color: colors.foreground }]}
|
|
>
|
|
{formatBytes(bytes)}
|
|
</Text>
|
|
<Pressable
|
|
hitSlop={8}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={`Remove download of ${collection.title}`}
|
|
onPress={() =>
|
|
confirm(
|
|
`Remove ${collection.title}?`,
|
|
() => void queue.removeCollection(collection)
|
|
)
|
|
}
|
|
>
|
|
<Ionicons
|
|
name="trash-outline"
|
|
size={20}
|
|
color={colors.mutedForeground}
|
|
/>
|
|
</Pressable>
|
|
</View>
|
|
)
|
|
)}
|
|
{breakdown.otherSongs.songIds.length > 0 ? (
|
|
<View style={styles.storageRow}>
|
|
<View style={styles.optionCopy}>
|
|
<Text style={{ color: colors.foreground, fontSize: 15 }}>
|
|
Other Downloaded Songs
|
|
</Text>
|
|
<Text style={{ color: colors.mutedForeground, fontSize: 13 }}>
|
|
{songCount(breakdown.otherSongs.songIds.length)}
|
|
</Text>
|
|
</View>
|
|
<Text style={[styles.storageSize, { color: colors.foreground }]}>
|
|
{formatBytes(breakdown.otherSongs.bytes)}
|
|
</Text>
|
|
<Pressable
|
|
hitSlop={8}
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Remove Other Downloaded Songs"
|
|
onPress={() =>
|
|
confirm(
|
|
"Remove Other Downloaded Songs?",
|
|
() => void queue.removeOtherSongs()
|
|
)
|
|
}
|
|
>
|
|
<Ionicons
|
|
name="trash-outline"
|
|
size={20}
|
|
color={colors.mutedForeground}
|
|
/>
|
|
</Pressable>
|
|
</View>
|
|
) : null}
|
|
<Button
|
|
title="Remove All Downloads"
|
|
variant="destructive"
|
|
onPress={() =>
|
|
confirm("Remove all downloads?", () => void queue.removeAll())
|
|
}
|
|
/>
|
|
</SettingsCard>
|
|
) : null}
|
|
</>
|
|
)
|
|
}
|
|
|
|
function LibrarySection() {
|
|
const updateLibrary = useAction(api.actions.updateLibrary)
|
|
const cleanupHlsStreams = useAction(api.actions.adminCleanupHlsStreams)
|
|
const [updating, setUpdating] = useState(false)
|
|
const [cleaningHls, setCleaningHls] = useState(false)
|
|
|
|
return (
|
|
<SettingsCard
|
|
title="Library Maintenance"
|
|
subtitle="Run administrative jobs for the shared music library."
|
|
>
|
|
<Button
|
|
title="Update Library"
|
|
icon="refresh-outline"
|
|
loading={updating}
|
|
onPress={async () => {
|
|
setUpdating(true)
|
|
try {
|
|
await updateLibrary({})
|
|
} catch (error) {
|
|
showError(
|
|
error instanceof Error
|
|
? error.message
|
|
: "Failed to update library"
|
|
)
|
|
} finally {
|
|
setUpdating(false)
|
|
}
|
|
}}
|
|
/>
|
|
<Button
|
|
title="Clean HLS Streams"
|
|
variant="secondary"
|
|
icon="sparkles-outline"
|
|
loading={cleaningHls}
|
|
onPress={async () => {
|
|
setCleaningHls(true)
|
|
try {
|
|
await cleanupHlsStreams({})
|
|
} catch (error) {
|
|
showError(
|
|
error instanceof Error
|
|
? error.message
|
|
: "Failed to clean HLS streams"
|
|
)
|
|
} finally {
|
|
setCleaningHls(false)
|
|
}
|
|
}}
|
|
/>
|
|
</SettingsCard>
|
|
)
|
|
}
|
|
|
|
function UsersSection({
|
|
users,
|
|
currentUserId,
|
|
}: {
|
|
users: AdminUser[]
|
|
currentUserId: Id<"users">
|
|
}) {
|
|
const colors = useThemeColors()
|
|
const appSettings = useQuery(api.users.getAppSettings)
|
|
const setDisableCreation = useMutation(api.users.setDisableAccountCreation)
|
|
const setAdmin = useMutation(api.users.setUserAdmin)
|
|
const deleteUserMutation = useMutation(api.users.deleteUser)
|
|
|
|
return (
|
|
<>
|
|
<SettingsCard title="Sign Up">
|
|
<SwitchRow
|
|
title="Disable account creation"
|
|
subtitle="Stop people from registering themselves. Admins can still create accounts."
|
|
value={appSettings?.disableAccountCreation ?? false}
|
|
onValueChange={(checked) =>
|
|
void setDisableCreation({ disabled: checked }).catch(
|
|
(error: Error) => showError(error.message)
|
|
)
|
|
}
|
|
/>
|
|
</SettingsCard>
|
|
|
|
<SettingsCard
|
|
title="People"
|
|
subtitle="Manage who can access this server."
|
|
>
|
|
<Button
|
|
title="Create account"
|
|
onPress={() => router.push("/(app)/create-account")}
|
|
/>
|
|
<View style={styles.userList}>
|
|
{users.map((user, index) => (
|
|
<View
|
|
key={user._id}
|
|
style={[
|
|
styles.userBlock,
|
|
index > 0
|
|
? [styles.userBlockSpaced, { borderTopColor: colors.border }]
|
|
: null,
|
|
]}
|
|
>
|
|
<View style={styles.userRow}>
|
|
<ProfileAvatar user={user} size={44} />
|
|
<View style={styles.userCopy}>
|
|
<View style={styles.userNameRow}>
|
|
<Text
|
|
style={[styles.userName, { color: colors.foreground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{user.name}
|
|
</Text>
|
|
{user.isAdmin ? (
|
|
<View
|
|
style={[
|
|
styles.badge,
|
|
{ backgroundColor: colors.muted },
|
|
]}
|
|
>
|
|
<Text
|
|
style={[
|
|
styles.badgeText,
|
|
{ color: colors.mutedForeground },
|
|
]}
|
|
>
|
|
Admin
|
|
</Text>
|
|
</View>
|
|
) : null}
|
|
</View>
|
|
<Text
|
|
style={{ color: colors.mutedForeground }}
|
|
numberOfLines={1}
|
|
>
|
|
{user.email}
|
|
</Text>
|
|
</View>
|
|
</View>
|
|
{user._id !== currentUserId ? (
|
|
<View style={styles.userActions}>
|
|
<Button
|
|
title={user.isAdmin ? "Remove Admin" : "Make Admin"}
|
|
variant="secondary"
|
|
onPress={() =>
|
|
void setAdmin({
|
|
userId: user._id,
|
|
isAdmin: !user.isAdmin,
|
|
}).catch((error: Error) => showError(error.message))
|
|
}
|
|
style={styles.userActionButton}
|
|
/>
|
|
<Button
|
|
title="Delete"
|
|
variant="destructive"
|
|
onPress={() =>
|
|
void deleteUserMutation({ userId: user._id }).catch(
|
|
(error: Error) => showError(error.message)
|
|
)
|
|
}
|
|
style={styles.userActionButton}
|
|
/>
|
|
</View>
|
|
) : null}
|
|
</View>
|
|
))}
|
|
</View>
|
|
</SettingsCard>
|
|
</>
|
|
)
|
|
}
|
|
|
|
function SessionSection({
|
|
onChangeServer,
|
|
onSignOut,
|
|
}: {
|
|
onChangeServer: () => void
|
|
onSignOut: () => void
|
|
}) {
|
|
const colors = useThemeColors()
|
|
|
|
return (
|
|
<SettingsCard title="Session">
|
|
<ActionRow
|
|
icon="server-outline"
|
|
title="Change Server"
|
|
subtitle="Disconnect from this server and choose another."
|
|
onPress={onChangeServer}
|
|
/>
|
|
<View style={[styles.divider, { backgroundColor: colors.border }]} />
|
|
<ActionRow
|
|
icon="log-out-outline"
|
|
title="Sign Out"
|
|
subtitle="Return to the sign-in screen."
|
|
destructive
|
|
onPress={onSignOut}
|
|
/>
|
|
</SettingsCard>
|
|
)
|
|
}
|
|
|
|
function SettingsCard({
|
|
title,
|
|
subtitle,
|
|
children,
|
|
}: {
|
|
title: string
|
|
subtitle?: string
|
|
children: React.ReactNode
|
|
}) {
|
|
const colors = useThemeColors()
|
|
|
|
return (
|
|
<View style={[styles.card, { backgroundColor: colors.card }]}>
|
|
<View style={styles.cardHeader}>
|
|
<Text style={[styles.cardTitle, { color: colors.foreground }]}>
|
|
{title}
|
|
</Text>
|
|
{subtitle ? (
|
|
<Text
|
|
style={[styles.cardSubtitle, { color: colors.mutedForeground }]}
|
|
>
|
|
{subtitle}
|
|
</Text>
|
|
) : null}
|
|
</View>
|
|
{children}
|
|
</View>
|
|
)
|
|
}
|
|
|
|
function Field({
|
|
label,
|
|
children,
|
|
}: {
|
|
label: string
|
|
children: React.ReactNode
|
|
}) {
|
|
const colors = useThemeColors()
|
|
|
|
return (
|
|
<View style={styles.field}>
|
|
<Text style={[styles.fieldLabel, { color: colors.mutedForeground }]}>
|
|
{label}
|
|
</Text>
|
|
{children}
|
|
</View>
|
|
)
|
|
}
|
|
|
|
function SwitchRow({
|
|
title,
|
|
subtitle,
|
|
value,
|
|
onValueChange,
|
|
disabled,
|
|
}: {
|
|
title: string
|
|
subtitle?: string
|
|
value: boolean
|
|
disabled?: boolean
|
|
onValueChange: (checked: boolean) => void
|
|
}) {
|
|
const colors = useThemeColors()
|
|
|
|
return (
|
|
<View style={styles.switchRow}>
|
|
<View style={styles.optionCopy}>
|
|
<Text style={[styles.optionTitle, { color: colors.foreground }]}>
|
|
{title}
|
|
</Text>
|
|
{subtitle ? (
|
|
<Text style={{ color: colors.mutedForeground }}>{subtitle}</Text>
|
|
) : null}
|
|
</View>
|
|
<Switch
|
|
value={value}
|
|
disabled={disabled}
|
|
onValueChange={onValueChange}
|
|
{...(Platform.OS === "android"
|
|
? {
|
|
trackColor: { false: colors.muted, true: colors.primary },
|
|
thumbColor: colors.foreground,
|
|
}
|
|
: {})}
|
|
/>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
function ActionRow({
|
|
icon,
|
|
title,
|
|
subtitle,
|
|
destructive,
|
|
onPress,
|
|
}: {
|
|
icon: React.ComponentProps<typeof Ionicons>["name"]
|
|
title: string
|
|
subtitle: string
|
|
destructive?: boolean
|
|
onPress: () => void
|
|
}) {
|
|
const colors = useThemeColors()
|
|
const tint = destructive ? colors.destructive : colors.foreground
|
|
|
|
return (
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
onPress={onPress}
|
|
style={({ pressed }) => [
|
|
styles.actionRow,
|
|
{ opacity: pressed ? 0.75 : 1 },
|
|
]}
|
|
>
|
|
<View style={[styles.actionIcon, { backgroundColor: colors.muted }]}>
|
|
<Ionicons name={icon} size={20} color={tint} />
|
|
</View>
|
|
<View style={styles.optionCopy}>
|
|
<Text style={[styles.optionTitle, { color: tint }]}>{title}</Text>
|
|
<Text style={{ color: colors.mutedForeground }}>{subtitle}</Text>
|
|
</View>
|
|
<Ionicons
|
|
name="chevron-forward"
|
|
size={18}
|
|
color={colors.mutedForeground}
|
|
/>
|
|
</Pressable>
|
|
)
|
|
}
|
|
|
|
function ProfileAvatar({
|
|
user,
|
|
size,
|
|
style,
|
|
}: {
|
|
user: Pick<CurrentUser, "name" | "image">
|
|
size: number
|
|
style?: StyleProp<ViewStyle>
|
|
}) {
|
|
const colors = useThemeColors()
|
|
const imageUrl = useProfileImageUrl(user.image)
|
|
const avatarStyle = [
|
|
styles.avatar,
|
|
{
|
|
width: size,
|
|
height: size,
|
|
borderRadius: size / 2,
|
|
backgroundColor: colors.muted,
|
|
},
|
|
style,
|
|
]
|
|
|
|
if (imageUrl) {
|
|
return (
|
|
<View style={avatarStyle}>
|
|
<Image
|
|
source={{ uri: imageUrl }}
|
|
style={styles.avatarImage}
|
|
contentFit="cover"
|
|
/>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<View style={avatarStyle}>
|
|
<Text
|
|
style={[
|
|
styles.avatarInitials,
|
|
{ color: colors.foreground, fontSize: size * 0.34 },
|
|
]}
|
|
>
|
|
{initialsFromName(user.name)}
|
|
</Text>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
gap: spacing.lg,
|
|
paddingHorizontal: spacing.lg,
|
|
paddingTop: spacing.md,
|
|
},
|
|
badge: {
|
|
alignSelf: "flex-start",
|
|
borderRadius: radius.full,
|
|
paddingHorizontal: spacing.sm,
|
|
paddingVertical: 2,
|
|
},
|
|
badgeText: {
|
|
fontSize: 12,
|
|
fontWeight: "700",
|
|
},
|
|
pillGroup: {
|
|
borderRadius: radius.xl,
|
|
padding: spacing.xs,
|
|
flexDirection: "row",
|
|
gap: spacing.xs,
|
|
},
|
|
pill: {
|
|
flex: 1,
|
|
minHeight: 48,
|
|
borderRadius: radius.full,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
card: {
|
|
borderRadius: radius.xl,
|
|
padding: spacing.lg,
|
|
gap: spacing.md,
|
|
},
|
|
cardHeader: {
|
|
gap: spacing.xs,
|
|
},
|
|
cardTitle: {
|
|
fontSize: 18,
|
|
fontWeight: "800",
|
|
},
|
|
cardSubtitle: {
|
|
fontSize: 14,
|
|
lineHeight: 20,
|
|
},
|
|
photoRow: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.lg,
|
|
},
|
|
photoActions: {
|
|
flex: 1,
|
|
gap: spacing.sm,
|
|
},
|
|
field: {
|
|
gap: spacing.sm,
|
|
},
|
|
fieldLabel: {
|
|
fontSize: 13,
|
|
fontWeight: "700",
|
|
textTransform: "uppercase",
|
|
},
|
|
divider: {
|
|
height: StyleSheet.hairlineWidth,
|
|
},
|
|
storageBar: {
|
|
height: 8,
|
|
borderRadius: 4,
|
|
overflow: "hidden",
|
|
flexDirection: "row",
|
|
},
|
|
storageRow: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.md,
|
|
minHeight: 36,
|
|
},
|
|
storageDot: { width: 8, height: 8, borderRadius: 4 },
|
|
storageSize: {
|
|
minWidth: 56,
|
|
textAlign: "right",
|
|
fontSize: 13,
|
|
fontVariant: ["tabular-nums"],
|
|
},
|
|
collectionMeta: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
flexWrap: "wrap",
|
|
gap: 4,
|
|
},
|
|
optionRow: {
|
|
borderWidth: 1,
|
|
borderRadius: radius.lg,
|
|
padding: spacing.md,
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.md,
|
|
},
|
|
optionCopy: {
|
|
flex: 1,
|
|
gap: 3,
|
|
},
|
|
optionTitle: {
|
|
fontSize: 16,
|
|
fontWeight: "700",
|
|
},
|
|
switchRow: {
|
|
minHeight: 56,
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.md,
|
|
},
|
|
userList: {
|
|
gap: spacing.lg,
|
|
},
|
|
userBlock: {
|
|
gap: spacing.md,
|
|
},
|
|
userBlockSpaced: {
|
|
paddingTop: spacing.lg,
|
|
borderTopWidth: StyleSheet.hairlineWidth,
|
|
},
|
|
userRow: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.md,
|
|
},
|
|
userCopy: {
|
|
flex: 1,
|
|
gap: 3,
|
|
},
|
|
userNameRow: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.sm,
|
|
},
|
|
userName: {
|
|
flexShrink: 1,
|
|
fontSize: 16,
|
|
fontWeight: "700",
|
|
},
|
|
userActions: {
|
|
flexDirection: "row",
|
|
gap: spacing.sm,
|
|
},
|
|
userActionButton: {
|
|
flex: 1,
|
|
},
|
|
actionRow: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.md,
|
|
},
|
|
actionIcon: {
|
|
width: 36,
|
|
height: 36,
|
|
borderRadius: 18,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
avatar: {
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
overflow: "hidden",
|
|
},
|
|
avatarImage: {
|
|
width: "100%",
|
|
height: "100%",
|
|
},
|
|
avatarInitials: {
|
|
fontWeight: "800",
|
|
},
|
|
})
|