({
pathCursor,
sort,
sentAt,
direction,
total,
nextDate,
prevDate,
loadMore = false,
}: {
pathCursor?: string;
sort?: string;
sentAt?: string;
direction?: string;
loadMore?: boolean;
total: number;
prevDate: bigint | string;
nextDate: bigint | string;
})
| 4 | // TODO: Add unit test to this function |
| 5 | |
| 6 | export async function buildCursor({ |
| 7 | pathCursor, |
| 8 | sort, |
| 9 | sentAt, |
| 10 | direction, |
| 11 | total, |
| 12 | nextDate, |
| 13 | prevDate, |
| 14 | loadMore = false, |
| 15 | }: { |
| 16 | pathCursor?: string; |
| 17 | sort?: string; |
| 18 | sentAt?: string; |
| 19 | direction?: string; |
| 20 | loadMore?: boolean; |
| 21 | total: number; |
| 22 | prevDate: bigint | string; |
| 23 | nextDate: bigint | string; |
| 24 | }): Promise<{ |
| 25 | next: string | null; |
| 26 | prev: string | null; |
| 27 | }> { |
| 28 | const hasMore = total === PAGE_SIZE; |
| 29 | |
| 30 | // if empty, there is no cursor to return |
| 31 | if (!total) return { prev: null, next: null }; |
| 32 | |
| 33 | // load more |
| 34 | if (loadMore) { |
| 35 | if (sort === 'desc') { |
| 36 | return { prev: encodeCursor(`desc:lt:${prevDate}`), next: null }; |
| 37 | } else { |
| 38 | return { |
| 39 | prev: null, |
| 40 | next: encodeCursor(`asc:gt:${nextDate}`), |
| 41 | }; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // first page |
| 46 | if (sort === 'asc' && sentAt === '0') { |
| 47 | return { |
| 48 | prev: null, |
| 49 | next: hasMore ? encodeCursor(`asc:gt:${nextDate}`) : null, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | // back to channel |
| 54 | if (sort === 'asc' && direction === 'gte') { |
| 55 | return { |
| 56 | prev: encodeCursor(`desc:lt:${prevDate}`), |
| 57 | next: hasMore ? encodeCursor(`asc:gt:${nextDate}`) : null, |
| 58 | }; |
| 59 | } |
| 60 | // N page |
| 61 | if (!!pathCursor) { |
| 62 | return { |
| 63 | prev: encodeCursor(`desc:lt:${prevDate}`), |
no test coverage detected