@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,183 @@
|
|
|
1
|
+
import { Badge, Button, TD, TR, useTranslation } from "@octarq/plugin-sdk";
|
|
2
|
+
import { Link } from "../api";
|
|
3
|
+
import { Copy, Check, Archive, Trash2, QrCode, ExternalLink, Lock, Bot, Clock, Tag, BarChart3, Edit3 } from "lucide-react";
|
|
4
|
+
|
|
5
|
+
export interface LinkTableRowProps {
|
|
6
|
+
link: Link;
|
|
7
|
+
isCopied: boolean;
|
|
8
|
+
canDeleteLink: boolean;
|
|
9
|
+
onCopy: (l: Link) => void;
|
|
10
|
+
onAnalytics: (l: Link) => void;
|
|
11
|
+
onQr: (l: Link) => void;
|
|
12
|
+
onEdit: (l: Link) => void;
|
|
13
|
+
onToggleArchive: (l: Link) => void;
|
|
14
|
+
onDelete: (l: Link) => void;
|
|
15
|
+
linkURL: (l: Link) => string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function LinkTableRow({
|
|
19
|
+
link: l,
|
|
20
|
+
isCopied,
|
|
21
|
+
canDeleteLink,
|
|
22
|
+
onCopy,
|
|
23
|
+
onAnalytics,
|
|
24
|
+
onQr,
|
|
25
|
+
onEdit,
|
|
26
|
+
onToggleArchive,
|
|
27
|
+
onDelete,
|
|
28
|
+
linkURL,
|
|
29
|
+
}: LinkTableRowProps) {
|
|
30
|
+
const { t } = useTranslation();
|
|
31
|
+
const tagList = (l.tags || "").split(",").map((tag) => tag.trim()).filter(Boolean);
|
|
32
|
+
const displayTitle = l.title || l.slug;
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<TR key={l.id} className="hover:bg-well transition-colors group">
|
|
36
|
+
{/* Title, Short Slug & Host */}
|
|
37
|
+
<TD>
|
|
38
|
+
<div className="flex flex-col gap-1">
|
|
39
|
+
<div className="text-sm font-medium text-foreground truncate max-w-[260px]" title={displayTitle}>
|
|
40
|
+
{displayTitle}
|
|
41
|
+
</div>
|
|
42
|
+
<div className="flex items-center gap-1.5">
|
|
43
|
+
<span className="font-mono text-xs text-muted-foreground truncate max-w-[200px]">
|
|
44
|
+
{l.host ? `${l.host}/` : "/"}
|
|
45
|
+
<span>{l.slug}</span>
|
|
46
|
+
</span>
|
|
47
|
+
<button
|
|
48
|
+
type="button"
|
|
49
|
+
onClick={() => onCopy(l)}
|
|
50
|
+
title={t("links.copyLink")}
|
|
51
|
+
aria-label={t("links.copyLink")}
|
|
52
|
+
className="p-0.5 text-muted-foreground hover:text-foreground rounded transition-colors cursor-pointer"
|
|
53
|
+
>
|
|
54
|
+
{isCopied ? (
|
|
55
|
+
<Check className="h-3.5 w-3.5 text-success-fg" />
|
|
56
|
+
) : (
|
|
57
|
+
<Copy className="h-3.5 w-3.5" />
|
|
58
|
+
)}
|
|
59
|
+
</button>
|
|
60
|
+
<a
|
|
61
|
+
href={linkURL(l)}
|
|
62
|
+
target="_blank"
|
|
63
|
+
rel="noreferrer"
|
|
64
|
+
title={t("links.openLink")}
|
|
65
|
+
aria-label={t("links.openLink")}
|
|
66
|
+
className="p-0.5 text-muted-foreground hover:text-accent-fg rounded transition-colors"
|
|
67
|
+
>
|
|
68
|
+
<ExternalLink className="h-3.5 w-3.5" />
|
|
69
|
+
</a>
|
|
70
|
+
</div>
|
|
71
|
+
{tagList.length > 0 && (
|
|
72
|
+
<div className="flex items-center gap-1 flex-wrap mt-0.5">
|
|
73
|
+
{tagList.map((tag) => (
|
|
74
|
+
<span
|
|
75
|
+
key={tag}
|
|
76
|
+
title={tag}
|
|
77
|
+
aria-label={tag}
|
|
78
|
+
className="inline-flex items-center gap-0.5 text-[10px] text-muted-foreground bg-surface-hover px-1.5 py-0.5 rounded font-mono"
|
|
79
|
+
>
|
|
80
|
+
<Tag className="h-2.5 w-2.5 text-muted-foreground" />
|
|
81
|
+
<span className="truncate max-w-[120px]">{tag}</span>
|
|
82
|
+
</span>
|
|
83
|
+
))}
|
|
84
|
+
</div>
|
|
85
|
+
)}
|
|
86
|
+
</div>
|
|
87
|
+
</TD>
|
|
88
|
+
|
|
89
|
+
{/* Destination Target URL & Badges */}
|
|
90
|
+
<TD>
|
|
91
|
+
<div className="flex flex-col gap-1">
|
|
92
|
+
<div className="font-mono text-xs text-foreground truncate max-w-[320px]" title={l.target}>
|
|
93
|
+
{l.target}
|
|
94
|
+
</div>
|
|
95
|
+
<div className="flex items-center gap-1.5 flex-wrap">
|
|
96
|
+
{l.hasPassword && (
|
|
97
|
+
<span
|
|
98
|
+
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}` : ""}`}
|
|
100
|
+
>
|
|
101
|
+
<Lock className="h-2.5 w-2.5" />
|
|
102
|
+
{l.password && <span>{l.password}</span>}
|
|
103
|
+
</span>
|
|
104
|
+
)}
|
|
105
|
+
{l.routingRules?.length ? (
|
|
106
|
+
<span className="inline-flex items-center gap-0.5 text-[10px] text-accent-fg bg-accent-soft px-1.5 py-0.5 rounded" title={t("links.routingRules")}>
|
|
107
|
+
<Bot className="h-2.5 w-2.5" />
|
|
108
|
+
{l.routingRules.length}
|
|
109
|
+
</span>
|
|
110
|
+
) : null}
|
|
111
|
+
{l.expiresAt && (
|
|
112
|
+
<span className="inline-flex items-center gap-0.5 text-[10px] text-muted-foreground bg-well px-1.5 py-0.5 rounded" title={new Date(l.expiresAt).toLocaleString()}>
|
|
113
|
+
<Clock className="h-2.5 w-2.5" />
|
|
114
|
+
</span>
|
|
115
|
+
)}
|
|
116
|
+
</div>
|
|
117
|
+
</div>
|
|
118
|
+
</TD>
|
|
119
|
+
|
|
120
|
+
{/* Clicks */}
|
|
121
|
+
<TD className="text-center font-mono">
|
|
122
|
+
<Badge tone="neutral" className="text-xs font-semibold">
|
|
123
|
+
{l.clicks || 0}
|
|
124
|
+
</Badge>
|
|
125
|
+
</TD>
|
|
126
|
+
|
|
127
|
+
{/* Status */}
|
|
128
|
+
<TD className="text-center">
|
|
129
|
+
<Badge tone={l.archived ? "neutral" : "info"} className="text-[10px]">
|
|
130
|
+
{l.archived ? t("links.archived") : t("links.active")}
|
|
131
|
+
</Badge>
|
|
132
|
+
</TD>
|
|
133
|
+
|
|
134
|
+
{/* Actions */}
|
|
135
|
+
<TD className="text-right">
|
|
136
|
+
<div className="flex items-center justify-end gap-1.5">
|
|
137
|
+
<Button
|
|
138
|
+
variant="ghost"
|
|
139
|
+
className="p-1.5 text-xs text-muted-foreground hover:text-accent-fg"
|
|
140
|
+
title={t("links.tabAnalytics")}
|
|
141
|
+
onClick={() => onAnalytics(l)}
|
|
142
|
+
>
|
|
143
|
+
<BarChart3 className="h-4 w-4" />
|
|
144
|
+
</Button>
|
|
145
|
+
<Button
|
|
146
|
+
variant="ghost"
|
|
147
|
+
className="p-1.5 text-xs text-muted-foreground hover:text-foreground"
|
|
148
|
+
title={t("links.tabQr")}
|
|
149
|
+
onClick={() => onQr(l)}
|
|
150
|
+
>
|
|
151
|
+
<QrCode className="h-4 w-4" />
|
|
152
|
+
</Button>
|
|
153
|
+
<Button
|
|
154
|
+
variant="ghost"
|
|
155
|
+
className="p-1.5 text-xs text-muted-foreground hover:text-foreground"
|
|
156
|
+
title={t("links.editLink")}
|
|
157
|
+
onClick={() => onEdit(l)}
|
|
158
|
+
>
|
|
159
|
+
<Edit3 className="h-4 w-4" />
|
|
160
|
+
</Button>
|
|
161
|
+
<Button
|
|
162
|
+
variant="ghost"
|
|
163
|
+
className="p-1.5 text-xs text-muted-foreground hover:text-foreground"
|
|
164
|
+
title={l.archived ? t("links.unarchive") : t("links.archive")}
|
|
165
|
+
onClick={() => onToggleArchive(l)}
|
|
166
|
+
>
|
|
167
|
+
<Archive className="h-4 w-4" />
|
|
168
|
+
</Button>
|
|
169
|
+
{canDeleteLink && (
|
|
170
|
+
<Button
|
|
171
|
+
variant="ghost"
|
|
172
|
+
className="p-1.5 text-xs text-danger-fg/70 hover:text-danger-fg"
|
|
173
|
+
title={t("links.delete")}
|
|
174
|
+
onClick={() => onDelete(l)}
|
|
175
|
+
>
|
|
176
|
+
<Trash2 className="h-4 w-4" />
|
|
177
|
+
</Button>
|
|
178
|
+
)}
|
|
179
|
+
</div>
|
|
180
|
+
</TD>
|
|
181
|
+
</TR>
|
|
182
|
+
);
|
|
183
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { StatCard, useTranslation } from "@octarq/plugin-sdk";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { linksApi, Link, LinkStats } from "../api";
|
|
4
|
+
import { Link2, Eye, ExternalLink, Tag, Globe, Settings, Share2, Filter, Target } from "lucide-react";
|
|
5
|
+
|
|
6
|
+
export function StatsView({ link }: { link: Link }) {
|
|
7
|
+
const { t } = useTranslation();
|
|
8
|
+
const [metric, setMetric] = useState<"uv" | "pv">("uv");
|
|
9
|
+
const [stats, setStats] = useState<LinkStats | null>(null);
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
setStats(null);
|
|
13
|
+
linksApi.linkStats(link.id, 30, metric).then(setStats);
|
|
14
|
+
}, [link.id, metric]);
|
|
15
|
+
|
|
16
|
+
if (!stats) return <div className="text-muted-foreground p-4 text-xs">{t("links.loadingAnalytics")}</div>;
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<div className="space-y-5 min-w-0">
|
|
20
|
+
<div className="flex flex-wrap items-center justify-between gap-3 min-w-0">
|
|
21
|
+
<h3 className="text-sm font-semibold text-foreground uppercase tracking-wider flex items-center gap-1.5 min-w-0">
|
|
22
|
+
<Eye className="h-4 w-4 text-accent-fg shrink-0" />
|
|
23
|
+
<span className="truncate">{t("links.clickPerformanceAnalytics")}</span>
|
|
24
|
+
</h3>
|
|
25
|
+
<div className="flex items-center gap-2 shrink-0">
|
|
26
|
+
<span className="text-xs text-muted-foreground font-medium">
|
|
27
|
+
{t("links.metricMode", { mode: metric === "uv" ? t("links.uvLabel") : t("links.pvLabel") })}
|
|
28
|
+
</span>
|
|
29
|
+
<div className="inline-flex rounded-lg bg-well p-0.5 border border-border">
|
|
30
|
+
<button
|
|
31
|
+
type="button"
|
|
32
|
+
onClick={() => setMetric("uv")}
|
|
33
|
+
className={`px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${
|
|
34
|
+
metric === "uv"
|
|
35
|
+
? "bg-accent text-accent-fg shadow-sm"
|
|
36
|
+
: "text-muted-foreground hover:text-foreground"
|
|
37
|
+
}`}
|
|
38
|
+
>
|
|
39
|
+
{t("links.uvShort")}
|
|
40
|
+
</button>
|
|
41
|
+
<button
|
|
42
|
+
type="button"
|
|
43
|
+
onClick={() => setMetric("pv")}
|
|
44
|
+
className={`px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${
|
|
45
|
+
metric === "pv"
|
|
46
|
+
? "bg-accent text-accent-fg shadow-sm"
|
|
47
|
+
: "text-muted-foreground hover:text-foreground"
|
|
48
|
+
}`}
|
|
49
|
+
>
|
|
50
|
+
{t("links.pvShort")}
|
|
51
|
+
</button>
|
|
52
|
+
</div>
|
|
53
|
+
</div>
|
|
54
|
+
</div>
|
|
55
|
+
<div className="grid grid-cols-1 sm:grid-cols-3 gap-4 min-w-0">
|
|
56
|
+
<StatCard label={t("links.totalClicks")} value={stats.total} index={0} />
|
|
57
|
+
<StatCard label={t("links.lastDays", { days: stats.days })} value={stats.windowed} index={1} />
|
|
58
|
+
<StatCard label={t("links.trackingWindow")} value={t("links.daysWindowUnit", { days: stats.series?.length || 0 })} index={2} />
|
|
59
|
+
</div>
|
|
60
|
+
<Spark series={stats.series} />
|
|
61
|
+
<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">
|
|
62
|
+
<TopList title={t("links.channels")} icon={<Share2 className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.channels} />
|
|
63
|
+
<TopList title={t("links.utmSources")} icon={<Tag className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.utmSources} />
|
|
64
|
+
<TopList title={t("links.utmMediums")} icon={<Filter className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.utmMediums} />
|
|
65
|
+
<TopList title={t("links.utmCampaigns")} icon={<Target className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.utmCampaigns} />
|
|
66
|
+
<TopList title={t("links.countries")} icon={<Globe className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.countries} />
|
|
67
|
+
<TopList title={t("links.regions")} icon={<ExternalLink className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.regions} />
|
|
68
|
+
<TopList title={t("links.devices")} icon={<Eye className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.devices} />
|
|
69
|
+
<TopList title={t("links.browsers")} icon={<Link2 className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.browsers} />
|
|
70
|
+
<TopList title={t("links.referers")} icon={<Tag className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.referers} />
|
|
71
|
+
{stats.variants && (
|
|
72
|
+
<TopList title={t("links.variants")} icon={<Settings className="h-3 w-3 text-muted-foreground mr-1 inline shrink-0" />} rows={stats.variants} />
|
|
73
|
+
)}
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function Spark({ series }: { series: { key: string; count: number }[] }) {
|
|
80
|
+
const { t } = useTranslation();
|
|
81
|
+
if (!series || !series.length) return <p className="text-xs text-muted-foreground italic">{t("links.noClickData")}</p>;
|
|
82
|
+
const max = Math.max(...series.map((s) => s.count), 1);
|
|
83
|
+
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">
|
|
85
|
+
{series.map((s) => (
|
|
86
|
+
<div
|
|
87
|
+
key={s.key}
|
|
88
|
+
title={t("links.clicksTooltip", { key: s.key, count: s.count })}
|
|
89
|
+
className="flex-1 min-w-[2px] rounded-t-md bg-primary/70 hover:bg-primary transition-all cursor-pointer"
|
|
90
|
+
style={{ height: `${(s.count / max) * 100}%`, minHeight: 3 }}
|
|
91
|
+
/>
|
|
92
|
+
))}
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function TopList({ title, icon, rows }: { title: string; icon?: React.ReactNode; rows: { key: string; count: number }[] | null }) {
|
|
98
|
+
const { t } = useTranslation();
|
|
99
|
+
return (
|
|
100
|
+
<div className="space-y-2 min-w-0">
|
|
101
|
+
<div className="text-[11px] font-semibold uppercase tracking-wider text-muted-foreground flex items-center min-w-0">
|
|
102
|
+
{icon}
|
|
103
|
+
<span className="truncate">{title}</span>
|
|
104
|
+
</div>
|
|
105
|
+
{!rows || rows.length === 0 ? (
|
|
106
|
+
<p className="text-xs text-muted-foreground italic">{t("links.emptyDash")}</p>
|
|
107
|
+
) : (
|
|
108
|
+
<div className="space-y-1.5 min-w-0">
|
|
109
|
+
{rows.map((r) => (
|
|
110
|
+
<div key={r.key} className="flex justify-between items-center text-xs font-normal min-w-0 gap-2">
|
|
111
|
+
<span className="truncate text-muted-foreground font-mono min-w-0 flex-1" title={r.key || t("links.directUnknown")}>{r.key || t("links.direct")}</span>
|
|
112
|
+
<span className="font-mono tnum text-muted-foreground font-semibold shrink-0">{r.count}</span>
|
|
113
|
+
</div>
|
|
114
|
+
))}
|
|
115
|
+
</div>
|
|
116
|
+
)}
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { afterEach, describe, it, expect, vi, beforeEach } from "vitest";
|
|
3
|
+
import { cleanup, render, screen, waitFor } from "@testing-library/react";
|
|
4
|
+
import { MemoryRouter } from "react-router-dom";
|
|
5
|
+
import { I18nProvider } from "@octarq/plugin-sdk";
|
|
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
|
+
vi.mock("@octarq/plugin-sdk", async (importOriginal) => {
|
|
12
|
+
const ui = await importOriginal<typeof import("@octarq/plugin-sdk")>();
|
|
13
|
+
return {
|
|
14
|
+
...ui,
|
|
15
|
+
Empty: (props: { children?: React.ReactNode; reason?: React.ReactNode; detail?: React.ReactNode; action?: React.ReactNode }) => (
|
|
16
|
+
<div data-testid="shared-empty">
|
|
17
|
+
{props.children}
|
|
18
|
+
{props.reason}
|
|
19
|
+
{props.detail}
|
|
20
|
+
{props.action}
|
|
21
|
+
</div>
|
|
22
|
+
),
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
vi.mock("../api", async (importOriginal) => {
|
|
27
|
+
const actual = await importOriginal<typeof import("../api")>();
|
|
28
|
+
return {
|
|
29
|
+
...actual,
|
|
30
|
+
linksApi: {
|
|
31
|
+
...actual.linksApi,
|
|
32
|
+
links: vi.fn().mockResolvedValue([]),
|
|
33
|
+
domains: vi.fn().mockResolvedValue([]),
|
|
34
|
+
},
|
|
35
|
+
api: {
|
|
36
|
+
...actual.api,
|
|
37
|
+
links: vi.fn().mockResolvedValue([]),
|
|
38
|
+
domains: vi.fn().mockResolvedValue([]),
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
import LinksPage from "./index";
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
vi.clearAllMocks();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Each test renders the whole page. Without this, the next test counts the
|
|
50
|
+
// previous render's buttons too.
|
|
51
|
+
afterEach(cleanup);
|
|
52
|
+
|
|
53
|
+
describe("LinksPage empty state", () => {
|
|
54
|
+
it("renders the shared Empty when there are no links (not a handwritten copy)", async () => {
|
|
55
|
+
render(
|
|
56
|
+
<MemoryRouter initialEntries={["/links"]}>
|
|
57
|
+
<I18nProvider>
|
|
58
|
+
<LinksPage />
|
|
59
|
+
</I18nProvider>
|
|
60
|
+
</MemoryRouter>
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
await waitFor(() => {
|
|
64
|
+
expect(screen.getAllByTestId("shared-empty").length).toBeGreaterThan(0);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("offers one create button while the list is empty", async () => {
|
|
69
|
+
render(
|
|
70
|
+
<MemoryRouter initialEntries={["/links"]}>
|
|
71
|
+
<I18nProvider>
|
|
72
|
+
<LinksPage />
|
|
73
|
+
</I18nProvider>
|
|
74
|
+
</MemoryRouter>
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
await waitFor(() => {
|
|
78
|
+
expect(screen.getAllByTestId("shared-empty").length).toBeGreaterThan(0);
|
|
79
|
+
});
|
|
80
|
+
|
|
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
|
+
expect(screen.getAllByRole("button", { name: "links.newLink" })).toHaveLength(1);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
7
|
+
import { MemoryRouter } from "react-router-dom";
|
|
8
|
+
import { registerUIPlugin, resetRegistry, I18nProvider } from "@octarq/plugin-sdk";
|
|
9
|
+
import linksPlugin from "../index";
|
|
10
|
+
import LinksPage from "./index";
|
|
11
|
+
|
|
12
|
+
function jsonResponse(body: unknown, status = 200) {
|
|
13
|
+
return {
|
|
14
|
+
ok: status >= 200 && status < 300,
|
|
15
|
+
status,
|
|
16
|
+
json: async () => body,
|
|
17
|
+
headers: { get: (name: string) => (name === "content-type" ? "application/json" : null) },
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function renderPage() {
|
|
22
|
+
return render(
|
|
23
|
+
<MemoryRouter initialEntries={["/links"]}>
|
|
24
|
+
<I18nProvider>
|
|
25
|
+
<LinksPage />
|
|
26
|
+
</I18nProvider>
|
|
27
|
+
</MemoryRouter>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("LinksPage — failed list load", () => {
|
|
32
|
+
let linkStatus = 500;
|
|
33
|
+
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
cleanup();
|
|
36
|
+
resetRegistry();
|
|
37
|
+
registerUIPlugin(linksPlugin);
|
|
38
|
+
linkStatus = 500;
|
|
39
|
+
vi.stubGlobal(
|
|
40
|
+
"fetch",
|
|
41
|
+
vi.fn(async (input: RequestInfo | URL) => {
|
|
42
|
+
const raw =
|
|
43
|
+
typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
44
|
+
const path = new URL(raw, "http://localhost").pathname;
|
|
45
|
+
if (path === "/api/links") {
|
|
46
|
+
return linkStatus >= 400
|
|
47
|
+
? jsonResponse({ message: "boom" }, linkStatus)
|
|
48
|
+
: jsonResponse([]);
|
|
49
|
+
}
|
|
50
|
+
if (path === "/api/domains") return jsonResponse([]);
|
|
51
|
+
throw new Error(`unexpected request in test: ${raw}`);
|
|
52
|
+
})
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("shows a retryable failure instead of the empty state", async () => {
|
|
57
|
+
renderPage();
|
|
58
|
+
|
|
59
|
+
expect(await screen.findByText("Failed to load data")).toBeTruthy();
|
|
60
|
+
expect(screen.getByText("HTTP 500")).toBeTruthy();
|
|
61
|
+
expect(screen.queryByText("This workspace has no short links yet.")).toBeNull();
|
|
62
|
+
expect(screen.getByRole("button", { name: "Retry" })).toBeTruthy();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("recovers to the real empty state when Retry succeeds", async () => {
|
|
66
|
+
renderPage();
|
|
67
|
+
|
|
68
|
+
const retry = await screen.findByRole("button", { name: "Retry" });
|
|
69
|
+
linkStatus = 200;
|
|
70
|
+
fireEvent.click(retry);
|
|
71
|
+
|
|
72
|
+
expect(await screen.findByText("This workspace has no short links yet.")).toBeTruthy();
|
|
73
|
+
expect(screen.queryByText("Failed to load data")).toBeNull();
|
|
74
|
+
});
|
|
75
|
+
});
|