import React from 'react'; import { DndContext, closestCenter, PointerSensor, useSensor, useSensors, } from '@dnd-kit/core'; import { arrayMove, SortableContext, verticalListSortingStrategy, } from '@dnd-kit/sortable'; import { restrictToVerticalAxis } from '@dnd-kit/modifiers'; import AudioTrimmer from './AudioTrimer'; import { ClipMetadata } from '../../redux/types'; import { useAppDispatch, useAppSelector } from '../hooks'; import apiFetch from '../api'; export interface ClipListProps { collection: string; } export default function ClipList({ collection }: ClipListProps) { const metadata = useAppSelector( (state) => state.collections.find((col) => col.name === collection) || { clips: [] }, ); const dispatch = useAppDispatch(); const sensors = useSensors( useSensor(PointerSensor, { activationConstraint: { distance: 5 } }), ); const handleDrop = (event: React.DragEvent) => { event.preventDefault(); console.log('Files dropped:', event.dataTransfer.files); const files = Array.from(event.dataTransfer.files).filter((file) => file.type.startsWith('audio/'), ); if (files.length > 0) { const formData = new FormData(); files.forEach((file) => formData.append('files', file)); // todo send the file to the backend and add to the collection // fetch('http://localhost:5010/file/upload', { // method: 'POST', // body: formData, // }) // .then((res) => res.json()) // .catch((err) => console.error('Error uploading files:', err)); // Implement your onDrop logic here // onDrop(files, selectedCollection); } }; const handleDragOver = (event: React.DragEvent) => { event.preventDefault(); }; async function handleDragEnd(event: any) { const { active, over } = event; if (active.id !== over?.id) { const oldIndex = metadata.clips.findIndex( (item) => item.filename === active.id, ); const newIndex = metadata.clips.findIndex( (item) => item.filename === over.id, ); const newMetadata = { ...metadata, clips: arrayMove(metadata.clips, oldIndex, newIndex), }; console.log('New order:', newMetadata); dispatch({ type: 'metadata/setCollections', payload: { collection, newMetadata }, }); try { const response = await apiFetch('meta/collection/clips/reorder', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name: collection, clips: newMetadata.clips, }), }); const data = await response.json(); console.log('handle reorder return:', data.collections); dispatch({ type: 'metadata/setAllData', payload: data }); } catch (error) { console.error('Error saving new clip order:', error); } // setMetadata(newMetadata); } } async function handleDelete(meta: ClipMetadata) { dispatch({ type: 'metadata/deleteClip', payload: { collection, clip: meta }, }); apiFetch('meta/collection/clips/remove', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name: collection, clip: meta, }), }) .then((res) => res.json()) .catch((err) => console.error('Error deleting clip:', err)); console.log('Deleting clip:', meta); } async function handleClipMove(targetCollection: string, meta: ClipMetadata) { console.log('Moving clip:', meta, 'to collection:', targetCollection); dispatch({ type: 'metadata/moveClip', payload: { sourceCollection: collection, targetCollection, clip: meta }, }); apiFetch('meta/collection/clips/move', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ sourceCollection: collection, targetCollection, clip: meta, }), }) .then((res) => res.json()) .catch((err) => console.error('Error moving clip:', err)); } async function handleClipSave(meta: ClipMetadata) { try { dispatch({ type: 'metadata/editClip', payload: { collection, clip: meta }, }); const response = await apiFetch('meta/collection/clips/edit', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name: collection, clip: meta, }), }); await response.json(); // console.log('handle clip save return:', data.collections); dispatch({ type: 'metadata/editClip', payload: { collection, clip: meta }, }); } catch (error) { console.error('Error saving clip metadata:', error); } } return (
item.filename)} strategy={verticalListSortingStrategy} > {metadata.clips.map((trimmer, idx) => ( {(idx + 1) % 10 === 0 && idx !== metadata.clips.length - 1 && (

-- Page {Math.ceil((idx + 1) / 10) + 1} --

)}
))} {/* {metadata.map((trimmer) => ( ))} */}
//
// // // //
); }