/** * The account menu, top right: what the admin's sidebar shows (workspace, * plan, key, booking site link) and "Disconnect this site" (not "Log out", * which WordPress's own admin bar already has). * * The confirmation says that booking data is untouched, that blocks stay on * pages but stop showing, and what happens to the key: revoked if this site * created it, only forgotten if it was pasted. */ import { useState } from 'react'; import { ChevronDown, ExternalLink, Globe, LogOut } from '../design-system/icons'; import { Avatar, Badge, ConfirmDialog, Dropdown } from '../ds'; import { api, messageOf } from '../lib/api'; import { boot } from '../lib/boot'; import { openHandoff } from '../lib/handoff'; import { hasFullAccessKey, initialsOf } from '../lib/people'; import { useToast } from '../components/Toasts'; import type { Connection } from '../lib/types'; interface AccountMenuProps { connection: Connection; } export function AccountMenu( { connection }: AccountMenuProps ) { const [ confirming, setConfirming ] = useState( false ); const [ working, setWorking ] = useState( false ); const toast = useToast(); // Reload: WordPress renders the menu for the connection state. const disconnect = async () => { setWorking( true ); try { await api.post( 'disconnect' ); window.location.reload(); } catch ( caught ) { toast( messageOf( caught, 'Couldn’t disconnect. Try again.' ), 'danger' ); setWorking( false ); } }; const fullAccess = hasFullAccessKey( connection ); const workspace = connection.workspace ?? boot().siteName; // Only shown once connected; `degraded` means RazorBooking can't be reached. const status = connection.status === 'connected' ? connection.plan ?? 'Connected' : 'Showing saved info'; return ( <>
{ workspace } { connection.plan ? ( { connection.plan } ) : null } { fullAccess ? 'Full-access key' : 'Site key' } { connection.key_last4 ? ( ••••{ connection.key_last4 } ) : null } { fullAccess ? 'This site has a full-access key, but it only needs booking access. Switch to a site key in Settings.' : 'This site has a site key. It can only handle booking, never billing, settings, or roles.' }
{ connection.booking_page_url ? ( { connection.booking_page_url.replace( /^https?:\/\//, '' ) } ) : null } openHandoff( 'home' ) }> Open RazorBooking setConfirming( true ) } className="text-destructive-fg data-[highlighted]:bg-destructive-bg" > Disconnect this site
setConfirming( false ) } onConfirm={ () => { setConfirming( false ); void disconnect(); } } /> ); }