import { useEffect, useState, ChangeEvent } from 'react'; import { Cards } from '@/components/cards'; import styles from '@/styles/app.module.css'; import { HelloNearContract } from '@/config'; import { useNearWallet } from 'near-connect-hooks'; interface useNearHook { signedAccountId: string | null; viewFunction: (params: { contractId: string; method: string; args?: Record }) => Promise; callFunction: (params: { contractId: string; method: string; args?: Record }) => Promise; } // Contract constant const CONTRACT = HelloNearContract as string; export default function HelloNear() { const { signedAccountId, viewFunction, callFunction } = useNearWallet() as useNearHook; const [greeting, setGreeting] = useState('loading...'); const [newGreeting, setNewGreeting] = useState('loading...'); const [loggedIn, setLoggedIn] = useState(false); const [showSpinner, setShowSpinner] = useState(false); useEffect(() => { viewFunction({ contractId: CONTRACT, method: 'get_greeting' }) .then((greeting) => setGreeting(greeting)) .catch(console.error); }, [viewFunction]); useEffect(() => { setLoggedIn(!!signedAccountId); }, [signedAccountId]); const saveGreeting = async () => { try { await callFunction({ contractId: CONTRACT, method: 'set_greeting', args: { greeting: newGreeting }, }); } catch (error) { console.error(error); const greeting = await viewFunction({ contractId: CONTRACT, method: 'get_greeting' }); setGreeting(greeting); } setShowSpinner(true); await new Promise((resolve) => setTimeout(resolve, 300)); setGreeting(newGreeting); setShowSpinner(false); }; const handleChange = (e: ChangeEvent) => { setNewGreeting(e.target.value); }; return (

Interacting with the contract:   {CONTRACT}

The contract says: {greeting}

{loggedIn ? (
) : (

Please login to change the greeting

)}
); }