forked from Cookie-Jar-DAO/cookie-jar-v3
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseJarMetadata.ts
More file actions
254 lines (230 loc) · 6.09 KB
/
useJarMetadata.ts
File metadata and controls
254 lines (230 loc) · 6.09 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"use client";
import { useCallback, useEffect, useState } from "react";
import {
useChainId,
useWaitForTransactionReceipt,
useWriteContract,
} from "wagmi";
import { contractAddresses } from "@/config/supported-networks";
import { cookieJarFactoryAbi } from "@/generated";
import {
createMetadataJson,
isValidUrl,
parseJarMetadata as parseMetadataUtil,
validateMetadataSize,
} from "@/lib/jar/metadata-utils";
import { useToast } from "../app/useToast";
/**
* Parsed jar metadata structure
*/
export interface JarMetadata {
/** Display name of the jar */
name: string;
/** Description of the jar's purpose */
description: string;
/** URL to jar image/logo */
image: string;
/** External link for more information */
link: string;
}
/**
* Configuration containing raw metadata
*/
export interface JarConfig {
/** Raw metadata string (JSON or legacy text) */
metadata?: string;
}
/**
* Custom hook to handle Cookie Jar metadata parsing, editing, and updates
*
* Provides comprehensive metadata management including parsing legacy and v2
* metadata formats, form state management for editing, validation, and
* on-chain updates through the factory contract.
*
* @param config - Jar configuration containing raw metadata string
* @returns Object with parsed metadata, editing state, and update functions
*
* @example
* ```tsx
* const {
* metadata,
* isEditingMetadata,
* startEditing,
* handleMetadataUpdate,
* editName,
* setEditName
* } = useJarMetadata(jarConfig);
*
* // Display current metadata
* console.log(metadata.name, metadata.description);
*
* // Start editing
* startEditing();
*
* // Update metadata on-chain
* handleMetadataUpdate(jarAddress, refetchJarData);
* ```
*/
export const useJarMetadata = (config: JarConfig | undefined) => {
const { toast } = useToast();
const chainId = useChainId();
// Metadata editing state
const [isEditingMetadata, setIsEditingMetadata] = useState(false);
const [editName, setEditName] = useState("");
const [editImage, setEditImage] = useState("");
const [editLink, setEditLink] = useState("");
const [editDescription, setEditDescription] = useState("");
// Parse metadata from config using consolidated utility
const parseMetadata = useCallback(
(metadataString: string | undefined): JarMetadata => {
const parsed = parseMetadataUtil(metadataString);
return {
name: parsed.name,
description: parsed.description,
image: parsed.image,
link: parsed.link,
};
},
[],
);
const metadata = parseMetadata(config?.metadata);
// Initialize edit fields when entering edit mode
const startEditing = useCallback(() => {
setEditName(metadata.name);
setEditImage(metadata.image);
setEditLink(metadata.link);
setEditDescription(metadata.description);
setIsEditingMetadata(true);
}, [metadata]);
// URL validation now imported from metadata-utils
// Validate metadata edit form
const validateMetadataEdit = useCallback(() => {
if (!editName || editName.length < 3) {
toast({
title: "Validation Error",
description: "Jar name must be at least 3 characters long.",
variant: "destructive",
});
return false;
}
if (editImage && !isValidUrl(editImage)) {
toast({
title: "Validation Error",
description: "Please enter a valid URL for the image.",
variant: "destructive",
});
return false;
}
if (editLink && !isValidUrl(editLink)) {
toast({
title: "Validation Error",
description: "Please enter a valid URL for the external link.",
variant: "destructive",
});
return false;
}
return true;
}, [editName, editImage, editLink, toast]);
// Get the factory address for the current chain
const factoryAddress = chainId
? contractAddresses.cookieJarFactory[chainId]
: undefined;
// Metadata update contract write
const {
writeContract: updateMetadata,
data: updateTxHash,
isPending: isUpdatingMetadata,
error: metadataUpdateError,
} = useWriteContract();
// Wait for metadata update transaction
const { isLoading: isWaitingForUpdate, isSuccess: isMetadataUpdateSuccess } =
useWaitForTransactionReceipt({
hash: updateTxHash,
query: { enabled: !!updateTxHash },
});
// Handle metadata update
const handleMetadataUpdate = useCallback(
(addressString: `0x${string}`, _refetch: () => void) => {
if (!validateMetadataEdit()) return;
if (!factoryAddress) {
toast({
title: "Error",
description: "Factory address not found for this network.",
variant: "destructive",
});
return;
}
const updatedMetadata = {
name: editName,
description: editDescription,
image: editImage,
link: editLink,
};
const metadataJson = createMetadataJson(updatedMetadata);
// Validate metadata size using consolidated utility
const sizeValidation = validateMetadataSize(metadataJson);
if (!sizeValidation.valid) {
toast({
title: "Metadata Too Large",
description: sizeValidation.error || "Metadata exceeds size limit.",
variant: "destructive",
});
return;
}
updateMetadata({
address: factoryAddress,
abi: cookieJarFactoryAbi,
functionName: "updateMetadata",
args: [addressString, metadataJson],
});
},
[
validateMetadataEdit,
factoryAddress,
editName,
editDescription,
editImage,
editLink,
updateMetadata,
toast,
],
);
// Handle metadata update success/error
useEffect(() => {
if (isMetadataUpdateSuccess) {
toast({
title: "Jar Info Updated",
description: "Your cookie jar details have been saved.",
});
setIsEditingMetadata(false);
}
}, [isMetadataUpdateSuccess, toast]);
useEffect(() => {
if (metadataUpdateError) {
toast({
title: "Update Failed",
description:
metadataUpdateError.message || "Failed to update jar information.",
variant: "destructive",
});
}
}, [metadataUpdateError, toast]);
return {
metadata,
isEditingMetadata,
setIsEditingMetadata,
editName,
setEditName,
editImage,
setEditImage,
editLink,
setEditLink,
editDescription,
setEditDescription,
startEditing,
handleMetadataUpdate,
isUpdatingMetadata,
isWaitingForUpdate,
parseMetadata,
};
};