mirror of
https://github.com/traberph/short.git
synced 2026-11-22 22:55:12 +00:00
Show which link pages use a short link
Page summaries include `usedOn` (the link pages with a button to the short link). The short link list shows it as a badge next to the path: "on /home" for one page, "on 2 pages" for several, with all paths in the tooltip. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+26
-3
@@ -147,7 +147,25 @@ function totalClicks(db: Db, pageIds: number[]): Map<number, number> {
|
|||||||
return new Map(rows.map((r) => [r.pageId, r.total]));
|
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<number, { id: number; path: string }[]> {
|
||||||
|
const result = new Map<number, { id: number; path: string }[]>();
|
||||||
|
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 {
|
return {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
namespace: row.namespace,
|
namespace: row.namespace,
|
||||||
@@ -158,6 +176,7 @@ function toSummary(row: PageRow, clicks: number, pinned: boolean): PageSummary {
|
|||||||
title: row.title,
|
title: row.title,
|
||||||
totalClicks: clicks,
|
totalClicks: clicks,
|
||||||
pinned,
|
pinned,
|
||||||
|
usedOn,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +190,11 @@ export function listPages(db: Db, actor: Actor): PageSummary[] {
|
|||||||
rows.map((r) => r.id),
|
rows.map((r) => r.id),
|
||||||
);
|
);
|
||||||
const pinned = getPinnedPageId(db);
|
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[] {
|
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 page = loadManagedPage(db, actor, id);
|
||||||
const clicks = totalClicks(db, [id]).get(id) ?? 0;
|
const clicks = totalClicks(db, [id]).get(id) ?? 0;
|
||||||
return {
|
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,
|
theme: (page.theme as Theme | null) ?? DEFAULT_THEME,
|
||||||
showLogos: page.showLogos,
|
showLogos: page.showLogos,
|
||||||
avatarUrl: avatarUrl(page.avatar),
|
avatarUrl: avatarUrl(page.avatar),
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ export interface PageSummary {
|
|||||||
title: string | null;
|
title: string | null;
|
||||||
totalClicks: number;
|
totalClicks: number;
|
||||||
pinned: boolean;
|
pinned: boolean;
|
||||||
|
/** Short links only: the link pages that have a button pointing to this link. */
|
||||||
|
usedOn: { id: number; path: string }[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Block {
|
export interface Block {
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ function Row({ page, showNamespace }: { page: PageSummary; showNamespace: boolea
|
|||||||
{page.path}
|
{page.path}
|
||||||
</Link>
|
</Link>
|
||||||
{showNamespace && page.namespace === "" && <Badge>Root</Badge>}
|
{showNamespace && page.namespace === "" && <Badge>Root</Badge>}
|
||||||
|
{page.usedOn.length > 0 && (
|
||||||
|
<span className="shrink-0" title={`Used on ${page.usedOn.map((u) => u.path).join(", ")}`}>
|
||||||
|
<Badge icon={<PanelsTopLeft />}>{page.usedOn.length === 1 ? `on ${page.usedOn[0]!.path}` : `on ${page.usedOn.length} pages`}</Badge>
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
{page.pinned && (
|
{page.pinned && (
|
||||||
<Badge tone="accent" icon={<Pin />}>
|
<Badge tone="accent" icon={<Pin />}>
|
||||||
Home
|
Home
|
||||||
|
|||||||
@@ -196,6 +196,24 @@ describe("link pages", () => {
|
|||||||
expect((await alice.api("PUT", `/pages/${page.id}/blocks/order`, { ids: [blocks[0].id] })).status).toBe(400);
|
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 () => {
|
it("creates a new short link while adding a link button", async () => {
|
||||||
const { app, user } = setup();
|
const { app, user } = setup();
|
||||||
const alice = user("alice");
|
const alice = user("alice");
|
||||||
|
|||||||
Reference in New Issue
Block a user