|
| 1 | +import { cn } from '@/lib/utils'; |
| 2 | +import { useMutation } from '@tanstack/react-query'; |
| 3 | +import { LoaderCircle } from 'lucide-react'; |
| 4 | +import { Button } from '@/components/ui/button'; |
| 5 | +import { getIExec } from '@/externals/iexecSdkClient'; |
| 6 | + |
| 7 | +export function DownloadLogs({ |
| 8 | + taskid, |
| 9 | + className, |
| 10 | +}: { |
| 11 | + taskid: string; |
| 12 | + className?: string; |
| 13 | +}) { |
| 14 | + const { |
| 15 | + mutate: downloadLogs, |
| 16 | + isPending, |
| 17 | + isError, |
| 18 | + } = useMutation({ |
| 19 | + mutationKey: ['downloadLogs', taskid], |
| 20 | + mutationFn: async () => { |
| 21 | + if (!taskid) { |
| 22 | + throw new Error('Task ID is required'); |
| 23 | + } |
| 24 | + |
| 25 | + const iexec = await getIExec(); |
| 26 | + const logs = await iexec.task.fetchLogs(taskid); |
| 27 | + |
| 28 | + const logsString = |
| 29 | + typeof logs === 'string' ? logs : JSON.stringify(logs, null, 2); |
| 30 | + |
| 31 | + const blob = new Blob([logsString], { type: 'text/plain' }); |
| 32 | + const url = URL.createObjectURL(blob); |
| 33 | + |
| 34 | + const link = document.createElement('a'); |
| 35 | + link.href = url; |
| 36 | + link.download = `task-${taskid}-logs.txt`; |
| 37 | + document.body.appendChild(link); |
| 38 | + link.click(); |
| 39 | + document.body.removeChild(link); |
| 40 | + |
| 41 | + URL.revokeObjectURL(url); |
| 42 | + |
| 43 | + return logs; |
| 44 | + }, |
| 45 | + onError: (error) => { |
| 46 | + console.error('Failed to download logs:', error); |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + if (!taskid) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + return ( |
| 55 | + <div className="flex items-center gap-2"> |
| 56 | + <Button |
| 57 | + onClick={() => downloadLogs()} |
| 58 | + variant="outline" |
| 59 | + size="sm" |
| 60 | + className={cn(className)} |
| 61 | + disabled={isPending} |
| 62 | + > |
| 63 | + {isPending && <LoaderCircle className="mr-2 h-4 w-4 animate-spin" />} |
| 64 | + {isPending ? 'Downloading...' : 'Download logs'} |
| 65 | + </Button> |
| 66 | + {isError && ( |
| 67 | + <p className="text-sm text-red-500">Failed to download logs</p> |
| 68 | + )} |
| 69 | + </div> |
| 70 | + ); |
| 71 | +} |
0 commit comments