Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 73 additions & 32 deletions src/app/groups/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,94 @@
"use client";

import { useParams } from "next/navigation";
import { Navbar } from "@/components/Navbar";
import { MemberList } from "@/components/MemberList";
import { RoundProgress } from "@/components/RoundProgress";
import { ContributeModal } from "@/components/ContributeModal";
import { useGroupPolling } from "@/hooks/useGroupPolling";
import { useState } from "react";
import { formatAmount, GroupStatus } from "@sorosave/sdk";

// TODO: Fetch real data from contract
const MOCK_GROUP = {
id: 1,
name: "Lagos Savings Circle",
admin: "GABCDEFGHIJKLMNOPQRSTUVWXYZ234567ABCDEFG",
token: "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC",
contributionAmount: 1000000000n,
cycleLength: 604800,
maxMembers: 5,
members: [
"GABCDEFGHIJKLMNOPQRSTUVWXYZ234567ABCDEFG",
"GEFGHIJKLMNOPQRSTUVWXYZ234567ABCDEFGHIJ",
"GIJKLMNOPQRSTUVWXYZ234567ABCDEFGHIJKLMN",
],
payoutOrder: [
"GABCDEFGHIJKLMNOPQRSTUVWXYZ234567ABCDEFG",
"GEFGHIJKLMNOPQRSTUVWXYZ234567ABCDEFGHIJ",
"GIJKLMNOPQRSTUVWXYZ234567ABCDEFGHIJKLMN",
],
currentRound: 1,
totalRounds: 3,
status: GroupStatus.Active,
createdAt: 1700000000,
};

export default function GroupDetailPage() {
const params = useParams();
const groupId = params.id as string;
const { group, isLoading, error, lastUpdated, isPolling, refresh } =
useGroupPolling(groupId);
const [showContributeModal, setShowContributeModal] = useState(false);
const group = MOCK_GROUP;

if (isLoading && !group) {
return (
<>
<Navbar />
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
<div className="animate-pulse space-y-6">
<div className="h-8 bg-gray-200 rounded w-1/3" />
<div className="h-4 bg-gray-200 rounded w-1/4" />
<div className="grid lg:grid-cols-3 gap-6">
<div className="lg:col-span-2 space-y-6">
<div className="bg-white rounded-xl shadow-sm border p-6 h-40" />
<div className="bg-white rounded-xl shadow-sm border p-6 h-60" />
</div>
<div className="bg-white rounded-xl shadow-sm border p-6 h-60" />
</div>
</div>
</main>
</>
);
}

if (error && !group) {
return (
<>
<Navbar />
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
<div className="bg-red-50 border border-red-200 rounded-xl p-6 text-center">
<p className="text-red-700 font-medium">Failed to load group</p>
<p className="text-red-500 text-sm mt-1">{error}</p>
<button
onClick={refresh}
className="mt-4 px-4 py-2 bg-red-600 text-white rounded-lg text-sm hover:bg-red-700 transition-colors"
>
Retry
</button>
</div>
</main>
</>
);
}

if (!group) return null;

return (
<>
<Navbar />
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
<div className="mb-8">
<h1 className="text-2xl font-bold text-gray-900">{group.name}</h1>
<p className="text-gray-600 mt-1">
{formatAmount(group.contributionAmount)} tokens per cycle
</p>
<div className="mb-8 flex items-start justify-between">
<div>
<h1 className="text-2xl font-bold text-gray-900">{group.name}</h1>
<p className="text-gray-600 mt-1">
{formatAmount(group.contributionAmount)} tokens per cycle
</p>
</div>
{error && (
<div className="flex items-center space-x-2 text-amber-600 bg-amber-50 px-3 py-1.5 rounded-lg text-xs">
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 9v2m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<span>Using cached data — reconnecting...</span>
</div>
)}
</div>

<div className="grid lg:grid-cols-3 gap-6">
<div className="lg:col-span-2 space-y-6">
<RoundProgress
currentRound={group.currentRound}
totalRounds={group.totalRounds}
contributionsReceived={1}
contributionsReceived={group.contributionsReceived}
totalMembers={group.members.length}
isPolling={isPolling}
lastUpdated={lastUpdated}
/>

<MemberList
Expand Down Expand Up @@ -83,6 +118,12 @@ export default function GroupDetailPage() {
Join Group
</button>
)}
<button
onClick={refresh}
className="w-full border border-gray-300 text-gray-700 py-3 rounded-lg font-medium hover:bg-gray-50 transition-colors text-sm"
>
Refresh Now
</button>
</div>
</div>

Expand Down
35 changes: 32 additions & 3 deletions src/components/RoundProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,37 @@ interface RoundProgressProps {
totalRounds: number;
contributionsReceived: number;
totalMembers: number;
isPolling?: boolean;
lastUpdated?: Date | null;
}

export function RoundProgress({
currentRound,
totalRounds,
contributionsReceived,
totalMembers,
isPolling = false,
lastUpdated = null,
}: RoundProgressProps) {
const roundProgress = totalRounds > 0 ? (currentRound / totalRounds) * 100 : 0;
const contributionProgress =
totalMembers > 0 ? (contributionsReceived / totalMembers) * 100 : 0;
const roundComplete = contributionsReceived >= totalMembers;

return (
<div className="bg-white rounded-xl shadow-sm border p-6">
<h3 className="text-lg font-semibold text-gray-900 mb-4">Progress</h3>
<div className="flex items-center justify-between mb-4">
<h3 className="text-lg font-semibold text-gray-900">Progress</h3>
{isPolling && (
<div className="flex items-center space-x-2">
<span className="relative flex h-2.5 w-2.5">
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75" />
<span className="relative inline-flex rounded-full h-2.5 w-2.5 bg-green-500" />
</span>
<span className="text-xs text-gray-500">Live</span>
</div>
)}
</div>

<div className="space-y-4">
<div>
Expand All @@ -31,7 +47,7 @@ export function RoundProgress({
</div>
<div className="w-full bg-gray-200 rounded-full h-2">
<div
className="bg-primary-500 h-2 rounded-full transition-all"
className="bg-primary-500 h-2 rounded-full transition-all duration-500 ease-out"
style={{ width: `${roundProgress}%` }}
/>
</div>
Expand All @@ -46,12 +62,25 @@ export function RoundProgress({
</div>
<div className="w-full bg-gray-200 rounded-full h-2">
<div
className="bg-blue-500 h-2 rounded-full transition-all"
className={`h-2 rounded-full transition-all duration-500 ease-out ${
roundComplete ? "bg-green-500" : "bg-blue-500"
}`}
style={{ width: `${contributionProgress}%` }}
/>
</div>
{roundComplete && (
<p className="text-xs text-green-600 mt-1 font-medium">
All contributions received — payout ready
</p>
)}
</div>
</div>

{lastUpdated && (
<p className="text-xs text-gray-400 mt-3 text-right">
Updated {lastUpdated.toLocaleTimeString()}
</p>
)}
</div>
);
}
92 changes: 92 additions & 0 deletions src/hooks/useGroupPolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"use client";

import { useState, useEffect, useCallback, useRef } from "react";
import { sorosaveClient } from "@/lib/sorosave";
import { GroupStatus } from "@sorosave/sdk";

const POLL_INTERVAL = 10_000;

export interface GroupData {
id: number;
name: string;
admin: string;
token: string;
contributionAmount: bigint;
cycleLength: number;
maxMembers: number;
members: string[];
payoutOrder: string[];
currentRound: number;
totalRounds: number;
status: GroupStatus;
createdAt: number;
contributionsReceived: number;
}

interface UseGroupPollingResult {
group: GroupData | null;
isLoading: boolean;
error: string | null;
lastUpdated: Date | null;
isPolling: boolean;
refresh: () => Promise<void>;
}

export function useGroupPolling(groupId: string): UseGroupPollingResult {
const [group, setGroup] = useState<GroupData | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
const [isPolling, setIsPolling] = useState(false);
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
const mountedRef = useRef(true);

const fetchGroupData = useCallback(async () => {
try {
const data = await sorosaveClient.getGroup(Number(groupId));
if (!mountedRef.current) return;
setGroup(data as GroupData);
setLastUpdated(new Date());
setError(null);
} catch (err) {
if (!mountedRef.current) return;
setError(err instanceof Error ? err.message : "Failed to fetch group data");
}
}, [groupId]);

const refresh = useCallback(async () => {
setIsLoading(true);
await fetchGroupData();
setIsLoading(false);
}, [fetchGroupData]);

// Initial fetch + cleanup
useEffect(() => {
mountedRef.current = true;
setIsLoading(true);
fetchGroupData().finally(() => {
if (mountedRef.current) setIsLoading(false);
});
return () => { mountedRef.current = false; };
}, [fetchGroupData]);

// Polling lifecycle: active/forming = poll, completed/failed = stop
useEffect(() => {
if (!group) return;
const shouldPoll = group.status === GroupStatus.Active || group.status === GroupStatus.Forming;
if (!shouldPoll) {
setIsPolling(false);
return;
}
setIsPolling(true);
intervalRef.current = setInterval(fetchGroupData, POLL_INTERVAL);
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
};
}, [group?.status, group?.currentRound, fetchGroupData]);

return { group, isLoading, error, lastUpdated, isPolling, refresh };
}