Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

2.4 KB
Raw Blame
'use client';

import { useEffect, useRef } from 'react';
import { useTranslations } from 'next-intl';
import type { Notification } from '@/lib/api/endpoints/notifications';
import ListSkeleton from '@/components/common/skeleton/ListSkeleton';
import InboxListItem from './InboxListItem';

// The scrollable notification list. Empty and loading states render in place. New
// pages load automatically when the bottom sentinel scrolls into view; selection
// and paging are owned by the page.
export default function InboxList({
  items,
  isLoading,
  selectedId,
  onSelect,
  onToggleRead,
  onSnooze,
  onDelete,
  hasNextPage,
  isFetchingNextPage,
  onLoadMore,
}: {
  items: Notification[];
  isLoading: boolean;
  selectedId: number | null;
  onSelect: (n: Notification) => void;
  onToggleRead: (n: Notification, read: boolean) => void;
  onSnooze: (n: Notification, until: string | null) => void;
  onDelete: (n: Notification) => void;
  hasNextPage: boolean;
  isFetchingNextPage: boolean;
  onLoadMore: () => void;
}) {
  const t = useTranslations('inbox');
  const sentinelRef = useRef<HTMLDivElement | null>(null);

  useEffect(() => {
    const el = sentinelRef.current;
    if (!el || !hasNextPage) return;
    const observer = new IntersectionObserver(
      (entries) => {
        if (entries[0].isIntersecting && !isFetchingNextPage) onLoadMore();
      },
      // Start loading a bit before the sentinel is fully visible.
      { rootMargin: '200px' },
    );
    observer.observe(el);
    return () => observer.disconnect();
  }, [hasNextPage, isFetchingNextPage, onLoadMore]);

  if (isLoading) return <ListSkeleton rows={6} className="p-3" rowClassName="h-16" />;
  if (items.length === 0) {
    return (
      <div className="flex h-full items-center justify-center px-6 text-center text-sm text-muted-foreground">
        {t('empty')}
      </div>
    );
  }
  return (
    <div className="min-h-0 flex-1 overflow-y-auto">
      {items.map((n) => (
        <InboxListItem
          key={n.id}
          notification={n}
          selected={n.id === selectedId}
          onSelect={() => onSelect(n)}
          onToggleRead={(read) => onToggleRead(n, read)}
          onSnooze={(until) => onSnooze(n, until)}
          onDelete={() => onDelete(n)}
        />
      ))}
      {hasNextPage && <div ref={sentinelRef} className="h-px" />}
      {isFetchingNextPage && <ListSkeleton rows={2} className="p-3" rowClassName="h-16" />}
    </div>
  );
}