import React, { useState, useEffect } from 'react' import API from '../API' import AudioPlayer from './AudioPlayer' import LoadingSpinner from './LoadingSpinner' interface ExamplesProps { fileType: 'image' | 'audio' | 'video' } type ExamplesData = { image_url: string audio_url?: string video_url?: string description: string } const Examples = ({ fileType }: ExamplesProps) => { const [examples, setExamples] = useState<{ [model: string]: { [attack: string]: ExamplesData[] } }>({}) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [selectedModel, setSelectedModel] = useState(null) const [selectedAttack, setSelectedAttack] = useState(null) useEffect(() => { setLoading(true) setError(null) API.fetchExamplesByType(fileType) .then((data) => { // data is a dictionary from {[model]: {[attack]: {image_url, audio_url, video_url, description}}} setExamples(data) // get the first model and attack if available const models = Object.keys(data) if (models.length > 0) { setSelectedModel(models[0]) const attacks = Object.keys(data[models[0]]) if (attacks.length > 0) { setSelectedAttack(attacks[0]) // Set the first attack of the first model } else { setSelectedAttack(null) // No attacks available } } else { setSelectedModel(null) setSelectedAttack(null) // No models available } // Reset loading state setLoading(false) }) .catch((err) => { setError(err.message) setLoading(false) }) }, [fileType]) // Define the Gallery component within this file const Gallery = ({ selectedModel, selectedAttack, examples, fileType, }: { selectedModel: string selectedAttack: string examples: { [model: string]: { [attack: string]: ExamplesData[] } } fileType: 'image' | 'audio' | 'video' }) => { const exampleItems = examples[selectedModel][selectedAttack] return (
{exampleItems.map((item, index) => (

{item.description}

{fileType === 'image' && ( {item.description} )} {fileType === 'audio' && item.audio_url && ( <> {item.description} )} {fileType === 'video' && (
))}
) } return (
Model
{selectedModel && (
Attack
)}
{loading && } {error &&

Error: {error}

} {selectedModel && selectedAttack && ( )}
) } export default Examples