Loading data from network...
import { useState, useEffect } from 'react'
import CreateGroupForm from '../components/CreateGroupForm.jsx'
import GroupCard from '../components/GroupCard.jsx'
import OnboardingBanner from '../components/OnboardingBanner.jsx'
import useGroups from '../hooks/useGroups.js'
import useProfile from '../hooks/useProfile.js'
import useNotifications from '../hooks/useNotifications.js'
import { fetchNotifications, postNotification } from '../services/notifications.js'
import { Link } from 'react-router-dom'
const promoImages = [
'https://images.pexels.com/photos/3184633/pexels-photo-3184633.jpeg?auto=compress&cs=tinysrgb&w=600',
'https://images.pexels.com/photos/3184357/pexels-photo-3184357.jpeg?auto=compress&cs=tinysrgb&w=600'
]
const paymentMethods = ['Bank transfer', 'Card', 'USSD']
const banks = ['GTBank', 'Access Bank', 'Zenith Bank', 'UBA']
export default function Dashboard() {
const { groups, transactions } = useGroups()
const { fullName } = useProfile()
const { notifications, unreadCount, addNotification, markAllRead } = useNotifications()
const [modal, setModal] = useState(null)
const [amount, setAmount] = useState('')
const [method, setMethod] = useState('Bank transfer')
const [bank, setBank] = useState('GTBank')
const [notice, setNotice] = useState('')
const [showNotifications, setShowNotifications] = useState(false)
const totalBalance = groups.reduce((sum, group) => sum + (group.amount || 0) * (group.members || 0), 0)
const nextDepositDue = groups.length ? groups[0].amount || 0 : 0
useEffect(() => {
const load = async () => {
const data = await fetchNotifications()
data.forEach((note) => addNotification(note))
}
load()
const interval = setInterval(load, 15000)
return () => clearInterval(interval)
}, [addNotification])
const closeModal = () => {
setModal(null)
setAmount('')
setNotice('')
}
const handleAddMoney = () => {
setNotice('Coming soon: we will guide you through bank transfer instructions.')
}
const handleWithdraw = () => {
setNotice('Withdrawal scheduled. You will receive a confirmation shortly.')
}
const toggleNotifications = () => {
setShowNotifications((prev) => !prev)
if (!showNotifications) {
markAllRead()
}
}
useEffect(() => {
if (modal === 'create') {
postNotification({ title: 'Create circle opened', detail: 'You started a new circle setup.' })
}
}, [modal])
return (
<div className="space-y-10 pb-20">
<OnboardingBanner />
<div className="grid gap-8 lg:grid-cols-[1.1fr,0.9fr]">
<div className="space-y-6">
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div>
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Dashboard</p>
<h1 className="mt-2 text-3xl font-display text-ink">Hi {fullName || 'Saver'}</h1>
</div>
<div className="relative flex items-center gap-3">
<button className="rounded-full border border-ink/10 bg-white/80 px-3 py-2 text-xs font-semibold text-ink/60" type="button">
<span className="inline-flex items-center gap-2">
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6">
<path d="M7 4v3M17 4v3M3 9h18M5 6h14a2 2 0 0 1 2 2v10a4 4 0 0 1-4 4H7a4 4 0 0 1-4-4V8a2 2 0 0 1 2-2Z" />
<path d="M12 12l1.2 2.4 2.6.4-1.9 1.9.4 2.6-2.3-1.2-2.3 1.2.4-2.6-1.9-1.9 2.6-.4Z" />
</svg>
Rewards
</span>
</button>
<button
className="relative flex h-10 w-10 items-center justify-center rounded-full border border-ink/10 bg-white/80"
type="button"
onClick={toggleNotifications}
>
<svg className="h-5 w-5 text-ink/70" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6">
<path d="M6 9a6 6 0 0 1 12 0v4l2 3H4l2-3V9Z" />
<path d="M9.5 19a2.5 2.5 0 0 0 5 0" />
</svg>
{unreadCount > 0 && (
<span className="absolute -top-1 -right-1 h-5 w-5 rounded-full bg-ember text-[10px] font-semibold text-white flex items-center justify-center">
{unreadCount}
</span>
)}
</button>
{showNotifications && (
<div className="absolute right-0 top-12 z-30 w-72 rounded-2xl border border-ink/10 bg-white p-4 shadow-xl">
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Notifications</p>
<div className="mt-3 space-y-3 text-sm">
{notifications.length === 0 && (
<p className="text-xs text-ink/50">No notifications yet.</p>
)}
{notifications.map((note) => (
<div key={note.id} className="rounded-2xl border border-ink/10 bg-white/90 px-3 py-2">
<p className="font-semibold text-ink">{note.title}</p>
<p className="text-xs text-ink/50">{note.detail}</p>
<p className="text-[10px] text-ink/40">{note.time}</p>
</div>
))}
</div>
</div>
)}
</div>
</div>
<div className="card rounded-[28px] border border-ink/10 bg-white/90 p-6">
<div className="flex items-center justify-between">
<div>
<p className="text-xs text-ink/50">Wallet balance</p>
<p className="mt-2 text-3xl font-display text-ink">NGN {totalBalance.toLocaleString()}</p>
</div>
<button className="rounded-full border border-ink/10 px-3 py-1 text-xs font-semibold text-ink/60" type="button">
{groups.length} groups
</button>
</div>
<div className="mt-5 flex flex-wrap gap-3">
<button className="rounded-full bg-ink px-5 py-2 text-xs font-semibold text-white" type="button" onClick={() => setModal('add')}>
Add Money
</button>
<button className="rounded-full border border-ink/10 bg-white px-5 py-2 text-xs font-semibold text-ink/70" type="button" onClick={() => setModal('withdraw')}>
Withdraw
</button>
</div>
<div className="mt-6">
<p className="text-xs text-ink/50">Next deposit due</p>
<p className="mt-1 text-sm font-semibold text-ink">NGN {nextDepositDue.toLocaleString()}</p>
</div>
</div>
<div className="card rounded-[28px] border border-ink/10 bg-white/90 p-6">
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Quick actions</p>
<div className="mt-4 grid gap-3 sm:grid-cols-3">
<button type="button" onClick={() => setModal('create')} className="flex w-full items-center gap-3 rounded-2xl border border-ink/10 bg-white/90 px-4 py-3 text-sm font-semibold text-ink">
<span className="flex h-10 w-10 items-center justify-center rounded-xl bg-ink/5">
<svg className="h-5 w-5 text-ink" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8">
<path d="M12 5v14M5 12h14" />
</svg>
</span>
Create circle
</button>
<Link to="/circles" className="flex w-full items-center gap-3 rounded-2xl border border-ink/10 bg-white/90 px-4 py-3 text-sm font-semibold text-ink">
<span className="flex h-10 w-10 items-center justify-center rounded-xl bg-ink/5">
<svg className="h-5 w-5 text-ink" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8">
<path d="M7 12l2 2 4-4" />
<path d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>
</span>
Join circle
</Link>
<Link to="/profile" className="flex w-full items-center gap-3 rounded-2xl border border-ink/10 bg-white/90 px-4 py-3 text-sm font-semibold text-ink">
<span className="flex h-10 w-10 items-center justify-center rounded-xl bg-ink/5">
<svg className="h-5 w-5 text-ink" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8">
<path d="M12 12a4 4 0 1 0-4-4 4 4 0 0 0 4 4Z" />
<path d="M4 20a8 8 0 0 1 16 0" />
</svg>
</span>
More
</Link>
</div>
</div>
<div className="space-y-4">
<div className="flex items-center justify-between">
<h2 className="text-lg font-semibold text-ink">Welcome to Cosave</h2>
<button className="text-xs font-semibold text-ocean" type="button">See all</button>
</div>
<div className="flex gap-4 overflow-x-auto pb-2">
{promoImages.map((src, index) => (
<div key={src} className="min-w-[200px] sm:min-w-[240px] rounded-2xl border border-ink/10 bg-white/90 shadow-sm">
<img src={src} alt={`Promo ${index + 1}`} className="h-32 w-full rounded-2xl object-cover" />
<div className="p-4">
<p className="text-sm font-semibold text-ink">Create your first circle</p>
<p className="mt-1 text-xs text-ink/60">Invite friends and automate payouts.</p>
</div>
</div>
))}
</div>
</div>
<div className="card rounded-[28px] border border-ink/10 bg-white/90 p-6">
<div className="flex items-center justify-between">
<div>
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">My Thrift Plans</p>
<p className="mt-2 text-lg font-semibold text-ink">Basic Thrift Plan 1</p>
</div>
<button className="text-xs font-semibold text-ocean" type="button">See all</button>
</div>
<div className="mt-4 grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
<div className="rounded-2xl border border-ink/10 bg-white px-4 py-3">
<p className="text-xs text-ink/50">Per Member</p>
<p className="mt-1 text-sm font-semibold text-ink">NGN 10,000</p>
</div>
<div className="rounded-2xl border border-ink/10 bg-white px-4 py-3">
<p className="text-xs text-ink/50">Duration</p>
<p className="mt-1 text-sm font-semibold text-ink">10 months</p>
</div>
<div className="rounded-2xl border border-ink/10 bg-white px-4 py-3">
<p className="text-xs text-ink/50">Pool Amount</p>
<p className="mt-1 text-sm font-semibold text-ink">NGN 500,000</p>
</div>
</div>
<div className="mt-5">
<div className="flex items-center justify-between text-xs text-ink/50">
<span>Progress</span>
<span>50%</span>
</div>
<div className="mt-2 h-2 w-full rounded-full bg-ink/10">
<div className="h-2 w-1/2 rounded-full bg-gradient-to-r from-ocean to-jade" />
</div>
</div>
</div>
</div>
<div className="space-y-6">
<CreateGroupForm />
<div className="space-y-4">
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Your circles</p>
<div className="grid gap-4">
{groups.length === 0 && (
<div className="card text-sm text-ink/70">
No groups yet. Create your first group and the factory will deploy it for you.
</div>
)}
{groups.map((group) => (
<GroupCard key={group.address} group={group} />
))}
</div>
</div>
<div className="card">
<p className="text-xs uppercase tracking-[0.2em] text-ink/50">Recent activity</p>
<div className="mt-4 space-y-3 text-sm text-ink/70">
{(transactions.length ? transactions : [
{ id: 1, title: 'Contribution received', detail: 'Lagos Savers NGN 5,000', time: '2h ago' },
{ id: 2, title: 'Payout completed', detail: 'Campus Circle NGN 25,000', time: 'Yesterday' },
{ id: 3, title: 'Reminder sent', detail: 'Friday cycle 3 members pending', time: '2 days ago' }
]).map((item) => (
<div key={item.id} className="flex flex-col gap-2 rounded-2xl border border-ink/10 bg-white/80 px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
<div>
<p className="font-semibold text-ink">{item.title}</p>
<p className="text-xs text-ink/50">{item.detail}</p>
</div>
<span className="text-xs text-ink/50">{item.time}</span>
</div>
))}
</div>
</div>
</div>
</div>
{modal && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 px-6">
<div className="w-full max-w-md rounded-[24px] bg-white p-6 shadow-2xl">
<div className="flex items-center justify-between">
<h3 className="text-lg font-semibold text-ink">
{modal === 'add' ? 'Add money' : modal === 'withdraw' ? 'Withdraw' : 'Create a circle'}
</h3>
<button className="text-sm text-ink/50" onClick={closeModal} type="button">
Close
</button>
</div>
{modal === 'create' ? (
<div className="mt-6">
<CreateGroupForm />
</div>
) : (
<>
<div className="mt-4 space-y-4 text-sm">
<label className="block">
Amount (NGN)
<input
className="mt-2 w-full rounded-2xl border border-ink/10 bg-white/90 px-4 py-3"
value={amount}
onChange={(event) => setAmount(event.target.value)}
placeholder="NGN 10,000"
/>
</label>
{modal === 'add' ? (
<label className="block">
Payment method
<select
className="mt-2 w-full rounded-2xl border border-ink/10 bg-white/90 px-4 py-3"
value={method}
onChange={(event) => setMethod(event.target.value)}
>
{paymentMethods.map((item) => (
<option key={item} value={item}>{item}</option>
))}
</select>
</label>
) : (
<label className="block">
Bank account
<select
className="mt-2 w-full rounded-2xl border border-ink/10 bg-white/90 px-4 py-3"
value={bank}
onChange={(event) => setBank(event.target.value)}
>
{banks.map((item) => (
<option key={item} value={item}>{item}</option>
))}
</select>
</label>
)}
</div>
<button
className="mt-6 w-full rounded-full bg-ink px-4 py-3 text-sm font-semibold text-white"
type="button"
onClick={modal === 'add' ? handleAddMoney : handleWithdraw}
>
Continue
</button>
{notice && <p className="mt-3 text-sm text-ink/70">{notice}</p>}
</>
)}
</div>
</div>
)}
<div className="fixed bottom-4 left-1/2 z-40 flex w-[92%] max-w-md -translate-x-1/2 items-center justify-between rounded-full border border-ink/10 bg-white/95 px-4 py-3 text-[11px] font-semibold text-ink/70 shadow-xl lg:hidden">
<Link className="flex flex-col items-center gap-1 text-ocean" to="/dashboard">
<svg className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6">
<path d="M3 11l9-7 9 7" />
<path d="M5 10v9a1 1 0 0 0 1 1h4v-6h4v6h4a1 1 0 0 0 1-1v-9" />
</svg>
Home
</Link>
<button className="flex flex-col items-center gap-1" type="button">
<svg className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6">
<path d="M4 7h16a2 2 0 0 1 2 2v8a3 3 0 0 1-3 3H5a3 3 0 0 1-3-3V9a2 2 0 0 1 2-2Z" />
<path d="M9 7V5a3 3 0 0 1 6 0v2" />
</svg>
Wallet
</button>
<Link className="flex flex-col items-center gap-1" to="/circles">
<svg className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6">
<path d="M8 12l2 2 4-4" />
<path d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>
Circles
</Link>
<Link className="flex flex-col items-center gap-1" to="/profile">
<svg className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6">
<path d="M12 12a4 4 0 1 0-4-4 4 4 0 0 0 4 4Z" />
<path d="M4 20a8 8 0 0 1 16 0" />
</svg>
Account
</Link>
</div>
</div>
)
}