import { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { usePresets, useAuth } from '@humanity-org/react-sdk';
import { verifyAllBatched, findPresetResult, isPassed, type PresetResult } from '../presets';
interface SocialAccount {
provider: string;
username?: string;
displayName?: string;
emails?: string[];
}
const SOCIAL_PLATFORMS = [
{ id: 'twitter', label: 'Twitter' },
{ id: 'discord', label: 'Discord' },
{ id: 'telegram', label: 'Telegram' },
{ id: 'github', label: 'GitHub' },
{ id: 'google', label: 'Google' },
{ id: 'linkedin', label: 'LinkedIn' },
] as const;
function SocialCard({ label, account }: { label: string; account?: SocialAccount }) {
const hasValue = Boolean(account?.username || account?.displayName);
const state = !account ? 'not-linked' : hasValue ? 'linked' : 'linked-empty';
return (
<div className={`social-card social-card--${state}`}>
<h3>{label}</h3>
{state === 'not-linked' && <p>Not linked</p>}
{state === 'linked-empty' && <p>Linked, no data returned</p>}
{state === 'linked' && (
<p>
under username: <span className="username-value">{account?.displayName ?? account?.username}</span>
</p>
)}
</div>
);
}
export function Dashboard() {
const { logout } = useAuth();
const { verifyAll } = usePresets();
const [results, setResults] = useState<PresetResult[] | null>(null);
useEffect(() => {
verifyAllBatched(verifyAll, ['is_human', 'social_accounts', 'humanity_uuid']).then(setResults);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const humanVerified = results !== null && isPassed(findPresetResult(results, 'is_human'));
const socialResult = findPresetResult(results, 'social_accounts');
const accounts = (
socialResult?.verified && Array.isArray(socialResult.value) ? socialResult.value : []
) as SocialAccount[];
const accountFor = (id: string) => accounts.find((a) => a.provider?.toLowerCase() === id);
const uuidResult = findPresetResult(results, 'humanity_uuid');
const uuidValue = uuidResult?.verified ? String(uuidResult.value ?? '') : null;
return (
<div className="dashboard">
<header>
<h1>Dashboard</h1>
<nav>
<Link to="/profile">Profile</Link>
<button onClick={logout}>Sign out</button>
</nav>
</header>
<section>
<h2>Public content</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Visible to any authenticated user, verified or
not.
</p>
</section>
<section>
{results === null ? (
<>
<h2>Linked accounts</h2>
<p>Checking verification...</p>
</>
) : !humanVerified ? (
<>
<h2>Linked accounts</h2>
<p>Human verification required to see linked accounts.</p>
</>
) : (
<>
<h2>Welcome {uuidValue ?? 'Verified human'}</h2>
<p className="caption">You can prove your humanity across the following social profiles</p>
<div className="social-grid">
{SOCIAL_PLATFORMS.map((platform) => (
<SocialCard key={platform.id} label={platform.label} account={accountFor(platform.id)} />
))}
</div>
</>
)}
</section>
</div>
);
}