src/examples/account-panel/WithUi.tsx

Every source

src/examples/account-panel/WithUi.tsx
'use client'

import { AccountPanel, useAccountPanelSection, useUser } from '@synqauth/react'
import { useState } from 'react'

// The account button of an app's header, with Synq's UI: signed out, the
// brand's ways in (the one this browser last used marked, and lifted out of
// More options); signed in, the profile. A first sign-in gets a welcome once, with
// the cards to set up, each skippable. The sections render in source order,
// so the app's own card sits between them, drawn as theirs are with
// AccountPanel.Card. Signing out stays on the page. Wrap the app in
// <SynqWalletProvider> (components/Providers.tsx) and a wallet signs in with
// one signature, in place.
function MemberCard() {
  const { profile } = useUser()
  // A first sign-in's onboarding keeps to what to set up.
  const { firstRun } = useAccountPanelSection()
  if (firstRun || !profile) return null
  return (
    <AccountPanel.Card title="Member since">
      <p>{new Date(profile.created_at).toLocaleDateString('en-US', { dateStyle: 'long' })}</p>
    </AccountPanel.Card>
  )
}

export function AccountPanelWithUi() {
  const [said, setSaid] = useState('')
  return (
    <header className="flex items-center justify-between gap-4">
      <span className="font-display text-lg font-bold">Your app</span>
      <AccountPanel
        methods={['wallet', 'google', 'github', 'email']}
        description="One profile for every way you sign in."
        accountHref="/build/accounts"
        onSignedOut={() => setSaid('Signed out.')}
      >
        <AccountPanel.Username />
        <MemberCard />
        <AccountPanel.Wallets />
        <AccountPanel.Linked>
          <AccountPanel.LinkRow
            provider="discord"
            description="Show your Discord on your profile."
          />
          <AccountPanel.LinkRow provider="x" />
        </AccountPanel.Linked>
      </AccountPanel>
      <p role="status" className="sr-only">
        {said}
      </p>
    </header>
  )
}