'use client' import React, { useState } from 'react' import BN from 'bn.js'; import Big from 'big.js'; import dynamic from 'next/dynamic' interface AIServiceCardProps { title: string description: string isWalletConnected: boolean onGenerate: (prompt: string) => Promise<{ imageUrl?: string, transactionSignature?: string, error?: string }> priceMTM: BN } interface GenerationState { loading: boolean processing: boolean imageUrl: string | null transactionSignature: string | null error: string | null } const baseUnitToWholeNumber = (value: BN, decimals: number): string => { const bigValue = new Big(value.toString()); const factor = new Big(10).pow(decimals); return bigValue.div(factor).round(0, Big.roundUp).toFixed(0); } const AIServiceCard: React.FC = ({ title, description, isWalletConnected, onGenerate, priceMTM }) => { const [inputText, setInputText] = useState('') const [generationState, setGenerationState] = useState({ loading: false, processing: false, imageUrl: null, transactionSignature: null, error: null, }) const handleGenerate = async (): Promise => { if (!inputText || !isWalletConnected) return setGenerationState({ ...generationState, loading: true, error: null, }) try { const result = await onGenerate(inputText) if (result.error) { setGenerationState({ ...generationState, loading: false, error: result.error, }) // Reload the page to get latest prices setTimeout(() => { window.location.reload(); }, 3000); return } if (result.imageUrl && result.transactionSignature) { setGenerationState({ loading: false, processing: false, imageUrl: result.imageUrl, transactionSignature: result.transactionSignature, error: null, }) } else { throw new Error('No image URL received') } } catch (error) { setGenerationState({ ...generationState, loading: false, error: error instanceof Error ? error.message : 'Generation failed', }) } } const generateTwitterShareUrl = (imageUrl: string, transactionSignature: string): string => { const baseUrl = window.location.href; const cid = imageUrl.split("/image/")[1]; const memePageUrl = `${baseUrl}memes/${cid}`; const tweetText = `Check out this meme that I generated! \n TX Hash: '${transactionSignature}' \n @${process.env.NEXT_PUBLIC_ACCOUNT_HANDLE} \n`; return `https://twitter.com/intent/tweet?text=${encodeURIComponent(tweetText)}&url=${encodeURIComponent(memePageUrl)}`; }; return (

{title}

{description}

Cost: {priceMTM ? baseUnitToWholeNumber(priceMTM, 6) : '...'} MTM