"use client" import { useEffect, useState, useCallback } from "react" import { useAuth } from "@/contexts/auth-context" import { useRouter } from "next/navigation" import { Card, CardContent } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Badge } from "@/components/ui/badge" import { Search, Edit, Save, X, Layers, Plus, Trash2, GripVertical } from "lucide-react" import { getApiUrl } from "@/lib/api-config" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog" import { Label } from "@/components/ui/label" interface SequenceSong { position: number song_id: number song_title: string } interface Sequence { id: number name: string slug: string description: string | null notes: string | null songs: SequenceSong[] } interface Song { id: number title: string slug: string } // Edit Sequence Form Component function EditSequenceForm({ sequence, allSongs, token, onSave, onCancel }: { sequence: Sequence allSongs: Song[] token: string onSave: () => void onCancel: () => void }) { const [name, setName] = useState(sequence.name) const [description, setDescription] = useState(sequence.description || "") const [notes, setNotes] = useState(sequence.notes || "") const [songIds, setSongIds] = useState(sequence.songs.map(s => s.song_id)) const [songSearch, setSongSearch] = useState("") const [saving, setSaving] = useState(false) const filteredSongs = allSongs.filter(s => s.title.toLowerCase().includes(songSearch.toLowerCase()) && !songIds.includes(s.id) ).slice(0, 15) const addSong = (id: number) => setSongIds([...songIds, id]) const removeSong = (id: number) => setSongIds(songIds.filter(sid => sid !== id)) const moveSong = (index: number, direction: -1 | 1) => { const newIds = [...songIds] const newIndex = index + direction if (newIndex < 0 || newIndex >= newIds.length) return [newIds[index], newIds[newIndex]] = [newIds[newIndex], newIds[index]] setSongIds(newIds) } const handleSave = async () => { setSaving(true) try { const res = await fetch(`${getApiUrl()}/sequences/${sequence.id}`, { method: "PATCH", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` }, body: JSON.stringify({ name, description, notes, song_ids: songIds }) }) if (res.ok) onSave() } catch (e) { console.error("Failed to save", e) } finally { setSaving(false) } } return (
setName(e.target.value)} />