diff --git a/src/server/lib/pages.ts b/src/server/lib/pages.ts index 579524e..751399f 100644 --- a/src/server/lib/pages.ts +++ b/src/server/lib/pages.ts @@ -147,7 +147,25 @@ function totalClicks(db: Db, pageIds: number[]): Map { return new Map(rows.map((r) => [r.pageId, r.total])); } -function toSummary(row: PageRow, clicks: number, pinned: boolean): PageSummary { +// For each short link, the link pages whose buttons point at it. +function linkPagesUsing(db: Db, targetIds: number[]): Map { + const result = new Map(); + if (targetIds.length === 0) return result; + const rows = db + .selectDistinct({ targetId: blocks.targetId, id: pages.id, namespace: pages.namespace, code: pages.code }) + .from(blocks) + .innerJoin(pages, eq(pages.id, blocks.pageId)) + .where(inArray(blocks.targetId, targetIds)) + .orderBy(asc(pages.namespace), asc(pages.code)) + .all(); + for (const r of rows) { + if (r.targetId === null) continue; + result.set(r.targetId, [...(result.get(r.targetId) ?? []), { id: r.id, path: pagePath(r) }]); + } + return result; +} + +function toSummary(row: PageRow, clicks: number, pinned: boolean, usedOn: { id: number; path: string }[] = []): PageSummary { return { id: row.id, namespace: row.namespace, @@ -158,6 +176,7 @@ function toSummary(row: PageRow, clicks: number, pinned: boolean): PageSummary { title: row.title, totalClicks: clicks, pinned, + usedOn, }; } @@ -171,7 +190,11 @@ export function listPages(db: Db, actor: Actor): PageSummary[] { rows.map((r) => r.id), ); const pinned = getPinnedPageId(db); - return rows.map((r) => toSummary(r, counts.get(r.id) ?? 0, r.id === pinned)); + const used = linkPagesUsing( + db, + rows.filter((r) => r.kind === "redirect").map((r) => r.id), + ); + return rows.map((r) => toSummary(r, counts.get(r.id) ?? 0, r.id === pinned, used.get(r.id))); } export function getBlocks(db: Db, pageId: number): Block[] { @@ -205,7 +228,7 @@ export function getPageDetail(db: Db, actor: Actor, id: number): PageDetail { const page = loadManagedPage(db, actor, id); const clicks = totalClicks(db, [id]).get(id) ?? 0; return { - ...toSummary(page, clicks, getPinnedPageId(db) === id), + ...toSummary(page, clicks, getPinnedPageId(db) === id, page.kind === "redirect" ? linkPagesUsing(db, [id]).get(id) : []), theme: (page.theme as Theme | null) ?? DEFAULT_THEME, showLogos: page.showLogos, avatarUrl: avatarUrl(page.avatar), diff --git a/src/shared/api-types.ts b/src/shared/api-types.ts index 936bb0b..4870975 100644 --- a/src/shared/api-types.ts +++ b/src/shared/api-types.ts @@ -22,6 +22,8 @@ export interface PageSummary { title: string | null; totalClicks: number; pinned: boolean; + /** Short links only: the link pages that have a button pointing to this link. */ + usedOn: { id: number; path: string }[]; } export interface Block { diff --git a/src/web/components/PageTable.tsx b/src/web/components/PageTable.tsx index 660dfa0..a60514d 100644 --- a/src/web/components/PageTable.tsx +++ b/src/web/components/PageTable.tsx @@ -24,6 +24,11 @@ function Row({ page, showNamespace }: { page: PageSummary; showNamespace: boolea {page.path} {showNamespace && page.namespace === "" && Root} + {page.usedOn.length > 0 && ( + u.path).join(", ")}`}> + }>{page.usedOn.length === 1 ? `on ${page.usedOn[0]!.path}` : `on ${page.usedOn.length} pages`} + + )} {page.pinned && ( }> Home diff --git a/test/api.test.ts b/test/api.test.ts index 869bedc..a84c82f 100644 --- a/test/api.test.ts +++ b/test/api.test.ts @@ -196,6 +196,24 @@ describe("link pages", () => { expect((await alice.api("PUT", `/pages/${page.id}/blocks/order`, { ids: [blocks[0].id] })).status).toBe(400); }); + it("lists the link pages a short link is used on", async () => { + const { user } = setup(); + const alice = user("alice"); + const link = await (await alice.api("POST", "/pages", { kind: "redirect", code: "gh", dest: "https://github.com" })).json(); + const unused = await (await alice.api("POST", "/pages", { kind: "redirect", code: "x", dest: "https://x.test" })).json(); + for (const code of ["b", "a"]) { + const page = await (await alice.api("POST", "/pages", { kind: "linkpage", code, title: code })).json(); + // Two buttons to the same link on one page still count as one page. + await alice.api("POST", `/pages/${page.id}/blocks`, { kind: "link", targetId: link.id, label: "GitHub" }); + await alice.api("POST", `/pages/${page.id}/blocks`, { kind: "link", targetId: link.id, label: "GitHub again" }); + } + + const list: { id: number; usedOn: { path: string }[] }[] = await (await alice.api("GET", "/pages")).json(); + expect(list.find((p) => p.id === link.id)!.usedOn.map((u) => u.path)).toEqual(["/alice/a", "/alice/b"]); + expect(list.find((p) => p.id === unused.id)!.usedOn).toEqual([]); + expect((await (await alice.api("GET", `/pages/${link.id}`)).json()).usedOn).toHaveLength(2); + }); + it("creates a new short link while adding a link button", async () => { const { app, user } = setup(); const alice = user("alice");