'use client'; import { exportParticipantDataAsJson } from '@/app/actions/participants'; import { useState } from 'react'; interface ExportParticipantDataButtonProps { experimentId: number; userId: string; } export default function ExportParticipantDataButton({ experimentId, userId, }: ExportParticipantDataButtonProps) { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const handleExport = async () => { setIsLoading(true); setError(null); try { const { jsonData, filename } = await exportParticipantDataAsJson(experimentId, userId); // Create blob and trigger download const blob = new Blob([jsonData], { type: 'application/json' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to export data'); console.error('Export error:', err); } finally { setIsLoading(false); } }; return (
{error &&
{error}
}
); }