-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmint-nft-dialog.tsx
138 lines (125 loc) · 3.79 KB
/
mint-nft-dialog.tsx
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
"use client"
import { useState } from "react"
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogFooter,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Loader2 } from "lucide-react"
import { mint, createCollection } from "./actions"
import { useToast } from "@/components/ui/use-toast"
interface MintNFTDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
memeImageUrl: string
memeName: string
textElements: any[]
}
export function MintNFTDialog({ open, onOpenChange, memeImageUrl, memeName, textElements }: MintNFTDialogProps) {
const [walletAddress, setWalletAddress] = useState("")
const [collectionId, setCollectionId] = useState("1")
const [isLoading, setIsLoading] = useState(false)
const { toast } = useToast()
const handleMint = async () => {
if (!walletAddress.trim()) {
toast({
title: "Error",
description: "Please enter a wallet address",
variant: "destructive",
})
return
}
try {
setIsLoading(true)
// Create metadata for the NFT
const metadata = {
name: memeName,
image: memeImageUrl,
description: `Meme created with Meme Generator`,
attributes: [
{
trait_type: "Text Elements",
value: textElements.length,
},
],
textElements: textElements,
}
await sql`INSERT INTO users(address) VALUES (${walletAddress})`
// Mint the NFT
const nftId = await mint(walletAddress, metadata)
toast({
title: "Success!",
description: `Your meme has been minted as NFT #${nftId}`,
})
onOpenChange(false)
} catch (error) {
console.error("Error minting NFT:", error)
toast({
title: "Error",
description: "Failed to mint NFT. Please try again.",
variant: "destructive",
})
} finally {
setIsLoading(false)
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent>
<DialogHeader>
<DialogTitle>Mint as NFT</DialogTitle>
<DialogDescription>Convert your meme into an NFT on the blockchain</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid gap-2">
<Label htmlFor="wallet">Wallet Address</Label>
<Input
id="wallet"
value={walletAddress}
onChange={(e) => setWalletAddress(e.target.value)}
placeholder="Enter your wallet address"
/>
</div>
<div className="grid gap-2">
<Label htmlFor="collection">Collection ID</Label>
<Input
id="collection"
value={collectionId}
onChange={(e) => setCollectionId(e.target.value)}
placeholder="Enter collection ID"
type="number"
/>
</div>
<div className="aspect-video relative">
<img
src={memeImageUrl || "/placeholder.svg"}
alt={memeName}
className="rounded-md w-full h-full object-contain border"
/>
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)}>
Cancel
</Button>
<Button onClick={handleMint} disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Minting...
</>
) : (
"Mint NFT"
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}