Skip to content

Commit 5039c0e

Browse files
committed
Paginating back/forwards fix
1 parent b0cc9de commit 5039c0e

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

apps/webapp/app/services/runsRepository.server.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class RunsRepository {
124124
} else {
125125
queryBuilder
126126
.where("run_id > {runId: String}", { runId: options.page.cursor })
127-
.orderBy("created_at DESC, run_id DESC")
127+
.orderBy("created_at ASC, run_id ASC")
128128
.limit(options.page.size + 1);
129129
}
130130
} else {
@@ -147,38 +147,33 @@ export class RunsRepository {
147147
let previousCursor: string | null = null;
148148

149149
//get cursors for next and previous pages
150-
if (options.page.cursor) {
151-
switch (options.page.direction) {
152-
case "forward":
153-
previousCursor = runIds.at(0) ?? null;
154-
if (hasMore) {
155-
// The next cursor should be the last run ID from this page
156-
nextCursor = runIds[options.page.size - 1];
157-
}
158-
break;
159-
case "backward":
160-
// No need to reverse since we're using DESC ordering consistently
161-
if (hasMore) {
162-
previousCursor = runIds[options.page.size - 1];
163-
}
164-
nextCursor = runIds.at(0) ?? null;
165-
break;
166-
default:
167-
// This shouldn't happen if cursor is provided, but handle it
168-
if (hasMore) {
169-
nextCursor = runIds[options.page.size - 1];
170-
}
171-
break;
150+
const direction = options.page.direction ?? "forward";
151+
switch (direction) {
152+
case "forward": {
153+
previousCursor = options.page.cursor ? runIds.at(0) ?? null : null;
154+
if (hasMore) {
155+
// The next cursor should be the last run ID from this page
156+
nextCursor = runIds[options.page.size - 1];
157+
}
158+
break;
172159
}
173-
} else {
174-
// Initial page - no cursor
175-
if (hasMore) {
176-
// The next cursor should be the last run ID from this page
177-
nextCursor = runIds[options.page.size - 1];
160+
case "backward": {
161+
const reversedRunIds = [...runIds].reverse();
162+
if (hasMore) {
163+
previousCursor = reversedRunIds.at(1) ?? null;
164+
nextCursor = reversedRunIds.at(options.page.size) ?? null;
165+
} else {
166+
nextCursor = reversedRunIds.at(options.page.size - 1) ?? null;
167+
}
168+
169+
break;
178170
}
179171
}
180172

181-
const runIdsToReturn = hasMore ? runIds.slice(0, -1) : runIds;
173+
const runIdsToReturn =
174+
options.page.direction === "backward" && hasMore
175+
? runIds.slice(1, options.page.size + 1)
176+
: runIds.slice(0, options.page.size);
182177

183178
const runs = await this.options.prisma.taskRun.findMany({
184179
where: {

0 commit comments

Comments
 (0)