-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdata-sets-section.tsx
More file actions
198 lines (192 loc) · 7.84 KB
/
data-sets-section.tsx
File metadata and controls
198 lines (192 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type { DataSetWithPieces, UseProvidersResult } from '@filoz/synapse-react'
import { useDeletePiece } from '@filoz/synapse-react'
import { CloudDownload, FileAudio, FileCode, FilePlay, FileText, Globe, Info, Trash } from 'lucide-react'
import { useState } from 'react'
import { toast } from 'sonner'
import { toastError } from '@/lib/utils.ts'
import { ButtonLoading } from '../custom-ui/button-loading.tsx'
import { ExplorerLink } from '../explorer-link.tsx'
import { PDPDatasetLink, PDPPieceLink, PDPProviderLink } from '../pdp-link.tsx'
import { Avatar, AvatarFallback, AvatarImage } from '../ui/avatar.tsx'
import { Button } from '../ui/button.tsx'
import { Item, ItemActions, ItemContent, ItemDescription, ItemMedia, ItemTitle } from '../ui/item.tsx'
import { Skeleton } from '../ui/skeleton.tsx'
import { Tooltip, TooltipContent, TooltipTrigger } from '../ui/tooltip.tsx'
import { CreateDataSetDialog } from './create-data-set.tsx'
export function DataSetsSection({
dataSets,
providers,
}: {
dataSets?: DataSetWithPieces[]
providers?: UseProvidersResult
}) {
const providerWithDataSets = providers?.filter((p) => dataSets?.some((d) => d.providerId === p.id))
const imagesMimeTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
const videosMimeTypes = ['video/mp4', 'video/quicktime', 'video/webm']
const audioMimeTypes = ['audio/mpeg', 'audio/ogg', 'audio/wav']
const documentsMimeTypes = [
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'image/svg+xml',
'text/markdown',
]
const codeMimeTypes = [
'text/plain',
'text/html',
'text/css',
'text/javascript',
'application/json',
'application/xml',
'application/x-www-form-urlencoded',
'application/x-yaml',
'application/x-toml',
'application/x-ini',
'application/x-toml',
]
const [deletingPiece, setDeletingPiece] = useState<bigint | null>(null)
const { mutate: deletePiece, isPending: isDeletingPiece } = useDeletePiece({
onHash: (hash) => {
toast.loading('Deleting piece...', {
description: <ExplorerLink hash={hash} />,
id: 'delete-piece',
})
},
mutation: {
onSuccess: () => {
toast.success('Piece deleted', {
id: 'delete-piece',
})
},
onError: (error) => {
toastError(error, 'delete-piece', 'Piece deletion failed')
},
onSettled: () => {
setDeletingPiece(null)
},
},
})
return providers ? (
<div>
<div className="flex flex-row gap-2 items-center justify-between">
<div className="flex flex-col gap-2">
<div className="leading-none font-semibold">Data Sets</div>
<div className="text-muted-foreground text-sm">Manage your data sets.</div>
</div>
<CreateDataSetDialog />
</div>
<div className="flex flex-col gap-2 mt-6">
{providerWithDataSets?.map((provider) => (
<div className="flex flex-col gap-4" key={provider.id}>
<h4 className="text-lg font-bold">
<PDPProviderLink address={provider.serviceProvider} name={provider.name} />
</h4>
{dataSets
?.filter((dataSet) => dataSet.providerId === provider.id)
.map((dataSet) => (
<div className="flex flex-col gap-2" key={dataSet.clientDataSetId}>
<p className="flex flex-row gap-2 items-center">
<PDPDatasetLink id={dataSet.dataSetId.toString()} />
<Tooltip>
<TooltipTrigger>
<Info className="w-4" />
</TooltipTrigger>
<TooltipContent>
<p>Files: {dataSet.pieces.length}</p>
{Object.keys(dataSet.metadata).map((key) => (
<p key={key}>
{key.charAt(0).toUpperCase() + key.slice(1)}: {dataSet.metadata[key]}
</p>
))}
</TooltipContent>
</Tooltip>
{dataSet.cdn && (
<Tooltip>
<TooltipTrigger>
<Globe className="w-4" />
</TooltipTrigger>
<TooltipContent>
<p>This data set is using CDN</p>
</TooltipContent>
</Tooltip>
)}
</p>
{dataSet.pieces.map((piece) => (
<Item key={`${piece.id}-${dataSet.dataSetId}`} size="default" variant="muted">
<ItemMedia
variant={
imagesMimeTypes.includes(piece.metadata.type)
? 'image'
: piece.metadata.type
? 'icon'
: 'default'
}
>
{imagesMimeTypes.includes(piece.metadata.type) ? (
<img
alt={piece.metadata.name || piece.cid.toString()}
className="object-cover"
height={48}
src={piece.url}
width={48}
/>
) : videosMimeTypes.includes(piece.metadata.type) ? (
<FilePlay className="w-10" />
) : audioMimeTypes.includes(piece.metadata.type) ? (
<FileAudio className="w-10" />
) : documentsMimeTypes.includes(piece.metadata.type) ? (
<FileText className="w-10" />
) : codeMimeTypes.includes(piece.metadata.type) ? (
<FileCode className="w-10" />
) : (
<Avatar className="size-10">
<AvatarImage src={piece.url} />
<AvatarFallback>NA</AvatarFallback>
</Avatar>
)}
</ItemMedia>
<ItemContent>
<ItemTitle className="break-all">
<PDPPieceLink cid={piece.cid.toString()} name={piece.metadata.name} />
</ItemTitle>
<ItemDescription>
{piece.metadata.type} {piece.id}
</ItemDescription>
</ItemContent>
<ItemActions>
<Button
onClick={() => {
window.open(piece.url, '_blank')
}}
>
<CloudDownload />
</Button>
<ButtonLoading
loading={isDeletingPiece && deletingPiece === piece.id}
onClick={async () => {
setDeletingPiece(piece.id)
deletePiece({
dataSet,
pieceId: piece.id,
})
}}
>
<Trash />
</ButtonLoading>
</ItemActions>
</Item>
))}
</div>
))}
</div>
))}
</div>
</div>
) : (
<Skeleton className="w-full h-20" />
)
}