'use client' import { getGroupsAction } from '@/app/groups/actions' import { getRecentGroups } from '@/app/groups/recent-groups-helpers' import { Button } from '@/components/ui/button' import { getGroups } from '@/lib/api' import { Calendar, Loader2, Users } from 'lucide-react' import Link from 'next/link' import { useEffect, useState } from 'react' import { z } from 'zod' const recentGroupsSchema = z.array( z.object({ id: z.string().min(1), name: z.string(), }), ) type RecentGroups = z.infer type State = | { status: 'pending' } | { status: 'partial'; groups: RecentGroups } | { status: 'complete' groups: RecentGroups groupsDetails: Awaited> } type Props = { getGroupsAction: (groupIds: string[]) => ReturnType } export function RecentGroupList() { const [state, setState] = useState({ status: 'pending' }) useEffect(() => { const groupsInStorage = getRecentGroups() setState({ status: 'partial', groups: groupsInStorage }) getGroupsAction(groupsInStorage.map((g) => g.id)).then((groupsDetails) => { setState({ status: 'complete', groups: groupsInStorage, groupsDetails }) }) }, []) if (state.status === 'pending') { return (

Loading recent groups…

) } if (state.groups.length === 0) { return (

You have not visited any group recently.

{' '} or ask a friend to send you the link to an existing one.

) } return (
    {state.groups.map((group) => { const details = state.status === 'complete' ? state.groupsDetails.find((d) => d.id === group.id) : null return (
  • ) })}
) }