summary refs log tree commit diff
path: root/ui/src/root.tsx
blob: 2026dde8d020daaa0efe8c97ab9a18e97f0453a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { useEffect, useState } from 'react';
import { Accounts } from '../bindings/'
import { Account } from '../bindings/Account';

export default function Root() {
  const [existingAccounts, setExistingAccounts] = useState<Account[]>([])
  const [signedIn, setSignedIn] = useState<{serverDomain: string, username: string} | null>(null)
  const [accountData, setAccountData] = useState('')

  async function signIn() {
    let [serverDomain, username] = await Accounts.startAccountAuth('social.tempest.dev')

    setSignedIn({serverDomain, username})
  }

  async function getSelf() {
    if (!signedIn) throw new Error("Not signed in")
    const {serverDomain, username} = signedIn;
    let result = await Accounts.getSelf(serverDomain, username)
    setAccountData(JSON.parse(result))
  }

  useEffect(() => {
    (async () => {
      const result = await Accounts.getAllAccounts()
      setExistingAccounts(result)
    })()
  }, [])

  return (
    <>
      {!signedIn ? (
        <>
          <p>Existing accounts:</p>
          {existingAccounts.map(({username, domain}) => (
            <>
              <button key={username + domain} onClick={() => setSignedIn({serverDomain: domain, username})}>@{username}@{domain}</button>
              <br/>
            </>
          ))}
          <button onClick={signIn}>Add another account</button>
        </>
      ) : (!accountData ? (
        <button onClick={getSelf}>Retrieve account data</button>
      ):(
        <p>Result from rust: <pre style={{whiteSpace:'pre'}}>{JSON.stringify(accountData, null, 2)}</pre></p>
      ))}
    </>
  )
}