Skip to content

Commit 6d02b7b

Browse files
committed
feat: add DownloadResult component for downloading task results from IPFS
1 parent b73ca79 commit 6d02b7b

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export const DETAIL_TABLE_LENGTH = 8;
77
export const TABLE_LENGTH = 16;
88
export const PREVIEW_TABLE_REFETCH_INTERVAL = 10_000;
99
export const TABLE_REFETCH_INTERVAL = 10_000;
10+
11+
export const IPFS_GATEWAY_URL = 'https://ipfs.iex.ec';
12+
1013
export const SUPPORTED_CHAINS = [
1114
{
1215
id: 134,

src/modules/deals/deal/buildDealDetails.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ export function buildDealDetails({
172172
</span>
173173
)}
174174
</span>
175-
176-
{/* {isClaimable && <ClaimFailedDeal taskId="" />} // TODO */}
177175
</span>
178176
),
179177
}),
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { IPFS_GATEWAY_URL } from '@/config';
2+
import { cn } from '@/lib/utils';
3+
import { useMutation, useQueryClient } from '@tanstack/react-query';
4+
import { LoaderCircle } from 'lucide-react';
5+
import { Button } from '@/components/ui/button';
6+
import { taskResultToObject } from '@/utils/format';
7+
8+
async function fetchZipFromIpfs(taskid: string, location: string) {
9+
const url = `${IPFS_GATEWAY_URL}${location}`;
10+
11+
const res = await fetch(url, {
12+
headers: {
13+
Accept: 'application/zip',
14+
},
15+
});
16+
17+
if (!res.ok) {
18+
throw new Error('Download error');
19+
}
20+
21+
const blob = await res.blob();
22+
const downloadUrl = URL.createObjectURL(blob);
23+
24+
const link = document.createElement('a');
25+
link.href = downloadUrl;
26+
link.download = `${taskid}.zip`;
27+
link.click();
28+
29+
URL.revokeObjectURL(downloadUrl);
30+
}
31+
32+
export function DownloadResult({
33+
taskid,
34+
taskResults,
35+
className,
36+
}: {
37+
taskid: string;
38+
taskResults?: string | null;
39+
className?: string;
40+
}) {
41+
const queryClient = useQueryClient();
42+
43+
const {
44+
mutate: downloadTaskResult,
45+
isPending,
46+
isError,
47+
} = useMutation({
48+
mutationFn: async () => {
49+
const { location } = taskResultToObject(taskResults);
50+
51+
if (!location) throw new Error('Invalid location');
52+
await fetchZipFromIpfs(taskid, location);
53+
},
54+
onSuccess: () => {
55+
queryClient.invalidateQueries({ queryKey: ['task', taskid] });
56+
},
57+
});
58+
59+
if (!taskid || !taskResults) return null;
60+
61+
return (
62+
<>
63+
<Button
64+
variant="link"
65+
className={cn('text-white underline', className)}
66+
onClick={() => downloadTaskResult()}
67+
>
68+
Download task result
69+
{isPending && <LoaderCircle className="ml-2 animate-spin" />}
70+
</Button>
71+
{isError && (
72+
<p className="text-danger-foreground content-center">
73+
An error occurred please try again
74+
</p>
75+
)}
76+
</>
77+
);
78+
}

src/modules/tasks/task/buildTaskDetails.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '@/utils/formatElapsedTime';
1212
import { truncateAddress } from '@/utils/truncateAddress';
1313
import { ClaimButton } from '../ClaimButton';
14+
import { DownloadResult } from '../DownloadResult';
1415
import StatusCell from '../StatusCell';
1516

1617
export function buildTaskDetails({ task }: { task: TaskQuery['task'] }) {
@@ -119,10 +120,7 @@ export function buildTaskDetails({ task }: { task: TaskQuery['task'] }) {
119120
<div>
120121
<StatusCell statusEnum={task.status} />
121122
<ClaimButton tasks={[task]} className="text-white underline" />
122-
{/* {task.results &&
123-
taskResultToObject(task.results)?.storage === 'ipfs' && (
124-
TODO add Download result archive button
125-
)} */}
123+
<DownloadResult taskid={task.taskid} taskResults={task.results} />
126124
</div>
127125
),
128126
}),

0 commit comments

Comments
 (0)