'use client' import { AddGroupByUrlButton } from '@/app/groups/add-group-by-url-button' import { RecentGroups, getArchivedGroups, getRecentGroups, getStarredGroups, } from '@/app/groups/recent-groups-helpers' import { Button } from '@/components/ui/button' import { getGroups } from '@/lib/api' import { trpc } from '@/trpc/client' import { AppRouterOutput } from '@/trpc/routers/_app' import { Loader2 } from 'lucide-react' import { useTranslations } from 'next-intl' import Link from 'next/link' import { PropsWithChildren, useEffect, useState } from 'react' import { RecentGroupListCard } from './recent-group-list-card' export type RecentGroupsState = | { status: 'pending' } | { status: 'partial' groups: RecentGroups starredGroups: string[] archivedGroups: string[] } | { status: 'complete' groups: RecentGroups groupsDetails: Awaited> starredGroups: string[] archivedGroups: string[] } function sortGroups({ groups, starredGroups, archivedGroups, }: { groups: RecentGroups starredGroups: string[] archivedGroups: string[] }) { const starredGroupInfo = [] const groupInfo = [] const archivedGroupInfo = [] for (const group of groups) { if (starredGroups.includes(group.id)) { starredGroupInfo.push(group) } else if (archivedGroups.includes(group.id)) { archivedGroupInfo.push(group) } else { groupInfo.push(group) } } return { starredGroupInfo, groupInfo, archivedGroupInfo, } } export function RecentGroupList() { const [state, setState] = useState({ status: 'pending' }) function loadGroups() { const groupsInStorage = getRecentGroups() const starredGroups = getStarredGroups() const archivedGroups = getArchivedGroups() setState({ status: 'partial', groups: groupsInStorage, starredGroups, archivedGroups, }) } useEffect(() => { loadGroups() }, []) if (state.status === 'pending') return null return ( loadGroups()} /> ) } function RecentGroupList_({ groups, starredGroups, archivedGroups, refreshGroupsFromStorage, }: { groups: RecentGroups starredGroups: string[] archivedGroups: string[] refreshGroupsFromStorage: () => void }) { const t = useTranslations('Groups') const { data, isLoading } = trpc.groups.list.useQuery({ groupIds: groups.map((group) => group.id), }) if (isLoading || !data) { return (

{' '} {t('loadingRecent')}

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

{t('NoRecent.description')}

{' '} {t('NoRecent.orAsk')}

) } const { starredGroupInfo, groupInfo, archivedGroupInfo } = sortGroups({ groups, starredGroups, archivedGroups, }) return ( {starredGroupInfo.length > 0 && ( <>

{t('starred')}

)} {groupInfo.length > 0 && ( <>

{t('recent')}

)} {archivedGroupInfo.length > 0 && ( <>

{t('archived')}

)}
) } function GroupList({ groups, groupDetails, starredGroups, archivedGroups, refreshGroupsFromStorage, }: { groups: RecentGroups groupDetails?: AppRouterOutput['groups']['list']['groups'] starredGroups: string[] archivedGroups: string[] refreshGroupsFromStorage: () => void }) { return (
    {groups.map((group) => ( groupDetail.id === group.id, )} isStarred={starredGroups.includes(group.id)} isArchived={archivedGroups.includes(group.id)} refreshGroupsFromStorage={refreshGroupsFromStorage} /> ))}
) } function GroupsPage({ children, reload, }: PropsWithChildren<{ reload: () => void }>) { const t = useTranslations('Groups') return ( <>

{t('myGroups')}

{children}
) }