Loading data from network...
import { useState } from 'react'
import { useParams } from 'react-router-dom'
import useGroups from '../hooks/useGroups.js'
import useAjoContract from '../hooks/useAjoContract.js'
import ContributionPanel from '../components/ContributionPanel.jsx'
import { formatAddress, formatAmount } from '../utils/helpers.js'
import useNotifications from '../hooks/useNotifications.js'
import { postNotification } from '../services/notifications.js'
export default function GroupDetails() {
const { address } = useParams()
const { getGroup } = useGroups()
const group = getGroup(address)
const ajo = useAjoContract(address)
const members = ajo.members.data || []
const payoutIndex = Number(ajo.payoutIndex.data || 0)
const [healthStatus, setHealthStatus] = useState('')
const { addNotification } = useNotifications()
const [status, setStatus] = useState('')
const handleJoin = async () => {
try {
setStatus('Joining group...')
await ajo.joinGroup()
setStatus('Joined successfully.')
} catch (error) {
setStatus(error.shortMessage || error.message || 'Join failed.')
}
}
return (
<div className="space-y-8">
<div className="grid gap-6 lg:grid-cols-[2fr,1fr]">
<div className="card">
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Group Overview</p>
<h1 className="mt-3 text-3xl font-display text-ocean">{group?.name || 'Ajo Circle'}</h1>
<div className="mt-4 grid gap-4 md:grid-cols-3">
<div>
<p className="text-xs text-ink/50">Contract</p>
<p className="text-sm font-semibold">{formatAddress(address)}</p>
</div>
<div>
<p className="text-xs text-ink/50">Pool balance</p>
<p className="text-sm font-semibold">{formatAmount(ajo.poolBalance.data)} NGN</p>
</div>
<div>
<p className="text-xs text-ink/50">Round</p>
<p className="text-sm font-semibold">{Number(ajo.currentRound.data || 0) + 1}</p>
</div>
</div>
<div className="mt-6 flex flex-wrap items-center gap-3">
<button className="button-primary" type="button" onClick={handleJoin}>
Join group
</button>
{status && <span className="text-sm text-ink/70">{status}</span>}
</div>
</div>
<div className="card">
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Payout Order</p>
<div className="mt-4 space-y-2 text-sm">
{members.length === 0 && <p className="text-ink/60">No members yet.</p>}
{members.map((member, index) => (
<div
key={member}
className={`flex items-center justify-between rounded-2xl px-4 py-2 ${index === payoutIndex ? 'bg-jade/10 text-jade' : 'bg-white/80 text-ink/70'}`}
>
<span>#{index + 1}</span>
<span>{formatAddress(member)}</span>
</div>
))}
</div>
</div>
</div>
<div className="grid gap-6 lg:grid-cols-[1.2fr,1fr]">
<div className="card">
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Members</p>
<div className="mt-4 space-y-3">
{members.length === 0 && <p className="text-ink/60">Members list will appear after joining.</p>}
{members.map((member) => (
<div key={member} className="flex items-center justify-between rounded-2xl border border-ink/10 bg-white/80 px-4 py-3">
<span className="text-sm font-semibold">{formatAddress(member)}</span>
<span className="rounded-full bg-ocean/10 px-3 py-1 text-xs font-semibold text-ocean">Active</span>
</div>
))}
</div>
</div>
<div className="space-y-6">
<div className="card space-y-4">
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Group health</p>
<div className="grid gap-3 sm:grid-cols-2">
<div className="rounded-2xl border border-ink/10 bg-white/80 px-4 py-3">
<p className="text-xs text-ink/50">On-time rate</p>
<p className="text-lg font-semibold text-ink">92%</p>
</div>
<div className="rounded-2xl border border-ink/10 bg-white/80 px-4 py-3">
<p className="text-xs text-ink/50">Missed payments</p>
<p className="text-lg font-semibold text-ink">1</p>
</div>
<div className="rounded-2xl border border-ink/10 bg-white/80 px-4 py-3">
<p className="text-xs text-ink/50">Next reminder</p>
<p className="text-sm font-semibold text-ink">In 18 hours</p>
</div>
<div className="rounded-2xl border border-ink/10 bg-white/80 px-4 py-3">
<p className="text-xs text-ink/50">Trust score</p>
<p className="text-lg font-semibold text-ink">86</p>
</div>
</div>
<button
className="button-secondary w-full"
type="button"
onClick={() => {
setHealthStatus('Reminder sent to all members.')
const notification = {
id: Date.now(),
title: 'Reminder sent',
detail: `${group?.name || 'Group'} • Members notified`,
time: 'Just now',
read: false
}
addNotification(notification)
postNotification(notification)
setTimeout(() => setHealthStatus(''), 2000)
}}
>
Send reminders
</button>
{healthStatus && <p className="text-sm text-ink/70">{healthStatus}</p>}
</div>
<ContributionPanel group={group} />
<div className="card">
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Contribution History</p>
<p className="mt-4 text-sm text-ink/70">
This MVP tracks contributions on-chain. Hook events to build a detailed history feed.
</p>
</div>
</div>
</div>
</div>
)
}