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.7 KB
Raw Blame
import type { Column } from '@/lib/api/endpoints/columns';
import type { FeedGroup, GroupedFeedPage } from '@/lib/api/endpoints/activity';
import ShowMoreButton from '@/components/common/ShowMoreButton';
import ListSkeleton from '@/components/common/skeleton/ListSkeleton';
import { type FeedSlice, useGroupedFeedQuery } from '../../services/comments.service';
import { statusColor } from '../../utils/timeline';
import IssueActivityGroup from './IssueActivityGroup';
import { type ComposerContext } from './CommentComposer';

// The activity log split by the status the issue was in when each entry was written,
// in the slice's order and paged 25 at a time like the flat one. The server does the
// splitting; this only joins a stretch whose entries fell into two pages.

export default function IssueGroupedFeed({
  issueId,
  slice,
  columns,
  imageByUserId,
  composer,
  emptyText,
}: {
  issueId: number;
  slice: FeedSlice;
  // The project's columns, for the status colors.
  columns: Column[];
  imageByUserId: Map<string, string | null>;
  composer: ComposerContext;
  emptyText: string;
}) {
  const feedQuery = useGroupedFeedQuery(issueId, slice);
  const groups = joinGroups(feedQuery.data?.pages ?? []);

  if (feedQuery.isLoading) return <ListSkeleton rows={3} rowClassName="h-12" />;

  if (groups.length === 0) {
    return <p className="text-sm text-muted-foreground">{emptyText}</p>;
  }

  return (
    <>
      <ol className="flex flex-col gap-5">
        {groups.map((group) => (
          <IssueActivityGroup
            key={group.from}
            group={group}
            color={statusColor(columns, group.status)}
            imageByUserId={imageByUserId}
            composer={composer}
          />
        ))}
      </ol>
      {feedQuery.hasNextPage && (
        <ShowMoreButton
          loading={feedQuery.isFetchingNextPage}
          onClick={() => void feedQuery.fetchNextPage()}
        />
      )}
    </>
  );
}

// The stretch a page ends in continues at the top of the next one, so groups are
// joined by their start. Entries are deduped by id for the same reason IssueFeedList
// does it: a boundary entry can shift between pages after a refetch.
function joinGroups(pages: GroupedFeedPage[]): FeedGroup[] {
  const byStart = new Map<string, FeedGroup>();
  const seen = new Set<number>();
  const groups: FeedGroup[] = [];
  for (const group of pages.flatMap((page) => page.groups)) {
    let joined = byStart.get(group.from);
    if (!joined) {
      joined = { ...group, items: [] };
      byStart.set(group.from, joined);
      groups.push(joined);
    }
    for (const item of group.items) {
      if (seen.has(item.id)) continue;
      seen.add(item.id);
      joined.items.push(item);
    }
  }
  return groups;
}