twentyhq / twenty

Building a modern alternative to Salesforce, powered by the community.
https://twenty.com
GNU Affero General Public License v3.0
16.07k stars 1.82k forks source link

Add pagination to timeline #5634

Closed Weiko closed 1 month ago

greptile-apps[bot] commented 4 months ago

Disclaimer: This comment was automatically generated with AI. It can give some useful hints as to where to look for, but sometimes it can also give very inaccurate answers so takes this with a pinch of salt :).

To add pagination to the timeline:

  1. Update GraphQL Query: Modify getTimelineThreadsFromPersonId.ts to include pagination parameters (e.g., limit, offset).
const GET_TIMELINE_THREADS = gql`
  query GetTimelineThreads($personId: ID!, $limit: Int, $offset: Int) {
    timelineThreads(personId: $personId, limit: $limit, offset: $offset) {
      ...TimelineThreadFields
    }
  }
`;
  1. Update Service Methods: Modify timeline-messaging.service.ts to handle pagination parameters.
async getTimelineThreads(personId: string, limit: number, offset: number): Promise<TimelineThreadsWithTotal> {
  return this.timelineThreadsRepository.find({ where: { personId }, take: limit, skip: offset });
}
  1. Update Resolver: Modify timeline-messaging.resolver.ts to accept and pass pagination parameters.
@Query(() => TimelineThreadsWithTotal)
async timelineThreads(
  @Args('personId') personId: string,
  @Args('limit', { type: () => Int, nullable: true }) limit: number,
  @Args('offset', { type: () => Int, nullable: true }) offset: number
): Promise<TimelineThreadsWithTotal> {
  return this.timelineMessagingService.getTimelineThreads(personId, limit, offset);
}
  1. Update DTO: Ensure timeline-threads-with-total.dto.ts includes pagination fields.
@ObjectType()
export class TimelineThreadsWithTotal {
  @Field(() => [TimelineThread])
  threads: TimelineThread[];

  @Field()
  total: number;

  @Field()
  limit: number;

  @Field()
  offset: number;
}
  1. Update Frontend: Modify EmailThreads.tsx to fetch more records on pagination events.
const fetchMoreThreads = (limit, offset) => {
  fetchMore({
    variables: { limit, offset },
    updateQuery: (prev, { fetchMoreResult }) => {
      if (!fetchMoreResult) return prev;
      return Object.assign({}, prev, {
        timelineThreads: {
          ...fetchMoreResult.timelineThreads,
          threads: [...prev.timelineThreads.threads, ...fetchMoreResult.timelineThreads.threads],
        },
      });
    },
  });
};

References