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.

4.2 KB
Raw Blame
// Issue feed (comments + activity) reads and comment writes, wrapping
// lib/api/endpoints/activity.

import {
  useInfiniteQuery,
  useMutation,
  useQueries,
  useQuery,
  useQueryClient,
} from '@tanstack/react-query';
import {
  type FeedCursor,
  type FeedFilter,
  type FeedOrder,
  getFeedCounts,
  listFeed,
  listGroupedFeed,
  listTimeline,
  listTimelineItems,
  createComment,
  updateComment,
  deleteComment,
} from '@/lib/api/endpoints/activity';
import { qk } from '@/services/queryKeys';

// Which slice of the feed a query reads and in what order: the tab the activity log
// is on and the direction it runs.
export interface FeedSlice {
  filter: FeedFilter | null;
  order: FeedOrder;
}

// The issue's timeline (comments + activity), 25 items a page; getNextPageParam
// yields the server's cursor until it returns null.
export function useFeedQuery(id: number, { filter, order }: FeedSlice, enabled = true) {
  return useInfiniteQuery({
    queryKey: qk.flatFeed(id, filter, order),
    queryFn: ({ pageParam }) =>
      listFeed(id, { cursor: pageParam, limit: 25, filter: filter ?? undefined, order }),
    initialPageParam: null as FeedCursor | null,
    getNextPageParam: (last) => last.nextCursor,
    enabled,
  });
}

// The same entries as useFeedQuery, split by the status the issue was in when each
// was written. Paged the same way, and by the same cursor: a stretch that spans a page
// boundary comes back in both pages, and the feed joins the halves.
export function useGroupedFeedQuery(id: number, { filter, order }: FeedSlice, enabled = true) {
  return useInfiniteQuery({
    queryKey: qk.groupedFeed(id, filter, order),
    queryFn: ({ pageParam }) =>
      listGroupedFeed(id, { cursor: pageParam, limit: 25, filter: filter ?? undefined, order }),
    initialPageParam: null as FeedCursor | null,
    getNextPageParam: (last) => last.nextCursor,
    enabled,
  });
}

// The number of entries behind each tab of the activity log.
export function useFeedCountsQuery(id: number) {
  return useQuery({ queryKey: qk.feedCounts(id), queryFn: () => getFeedCounts(id) });
}

// The stretches the issue spent in one status, oldest first. Carries no entries,
// so it stays small however long the issue's history is.
export function useTimelineQuery(id: number) {
  return useQuery({ queryKey: qk.timeline(id), queryFn: () => listTimeline(id) });
}

// One time range of the issue's activity, as the timeline addresses its stretches.
export interface TimelineRange {
  from: string;
  to: string | null;
}

// The entries of the given stretches, merged in chronological order. A status may
// have been visited more than once, so a lane's popover asks for several ranges at
// once. Called from inside an open popover, which is what makes it a read on demand.
export function useTimelineItemsQuery(issueId: number, ranges: TimelineRange[]) {
  return useQueries({
    queries: ranges.map((range) => ({
      queryKey: qk.timelineItems(issueId, range.from, range.to),
      queryFn: () => listTimelineItems(issueId, range.from, range.to),
    })),
    combine: (results) => ({
      isPending: results.some((r) => r.isPending),
      isError: results.some((r) => r.isError),
      items: results.flatMap((r) => r.data ?? []),
    }),
  });
}

export function useCreateComment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      issueId,
      input,
    }: {
      issueId: number;
      input: { body: string; replyToId?: number };
    }) => createComment(issueId, input),
    onSuccess: (_data, { issueId }) => void qc.invalidateQueries({ queryKey: qk.feed(issueId) }),
  });
}

export function useUpdateComment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ commentId, body }: { issueId: number; commentId: number; body: string }) =>
      updateComment(commentId, { body }),
    onSuccess: (_data, { issueId }) => void qc.invalidateQueries({ queryKey: qk.feed(issueId) }),
  });
}

export function useDeleteComment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ commentId }: { issueId: number; commentId: number }) => deleteComment(commentId),
    onSuccess: (_data, { issueId }) => void qc.invalidateQueries({ queryKey: qk.feed(issueId) }),
  });
}