src/examples/wallets/Headless.tsx

Every source

src/examples/wallets/Headless.tsx
'use client'

import { useAuth, useLinkedAccounts, useWallets } from '@synqauth/react'

// Wallets headless: useWallets lists them, wallets.rename labels one,
// wallets.setPrimary makes it the primary, link('wallet') adds one through
// the hosted page, and removing one is unlinking its sign-in identity.
export function WalletsHeadless() {
  const { data: wallets, error } = useWallets()
  const { data: identities } = useLinkedAccounts()
  const { link, wallets: actions, accounts } = useAuth()
  if (error) return <p role="alert">{error.message}</p>
  if (!wallets) return <p>Loading…</p>
  const identityOf = (address: string) =>
    identities?.find((i) => i.provider === 'solana' && i.subject === address)
  return (
    <>
      <ul aria-label="Your wallets">
        {wallets.map((wallet) => (
          <li key={wallet.id}>
            <code>{wallet.address}</code> {wallet.label ?? 'Unnamed'}
            {wallet.primary ? ' (primary)' : ''}{' '}
            <button type="button" onClick={() => void actions.rename(wallet.id, 'Main wallet')}>
              Label it “Main wallet”
            </button>
            {!wallet.primary && (
              <button type="button" onClick={() => void actions.setPrimary(wallet.id)}>
                Make primary
              </button>
            )}
            <button
              type="button"
              onClick={() => {
                const identity = identityOf(wallet.address)
                if (identity) void accounts.unlink(identity.id)
              }}
            >
              Remove
            </button>
          </li>
        ))}
      </ul>
      <button type="button" onClick={() => void link('wallet')}>
        Add a wallet
      </button>
    </>
  )
}