import { useState } from 'react';
import { invoke } from '@tauri-apps/api/core';

export default function Root() {
  const [signedIn, setSignedIn] = useState(false)
  const [accountData, setAccountData] = useState('')

  async function signIn() {
    await invoke('start_account_auth', {instanceDomain: 'social.tempest.dev'})
    setSignedIn(true)
  }

  async function getSelf() {
    let result = await invoke('get_self') as string
    setAccountData(JSON.parse(result))
  }

  return (
    <>
      {!signedIn ? (
        <button onClick={signIn}>Sign in</button>
      ) : (!accountData ? (
        <button onClick={getSelf}>Retrieve account data</button>
      ):(
        <p>Result from rust: <pre style={{whiteSpace:'pre'}}>{JSON.stringify(accountData, null, 2)}</pre></p>
      ))}
    </>
  )
}
