feat: refactor fetchSounds to use useCallback and remove mock data for improved API integration
This commit is contained in:
@@ -19,7 +19,7 @@ import {
|
|||||||
Volume2,
|
Volume2,
|
||||||
X
|
X
|
||||||
} from 'lucide-react'
|
} from 'lucide-react'
|
||||||
import { useEffect, useState } from 'react'
|
import { useCallback, useEffect, useState } from 'react'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
interface DraggableSoundProps {
|
interface DraggableSoundProps {
|
||||||
@@ -114,70 +114,26 @@ export function SoundLibrary() {
|
|||||||
return () => clearTimeout(handler)
|
return () => clearTimeout(handler)
|
||||||
}, [searchQuery])
|
}, [searchQuery])
|
||||||
|
|
||||||
const fetchSounds = async () => {
|
const fetchSounds = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
setError(null)
|
setError(null)
|
||||||
|
|
||||||
// Mock sounds for testing drag functionality
|
// Build API params
|
||||||
const mockSounds: Sound[] = [
|
const params: { types?: string[]; search?: string } = {}
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
filename: 'kick.mp3',
|
|
||||||
name: 'Kick Drum',
|
|
||||||
duration: 2000,
|
|
||||||
size: 48000,
|
|
||||||
type: 'SDB',
|
|
||||||
play_count: 5,
|
|
||||||
hash: 'mock-hash-1',
|
|
||||||
is_normalized: false,
|
|
||||||
created_at: new Date().toISOString(),
|
|
||||||
updated_at: new Date().toISOString()
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 2,
|
|
||||||
filename: 'snare.wav',
|
|
||||||
name: 'Snare Hit',
|
|
||||||
duration: 1500,
|
|
||||||
size: 36000,
|
|
||||||
type: 'SDB',
|
|
||||||
play_count: 3,
|
|
||||||
hash: 'mock-hash-2',
|
|
||||||
is_normalized: false,
|
|
||||||
created_at: new Date().toISOString(),
|
|
||||||
updated_at: new Date().toISOString()
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 3,
|
|
||||||
filename: 'hello.mp3',
|
|
||||||
name: 'Hello World',
|
|
||||||
duration: 3000,
|
|
||||||
size: 72000,
|
|
||||||
type: 'TTS',
|
|
||||||
play_count: 1,
|
|
||||||
hash: 'mock-hash-3',
|
|
||||||
is_normalized: false,
|
|
||||||
created_at: new Date().toISOString(),
|
|
||||||
updated_at: new Date().toISOString()
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
// Filter by search query
|
|
||||||
let filteredSounds = mockSounds
|
|
||||||
if (debouncedSearchQuery.trim()) {
|
|
||||||
const query = debouncedSearchQuery.toLowerCase()
|
|
||||||
filteredSounds = mockSounds.filter(sound =>
|
|
||||||
sound.name.toLowerCase().includes(query) ||
|
|
||||||
sound.filename.toLowerCase().includes(query)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter by type
|
// Filter by type
|
||||||
if (soundType !== 'all') {
|
if (soundType !== 'all') {
|
||||||
filteredSounds = filteredSounds.filter(sound => sound.type === soundType)
|
params.types = [soundType]
|
||||||
}
|
}
|
||||||
|
|
||||||
setSounds(filteredSounds)
|
// Filter by search query
|
||||||
|
if (debouncedSearchQuery.trim()) {
|
||||||
|
params.search = debouncedSearchQuery.trim()
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchedSounds = await soundsService.getSounds(params)
|
||||||
|
setSounds(fetchedSounds)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const errorMessage = err instanceof Error ? err.message : 'Failed to fetch sounds'
|
const errorMessage = err instanceof Error ? err.message : 'Failed to fetch sounds'
|
||||||
setError(errorMessage)
|
setError(errorMessage)
|
||||||
@@ -185,11 +141,11 @@ export function SoundLibrary() {
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}
|
}, [debouncedSearchQuery, soundType])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchSounds()
|
fetchSounds()
|
||||||
}, [debouncedSearchQuery, soundType])
|
}, [fetchSounds])
|
||||||
|
|
||||||
const renderContent = () => {
|
const renderContent = () => {
|
||||||
if (loading) {
|
if (loading) {
|
||||||
|
|||||||
Reference in New Issue
Block a user