@octarq/plugin-links 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/api.ts +223 -0
- package/components/BotToggle.tsx +18 -0
- package/filters.test.ts +68 -0
- package/filters.ts +29 -0
- package/i18n.ts +730 -0
- package/index.ts +20 -0
- package/instanceScope.test.ts +45 -0
- package/package.json +26 -3
- package/pages/LinkEditorForm.test.tsx +592 -0
- package/pages/LinkEditorForm.tsx +458 -0
- package/pages/LinkModals.tsx +138 -0
- package/pages/LinkTableRow.tsx +183 -0
- package/pages/StatsView.tsx +119 -0
- package/pages/emptyState.test.tsx +86 -0
- package/pages/failureState.test.tsx +75 -0
- package/pages/index.tsx +462 -0
- package/pages/linkPassword.test.tsx +102 -0
- package/pages/linksTitlePriority.test.tsx +188 -0
- package/pages/tabs.test.tsx +92 -0
- package/store.ts +21 -0
- package/tsconfig.json +15 -0
- package/widgets/ClicksChartWidget.tsx +36 -0
- package/widgets/DevicesPanelWidget.tsx +18 -0
- package/widgets/LinksSetupStepWidget.tsx +27 -0
- package/widgets/LinksStatCardWidget.tsx +42 -0
- package/widgets/TopCitiesPanelWidget.tsx +31 -0
- package/widgets/TopLinksPanelWidget.test.tsx +77 -0
- package/widgets/TopLinksPanelWidget.tsx +67 -0
- package/README.md +0 -3
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
3
|
+
import { cleanup, render, screen, waitFor, fireEvent } from "@testing-library/react";
|
|
4
|
+
import { MemoryRouter } from "react-router-dom";
|
|
5
|
+
import { registerUIPlugin, resetRegistry, I18nProvider } from "@octarq/plugin-sdk";
|
|
6
|
+
import linksPlugin from "../index";
|
|
7
|
+
import LinksPage from "./index";
|
|
8
|
+
import { Link, LinkStats } from "../api";
|
|
9
|
+
|
|
10
|
+
const mockLinks: Link[] = [
|
|
11
|
+
{
|
|
12
|
+
id: 1,
|
|
13
|
+
host: "link.acme.com",
|
|
14
|
+
slug: "summer-sale",
|
|
15
|
+
title: "Summer Sale 2026",
|
|
16
|
+
tags: "ecommerce, promo, vip",
|
|
17
|
+
target: "https://acme.com/summer-sale",
|
|
18
|
+
note: "Summer promo link",
|
|
19
|
+
expiresAt: null,
|
|
20
|
+
expiredUrl: "",
|
|
21
|
+
clickLimit: 0,
|
|
22
|
+
archived: false,
|
|
23
|
+
enabled: true,
|
|
24
|
+
clicks: 88,
|
|
25
|
+
hasPassword: false,
|
|
26
|
+
routingRules: [],
|
|
27
|
+
createdAt: "2026-06-01T00:00:00Z",
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
id: 2,
|
|
31
|
+
host: "",
|
|
32
|
+
slug: "random-slug-without-title",
|
|
33
|
+
title: "",
|
|
34
|
+
tags: "",
|
|
35
|
+
target: "https://acme.com/about",
|
|
36
|
+
note: "",
|
|
37
|
+
expiresAt: null,
|
|
38
|
+
expiredUrl: "",
|
|
39
|
+
clickLimit: 0,
|
|
40
|
+
archived: false,
|
|
41
|
+
enabled: true,
|
|
42
|
+
clicks: 12,
|
|
43
|
+
hasPassword: false,
|
|
44
|
+
routingRules: [],
|
|
45
|
+
createdAt: "2026-06-02T00:00:00Z",
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
const mockStats: LinkStats = {
|
|
50
|
+
total: 88,
|
|
51
|
+
windowed: 40,
|
|
52
|
+
days: 30,
|
|
53
|
+
metric: "uv",
|
|
54
|
+
series: [{ key: "2026-06-01", count: 10 }],
|
|
55
|
+
referers: [],
|
|
56
|
+
channels: [],
|
|
57
|
+
countries: [],
|
|
58
|
+
regions: [],
|
|
59
|
+
devices: [],
|
|
60
|
+
browsers: [],
|
|
61
|
+
utmSources: [],
|
|
62
|
+
utmMediums: [],
|
|
63
|
+
utmCampaigns: [],
|
|
64
|
+
variants: [],
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
function jsonResponse(body: unknown, status = 200) {
|
|
68
|
+
return {
|
|
69
|
+
ok: status >= 200 && status < 300,
|
|
70
|
+
status,
|
|
71
|
+
json: async () => body,
|
|
72
|
+
headers: { get: (name: string) => (name === "content-type" ? "application/json" : null) },
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
describe("LinksPage title priority and tag badges", () => {
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
cleanup();
|
|
79
|
+
resetRegistry();
|
|
80
|
+
registerUIPlugin(linksPlugin);
|
|
81
|
+
|
|
82
|
+
const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
83
|
+
const method = (init?.method ?? "GET").toUpperCase();
|
|
84
|
+
const rawUrl = typeof input === "string" ? input : input instanceof URL ? input.toString() : (input as Request).url;
|
|
85
|
+
const urlObj = new URL(rawUrl, "http://localhost");
|
|
86
|
+
const path = urlObj.pathname;
|
|
87
|
+
if (method === "GET" && path === "/api/links") return jsonResponse(mockLinks);
|
|
88
|
+
if (method === "GET" && path.startsWith("/api/links/1/stats")) return jsonResponse(mockStats);
|
|
89
|
+
if (method === "GET" && path.startsWith("/api/links/2/stats")) return jsonResponse(mockStats);
|
|
90
|
+
if (method === "GET" && path === "/api/domains") return jsonResponse([]);
|
|
91
|
+
throw new Error(`unexpected request in test: ${method} ${rawUrl}`);
|
|
92
|
+
});
|
|
93
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("renders title as primary, slug as secondary, and tags split into individual badges", async () => {
|
|
97
|
+
render(
|
|
98
|
+
<MemoryRouter initialEntries={["/links"]}>
|
|
99
|
+
<I18nProvider>
|
|
100
|
+
<LinksPage />
|
|
101
|
+
</I18nProvider>
|
|
102
|
+
</MemoryRouter>
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// Wait for links to load
|
|
106
|
+
await waitFor(() => {
|
|
107
|
+
expect(screen.getByText("Summer Sale 2026")).toBeDefined();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Primary title has font-medium and text-foreground
|
|
111
|
+
const title = screen.getByText("Summer Sale 2026");
|
|
112
|
+
expect(title.className).toContain("font-medium");
|
|
113
|
+
expect(title.className).toContain("text-foreground");
|
|
114
|
+
|
|
115
|
+
// Secondary short slug
|
|
116
|
+
expect(screen.getByText("link.acme.com/")).toBeDefined();
|
|
117
|
+
expect(screen.getByText("summer-sale")).toBeDefined();
|
|
118
|
+
|
|
119
|
+
// Tags are rendered as separate badges, not one single concatenated string
|
|
120
|
+
const tagEcommerce = screen.getByTitle("ecommerce");
|
|
121
|
+
const tagPromo = screen.getByTitle("promo");
|
|
122
|
+
const tagVip = screen.getByTitle("vip");
|
|
123
|
+
expect(tagEcommerce).toBeDefined();
|
|
124
|
+
expect(tagPromo).toBeDefined();
|
|
125
|
+
expect(tagVip).toBeDefined();
|
|
126
|
+
expect(tagEcommerce.textContent).toContain("ecommerce");
|
|
127
|
+
expect(tagPromo.textContent).toContain("promo");
|
|
128
|
+
expect(tagVip.textContent).toContain("vip");
|
|
129
|
+
|
|
130
|
+
// Link without title falls back to slug in primary title and secondary slug
|
|
131
|
+
const untitledMatches = screen.getAllByText("random-slug-without-title");
|
|
132
|
+
expect(untitledMatches.length).toBe(2);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("displays link title in Edit, Analytics, and QR modals with slug fallback", async () => {
|
|
136
|
+
render(
|
|
137
|
+
<MemoryRouter initialEntries={["/links"]}>
|
|
138
|
+
<I18nProvider>
|
|
139
|
+
<LinksPage />
|
|
140
|
+
</I18nProvider>
|
|
141
|
+
</MemoryRouter>
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
await waitFor(() => {
|
|
145
|
+
expect(screen.getByText("Summer Sale 2026")).toBeDefined();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Test Analytics modal title for link with title
|
|
149
|
+
const analyticsButtons = screen.getAllByTitle("Analytics");
|
|
150
|
+
fireEvent.click(analyticsButtons[0]);
|
|
151
|
+
|
|
152
|
+
await waitFor(() => {
|
|
153
|
+
expect(screen.getByText("Click performance analytics — Summer Sale 2026")).toBeDefined();
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// Close Analytics modal
|
|
157
|
+
fireEvent.click(screen.getByRole("button", { name: "Close" }));
|
|
158
|
+
|
|
159
|
+
// Test QR modal title for link with title
|
|
160
|
+
const qrButtons = screen.getAllByTitle("QR code");
|
|
161
|
+
fireEvent.click(qrButtons[0]);
|
|
162
|
+
|
|
163
|
+
await waitFor(() => {
|
|
164
|
+
expect(screen.getByText("Link QR code — Summer Sale 2026")).toBeDefined();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// Close QR modal
|
|
168
|
+
fireEvent.click(screen.getByRole("button", { name: "Close" }));
|
|
169
|
+
|
|
170
|
+
// Test Edit modal title for link with title
|
|
171
|
+
const editButtons = screen.getAllByTitle("Edit link");
|
|
172
|
+
fireEvent.click(editButtons[0]);
|
|
173
|
+
|
|
174
|
+
await waitFor(() => {
|
|
175
|
+
expect(screen.getByText("Edit link — Summer Sale 2026")).toBeDefined();
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// Close Edit modal
|
|
179
|
+
fireEvent.click(screen.getByRole("button", { name: "Close" }));
|
|
180
|
+
|
|
181
|
+
// Test Edit modal title for link without title (fallback to /slug)
|
|
182
|
+
fireEvent.click(editButtons[1]);
|
|
183
|
+
|
|
184
|
+
await waitFor(() => {
|
|
185
|
+
expect(screen.getByText("Edit link — /random-slug-without-title")).toBeDefined();
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
3
|
+
import { render, screen, fireEvent, waitFor, cleanup } from "@testing-library/react";
|
|
4
|
+
import { MemoryRouter } from "react-router-dom";
|
|
5
|
+
import { I18nProvider, RoleProvider } from "@octarq/plugin-sdk";
|
|
6
|
+
import LinksPage from "./index";
|
|
7
|
+
import { linksApi, api } from "../api";
|
|
8
|
+
|
|
9
|
+
vi.mock("../api", () => ({
|
|
10
|
+
linksApi: {
|
|
11
|
+
links: vi.fn(),
|
|
12
|
+
updateLink: vi.fn(),
|
|
13
|
+
deleteLink: vi.fn(),
|
|
14
|
+
domains: vi.fn(),
|
|
15
|
+
},
|
|
16
|
+
api: {
|
|
17
|
+
links: vi.fn(),
|
|
18
|
+
updateLink: vi.fn(),
|
|
19
|
+
deleteLink: vi.fn(),
|
|
20
|
+
domains: vi.fn(),
|
|
21
|
+
},
|
|
22
|
+
effectiveLinkHosts: (d: any) => [d.domain || d.name],
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
function renderPage() {
|
|
26
|
+
return render(
|
|
27
|
+
<RoleProvider value={{ role: "admin", isInstanceAdmin: true }}>
|
|
28
|
+
<I18nProvider>
|
|
29
|
+
<MemoryRouter>
|
|
30
|
+
<LinksPage />
|
|
31
|
+
</MemoryRouter>
|
|
32
|
+
</I18nProvider>
|
|
33
|
+
</RoleProvider>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe("LinksPage — 3 Horizontal Projection Tabs", () => {
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
vi.clearAllMocks();
|
|
40
|
+
(api.domains as any).mockResolvedValue([{ id: 1, domain: "go.link" }]);
|
|
41
|
+
(linksApi.domains as any).mockResolvedValue([{ id: 1, domain: "go.link" }]);
|
|
42
|
+
(linksApi.links as any).mockResolvedValue([
|
|
43
|
+
{
|
|
44
|
+
id: 1,
|
|
45
|
+
slug: "docs",
|
|
46
|
+
target: "https://docs.example.com",
|
|
47
|
+
clicks: 42,
|
|
48
|
+
active: true,
|
|
49
|
+
archived: false,
|
|
50
|
+
host: "go.link",
|
|
51
|
+
},
|
|
52
|
+
]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
cleanup();
|
|
57
|
+
document.body.innerHTML = "";
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("renders 3 horizontal projection tabs and switches between them cleanly", async () => {
|
|
61
|
+
renderPage();
|
|
62
|
+
|
|
63
|
+
// Verify all 3 tabs are present
|
|
64
|
+
expect(screen.getByTestId("tab-links")).toBeDefined();
|
|
65
|
+
expect(screen.getByTestId("tab-anomalies")).toBeDefined();
|
|
66
|
+
expect(screen.getByTestId("tab-reserved")).toBeDefined();
|
|
67
|
+
|
|
68
|
+
// Default projection: Links view
|
|
69
|
+
await waitFor(() => {
|
|
70
|
+
expect(screen.getByTestId("links-projection-view")).toBeDefined();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Switch to Anomaly Monitoring projection
|
|
74
|
+
fireEvent.click(screen.getByTestId("tab-anomalies"));
|
|
75
|
+
expect(screen.getByTestId("anomalies-projection-view")).toBeDefined();
|
|
76
|
+
expect(screen.queryByTestId("links-projection-view")).toBeNull();
|
|
77
|
+
expect(screen.getByText("Traffic Anomalies")).toBeDefined();
|
|
78
|
+
expect(screen.getByText("Bot Filtering")).toBeDefined();
|
|
79
|
+
|
|
80
|
+
// Switch to Reserved Words projection
|
|
81
|
+
fireEvent.click(screen.getByTestId("tab-reserved"));
|
|
82
|
+
expect(screen.getByTestId("reserved-projection-view")).toBeDefined();
|
|
83
|
+
expect(screen.queryByTestId("anomalies-projection-view")).toBeNull();
|
|
84
|
+
expect(screen.getByText("System Reserved Words")).toBeDefined();
|
|
85
|
+
expect(screen.getByText("/api")).toBeDefined();
|
|
86
|
+
expect(screen.getByText("/instance")).toBeDefined();
|
|
87
|
+
|
|
88
|
+
// Switch back to Links projection
|
|
89
|
+
fireEvent.click(screen.getByTestId("tab-links"));
|
|
90
|
+
expect(screen.getByTestId("links-projection-view")).toBeDefined();
|
|
91
|
+
});
|
|
92
|
+
});
|
package/store.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
let globalIncludeBot = false;
|
|
4
|
+
const listeners = new Set<(val: boolean) => void>();
|
|
5
|
+
|
|
6
|
+
export function setIncludeBot(val: boolean) {
|
|
7
|
+
globalIncludeBot = val;
|
|
8
|
+
listeners.forEach((l) => l(val));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function useIncludeBot(): [boolean, (val: boolean) => void] {
|
|
12
|
+
const [includeBot, setLocal] = useState(globalIncludeBot);
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
const listener = (val: boolean) => setLocal(val);
|
|
15
|
+
listeners.add(listener);
|
|
16
|
+
return () => {
|
|
17
|
+
listeners.delete(listener);
|
|
18
|
+
};
|
|
19
|
+
}, []);
|
|
20
|
+
return [includeBot, setIncludeBot];
|
|
21
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"noEmit": true,
|
|
11
|
+
"allowImportingTsExtensions": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["."],
|
|
14
|
+
"exclude": ["**/*.test.ts", "**/*.test.tsx"]
|
|
15
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Panel, AreaChart, useTranslation, useOverviewData, type StatKV } from "@octarq/plugin-sdk";
|
|
2
|
+
import { useIncludeBot } from "../store";
|
|
3
|
+
import { BotToggle } from "../components/BotToggle";
|
|
4
|
+
|
|
5
|
+
export default function ClicksChartWidget() {
|
|
6
|
+
const [includeBot, setIncludeBot] = useIncludeBot();
|
|
7
|
+
const o = useOverviewData(includeBot);
|
|
8
|
+
const { t } = useTranslation();
|
|
9
|
+
|
|
10
|
+
if (!o || o.series === undefined) return null;
|
|
11
|
+
|
|
12
|
+
const series = (o.series as StatKV[]) ?? [];
|
|
13
|
+
const clicks30d = (o.clicks30d as number) ?? 0;
|
|
14
|
+
const botClicks30d = (o.botClicks30d as number) ?? 0;
|
|
15
|
+
|
|
16
|
+
const botLabel = includeBot
|
|
17
|
+
? t("links.botInclLabel", { count: botClicks30d })
|
|
18
|
+
: t("links.botHiddenLabel", { count: botClicks30d });
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<Panel
|
|
22
|
+
className="mb-6"
|
|
23
|
+
title={t("links.clicksLast30")}
|
|
24
|
+
action={
|
|
25
|
+
<div className="flex items-center gap-3">
|
|
26
|
+
<span className="text-sm text-muted-foreground">
|
|
27
|
+
{t("links.clicksTotal", { count: clicks30d })} · {botLabel}
|
|
28
|
+
</span>
|
|
29
|
+
<BotToggle value={includeBot} onChange={setIncludeBot} />
|
|
30
|
+
</div>
|
|
31
|
+
}
|
|
32
|
+
>
|
|
33
|
+
<AreaChart series={series} />
|
|
34
|
+
</Panel>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Panel, BarList, useTranslation, useOverviewData, type StatKV } from "@octarq/plugin-sdk";
|
|
2
|
+
import { useIncludeBot } from "../store";
|
|
3
|
+
|
|
4
|
+
export default function DevicesPanelWidget() {
|
|
5
|
+
const [includeBot] = useIncludeBot();
|
|
6
|
+
const o = useOverviewData(includeBot);
|
|
7
|
+
const { t } = useTranslation();
|
|
8
|
+
|
|
9
|
+
if (!o || o.devices === undefined) return null;
|
|
10
|
+
|
|
11
|
+
const devices = (o.devices as StatKV[]) ?? [];
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<Panel title={`${t("links.devices")}${includeBot ? " " + t("links.inclBots") : ""}`}>
|
|
15
|
+
<BarList rows={devices} />
|
|
16
|
+
</Panel>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { useNavigate } from "react-router-dom";
|
|
3
|
+
import { SetupStep, useTranslation } from "@octarq/plugin-sdk";
|
|
4
|
+
import { api, Overview } from "../api";
|
|
5
|
+
|
|
6
|
+
export default function LinksSetupStepWidget() {
|
|
7
|
+
const [o, setO] = useState<Overview | null>(null);
|
|
8
|
+
const nav = useNavigate();
|
|
9
|
+
const { t } = useTranslation();
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
api.overview().then(setO).catch(() => {});
|
|
13
|
+
}, []);
|
|
14
|
+
|
|
15
|
+
if (!o || o.links === undefined) return null;
|
|
16
|
+
|
|
17
|
+
const completed = (o.links ?? 0) > 0;
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<SetupStep
|
|
21
|
+
title={t("overview.stepLinkTitle")}
|
|
22
|
+
description={t("overview.stepLinkDesc")}
|
|
23
|
+
completed={completed}
|
|
24
|
+
onClick={() => nav("/links")}
|
|
25
|
+
/>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { StatCard, useTranslation } from "@octarq/plugin-sdk";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { useNavigate } from "react-router-dom";
|
|
4
|
+
import { api, Overview } from "../api";
|
|
5
|
+
import { Link2, MousePointerClick } from "lucide-react";
|
|
6
|
+
|
|
7
|
+
export default function LinksStatCardWidget() {
|
|
8
|
+
const [o, setO] = useState<Overview | null>(null);
|
|
9
|
+
const nav = useNavigate();
|
|
10
|
+
const { t } = useTranslation();
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
api.overview().then(setO).catch(() => {});
|
|
14
|
+
}, []);
|
|
15
|
+
|
|
16
|
+
if (!o || (o.links === undefined && o.totalClicks === undefined)) return null;
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<>
|
|
20
|
+
{o.totalClicks !== undefined && (
|
|
21
|
+
<StatCard
|
|
22
|
+
label={t("overview.totalClicks")}
|
|
23
|
+
value={(o.totalClicks ?? 0).toLocaleString()}
|
|
24
|
+
delta={t("overview.clicks7d", { count: o.clicks7d ?? 0 })}
|
|
25
|
+
icon={<MousePointerClick className="h-4 w-4" />}
|
|
26
|
+
onClick={() => nav("/links")}
|
|
27
|
+
index={0}
|
|
28
|
+
/>
|
|
29
|
+
)}
|
|
30
|
+
{o.links !== undefined && (
|
|
31
|
+
<StatCard
|
|
32
|
+
label={t("overview.shortLinks")}
|
|
33
|
+
value={(o.links ?? 0).toLocaleString()}
|
|
34
|
+
delta={t("overview.activeLinks", { count: o.activeLinks ?? 0 })}
|
|
35
|
+
icon={<Link2 className="h-4 w-4" />}
|
|
36
|
+
onClick={() => nav("/links")}
|
|
37
|
+
index={1}
|
|
38
|
+
/>
|
|
39
|
+
)}
|
|
40
|
+
</>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Panel, BarList, useTranslation, useOverviewData, type StatKV } from "@octarq/plugin-sdk";
|
|
2
|
+
import { useIncludeBot } from "../store";
|
|
3
|
+
|
|
4
|
+
export default function TopCitiesPanelWidget() {
|
|
5
|
+
const [includeBot] = useIncludeBot();
|
|
6
|
+
const o = useOverviewData(includeBot);
|
|
7
|
+
const { t } = useTranslation();
|
|
8
|
+
|
|
9
|
+
if (!o || o.cities === undefined) return null;
|
|
10
|
+
|
|
11
|
+
const cities = (o.cities as StatKV[]) ?? [];
|
|
12
|
+
// cities only spans the last 30 days; clicks30d is the matching window.
|
|
13
|
+
const hasClicksInWindow = (o.clicks30d ?? 0) > 0;
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
<Panel title={`${t("links.topCities")}${includeBot ? " " + t("links.inclBots") : ""}`}>
|
|
17
|
+
<BarList rows={cities} empty={
|
|
18
|
+
hasClicksInWindow ? (
|
|
19
|
+
<span>
|
|
20
|
+
{t("links.noGeoData")}{" "}
|
|
21
|
+
<a href="/admin/help" className="text-accent-fg hover:underline">
|
|
22
|
+
{t("links.noGeoDataHelp")}
|
|
23
|
+
</a>
|
|
24
|
+
</span>
|
|
25
|
+
) : (
|
|
26
|
+
t("links.noClickData")
|
|
27
|
+
)
|
|
28
|
+
} />
|
|
29
|
+
</Panel>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
3
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
4
|
+
import { MemoryRouter } from "react-router-dom";
|
|
5
|
+
import { registerUIPlugin, resetRegistry, I18nProvider } from "@octarq/plugin-sdk";
|
|
6
|
+
import * as sdkModule from "@octarq/plugin-sdk";
|
|
7
|
+
import linksPlugin from "../index";
|
|
8
|
+
import TopLinksPanelWidget from "./TopLinksPanelWidget";
|
|
9
|
+
|
|
10
|
+
vi.mock("@octarq/plugin-sdk", async (importOriginal) => {
|
|
11
|
+
const actual = await importOriginal<typeof import("@octarq/plugin-sdk")>();
|
|
12
|
+
return {
|
|
13
|
+
...actual,
|
|
14
|
+
useOverviewData: vi.fn(),
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("TopLinksPanelWidget title priority", () => {
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
cleanup();
|
|
21
|
+
resetRegistry();
|
|
22
|
+
registerUIPlugin(linksPlugin);
|
|
23
|
+
vi.restoreAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("renders title as primary, slug as secondary, and tags as badges", () => {
|
|
27
|
+
vi.mocked(sdkModule.useOverviewData).mockReturnValue({
|
|
28
|
+
topLinks: [
|
|
29
|
+
{
|
|
30
|
+
id: 1,
|
|
31
|
+
slug: "promo-2026",
|
|
32
|
+
host: "go.octarq.com",
|
|
33
|
+
title: "Summer Promotion",
|
|
34
|
+
tags: "marketing, summer, campaign",
|
|
35
|
+
clicks: 120,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
id: 2,
|
|
39
|
+
slug: "untitled-link",
|
|
40
|
+
host: "",
|
|
41
|
+
title: "",
|
|
42
|
+
tags: "",
|
|
43
|
+
clicks: 45,
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
} as any);
|
|
47
|
+
|
|
48
|
+
render(
|
|
49
|
+
<MemoryRouter>
|
|
50
|
+
<I18nProvider>
|
|
51
|
+
<TopLinksPanelWidget />
|
|
52
|
+
</I18nProvider>
|
|
53
|
+
</MemoryRouter>
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
// Link 1: title priority
|
|
57
|
+
const titleEl = screen.getByText("Summer Promotion");
|
|
58
|
+
expect(titleEl).toBeDefined();
|
|
59
|
+
expect(titleEl.className).toContain("text-foreground");
|
|
60
|
+
expect(titleEl.className).toContain("font-medium");
|
|
61
|
+
|
|
62
|
+
// Link 1: secondary slug
|
|
63
|
+
const secondarySlug = screen.getByText("go.octarq.com/promo-2026");
|
|
64
|
+
expect(secondarySlug).toBeDefined();
|
|
65
|
+
expect(secondarySlug.className).toContain("text-muted-foreground");
|
|
66
|
+
|
|
67
|
+
// Link 1: tags (first 2 badges visible + overflow badge "+1")
|
|
68
|
+
expect(screen.getByTitle("marketing")).toBeDefined();
|
|
69
|
+
expect(screen.getByTitle("summer")).toBeDefined();
|
|
70
|
+
expect(screen.getByTitle("campaign")).toBeDefined(); // overflow title
|
|
71
|
+
expect(screen.getByText("+1")).toBeDefined();
|
|
72
|
+
|
|
73
|
+
// Link 2: fallback to slug when title is empty
|
|
74
|
+
expect(screen.getByText("untitled-link")).toBeDefined();
|
|
75
|
+
expect(screen.getByText("/untitled-link")).toBeDefined();
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Panel, useTranslation, useOverviewData } from "@octarq/plugin-sdk";
|
|
2
|
+
import { useNavigate } from "react-router-dom";
|
|
3
|
+
|
|
4
|
+
export default function TopLinksPanelWidget() {
|
|
5
|
+
const o = useOverviewData();
|
|
6
|
+
const nav = useNavigate();
|
|
7
|
+
const { t } = useTranslation();
|
|
8
|
+
|
|
9
|
+
if (!o || o.topLinks === undefined) return null;
|
|
10
|
+
|
|
11
|
+
const topLinks = o.topLinks as { id: number; slug: string; host: string; title: string; tags: string; clicks: number }[] | null;
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<Panel title={t("links.topLinks")}>
|
|
15
|
+
{!topLinks || topLinks.length === 0 ? (
|
|
16
|
+
<p className="text-sm text-muted-foreground">{t("links.noLinks")}</p>
|
|
17
|
+
) : (
|
|
18
|
+
<div className="space-y-1">
|
|
19
|
+
{topLinks.map((l) => {
|
|
20
|
+
const rawTags = (l.tags || "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
21
|
+
const visibleTags = rawTags.slice(0, 2);
|
|
22
|
+
const remainingCount = rawTags.length - visibleTags.length;
|
|
23
|
+
const displayTitle = l.title || l.slug;
|
|
24
|
+
const secondarySlug = l.host ? `${l.host}/${l.slug}` : `/${l.slug}`;
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<button
|
|
28
|
+
key={l.id}
|
|
29
|
+
onClick={() => nav("/links")}
|
|
30
|
+
className="flex w-full items-center justify-between rounded-xl px-3 py-2 text-left text-sm hover:bg-surface-hover transition-colors"
|
|
31
|
+
>
|
|
32
|
+
<div className="min-w-0 flex-1 pr-2">
|
|
33
|
+
<div className="flex items-center gap-1.5 min-w-0">
|
|
34
|
+
<span className="truncate text-foreground font-medium">{displayTitle}</span>
|
|
35
|
+
{visibleTags.map((tag) => (
|
|
36
|
+
<span
|
|
37
|
+
key={tag}
|
|
38
|
+
title={tag}
|
|
39
|
+
aria-label={tag}
|
|
40
|
+
className="inline-flex items-center text-[10px] px-1.5 py-0.5 rounded bg-surface-hover text-muted-foreground font-mono shrink-0"
|
|
41
|
+
>
|
|
42
|
+
{tag}
|
|
43
|
+
</span>
|
|
44
|
+
))}
|
|
45
|
+
{remainingCount > 0 && (
|
|
46
|
+
<span
|
|
47
|
+
title={rawTags.slice(2).join(", ")}
|
|
48
|
+
aria-label={`+${remainingCount}`}
|
|
49
|
+
className="inline-flex items-center text-[10px] px-1 py-0.5 rounded bg-surface-hover text-muted-foreground font-mono shrink-0"
|
|
50
|
+
>
|
|
51
|
+
+{remainingCount}
|
|
52
|
+
</span>
|
|
53
|
+
)}
|
|
54
|
+
</div>
|
|
55
|
+
<div className="font-mono text-xs text-muted-foreground truncate">
|
|
56
|
+
{secondarySlug}
|
|
57
|
+
</div>
|
|
58
|
+
</div>
|
|
59
|
+
<span className="shrink-0 font-semibold">{l.clicks}</span>
|
|
60
|
+
</button>
|
|
61
|
+
);
|
|
62
|
+
})}
|
|
63
|
+
</div>
|
|
64
|
+
)}
|
|
65
|
+
</Panel>
|
|
66
|
+
);
|
|
67
|
+
}
|
package/README.md
DELETED