|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { lazy, useEffect, useState } from "react"; |
| 3 | +import { useEffect, useState } from "react"; |
4 | 4 | import type { NextPage } from "next"; |
5 | 5 | import { notification } from "~~/utils/scaffold-stark/notification"; |
6 | 6 | import { getMetadataFromIPFS } from "~~/utils/simpleNFT/ipfs-fetch"; |
7 | 7 | import { INITIAL_ATTEMPT, MAX_ATTEMPTS } from "~~/utils/simpleNFT/constants"; |
8 | 8 |
|
9 | | -const LazyReactJson = lazy(() => import("react-json-view")); |
| 9 | +// Import the JSON editor component and its core CSS |
| 10 | +import { JsonEditor as Editor } from "jsoneditor-react"; |
| 11 | + |
10 | 12 |
|
11 | 13 | const IpfsDownload: NextPage = () => { |
12 | | - const [yourJSON, setYourJSON] = useState({}); |
| 14 | + const [yourJSON, setYourJSON] = useState<object | null>(null); |
13 | 15 | const [ipfsPath, setIpfsPath] = useState(""); |
14 | 16 | const [loading, setLoading] = useState(false); |
15 | 17 | const [mounted, setMounted] = useState(false); |
| 18 | + // In your state |
| 19 | + const [editorKey, setEditorKey] = useState(0); |
16 | 20 | useEffect(() => { |
17 | 21 | setMounted(true); |
18 | | - }, []); |
19 | | - |
| 22 | + console.log("Updated JSON to editor:", yourJSON); |
| 23 | + }, [yourJSON]); |
20 | 24 |
|
21 | 25 | const handleIpfsDownload = async () => { |
22 | 26 | setLoading(true); |
23 | 27 | const loadingNotificationId = notification.loading("Getting data from IPFS..."); |
24 | 28 | let retryNotificationId = null; |
25 | 29 | let attempt = INITIAL_ATTEMPT; |
26 | 30 | const maxAttempts = MAX_ATTEMPTS; |
| 31 | + |
27 | 32 | while (attempt < maxAttempts) { |
28 | 33 | try { |
29 | 34 | const metaData = await getMetadataFromIPFS(ipfsPath); |
| 35 | + |
30 | 36 | notification.remove(loadingNotificationId); |
31 | | - retryNotificationId && notification.remove(retryNotificationId); |
| 37 | + if (retryNotificationId) notification.remove(retryNotificationId); |
32 | 38 | notification.success("Downloaded from IPFS"); |
33 | | - setYourJSON(metaData); |
| 39 | + |
| 40 | + // Fix: Ensure it's serializable |
| 41 | + const cleanData = JSON.parse(JSON.stringify(metaData)); |
| 42 | + console.log("meta-data:", cleanData); |
| 43 | + |
| 44 | + setYourJSON(cleanData); |
| 45 | + setEditorKey(prevKey => prevKey + 1); |
34 | 46 | break; |
35 | 47 | } catch (error) { |
36 | 48 | attempt++; |
37 | 49 | if (attempt < maxAttempts) { |
38 | 50 | retryNotificationId = notification.info(`Retrying download... (${attempt}/${maxAttempts})`); |
39 | 51 | } else { |
40 | 52 | notification.remove(loadingNotificationId); |
41 | | - retryNotificationId && notification.remove(retryNotificationId); |
| 53 | + if (retryNotificationId) notification.remove(retryNotificationId); |
42 | 54 | notification.error(error instanceof Error ? error.message : "Error downloading from IPFS"); |
43 | 55 | console.error("IPFS Download Error:", error); |
44 | 56 | } |
45 | 57 | } |
46 | 58 | } |
| 59 | + |
47 | 60 | setLoading(false); |
48 | 61 | }; |
49 | 62 |
|
50 | 63 | return ( |
51 | | - <> |
52 | | - <div className="flex items-center flex-col flex-grow pt-10"> |
53 | | - <h1 className="text-center mb-4"> |
54 | | - <span className="block text-4xl font-bold">Download from IPFS</span> |
55 | | - </h1> |
56 | | - <div |
57 | | - className={`flex border-2 border-accent/95 bg-base-200 rounded-full text-accent w-96`} |
58 | | - > |
59 | | - <input |
60 | | - className="input input-ghost focus:outline-none focus:bg-transparent focus:text-secondary-content h-[2.2rem] min-h-[2.2rem] px-4 border w-full font-medium placeholder:text-accent/50 text-secondary-content/75" |
61 | | - placeholder="IPFS CID" |
62 | | - value={ipfsPath} |
63 | | - onChange={(e) => setIpfsPath(e.target.value)} |
64 | | - autoComplete="off" |
65 | | - /> |
66 | | - </div> |
67 | | - <button |
68 | | - className={`btn btn-secondary text-white my-6 ${loading ? "loading" : ""}`} |
69 | | - disabled={loading} |
70 | | - onClick={handleIpfsDownload} |
71 | | - > |
72 | | - Download from IPFS |
73 | | - </button> |
| 64 | + <div className="flex items-center flex-col flex-grow pt-10"> |
| 65 | + <h1 className="text-center mb-4"> |
| 66 | + <span className="block text-4xl font-bold">Download from IPFS</span> |
| 67 | + </h1> |
74 | 68 |
|
75 | | - {mounted && ( |
76 | | - <LazyReactJson |
77 | | - style={{ padding: "1rem", borderRadius: "0.75rem" }} |
78 | | - src={yourJSON} |
79 | | - theme="solarized" |
80 | | - enableClipboard={false} |
81 | | - onEdit={(edit) => { |
82 | | - setYourJSON(edit.updated_src); |
83 | | - }} |
84 | | - onAdd={(add) => { |
85 | | - setYourJSON(add.updated_src); |
86 | | - }} |
87 | | - onDelete={(del) => { |
88 | | - setYourJSON(del.updated_src); |
89 | | - }} |
90 | | - /> |
91 | | - )} |
| 69 | + <div className={`flex border-2 border-accent/95 bg-base-200 rounded-full text-accent w-96`}> |
| 70 | + <input |
| 71 | + className="input input-ghost focus:outline-none focus:bg-transparent focus:text-secondary-content h-[2.2rem] min-h-[2.2rem] px-4 border w-full font-medium placeholder:text-accent/50 text-secondary-content/75" |
| 72 | + placeholder="IPFS CID" |
| 73 | + value={ipfsPath} |
| 74 | + onChange={(e) => setIpfsPath(e.target.value)} |
| 75 | + autoComplete="off" |
| 76 | + /> |
92 | 77 | </div> |
93 | | - </> |
| 78 | + |
| 79 | + <button |
| 80 | + className={`btn btn-secondary text-white my-6 ${loading ? "loading" : ""}`} |
| 81 | + disabled={loading} |
| 82 | + onClick={handleIpfsDownload} |
| 83 | + > |
| 84 | + Download from IPFS |
| 85 | + </button> |
| 86 | + |
| 87 | + {mounted && yourJSON && Object.keys(yourJSON).length > 0 && ( |
| 88 | + <div style={{ width: "100%", maxWidth: "1000px" }}> |
| 89 | + <Editor |
| 90 | + key={editorKey} // Add this line |
| 91 | + value={yourJSON} |
| 92 | + onChange={(updatedJson: object) => setYourJSON(updatedJson)} |
| 93 | + mode="tree" |
| 94 | + modes={["tree", "code", "form", "text", "view"]} |
| 95 | + htmlElementProps={{ |
| 96 | + style: { |
| 97 | + height: "500px", |
| 98 | + borderRadius: "0.75rem", |
| 99 | + border: "1px solid #ccc", |
| 100 | + overflow: "hidden", |
| 101 | + }, |
| 102 | + }} |
| 103 | + /> |
| 104 | + </div> |
| 105 | +)} |
| 106 | + </div> |
94 | 107 | ); |
95 | 108 | }; |
96 | 109 |
|
|
0 commit comments