@octarq/plugin-links 0.1.0 → 0.2.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.
@@ -1,4 +1,4 @@
1
- import { Badge, Button, TD, TR, useTranslation } from "@octarq/plugin-sdk";
1
+ import { Badge, Button, StatusBadge, TD, TR, useTranslation } from "@octarq/plugin-sdk";
2
2
  import { Link } from "../api";
3
3
  import { Copy, Check, Archive, Trash2, QrCode, ExternalLink, Lock, Bot, Clock, Tag, BarChart3, Edit3 } from "lucide-react";
4
4
 
@@ -33,7 +33,6 @@ export function LinkTableRow({
33
33
 
34
34
  return (
35
35
  <TR key={l.id} className="hover:bg-well transition-colors group">
36
- {/* Title, Short Slug & Host */}
37
36
  <TD>
38
37
  <div className="flex flex-col gap-1">
39
38
  <div className="text-sm font-medium text-foreground truncate max-w-[260px]" title={displayTitle}>
@@ -86,20 +85,18 @@ export function LinkTableRow({
86
85
  </div>
87
86
  </TD>
88
87
 
89
- {/* Destination Target URL & Badges */}
90
- <TD>
88
+ <TD className="max-w-[320px] align-top">
91
89
  <div className="flex flex-col gap-1">
92
- <div className="font-mono text-xs text-foreground truncate max-w-[320px]" title={l.target}>
90
+ <div className="max-w-[320px] whitespace-normal break-all font-mono text-xs text-foreground" title={l.target}>
93
91
  {l.target}
94
92
  </div>
95
93
  <div className="flex items-center gap-1.5 flex-wrap">
96
94
  {l.hasPassword && (
97
95
  <span
98
96
  className="inline-flex items-center gap-1 text-[10px] text-warning-fg bg-warning-bg px-1.5 py-0.5 rounded font-mono"
99
- title={`${t("links.accessProtectionPassword")}${l.password ? `: ${l.password}` : ""}`}
97
+ title={t("links.accessProtectionPassword")}
100
98
  >
101
99
  <Lock className="h-2.5 w-2.5" />
102
- {l.password && <span>{l.password}</span>}
103
100
  </span>
104
101
  )}
105
102
  {l.routingRules?.length ? (
@@ -117,21 +114,18 @@ export function LinkTableRow({
117
114
  </div>
118
115
  </TD>
119
116
 
120
- {/* Clicks */}
121
117
  <TD className="text-center font-mono">
122
- <Badge tone="neutral" className="text-xs font-semibold">
118
+ <Badge tone="default" className="text-xs font-semibold">
123
119
  {l.clicks || 0}
124
120
  </Badge>
125
121
  </TD>
126
122
 
127
- {/* Status */}
128
123
  <TD className="text-center">
129
- <Badge tone={l.archived ? "neutral" : "info"} className="text-[10px]">
124
+ <StatusBadge tone={l.archived ? "paused" : "success"} className="text-[10px]">
130
125
  {l.archived ? t("links.archived") : t("links.active")}
131
- </Badge>
126
+ </StatusBadge>
132
127
  </TD>
133
128
 
134
- {/* Actions */}
135
129
  <TD className="text-right">
136
130
  <div className="flex items-center justify-end gap-1.5">
137
131
  <Button
@@ -1,4 +1,4 @@
1
- import { StatCard, useTranslation } from "@octarq/plugin-sdk";
1
+ import { Skeleton, StatCard, useTranslation } from "@octarq/plugin-sdk";
2
2
  import { useEffect, useState } from "react";
3
3
  import { linksApi, Link, LinkStats } from "../api";
4
4
  import { Link2, Eye, ExternalLink, Tag, Globe, Settings, Share2, Filter, Target } from "lucide-react";
@@ -13,7 +13,49 @@ export function StatsView({ link }: { link: Link }) {
13
13
  linksApi.linkStats(link.id, 30, metric).then(setStats);
14
14
  }, [link.id, metric]);
15
15
 
16
- if (!stats) return <div className="text-muted-foreground p-4 text-xs">{t("links.loadingAnalytics")}</div>;
16
+ if (!stats) {
17
+ return (
18
+ <div
19
+ role="status"
20
+ aria-busy="true"
21
+ aria-label={t("links.loadingAnalytics")}
22
+ className="space-y-5 min-w-0"
23
+ >
24
+ <div className="flex flex-wrap items-center justify-between gap-3 min-w-0">
25
+ <Skeleton className="h-5 w-48" />
26
+ <div className="flex items-center gap-2 shrink-0">
27
+ <Skeleton className="h-4 w-20" />
28
+ <Skeleton className="h-7 w-24 rounded-lg" />
29
+ </div>
30
+ </div>
31
+ <div className="grid grid-cols-1 sm:grid-cols-3 gap-4 min-w-0">
32
+ {Array.from({ length: 3 }).map((_, i) => (
33
+ <div key={i} className="rounded-lg border border-border bg-card p-5 space-y-3">
34
+ <Skeleton className="h-3.5 w-24" />
35
+ <Skeleton className="h-7 w-16" />
36
+ </div>
37
+ ))}
38
+ </div>
39
+ <div className="rounded-lg bg-well border border-border flex h-24 items-end gap-1.5 p-3 min-w-0 overflow-hidden">
40
+ {Array.from({ length: 14 }).map((_, i) => (
41
+ <Skeleton key={i} className="flex-1 rounded-t-md" style={{ height: `${30 + ((i * 19) % 60)}%` }} />
42
+ ))}
43
+ </div>
44
+ <div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-3 gap-6 pt-2 border-t border-border min-w-0">
45
+ {Array.from({ length: 6 }).map((_, i) => (
46
+ <div key={i} className="space-y-2 min-w-0">
47
+ <Skeleton className="h-3 w-24" />
48
+ <div className="space-y-1.5">
49
+ <Skeleton className="h-3.5 w-full" />
50
+ <Skeleton className="h-3.5 w-5/6" />
51
+ <Skeleton className="h-3.5 w-2/3" />
52
+ </div>
53
+ </div>
54
+ ))}
55
+ </div>
56
+ </div>
57
+ );
58
+ }
17
59
 
18
60
  return (
19
61
  <div className="space-y-5 min-w-0">
@@ -30,10 +72,10 @@ export function StatsView({ link }: { link: Link }) {
30
72
  <button
31
73
  type="button"
32
74
  onClick={() => setMetric("uv")}
33
- className={`px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${
75
+ className={`px-2.5 py-1 text-xs font-medium rounded-md border transition-colors ${
34
76
  metric === "uv"
35
- ? "bg-accent text-accent-fg shadow-sm"
36
- : "text-muted-foreground hover:text-foreground"
77
+ ? "bg-card text-foreground border-border"
78
+ : "border-transparent text-muted-foreground hover:text-foreground"
37
79
  }`}
38
80
  >
39
81
  {t("links.uvShort")}
@@ -41,10 +83,10 @@ export function StatsView({ link }: { link: Link }) {
41
83
  <button
42
84
  type="button"
43
85
  onClick={() => setMetric("pv")}
44
- className={`px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${
86
+ className={`px-2.5 py-1 text-xs font-medium rounded-md border transition-colors ${
45
87
  metric === "pv"
46
- ? "bg-accent text-accent-fg shadow-sm"
47
- : "text-muted-foreground hover:text-foreground"
88
+ ? "bg-card text-foreground border-border"
89
+ : "border-transparent text-muted-foreground hover:text-foreground"
48
90
  }`}
49
91
  >
50
92
  {t("links.pvShort")}
@@ -81,7 +123,7 @@ function Spark({ series }: { series: { key: string; count: number }[] }) {
81
123
  if (!series || !series.length) return <p className="text-xs text-muted-foreground italic">{t("links.noClickData")}</p>;
82
124
  const max = Math.max(...series.map((s) => s.count), 1);
83
125
  return (
84
- <div className="rounded-xl bg-well border border-border flex h-24 items-end gap-1 p-3 min-w-0 overflow-hidden">
126
+ <div className="rounded-lg bg-well border border-border flex h-24 items-end gap-1 p-3 min-w-0 overflow-hidden">
85
127
  {series.map((s) => (
86
128
  <div
87
129
  key={s.key}
@@ -4,10 +4,6 @@ import { cleanup, render, screen, waitFor } from "@testing-library/react";
4
4
  import { MemoryRouter } from "react-router-dom";
5
5
  import { I18nProvider } from "@octarq/plugin-sdk";
6
6
 
7
- // The page's empty state must render THROUGH the shared Empty primitive — not
8
- // a handwritten copy. Mock Empty at the barrel the page imports from, tagging
9
- // it with a testid; if the page ever regresses to a hand-rolled div, this
10
- // marker disappears and the test goes red.
11
7
  vi.mock("@octarq/plugin-sdk", async (importOriginal) => {
12
8
  const ui = await importOriginal<typeof import("@octarq/plugin-sdk")>();
13
9
  return {
@@ -46,8 +42,6 @@ beforeEach(() => {
46
42
  vi.clearAllMocks();
47
43
  });
48
44
 
49
- // Each test renders the whole page. Without this, the next test counts the
50
- // previous render's buttons too.
51
45
  afterEach(cleanup);
52
46
 
53
47
  describe("LinksPage empty state", () => {
@@ -78,9 +72,6 @@ describe("LinksPage empty state", () => {
78
72
  expect(screen.getAllByTestId("shared-empty").length).toBeGreaterThan(0);
79
73
  });
80
74
 
81
- // The header and the empty state used to open the same dialog from two
82
- // buttons with the same label. The test harness renders keys, so the label
83
- // here is the key both buttons use.
84
75
  expect(screen.getAllByRole("button", { name: "links.newLink" })).toHaveLength(1);
85
76
  });
86
77
  });
@@ -0,0 +1,108 @@
1
+ // @vitest-environment happy-dom
2
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
3
+ import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
4
+ import { MemoryRouter } from "react-router-dom";
5
+ import { I18nProvider, registerUIPlugin, resetRegistry, toast } from "@octarq/plugin-sdk";
6
+ import linksPlugin from "../index";
7
+ import LinksPage from "./index";
8
+
9
+ function jsonResponse(body: unknown, status = 200) {
10
+ return {
11
+ ok: status >= 200 && status < 300,
12
+ status,
13
+ json: async () => body,
14
+ headers: { get: (name: string) => (name === "content-type" ? "application/json" : null) },
15
+ };
16
+ }
17
+
18
+ describe("LinksPage CSV Export", () => {
19
+ beforeEach(() => {
20
+ cleanup();
21
+ resetRegistry();
22
+ registerUIPlugin(linksPlugin);
23
+ });
24
+
25
+ afterEach(() => {
26
+ cleanup();
27
+ vi.restoreAllMocks();
28
+ });
29
+
30
+ it("downloads links.csv from GET /api/links/export.csv when Export CSV is clicked", async () => {
31
+ const csvContent = "ID,Host,Slug,Target,Title,Clicks,CreatedAt\n1,go.link,docs,https://example.com,Docs,5,2026-01-01T00:00:00Z\n";
32
+ const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
33
+ const method = (init?.method ?? "GET").toUpperCase();
34
+ const rawUrl = typeof input === "string" ? input : input instanceof URL ? input.toString() : (input as Request).url;
35
+ const path = new URL(rawUrl, "http://localhost").pathname;
36
+ if (method === "GET" && path === "/api/links") return jsonResponse([]);
37
+ if (method === "GET" && path === "/api/domains") return jsonResponse([]);
38
+ if (method === "GET" && path === "/api/links/export.csv") {
39
+ return {
40
+ ok: true,
41
+ status: 200,
42
+ blob: async () => new Blob([csvContent], { type: "text/csv" }),
43
+ headers: { get: () => "text/csv" },
44
+ };
45
+ }
46
+ throw new Error(`Unexpected fetch: ${method} ${rawUrl}`);
47
+ });
48
+ vi.stubGlobal("fetch", fetchMock);
49
+
50
+ const createObjectURLSpy = vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:links-csv");
51
+ const revokeObjectURLSpy = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
52
+ const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, "click").mockImplementation(() => {});
53
+
54
+ render(
55
+ <MemoryRouter initialEntries={["/links"]}>
56
+ <I18nProvider>
57
+ <LinksPage />
58
+ </I18nProvider>
59
+ </MemoryRouter>
60
+ );
61
+
62
+ const exportBtn = await screen.findByRole("button", { name: "Export CSV" });
63
+ fireEvent.click(exportBtn);
64
+
65
+ await waitFor(() => {
66
+ expect(fetchMock).toHaveBeenCalledWith(
67
+ expect.stringContaining("/api/links/export.csv"),
68
+ expect.objectContaining({ credentials: "same-origin" })
69
+ );
70
+ });
71
+ await waitFor(() => {
72
+ expect(createObjectURLSpy).toHaveBeenCalledTimes(1);
73
+ expect(clickSpy).toHaveBeenCalledTimes(1);
74
+ expect(revokeObjectURLSpy).toHaveBeenCalledWith("blob:links-csv");
75
+ });
76
+ });
77
+
78
+ it("shows toast.error when CSV export fails", async () => {
79
+ const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
80
+ const method = (init?.method ?? "GET").toUpperCase();
81
+ const rawUrl = typeof input === "string" ? input : input instanceof URL ? input.toString() : (input as Request).url;
82
+ const path = new URL(rawUrl, "http://localhost").pathname;
83
+ if (method === "GET" && path === "/api/links") return jsonResponse([]);
84
+ if (method === "GET" && path === "/api/domains") return jsonResponse([]);
85
+ if (method === "GET" && path === "/api/links/export.csv") {
86
+ return { ok: false, status: 500, headers: { get: () => "application/json" } };
87
+ }
88
+ throw new Error(`Unexpected fetch: ${method} ${rawUrl}`);
89
+ });
90
+ vi.stubGlobal("fetch", fetchMock);
91
+ const toastErrorSpy = vi.spyOn(toast, "error").mockImplementation(() => "" as any);
92
+
93
+ render(
94
+ <MemoryRouter initialEntries={["/links"]}>
95
+ <I18nProvider>
96
+ <LinksPage />
97
+ </I18nProvider>
98
+ </MemoryRouter>
99
+ );
100
+
101
+ const exportBtn = await screen.findByRole("button", { name: "Export CSV" });
102
+ fireEvent.click(exportBtn);
103
+
104
+ await waitFor(() => {
105
+ expect(toastErrorSpy).toHaveBeenCalledWith("Action failed. Try again.");
106
+ });
107
+ });
108
+ });
@@ -1,7 +1,4 @@
1
1
  // @vitest-environment happy-dom
2
- // A failed first page used to be swallowed (`catch` only handled 402/404), so
3
- // `links` stayed empty and the page rendered "This workspace has no short links
4
- // yet." over a 500 — telling the operator their links were gone.
5
2
  import { beforeEach, describe, expect, it, vi } from "vitest";
6
3
  import { cleanup, fireEvent, render, screen } from "@testing-library/react";
7
4
  import { MemoryRouter } from "react-router-dom";