import { useEffect } from 'react' import { useQuery } from '@tanstack/react-query' import api from '#/services/api' import { STATUS_OPTIONS } from './StatusForm' const StatusList = () => { // Use React Query to fetch and cache statuses const { data, isPending, isError, error } = useQuery({ queryKey: ['statuses'], queryFn: async () => { const { data } = await api.getStatuses({ limit: 30 }) return data }, placeholderData: (previousData) => previousData, // Use previous data while refetching refetchInterval: 30e3, // Refetch every 30 seconds }) useEffect(() => { if (error) { console.error(error) } }, [error]) // Destructure data const statuses = data?.statuses || [] // Get a random emoji from the STATUS_OPTIONS array const randomEmoji = STATUS_OPTIONS[Math.floor(Math.random() * STATUS_OPTIONS.length)] if (isPending && !data) { return (
{randomEmoji}
Loading statuses...
) } if (isError) { return (
{(error as Error)?.message || 'Failed to load statuses'}
) } if (statuses.length === 0) { return (
No statuses yet.
) } // Helper to format dates const formatDate = (dateString: string) => { const date = new Date(dateString) const today = new Date() const isToday = date.getDate() === today.getDate() && date.getMonth() === today.getMonth() && date.getFullYear() === today.getFullYear() if (isToday) { return 'today' } else { return date.toLocaleDateString(undefined, { year: 'numeric', month: 'long', day: 'numeric', }) } } return (
{statuses.map((status) => { const handle = status.profile.handle || status.profile.did.substring(0, 15) + '...' const formattedDate = formatDate(status.createdAt) const isToday = formattedDate === 'today' return (
{status.status}
@{handle} {' '} {isToday ? ( is feeling{' '} {status.status}{' '} today ) : ( was feeling{' '} {status.status} on{' '} {formattedDate} )}
) })}
) } export default StatusList