Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(web): use lightweight bookmark counts on the lists page

Mohamed Bassem2d7cc4bdc644
diff --git a/apps/web/app/dashboard/lists/page.tsx b/apps/web/app/dashboard/lists/page.tsx
index 004a2064..3ff7b594 100644
--- a/apps/web/app/dashboard/lists/page.tsx
+++ b/apps/web/app/dashboard/lists/page.tsx
@@ -9,8 +9,10 @@ import { Plus } from "lucide-react";
export default async function ListsPage() {
// oxlint-disable-next-line rules-of-hooks
const { t } = await useTranslation();
- const lists = await api.lists.list();
- const stats = await api.users.stats();
+ const [lists, stats] = await Promise.all([
+ api.lists.list(),
+ api.users.bookmarkCounts(),
+ ]);
return (
<div className="flex flex-col gap-8">
diff --git a/packages/shared/types/users.ts b/packages/shared/types/users.ts
index 1ee5a281..530af047 100644
--- a/packages/shared/types/users.ts
+++ b/packages/shared/types/users.ts
@@ -65,6 +65,12 @@ export const zWhoAmIResponseSchema = z.object({
localUser: z.boolean(),
});
+export const zUserBookmarkCountsResponseSchema = z.object({
+ numBookmarks: z.number(),
+ numFavorites: z.number(),
+ numArchived: z.number(),
+});
+
export const zUserStatsResponseSchema = z.object({
numBookmarks: z.number(),
numFavorites: z.number(),
diff --git a/packages/trpc/models/users.ts b/packages/trpc/models/users.ts
index 27350b4d..77e545dc 100644
--- a/packages/trpc/models/users.ts
+++ b/packages/trpc/models/users.ts
@@ -22,6 +22,7 @@ import serverConfig from "@karakeep/shared/config";
import {
zUpdateUserSettingsSchema,
zUserSettingsSchema,
+ zUserBookmarkCountsResponseSchema,
zUserStatsResponseSchema,
zWhoAmIResponseSchema,
zWrappedStatsResponseSchema,
@@ -381,6 +382,20 @@ export class User {
}).catch(() => ({}));
}
+ async getBookmarkCounts(): Promise<
+ z.infer<typeof zUserBookmarkCountsResponseSchema>
+ > {
+ const [res] = await this.ctx.db
+ .select({
+ numBookmarks: count(),
+ numFavorites: sql<number>`COALESCE(SUM(CASE WHEN ${bookmarks.favourited} THEN 1 ELSE 0 END), 0)`,
+ numArchived: sql<number>`COALESCE(SUM(CASE WHEN ${bookmarks.archived} THEN 1 ELSE 0 END), 0)`,
+ })
+ .from(bookmarks)
+ .where(eq(bookmarks.userId, this.user.id));
+ return res;
+ }
+
async getStats(): Promise<z.infer<typeof zUserStatsResponseSchema>> {
const userObj = await this.ctx.db.query.users.findFirst({
where: eq(users.id, this.user.id),
diff --git a/packages/trpc/routers/users.test.ts b/packages/trpc/routers/users.test.ts
index 786ba54e..1be603cd 100644
--- a/packages/trpc/routers/users.test.ts
+++ b/packages/trpc/routers/users.test.ts
@@ -609,6 +609,59 @@ describe("User Routes", () => {
).toBe(true);
});
+ test<CustomTestContext>("bookmark counts", async ({ db }) => {
+ const user1 = await createTestUser(db, {
+ name: "User 1",
+ email: "[email protected]",
+ password: "pass1234",
+ });
+ const user2 = await createTestUser(db, {
+ name: "User 2",
+ email: "[email protected]",
+ password: "pass1234",
+ });
+ const caller = getApiCaller(db, user1.id);
+
+ expect(await caller.users.bookmarkCounts()).toEqual({
+ numBookmarks: 0,
+ numFavorites: 0,
+ numArchived: 0,
+ });
+
+ await db.insert(bookmarks).values([
+ {
+ userId: user1.id,
+ type: BookmarkTypes.LINK,
+ archived: false,
+ favourited: true,
+ },
+ {
+ userId: user1.id,
+ type: BookmarkTypes.LINK,
+ archived: true,
+ favourited: true,
+ },
+ {
+ userId: user1.id,
+ type: BookmarkTypes.LINK,
+ archived: false,
+ favourited: false,
+ },
+ {
+ userId: user2.id,
+ type: BookmarkTypes.LINK,
+ archived: true,
+ favourited: true,
+ },
+ ]);
+
+ expect(await caller.users.bookmarkCounts()).toEqual({
+ numBookmarks: 3,
+ numFavorites: 2,
+ numArchived: 1,
+ });
+ });
+
describe("Delete Account", () => {
test<CustomTestContext>("deleteAccount - with password", async ({ db }) => {
const user = await createTestUser(db, {
diff --git a/packages/trpc/routers/users.ts b/packages/trpc/routers/users.ts
index 956d98ce..42e015d5 100644
--- a/packages/trpc/routers/users.ts
+++ b/packages/trpc/routers/users.ts
@@ -6,6 +6,7 @@ import { zAdminUserSchema } from "@karakeep/shared/types/admin";
import {
zUpdateUserSettingsSchema,
zUserSettingsSchema,
+ zUserBookmarkCountsResponseSchema,
zUserStatsResponseSchema,
zWhoAmIResponseSchema,
zWrappedStatsResponseSchema,
@@ -76,6 +77,12 @@ export const usersAppRouter = router({
const user = await User.fromCtx(ctx);
return await user.asWhoAmI();
}),
+ bookmarkCounts: usersProcedure
+ .output(zUserBookmarkCountsResponseSchema)
+ .query(async ({ ctx }) => {
+ const user = await User.fromCtx(ctx);
+ return await user.getBookmarkCounts();
+ }),
stats: usersProcedure
.output(zUserStatsResponseSchema)
.query(async ({ ctx }) => {