import { decodeIsoDateCursor, encodeIsoDateCursor } from '@/modules/shared/helpers/graphqlHelper' type Collection = { cursor: string | null totalCount: number items: T[] } type GetPaginatedItemsArgs = { limit: number cursor?: string } export const getPaginatedItemsFactory = ({ getItems, getTotalCount }: { getItems: (args: TArgs) => Promise getTotalCount: (args: Omit) => Promise }) => async (args: TArgs): Promise> => { const maybeDecodedCursor = args.cursor ? decodeIsoDateCursor(args.cursor) : null const items = await getItems({ ...args, cursor: maybeDecodedCursor ?? undefined }) const totalCount = await getTotalCount(args) let cursor = null if (items.length === args.limit) { const lastItem = items.at(-1) cursor = lastItem ? encodeIsoDateCursor(lastItem.createdAt) : null } return { items, cursor, totalCount } }