Weird stopping point but whatever

This commit is contained in:
2025-09-05 16:07:38 -05:00
parent 5ca78bd92c
commit 3032d76e43
6 changed files with 101 additions and 43 deletions

View File

@@ -26,6 +26,12 @@ type StatusRow = {
} | null,
};
type Paginated<T> = {
page: T[],
isDone: boolean,
continueCursor: string | null,
};
// CHANGED: typed helpers
const ensureUser = async (ctx: RWCtx, userId: Id<'users'>) => {
const user = await ctx.db.get(userId);
@@ -228,35 +234,76 @@ export const getCurrentForAll = query({
});
/**
* Paginated history for a specific user (newest first).
* Paginated history for all users or for a specific user.
*/
export const listHistoryByUser = query({
export const listHistory = query({
args: {
userId: v.id('users'),
userId: v.optional(v.id('users')),
paginationOpts: paginationOptsValidator,
},
handler: async (ctx, { userId, paginationOpts }) => {
await ensureUser(ctx, userId);
handler: async (ctx, { userId, paginationOpts }): Promise<
Paginated<StatusRow>
> => {
// Query statuses newest-first, optionally filtered by user
const result = userId
? await ctx.db
.query('statuses')
.withIndex('by_user_updatedAt', (q) => q.eq('userId', userId))
.order('desc')
.paginate(paginationOpts)
: await ctx.db
.query('statuses')
.order('desc')
.paginate(paginationOpts);
return await ctx.db
.query('statuses')
.withIndex('by_user_updatedAt', (q) => q.eq('userId', userId))
.order('desc')
.paginate(paginationOpts);
// Cache user display objects to avoid refetching repeatedly
const displayCache = new Map<string, StatusRow['user']>();
const getDisplay = async (
uid: Id<'users'>,
): Promise<StatusRow['user']> => {
const key = uid as unknown as string;
const cached = displayCache.get(key);
if (cached) return cached;
const user = await ctx.db.get(uid);
if (!user) throw new ConvexError('User not found.');
const imgId = getImageId(user);
const imgUrl = imgId ? await ctx.storage.getUrl(imgId) : null;
const display: StatusRow['user'] = {
id: user._id,
name: getName(user),
imageUrl: imgUrl,
};
displayCache.set(key, display);
return display;
};
const page: StatusRow[] = [];
for (const s of result.page) {
const owner = await getDisplay(s.userId);
const updatedBy =
s.updatedBy !== s.userId ? await getDisplay(s.updatedBy) : null;
page.push({
user: owner,
status: {
id: s._id,
message: s.message,
updatedAt: s.updatedAt,
updatedBy,
},
});
}
return {
page,
isDone: result.isDone,
continueCursor: result.continueCursor,
};
},
});
/**
* Global paginated history (all users, newest first).
* - Add an index on updatedAt if you want to avoid full-table scans
* when the collection grows large.
*/
export const listHistoryAll = query({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, { paginationOpts }) => {
return await ctx.db
.query('statuses')
.order('desc')
.paginate(paginationOpts);
},
});