import React, { useState } from 'react'; import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Button } from "@/components/ui/button" import { Label } from "@/components/ui/label" import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card" import { useAccount, useSignMessage } from 'wagmi' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog" import { Loader2 } from "lucide-react" import QRCode from '@/components/QRCode'; import { v4 as uuidv4 } from 'uuid'; import { useWaku } from '@waku/react'; import { LightNode } from '@waku/sdk'; import { createMessage, encoder } from '@/lib/waku'; interface FormData { title: string; description: string; uuid: string; } const DEFAULT_FORM_DATA: FormData = { title: 'Devcon24 DeFi Dynamo', description: 'A revolutionary blockchain for Devcon 24, focusing on scalable DeFi solutions and cross-chain interoperability.', uuid: uuidv4(), } const ChainCreationForm: React.FC = () => { const [formData, setFormData] = useState(DEFAULT_FORM_DATA); const [errors, setErrors] = useState>({}); const [showModal, setShowModal] = useState(false); const [isSigning, setIsSigning] = useState(false); const [isSuccess, setIsSuccess] = useState(false); const [sendError, setSendError] = useState(null); const [signedMessage, setSignedMessage] = useState(null); const { node } = useWaku(); const { address } = useAccount(); const { signMessage } = useSignMessage({ mutation: { async onSuccess(signature: string) { if (!address || !node) return; setSignedMessage(signature); const message = createMessage({ chainUUID: formData.uuid, blockUUID: uuidv4(), title: formData.title, description: formData.description, signedMessage: signature, timestamp: Date.now(), signatures: [{address, signature}], parentBlockUUID: null }); await node?.lightPush.send(encoder, message) setIsSuccess(true); setIsSigning(false); }, onError(error: Error) { console.error('Error signing message:', error); setIsSigning(false); setSendError('Error signing message. Please try again.'); } } }); const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prevData => ({ ...prevData, [name]: value, })); if (errors[name as keyof FormData]) { setErrors(prevErrors => ({ ...prevErrors, [name]: undefined, })); } }; const validateForm = (): boolean => { const newErrors: Partial = {}; if (!formData.title.trim()) { newErrors.title = 'Title is required'; } if (!formData.description.trim()) { newErrors.description = 'Description is required'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleCreateChain = (e: React.FormEvent) => { e.preventDefault(); if (validateForm()) { setShowModal(true); } }; const handleSubmit = async () => { setIsSigning(true); setSendError(null); const message = `Create Chain: Chain UUID: ${formData.uuid} Title: ${formData.title} Description: ${formData.description} Timestamp: ${new Date().getTime()} Signed by: ${address}`; signMessage({ message }); }; const handleCloseModal = () => { setShowModal(false); setIsSuccess(false); setIsSigning(false); setSendError(null); }; return ( Create a New Chain
{errors.title &&

{errors.title}

}