import { useState } from 'react'; import Link from 'next/link'; import Head from 'next/head'; import { useRouter } from 'next/router'; import PrivacySettings from '../../components/PrivacySettings'; import { PrivacySettings as IPrivacySettings } from '../../lib/privacy/anonymity'; export default function RegisterAnonymousPlant() { const router = useRouter(); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(false); const [walletAddress, setWalletAddress] = useState(''); const [formData, setFormData] = useState({ commonName: '', scientificName: '', species: '', genus: '', family: '', latitude: '', longitude: '', pseudonym: '', encryptionKey: '', }); const [privacySettings, setPrivacySettings] = useState({ anonymousMode: true, locationPrivacy: 'fuzzy', identityPrivacy: 'anonymous', sharePlantDetails: true, }); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); try { const response = await fetch('/api/plants/register-anonymous', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ commonName: formData.commonName, scientificName: formData.scientificName || undefined, species: formData.species || undefined, genus: formData.genus || undefined, family: formData.family || undefined, location: { latitude: parseFloat(formData.latitude), longitude: parseFloat(formData.longitude), }, privacySettings, pseudonym: formData.pseudonym || undefined, encryptionKey: formData.encryptionKey || undefined, }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Failed to register plant'); } setSuccess(true); setWalletAddress(data.privacy.walletAddress); setTimeout(() => { router.push(`/plants/${data.plant.id}`); }, 3000); } catch (err: any) { setError(err.message || 'An error occurred'); } finally { setLoading(false); } }; const handleChange = ( e: React.ChangeEvent ) => { setFormData({ ...formData, [e.target.name]: e.target.value, }); }; const getCurrentLocation = () => { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( (position) => { setFormData({ ...formData, latitude: position.coords.latitude.toString(), longitude: position.coords.longitude.toString(), }); }, (error) => { setError('Unable to get your location: ' + error.message); } ); } else { setError('Geolocation is not supported by your browser'); } }; return (
Anonymous Plant Registration - LocalGreenChain {/* Header */}
{/* Main Content */}

🔒 Anonymous Plant Registration

Register your plant with maximum privacy protection

{error && (
{error}
)} {success && (

✓ Plant registered anonymously!

Your anonymous wallet address:

{walletAddress}

Save this address to manage your plant. Redirecting...

)}
{/* Left Column - Privacy Settings */}
{/* Right Column - Plant Information */}

Plant Information

{/* Basic Plant Info */}
{privacySettings.sharePlantDetails && ( <>
)}
{/* Location */}

Location (will be obfuscated based on privacy settings)

Your exact location will be obfuscated based on your privacy settings

{/* Optional Pseudonym */} {privacySettings.identityPrivacy === 'pseudonym' && (
)} {/* Encryption Key */} {privacySettings.anonymousMode && (

Used to generate your anonymous contact address

)} {/* Submit Button */}
Cancel
); }