Skip to content

Commit 5521c55

Browse files
committed
fix(ui): Added a total event count to the profile loader on public profiles. Before, the number of hosted events was only shown when there was no pagination (few events) and kept hidden when there was pagination (many events).
1 parent 02d94f8 commit 5521c55

4 files changed

Lines changed: 59 additions & 16 deletions

File tree

apps/backend/src/user/user.service.ts

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -898,19 +898,45 @@ export class UserService {
898898
}
899899
}
900900

901-
const events = await this.prisma.event.findMany({
902-
where: {
903-
authorId: userId,
904-
isPublished: true,
905-
isPublic: true,
906-
...cursorFilter,
907-
},
908-
include: {
909-
location: true,
910-
},
911-
orderBy: [{ startAt: 'desc' }, { id: 'desc' }],
912-
take: limit + 1,
913-
});
901+
const pageWhere: Prisma.EventWhereInput = {
902+
authorId: userId,
903+
isPublished: true,
904+
isPublic: true,
905+
...cursorFilter,
906+
};
907+
const countWhere: Prisma.EventWhereInput = {
908+
authorId: userId,
909+
isPublished: true,
910+
isPublic: true,
911+
};
912+
913+
let events;
914+
let hostedPublicTotal: number | undefined;
915+
916+
if (!cursor) {
917+
const [page, total] = await Promise.all([
918+
this.prisma.event.findMany({
919+
where: pageWhere,
920+
include: {
921+
location: true,
922+
},
923+
orderBy: [{ startAt: 'desc' }, { id: 'desc' }],
924+
take: limit + 1,
925+
}),
926+
this.prisma.event.count({ where: countWhere }),
927+
]);
928+
events = page;
929+
hostedPublicTotal = total;
930+
} else {
931+
events = await this.prisma.event.findMany({
932+
where: pageWhere,
933+
include: {
934+
location: true,
935+
},
936+
orderBy: [{ startAt: 'desc' }, { id: 'desc' }],
937+
take: limit + 1,
938+
});
939+
}
914940

915941
const hasMore = events.length > limit;
916942
const sliced = hasMore ? events.slice(0, limit) : events;
@@ -919,6 +945,15 @@ export class UserService {
919945
? Buffer.from(`${last.startAt.toISOString()}|${String(last.id)}`).toString('base64')
920946
: null;
921947

948+
const pagination: {
949+
nextCursor: string | null;
950+
hasMore: boolean;
951+
total?: number;
952+
} = { nextCursor, hasMore };
953+
if (hostedPublicTotal !== undefined) {
954+
pagination.total = hostedPublicTotal;
955+
}
956+
922957
return {
923958
data: sliced.map((event) => ({
924959
id: event.id,
@@ -928,7 +963,7 @@ export class UserService {
928963
imageKey: event.imageKey,
929964
location: event.location,
930965
})),
931-
pagination: { nextCursor, hasMore },
966+
pagination,
932967
};
933968
}
934969
}

apps/frontend/src/hooks/useInfiniteScroll.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ interface PaginatedItem {
77
export interface Pagination {
88
nextCursor: string | null;
99
hasMore: boolean;
10+
/** Set on first page when API returns total (e.g. hosted events count). */
11+
total?: number;
1012
}
1113

1214
/**

apps/frontend/src/pages/public-profile/components/ProfileTabs.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ export function ProfileTabs({
3939
}
4040
);
4141

42+
const eventsTabCount = initialPagination.total ?? events.length;
43+
4244
return (
4345
<Tabs defaultValue="info" className="w-full" onValueChange={setActiveTab}>
4446
<TabsList variant="brutalist" className="w-full md:w-auto">
4547
<TabsTrigger value="info" variant="brutalist" className="text-xs md:text-sm">
4648
Info
4749
</TabsTrigger>
4850
<TabsTrigger value="events" variant="brutalist" className="text-xs md:text-sm">
49-
Events {initialPagination.hasMore ? '' : `(${events.length})`}
51+
Events ({eventsTabCount})
5052
</TabsTrigger>
5153
</TabsList>
5254

packages/schema/src/user.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ export type ResUserPublicEvents = z.infer<typeof ResUserPublicEventsSchema>;
102102

103103
export const ResUserPublicEventsPaginatedSchema = z.object({
104104
data: ResUserPublicEventsSchema,
105-
pagination: z.object({ nextCursor: z.string().nullable(), hasMore: z.boolean() }),
105+
pagination: z.object({
106+
nextCursor: z.string().nullable(),
107+
hasMore: z.boolean(),
108+
total: z.number().int().nonnegative().optional(),
109+
}),
106110
});
107111
export type ResUserPublicEventsPaginated = z.infer<typeof ResUserPublicEventsPaginatedSchema>;
108112

0 commit comments

Comments
 (0)