- 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
975 lines
28 KiB
TypeScript
975 lines
28 KiB
TypeScript
"use client"
|
|
|
|
import { useAction, useMutation, useQuery } from "convex/react"
|
|
import {
|
|
MoreHorizontal,
|
|
Trash2,
|
|
Upload,
|
|
UserPlus,
|
|
} from "lucide-react"
|
|
import { useCallback, useEffect, useRef, useState } from "react"
|
|
import { toast } from "sonner"
|
|
|
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
|
|
import { Button } from "@/components/ui/button"
|
|
import { Checkbox } from "@/components/ui/checkbox"
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog"
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu"
|
|
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
|
|
import { Input } from "@/components/ui/input"
|
|
import { Label } from "@/components/ui/label"
|
|
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
|
|
import { ScrollArea } from "@/components/ui/scroll-area"
|
|
import { Separator } from "@/components/ui/separator"
|
|
import { Spinner } from "@/components/ui/spinner"
|
|
import { Switch } from "@/components/ui/switch"
|
|
import {
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
} from "@/components/ui/table"
|
|
import { usePlayback } from "@/components/playback-provider"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { Doc, Id } from "@otunes/convex/_generated/dataModel"
|
|
import { resizeProfileImageToDataUrl } from "@/lib/resize-profile-image"
|
|
import { cn } from "@/lib/utils"
|
|
|
|
type Section = "profile" | "quality" | "users" | "library"
|
|
|
|
type PlaybackQuality = "low" | "medium" | "high" | "lossless"
|
|
|
|
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" },
|
|
]
|
|
|
|
function useDebouncedCallback<T extends (...args: never[]) => void>(
|
|
callback: T,
|
|
delay: number
|
|
) {
|
|
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
const callbackRef = useRef(callback)
|
|
|
|
useEffect(() => {
|
|
callbackRef.current = callback
|
|
})
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (timeoutRef.current) clearTimeout(timeoutRef.current)
|
|
}
|
|
}, [])
|
|
|
|
return useCallback(
|
|
(...args: Parameters<T>) => {
|
|
if (timeoutRef.current) clearTimeout(timeoutRef.current)
|
|
timeoutRef.current = setTimeout(() => {
|
|
callbackRef.current(...args)
|
|
}, delay)
|
|
},
|
|
[delay]
|
|
)
|
|
}
|
|
|
|
function NavButton({
|
|
active,
|
|
onClick,
|
|
children,
|
|
}: {
|
|
active: boolean
|
|
onClick: () => void
|
|
children: React.ReactNode
|
|
}) {
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={onClick}
|
|
className={cn(
|
|
"w-full rounded-lg px-2.5 py-1.5 text-left text-sm transition-colors",
|
|
active
|
|
? "bg-muted font-medium text-foreground"
|
|
: "text-muted-foreground hover:bg-muted/60 hover:text-foreground"
|
|
)}
|
|
>
|
|
{children}
|
|
</button>
|
|
)
|
|
}
|
|
|
|
function UserAvatar({
|
|
user,
|
|
size = "default",
|
|
}: {
|
|
user: Pick<Doc<"users">, "name" | "image">
|
|
size?: "default" | "sm"
|
|
}) {
|
|
const initials = user.name
|
|
.split(" ")
|
|
.map((part) => part[0])
|
|
.join("")
|
|
.slice(0, 2)
|
|
.toUpperCase()
|
|
|
|
return (
|
|
<Avatar
|
|
key={user.image ?? "fallback"}
|
|
size={size === "sm" ? "sm" : "default"}
|
|
>
|
|
{user.image ? (
|
|
<AvatarImage src={user.image} alt={user.name} />
|
|
) : null}
|
|
<AvatarFallback>{initials}</AvatarFallback>
|
|
</Avatar>
|
|
)
|
|
}
|
|
|
|
function ProfileSection({ user }: { user: Doc<"users"> }) {
|
|
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 [prevUser, setPrevUser] = useState({
|
|
name: user.name,
|
|
email: user.email,
|
|
})
|
|
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)
|
|
const fileInputRef = useRef<HTMLInputElement>(null)
|
|
|
|
if (user.name !== prevUser.name || user.email !== prevUser.email) {
|
|
setPrevUser({ name: user.name, email: user.email })
|
|
if (user.name !== prevUser.name) {
|
|
setName(user.name)
|
|
}
|
|
if (user.email !== prevUser.email) {
|
|
setEmail(user.email)
|
|
}
|
|
}
|
|
|
|
const debouncedUpdateName = useDebouncedCallback((value: string) => {
|
|
if (value.trim() === user.name) return
|
|
updateName({ name: value }).catch((error: Error) =>
|
|
toast.error(error.message)
|
|
)
|
|
}, 500)
|
|
|
|
const debouncedUpdateEmail = useDebouncedCallback((value: string) => {
|
|
if (value.trim().toLowerCase() === user.email) return
|
|
updateEmail({ email: value }).catch((error: Error) =>
|
|
toast.error(error.message)
|
|
)
|
|
}, 500)
|
|
|
|
async function handleImageUpload(file: File) {
|
|
if (!file.type.startsWith("image/")) {
|
|
toast.error("Please upload an image file")
|
|
return
|
|
}
|
|
if (file.size > 2 * 1024 * 1024) {
|
|
toast.error("Image must be smaller than 2 MB")
|
|
return
|
|
}
|
|
|
|
try {
|
|
const dataUrl = await resizeProfileImageToDataUrl(file)
|
|
await updateImage({ image: dataUrl })
|
|
toast.success("Profile photo updated")
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update photo"
|
|
)
|
|
}
|
|
}
|
|
|
|
async function handleClearImage() {
|
|
try {
|
|
await updateImage({ image: null })
|
|
toast.success("Profile photo removed")
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to remove photo"
|
|
)
|
|
}
|
|
}
|
|
|
|
async function handlePasswordChange() {
|
|
if (newPassword !== confirmPassword) {
|
|
toast.error("Passwords do not match")
|
|
return
|
|
}
|
|
if (newPassword.length < 8) {
|
|
toast.error("Password must be at least 8 characters")
|
|
return
|
|
}
|
|
|
|
setChangingPassword(true)
|
|
try {
|
|
await updatePassword({ newPassword })
|
|
setNewPassword("")
|
|
setConfirmPassword("")
|
|
toast.success("Password updated")
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update password"
|
|
)
|
|
} finally {
|
|
setChangingPassword(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-8">
|
|
<div>
|
|
<h3 className="text-base font-medium">Profile</h3>
|
|
<p className="text-sm text-muted-foreground">
|
|
Manage your profile photo and account details.
|
|
</p>
|
|
</div>
|
|
|
|
<FieldGroup>
|
|
<Field>
|
|
<FieldLabel>Profile photo</FieldLabel>
|
|
<div className="flex items-center gap-4">
|
|
<UserAvatar user={user} size="default" />
|
|
<div className="flex flex-wrap gap-2">
|
|
<input
|
|
ref={fileInputRef}
|
|
type="file"
|
|
accept="image/*"
|
|
className="hidden"
|
|
onChange={(event) => {
|
|
const file = event.target.files?.[0]
|
|
if (file) void handleImageUpload(file)
|
|
event.target.value = ""
|
|
}}
|
|
/>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => fileInputRef.current?.click()}
|
|
>
|
|
<Upload />
|
|
Upload image
|
|
</Button>
|
|
{user.image ? (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => void handleClearImage()}
|
|
>
|
|
Remove photo
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
</Field>
|
|
|
|
<Field>
|
|
<FieldLabel htmlFor="profile-name">Name</FieldLabel>
|
|
<Input
|
|
id="profile-name"
|
|
value={name}
|
|
onChange={(event) => {
|
|
setName(event.target.value)
|
|
debouncedUpdateName(event.target.value)
|
|
}}
|
|
/>
|
|
</Field>
|
|
|
|
<Field>
|
|
<FieldLabel htmlFor="profile-email">Email</FieldLabel>
|
|
<Input
|
|
id="profile-email"
|
|
type="email"
|
|
value={email}
|
|
onChange={(event) => {
|
|
setEmail(event.target.value)
|
|
debouncedUpdateEmail(event.target.value)
|
|
}}
|
|
/>
|
|
</Field>
|
|
</FieldGroup>
|
|
|
|
<Separator />
|
|
|
|
<div className="space-y-4">
|
|
<div>
|
|
<h4 className="text-sm font-medium">Password</h4>
|
|
<p className="text-sm text-muted-foreground">
|
|
Confirm your new password to update it.
|
|
</p>
|
|
</div>
|
|
<FieldGroup>
|
|
<Field>
|
|
<FieldLabel htmlFor="new-password">New password</FieldLabel>
|
|
<Input
|
|
id="new-password"
|
|
type="password"
|
|
autoComplete="new-password"
|
|
value={newPassword}
|
|
onChange={(event) => setNewPassword(event.target.value)}
|
|
/>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor="confirm-password">Confirm password</FieldLabel>
|
|
<Input
|
|
id="confirm-password"
|
|
type="password"
|
|
autoComplete="new-password"
|
|
value={confirmPassword}
|
|
onChange={(event) => setConfirmPassword(event.target.value)}
|
|
/>
|
|
</Field>
|
|
<Field>
|
|
<Button
|
|
type="button"
|
|
disabled={
|
|
changingPassword ||
|
|
!newPassword ||
|
|
!confirmPassword ||
|
|
newPassword !== confirmPassword
|
|
}
|
|
onClick={() => void handlePasswordChange()}
|
|
>
|
|
{changingPassword && <Spinner />}
|
|
Update password
|
|
</Button>
|
|
</Field>
|
|
</FieldGroup>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function QualitySection({ user }: { user: Doc<"users"> }) {
|
|
const { dolbyAtmosSupported } = usePlayback()
|
|
const updateQuality = useMutation(api.users.updatePlaybackQuality)
|
|
const updateAtmos = useMutation(api.users.updatePreferAtmos)
|
|
|
|
const quality = user.playbackQuality ?? "medium"
|
|
const atmosToggleDisabled = dolbyAtmosSupported !== true
|
|
|
|
return (
|
|
<div className="space-y-8">
|
|
<div>
|
|
<h3 className="text-base font-medium">Quality</h3>
|
|
<p className="text-sm text-muted-foreground">
|
|
Choose your preferred playback quality and spatial audio options.
|
|
</p>
|
|
</div>
|
|
|
|
<RadioGroup
|
|
value={quality}
|
|
onValueChange={(value) => {
|
|
updateQuality({ playbackQuality: value as PlaybackQuality }).catch(
|
|
(error: Error) => toast.error(error.message)
|
|
)
|
|
}}
|
|
className="gap-3"
|
|
>
|
|
{QUALITY_OPTIONS.map((option) => (
|
|
<div
|
|
key={option.value}
|
|
className="flex items-start gap-3 rounded-lg border p-3"
|
|
>
|
|
<RadioGroupItem
|
|
value={option.value}
|
|
id={`quality-${option.value}`}
|
|
className="mt-0.5"
|
|
/>
|
|
<Label
|
|
htmlFor={`quality-${option.value}`}
|
|
className="flex cursor-pointer flex-col gap-0.5 font-normal"
|
|
>
|
|
<span className="font-medium">
|
|
{option.label} · {option.description}
|
|
</span>
|
|
</Label>
|
|
</div>
|
|
))}
|
|
</RadioGroup>
|
|
|
|
<div
|
|
className={cn(
|
|
"flex items-start gap-3 rounded-lg border p-4",
|
|
atmosToggleDisabled && "opacity-60"
|
|
)}
|
|
>
|
|
<Checkbox
|
|
id="prefer-atmos"
|
|
checked={user.preferAtmos ?? false}
|
|
disabled={atmosToggleDisabled}
|
|
onCheckedChange={(checked) => {
|
|
updateAtmos({ preferAtmos: checked === true }).catch(
|
|
(error: Error) => toast.error(error.message)
|
|
)
|
|
}}
|
|
/>
|
|
<div className="space-y-1">
|
|
<Label
|
|
htmlFor="prefer-atmos"
|
|
className={cn(
|
|
"font-normal",
|
|
!atmosToggleDisabled && "cursor-pointer"
|
|
)}
|
|
>
|
|
Prefer Dolby Atmos when available
|
|
</Label>
|
|
{atmosToggleDisabled ? (
|
|
<p className="text-sm text-muted-foreground">
|
|
Your browser does not support Dolby Atmos playback.
|
|
</p>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function CreateUserDialog({
|
|
open,
|
|
onOpenChange,
|
|
}: {
|
|
open: boolean
|
|
onOpenChange: (open: boolean) => void
|
|
}) {
|
|
const createUser = useAction(api.users.adminCreateUser)
|
|
const [name, setName] = useState("")
|
|
const [email, setEmail] = useState("")
|
|
const [password, setPassword] = useState("")
|
|
const [isAdmin, setIsAdmin] = useState(false)
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
function reset() {
|
|
setName("")
|
|
setEmail("")
|
|
setPassword("")
|
|
setIsAdmin(false)
|
|
}
|
|
|
|
async function handleSubmit(event: React.FormEvent) {
|
|
event.preventDefault()
|
|
setSubmitting(true)
|
|
try {
|
|
await createUser({ name, email, password, isAdmin })
|
|
toast.success("User created")
|
|
reset()
|
|
onOpenChange(false)
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to create user"
|
|
)
|
|
} finally {
|
|
setSubmitting(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Create user</DialogTitle>
|
|
<DialogDescription>
|
|
Add a new account with email and password.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<form onSubmit={(event) => void handleSubmit(event)}>
|
|
<FieldGroup>
|
|
<Field>
|
|
<FieldLabel htmlFor="create-name">Name</FieldLabel>
|
|
<Input
|
|
id="create-name"
|
|
value={name}
|
|
onChange={(event) => setName(event.target.value)}
|
|
required
|
|
/>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor="create-email">Email</FieldLabel>
|
|
<Input
|
|
id="create-email"
|
|
type="email"
|
|
value={email}
|
|
onChange={(event) => setEmail(event.target.value)}
|
|
required
|
|
/>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor="create-password">Password</FieldLabel>
|
|
<Input
|
|
id="create-password"
|
|
type="password"
|
|
value={password}
|
|
onChange={(event) => setPassword(event.target.value)}
|
|
required
|
|
minLength={8}
|
|
/>
|
|
</Field>
|
|
<div className="flex items-center gap-2">
|
|
<Checkbox
|
|
id="create-admin"
|
|
checked={isAdmin}
|
|
onCheckedChange={(checked) => setIsAdmin(checked === true)}
|
|
/>
|
|
<Label htmlFor="create-admin" className="font-normal">
|
|
Grant admin access
|
|
</Label>
|
|
</div>
|
|
</FieldGroup>
|
|
<DialogFooter className="mt-4">
|
|
<Button type="submit" disabled={submitting}>
|
|
{submitting && <Spinner />}
|
|
Create user
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
function ChangePasswordDialog({
|
|
user,
|
|
open,
|
|
onOpenChange,
|
|
}: {
|
|
user: Doc<"users">
|
|
open: boolean
|
|
onOpenChange: (open: boolean) => void
|
|
}) {
|
|
const changePassword = useAction(api.users.adminChangeUserPassword)
|
|
const [password, setPassword] = useState("")
|
|
const [confirm, setConfirm] = useState("")
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
async function handleSubmit(event: React.FormEvent) {
|
|
event.preventDefault()
|
|
if (password !== confirm) {
|
|
toast.error("Passwords do not match")
|
|
return
|
|
}
|
|
setSubmitting(true)
|
|
try {
|
|
await changePassword({ userId: user._id, newPassword: password })
|
|
toast.success(`Password updated for ${user.name}`)
|
|
setPassword("")
|
|
setConfirm("")
|
|
onOpenChange(false)
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update password"
|
|
)
|
|
} finally {
|
|
setSubmitting(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Change password</DialogTitle>
|
|
<DialogDescription>
|
|
Set a new password for {user.name} ({user.email}).
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<form onSubmit={(event) => void handleSubmit(event)}>
|
|
<FieldGroup>
|
|
<Field>
|
|
<FieldLabel htmlFor="admin-new-password">New password</FieldLabel>
|
|
<Input
|
|
id="admin-new-password"
|
|
type="password"
|
|
value={password}
|
|
onChange={(event) => setPassword(event.target.value)}
|
|
required
|
|
minLength={8}
|
|
/>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor="admin-confirm-password">
|
|
Confirm password
|
|
</FieldLabel>
|
|
<Input
|
|
id="admin-confirm-password"
|
|
type="password"
|
|
value={confirm}
|
|
onChange={(event) => setConfirm(event.target.value)}
|
|
required
|
|
minLength={8}
|
|
/>
|
|
</Field>
|
|
</FieldGroup>
|
|
<DialogFooter className="mt-4">
|
|
<Button
|
|
type="submit"
|
|
disabled={
|
|
submitting || !password || !confirm || password !== confirm
|
|
}
|
|
>
|
|
{submitting && <Spinner />}
|
|
Update password
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
function UserRowActions({
|
|
user,
|
|
currentUserId,
|
|
}: {
|
|
user: Doc<"users">
|
|
currentUserId: Id<"users">
|
|
}) {
|
|
const setAdmin = useMutation(api.users.setUserAdmin)
|
|
const deleteUserMutation = useMutation(api.users.deleteUser)
|
|
const [passwordDialogOpen, setPasswordDialogOpen] = useState(false)
|
|
const [deleting, setDeleting] = useState(false)
|
|
|
|
const isSelf = user._id === currentUserId
|
|
|
|
return (
|
|
<>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="ghost" size="icon-sm">
|
|
<MoreHorizontal />
|
|
<span className="sr-only">Actions</span>
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
{!isSelf && (
|
|
<DropdownMenuItem
|
|
onClick={() => {
|
|
setAdmin({ userId: user._id, isAdmin: !user.isAdmin })
|
|
.then(() =>
|
|
toast.success(
|
|
user.isAdmin ? "Admin access removed" : "User is now admin"
|
|
)
|
|
)
|
|
.catch((error: Error) => toast.error(error.message))
|
|
}}
|
|
>
|
|
{user.isAdmin ? "Remove admin" : "Make admin"}
|
|
</DropdownMenuItem>
|
|
)}
|
|
<DropdownMenuItem onClick={() => setPasswordDialogOpen(true)}>
|
|
Change password
|
|
</DropdownMenuItem>
|
|
{!isSelf && (
|
|
<>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem
|
|
variant="destructive"
|
|
disabled={deleting}
|
|
onClick={() => {
|
|
setDeleting(true)
|
|
deleteUserMutation({ userId: user._id })
|
|
.then(() => toast.success("User deleted"))
|
|
.catch((error: Error) => toast.error(error.message))
|
|
.finally(() => setDeleting(false))
|
|
}}
|
|
>
|
|
<Trash2 />
|
|
Delete account
|
|
</DropdownMenuItem>
|
|
</>
|
|
)}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
<ChangePasswordDialog
|
|
user={user}
|
|
open={passwordDialogOpen}
|
|
onOpenChange={setPasswordDialogOpen}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
function LibrarySection() {
|
|
const updateLibrary = useAction(api.actions.updateLibrary)
|
|
const cleanupHlsStreams = useAction(api.actions.adminCleanupHlsStreams)
|
|
const [updating, setUpdating] = useState(false)
|
|
const [cleaningHls, setCleaningHls] = useState(false)
|
|
|
|
async function handleUpdateLibrary() {
|
|
setUpdating(true)
|
|
try {
|
|
await updateLibrary({})
|
|
toast.success("Library updated")
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update library"
|
|
)
|
|
} finally {
|
|
setUpdating(false)
|
|
}
|
|
}
|
|
|
|
async function handleCleanupHlsStreams() {
|
|
setCleaningHls(true)
|
|
try {
|
|
const result = await cleanupHlsStreams({})
|
|
toast.success(
|
|
result.removed === 1
|
|
? "Removed 1 idle HLS stream"
|
|
: `Removed ${result.removed} idle HLS streams`
|
|
)
|
|
if (result.failed > 0) {
|
|
toast.warning(
|
|
result.failed === 1
|
|
? "1 HLS stream could not be removed"
|
|
: `${result.failed} HLS streams could not be removed`
|
|
)
|
|
}
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to clean HLS streams"
|
|
)
|
|
} finally {
|
|
setCleaningHls(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div>
|
|
<h3 className="text-base font-medium">Library</h3>
|
|
<p className="text-sm text-muted-foreground">
|
|
Sync the music library with files on disk.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
<Button
|
|
type="button"
|
|
disabled={updating}
|
|
onClick={() => void handleUpdateLibrary()}
|
|
>
|
|
{updating && <Spinner />}
|
|
Update library
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={cleaningHls}
|
|
onClick={() => void handleCleanupHlsStreams()}
|
|
>
|
|
{cleaningHls && <Spinner />}
|
|
Clean HLS streams
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function UsersSection({
|
|
users,
|
|
currentUserId,
|
|
}: {
|
|
users: Doc<"users">[]
|
|
currentUserId: Id<"users">
|
|
}) {
|
|
const appSettings = useQuery(api.users.getAppSettings)
|
|
const setDisableCreation = useMutation(api.users.setDisableAccountCreation)
|
|
const [createOpen, setCreateOpen] = useState(false)
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div>
|
|
<h3 className="text-base font-medium">Users</h3>
|
|
<p className="text-sm text-muted-foreground">
|
|
Manage accounts, permissions, and sign-up settings.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex flex-col gap-4 rounded-lg border p-4 sm:flex-row sm:items-center sm:justify-between">
|
|
<div className="flex items-center gap-3">
|
|
<Switch
|
|
id="disable-signups"
|
|
checked={appSettings?.disableAccountCreation ?? false}
|
|
onCheckedChange={(checked) => {
|
|
setDisableCreation({ disabled: checked })
|
|
.then(() =>
|
|
toast.success(
|
|
checked
|
|
? "Account creation disabled"
|
|
: "Account creation enabled"
|
|
)
|
|
)
|
|
.catch((error: Error) => toast.error(error.message))
|
|
}}
|
|
/>
|
|
<Label htmlFor="disable-signups" className="font-normal">
|
|
Disable account creation
|
|
</Label>
|
|
</div>
|
|
<Button type="button" size="sm" onClick={() => setCreateOpen(true)}>
|
|
<UserPlus />
|
|
Create user
|
|
</Button>
|
|
</div>
|
|
|
|
<Table>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<TableHead>User</TableHead>
|
|
<TableHead>Name</TableHead>
|
|
<TableHead>Email</TableHead>
|
|
<TableHead className="w-10" />
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
{users.map((user) => (
|
|
<TableRow key={user._id}>
|
|
<TableCell>
|
|
<div className="flex items-center gap-2">
|
|
<UserAvatar user={user} size="sm" />
|
|
{user.isAdmin ? (
|
|
<span className="text-xs text-muted-foreground">Admin</span>
|
|
) : null}
|
|
</div>
|
|
</TableCell>
|
|
<TableCell>{user.name}</TableCell>
|
|
<TableCell>{user.email}</TableCell>
|
|
<TableCell>
|
|
<UserRowActions user={user} currentUserId={currentUserId} />
|
|
</TableCell>
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
|
|
<CreateUserDialog open={createOpen} onOpenChange={setCreateOpen} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export function OptionsModal({
|
|
open,
|
|
onOpenChange,
|
|
}: {
|
|
open: boolean
|
|
onOpenChange: (open: boolean) => void
|
|
}) {
|
|
const user = useQuery(api.auth.currentUser)
|
|
const allUsers = useQuery(api.users.listUsers)
|
|
const [section, setSection] = useState<Section>("profile")
|
|
|
|
const isAdmin = user?.isAdmin === true
|
|
const activeSection: Section =
|
|
!isAdmin && (section === "users" || section === "library")
|
|
? "profile"
|
|
: section
|
|
|
|
const handleOpenChange = (nextOpen: boolean) => {
|
|
if (!nextOpen) {
|
|
setSection("profile")
|
|
}
|
|
onOpenChange(nextOpen)
|
|
}
|
|
|
|
if (!user) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={handleOpenChange}>
|
|
<DialogContent
|
|
showCloseButton
|
|
className="flex h-[min(32rem,85vh)] max-h-[85vh] flex-col gap-0 overflow-hidden p-0 sm:max-w-4xl"
|
|
>
|
|
<DialogTitle className="sr-only">Settings</DialogTitle>
|
|
<div className="flex min-h-0 flex-1">
|
|
<aside className="flex w-52 shrink-0 flex-col border-r bg-muted/20 p-4">
|
|
<p className="mb-2 px-2.5 text-xs font-medium tracking-wide text-muted-foreground uppercase">
|
|
User Settings
|
|
</p>
|
|
<nav className="flex flex-col gap-0.5">
|
|
<NavButton
|
|
active={activeSection === "profile"}
|
|
onClick={() => setSection("profile")}
|
|
>
|
|
Profile
|
|
</NavButton>
|
|
<NavButton
|
|
active={activeSection === "quality"}
|
|
onClick={() => setSection("quality")}
|
|
>
|
|
Quality
|
|
</NavButton>
|
|
</nav>
|
|
|
|
{isAdmin ? (
|
|
<>
|
|
<Separator className="my-4" />
|
|
<p className="mb-2 px-2.5 text-xs font-medium tracking-wide text-muted-foreground uppercase">
|
|
Admin Settings
|
|
</p>
|
|
<nav className="flex flex-col gap-0.5">
|
|
<NavButton
|
|
active={activeSection === "library"}
|
|
onClick={() => setSection("library")}
|
|
>
|
|
Library
|
|
</NavButton>
|
|
<NavButton
|
|
active={activeSection === "users"}
|
|
onClick={() => setSection("users")}
|
|
>
|
|
Users
|
|
</NavButton>
|
|
</nav>
|
|
</>
|
|
) : null}
|
|
</aside>
|
|
|
|
<ScrollArea className="min-h-0 flex-1">
|
|
<div className="p-6">
|
|
{activeSection === "profile" && <ProfileSection user={user} />}
|
|
{activeSection === "quality" && <QualitySection user={user} />}
|
|
{activeSection === "library" && isAdmin ? (
|
|
<LibrarySection />
|
|
) : null}
|
|
{activeSection === "users" && isAdmin && allUsers ? (
|
|
<UsersSection users={allUsers} currentUserId={user._id} />
|
|
) : null}
|
|
</div>
|
|
</ScrollArea>
|
|
</div>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|