-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathuse-dataset-pieces.ts
More file actions
188 lines (167 loc) · 6.04 KB
/
use-dataset-pieces.ts
File metadata and controls
188 lines (167 loc) · 6.04 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
import { METADATA_KEYS } from '@filoz/synapse-sdk'
import { getDetailedDataSet } from 'filecoin-pin/core/data-set'
import { useCallback, useEffect, useState } from 'react'
import { useFilecoinPinContext } from './use-filecoin-pin-context.ts'
export interface DatasetPiece {
id: string
fileName: string
fileSize: string
cid: string
pieceCid: string
providerName: string
datasetId: string
providerId: string
serviceURL: string
transactionHash: string
network: string
uploadedAt: number // timestamp
pieceId: number
}
/**
* Fetches and normalizes dataset pieces for the active wallet/provider combo.
*
* Abstracts away Synapse warm storage + PDP interactions so UI components
* simply call this hook and render the returned list.
*/
export const useDatasetPieces = () => {
const [pieces, setPieces] = useState<DatasetPiece[]>([])
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const [hasLoaded, setHasLoaded] = useState(false)
const { storageContext, providerInfo, wallet, synapse } = useFilecoinPinContext()
const dataSetId = storageContext?.dataSetId
const loadPieces = useCallback(async () => {
if (!storageContext || !providerInfo || !synapse) {
console.debug(
'[DatasetPieces] Missing dependencies - storageContext:',
!!storageContext,
'providerInfo:',
!!providerInfo,
'synapse:',
!!synapse
)
setPieces([])
setHasLoaded(false)
return
}
setHasLoaded(false)
setIsLoading(true)
setError(null)
try {
console.debug('[DatasetPieces] Loading pieces from dataset:', storageContext.dataSetId)
console.debug('[DatasetPieces] Wallet address:', wallet?.status === 'ready' ? wallet.data.address : 'not ready')
// Get the PDP service URL from the provider
const serviceURL = providerInfo.products?.PDP?.data?.serviceURL
if (!serviceURL) {
console.warn('[DatasetPieces] Provider does not expose a PDP service URL')
setPieces([])
return
}
if (!dataSetId) {
console.warn('[DatasetPieces] Storage context does not have a data set ID')
setPieces([])
return
}
console.debug('[DatasetPieces] Fetching pieces for dataSetId:', dataSetId)
const dataSetData = await getDetailedDataSet(synapse, dataSetId)
if (typeof dataSetData.pieces === 'undefined') {
throw new Error('[DatasetPieces] Pieces data unavailable')
}
console.debug('[DatasetPieces] Found', dataSetData.pieces.length, 'pieces in dataset id: ', dataSetId)
if (dataSetData.pieces.length === 0) {
setPieces([])
return
}
// For each piece, fetch its metadata
const piecesWithMetadata: DatasetPiece[] = await Promise.all(
dataSetData.pieces.map(async (piece) => {
try {
const pieceId = piece.pieceId
const pieceCid = piece.pieceCid.toString()
if (typeof piece.metadata === 'undefined') {
throw new Error('[DatasetPiece] Piece metadata unavailable')
}
// Extract relevant metadata
const ipfsRootCid = piece.metadata[METADATA_KEYS.IPFS_ROOT_CID] || ''
const fileName = piece.metadata.label || ipfsRootCid || `unknown`
const fileSize = piece.metadata.fileSize || 'Unknown'
return {
id: `piece-${pieceId}`,
fileName,
fileSize,
cid: ipfsRootCid,
pieceCid,
providerName: providerInfo.name || 'unknown',
datasetId: String(dataSetId),
providerId: providerInfo.id.toString(),
serviceURL: providerInfo.products?.PDP?.data?.serviceURL ?? '',
network: wallet?.status === 'ready' ? wallet.data.network : 'calibration',
uploadedAt: piece.metadata.uploadedAt ? Number(piece.metadata.uploadedAt) : Date.now(),
pieceId,
transactionHash: piece.metadata.transactionHash || '',
}
} catch (err) {
console.warn(err)
// Return minimal data
return {
id: `piece-${piece.pieceId}`,
fileName: `Piece ${piece.pieceId}`,
fileSize: 'Unknown',
cid: '',
pieceCid: piece.pieceCid.toString(),
providerName: providerInfo.name || 'unknown',
datasetId: String(dataSetId),
providerId: providerInfo.id.toString(),
serviceURL: providerInfo.products?.PDP?.data?.serviceURL ?? '',
network: wallet?.status === 'ready' ? wallet.data.network : 'calibration',
uploadedAt: Date.now(),
pieceId: piece.pieceId,
transactionHash: '',
}
}
})
)
// Sort by piece ID (newest first, assuming higher IDs are newer)
piecesWithMetadata.sort((a, b) => (b.pieceId || 0) - (a.pieceId || 0))
setPieces(piecesWithMetadata)
} catch (err) {
console.error('[DatasetPieces] Failed to load pieces:', err)
setError(err instanceof Error ? err.message : 'Failed to load pieces')
setPieces([])
} finally {
setIsLoading(false)
setHasLoaded(true)
}
}, [dataSetId, providerInfo, wallet, synapse])
// Load pieces when storage context is ready
useEffect(() => {
if (storageContext && providerInfo) {
loadPieces()
} else {
setPieces([])
setHasLoaded(false)
}
}, [loadPieces])
const refreshPieces = useCallback(() => {
loadPieces()
}, [loadPieces])
/**
* Add a new piece to the history without refetching from backend.
* This is used when an upload completes and we already have all the data.
*/
const addPiece = useCallback((piece: DatasetPiece) => {
setPieces((prev) => {
// Add new piece at the beginning (newest first)
const updated = [piece, ...prev]
return updated
})
}, [])
return {
pieces,
isLoading,
error,
refreshPieces,
addPiece,
hasLoaded,
}
}