sanity-plugin-seo-copilot 0.1.2 → 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.
@@ -0,0 +1,343 @@
1
+ import { LoadingBars } from "../SEOCopilotPanel";
2
+ import { FeedbackButton } from "../FeedbackButton";
3
+ import { ScoreDonut, ScoreDonutLegend } from "../ScoreDonut";
4
+ import { MJH_BLUE } from "../styles";
5
+ import type { SEOAnalysis, LinkingSuggestion } from "../../lib/types";
6
+
7
+ interface LinkingTabProps {
8
+ analysis: SEOAnalysis | null;
9
+ suggestions: LinkingSuggestion[];
10
+ isLoading: boolean;
11
+ error?: string | null;
12
+ /** True when the article changed materially since these suggestions were fetched. */
13
+ isStale?: boolean;
14
+ onRefresh: () => void;
15
+ text?: string;
16
+ documentId?: string;
17
+ publication?: string;
18
+ }
19
+
20
+ const cardStyle: React.CSSProperties = {
21
+ borderRadius: 14,
22
+ background: "rgba(255,255,255,0.55)",
23
+ backdropFilter: "blur(16px) saturate(170%)",
24
+ WebkitBackdropFilter: "blur(16px) saturate(170%)",
25
+ boxShadow: "inset 0 0 0 1px rgba(255,255,255,0.85), inset 1.5px 2px 1px -1px rgba(255,255,255,1), inset -2px -3px 2px -1px rgba(255,255,255,0.6), 0 2px 6px rgba(0,0,0,0.07), 0 10px 26px rgba(0,0,0,0.08)",
26
+ padding: "14px",
27
+ };
28
+
29
+ // Small "AI" pill - inline label marking generative content at the point of generation.
30
+ function AiPill() {
31
+ return (
32
+ <span
33
+ style={{
34
+ fontSize: 8.5,
35
+ fontWeight: 700,
36
+ letterSpacing: "0.06em",
37
+ padding: "1px 5px",
38
+ borderRadius: 99,
39
+ background: "rgba(0,0,0,0.06)",
40
+ color: "#4b5563",
41
+ flexShrink: 0,
42
+ }}
43
+ title="Generated by AI - verify before publishing"
44
+ >
45
+ AI
46
+ </span>
47
+ );
48
+ }
49
+
50
+ function SectionHeader({
51
+ label,
52
+ iconColor,
53
+ ai,
54
+ action,
55
+ }: {
56
+ label: string;
57
+ iconColor: string;
58
+ ai?: boolean;
59
+ action?: React.ReactNode;
60
+ }) {
61
+ return (
62
+ <div style={{ display: "flex", alignItems: "center", gap: 7, marginBottom: 8 }}>
63
+ <div style={{ width: 7, height: 7, borderRadius: "50%", background: iconColor, flexShrink: 0 }} />
64
+ <span style={{ fontSize: 10.5, textTransform: "uppercase", letterSpacing: "0.06em", color: "#4b5563", fontWeight: 600, flex: 1 }}>
65
+ {label}
66
+ </span>
67
+ {ai && <AiPill />}
68
+ {action}
69
+ </div>
70
+ );
71
+ }
72
+
73
+ function formatDate(iso?: string): string {
74
+ if (!iso) return "";
75
+ try {
76
+ return new Date(iso).toLocaleDateString(undefined, { year: "numeric", month: "short", day: "numeric" });
77
+ } catch {
78
+ return "";
79
+ }
80
+ }
81
+
82
+ function freshnessChip(label: LinkingSuggestion["freshnessLabel"], score: number) {
83
+ const map = {
84
+ fresh: { bg: "rgba(22,163,74,0.1)", color: "#16a34a", text: "Fresh" },
85
+ recent: { bg: "rgba(0,93,172,0.08)", color: MJH_BLUE, text: "Recent" },
86
+ older: { bg: "rgba(230,192,27,0.18)", color: "#8B7310", text: "Older" },
87
+ "very-old": { bg: "rgba(220,38,38,0.1)", color: "#dc2626", text: "Very old" },
88
+ } as const;
89
+ const c = map[label];
90
+ return (
91
+ <span
92
+ style={{
93
+ fontSize: 9,
94
+ fontWeight: 700,
95
+ textTransform: "uppercase",
96
+ letterSpacing: "0.05em",
97
+ padding: "1px 6px",
98
+ borderRadius: 99,
99
+ background: c.bg,
100
+ color: c.color,
101
+ flexShrink: 0,
102
+ }}
103
+ title={`Freshness score: ${score}/100`}
104
+ >
105
+ {c.text}
106
+ </span>
107
+ );
108
+ }
109
+
110
+ function RefreshIcon({ spinning }: { spinning: boolean }) {
111
+ if (spinning) return <LoadingBars size="xs" color="#9ca3af" />;
112
+ return (
113
+ <svg width="12" height="12" viewBox="0 0 20 20" fill="currentColor">
114
+ <path fillRule="evenodd" d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H4.598a.75.75 0 00-.75.75v3.634a.75.75 0 001.5 0v-2.033l.312.311a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm-10.624-2.85a5.5 5.5 0 019.201-2.466l.312.311h-2.433a.75.75 0 000 1.5h3.634a.75.75 0 00.75-.75V3.535a.75.75 0 00-1.5 0v2.033l-.312-.311A7 7 0 002.628 8.395a.75.75 0 001.449.39z" clipRule="evenodd" />
115
+ </svg>
116
+ );
117
+ }
118
+
119
+ export function LinkingTab({
120
+ analysis,
121
+ suggestions,
122
+ isLoading,
123
+ error,
124
+ isStale,
125
+ onRefresh,
126
+ text,
127
+ documentId,
128
+ publication,
129
+ }: LinkingTabProps) {
130
+ if (!analysis) {
131
+ return (
132
+ <div style={{ padding: 24, textAlign: "center" }}>
133
+ <div style={{ fontSize: 13, color: "#4b5563", fontWeight: 500 }}>Run keyword analysis first</div>
134
+ <div style={{ fontSize: 11, color: "#4b5563", marginTop: 4 }}>Internal link suggestions need keywords to start from</div>
135
+ </div>
136
+ );
137
+ }
138
+
139
+ // Healthy contextual link counts scale with article length (roughly 2-5 per 1,000 words).
140
+ const wordCount = text ? text.split(/\s+/).filter(Boolean).length : 0;
141
+ const recommendedMin = Math.max(1, Math.round((wordCount / 1000) * 2));
142
+ const recommendedMax = Math.max(2, Math.round((wordCount / 1000) * 5));
143
+
144
+ return (
145
+ <div style={{ padding: 12, display: "flex", flexDirection: "column", gap: 12 }}>
146
+ <div id="anchor-linking-suggestions" style={cardStyle}>
147
+ <SectionHeader
148
+ label="Suggested Links"
149
+ iconColor={MJH_BLUE}
150
+ action={
151
+ <button
152
+ onClick={onRefresh}
153
+ disabled={isLoading}
154
+ style={{
155
+ background: "none",
156
+ border: "none",
157
+ cursor: isLoading ? "wait" : "pointer",
158
+ padding: 2,
159
+ display: "flex",
160
+ color: isLoading ? "#d1d5db" : "#9ca3af",
161
+ fontSize: 10,
162
+ }}
163
+ title="Refresh"
164
+ >
165
+ <RefreshIcon spinning={isLoading} />
166
+ </button>
167
+ }
168
+ />
169
+ <div style={{ fontSize: 11, color: "#1f2937", marginBottom: 8, lineHeight: 1.55 }}>
170
+ Other articles on this site that fit this topic.
171
+ {wordCount > 0 && (
172
+ <>
173
+ {" "}For a {wordCount.toLocaleString()}-word article, aim for{" "}
174
+ <strong>{recommendedMin}-{recommendedMax} internal links</strong>.
175
+ </>
176
+ )}
177
+ </div>
178
+ <div style={{ fontSize: 10, color: "#4b5563", marginBottom: 6, lineHeight: 1.5 }}>
179
+ Each suggestion gets a 0-100% match score from three signals - <strong>topic match</strong>, <strong>freshness</strong>, and <strong>section fit</strong> (where in your article the link belongs). The percentage on each card is the weighted total:
180
+ </div>
181
+ <div style={{ marginBottom: 10 }}>
182
+ <ScoreDonutLegend />
183
+ </div>
184
+
185
+ {/* Stale banner - article changed since these were generated */}
186
+ {isStale && suggestions.length > 0 && !isLoading && (
187
+ <div
188
+ onClick={onRefresh}
189
+ style={{
190
+ display: "flex",
191
+ alignItems: "center",
192
+ justifyContent: "space-between",
193
+ gap: 8,
194
+ padding: "7px 10px",
195
+ marginBottom: 10,
196
+ background: "#fffbeb",
197
+ border: "1px solid #fde68a",
198
+ borderRadius: 8,
199
+ cursor: "pointer",
200
+ }}
201
+ >
202
+ <span style={{ fontSize: 11, color: "#92400e" }}>
203
+ Your article changed since these were generated.
204
+ </span>
205
+ <span style={{ fontSize: 11, fontWeight: 700, color: "#8B7310", textDecoration: "underline", flexShrink: 0 }}>
206
+ Refresh
207
+ </span>
208
+ </div>
209
+ )}
210
+
211
+ {/* Loading (first run, nothing to show yet) */}
212
+ {isLoading && suggestions.length === 0 && (
213
+ <div style={{ display: "flex", alignItems: "center", justifyContent: "center", gap: 8, padding: "16px 0" }}>
214
+ <LoadingBars size="xs" color={MJH_BLUE} />
215
+ <span style={{ fontSize: 11, color: "#4b5563" }}>Ranking candidates...</span>
216
+ </div>
217
+ )}
218
+
219
+ {/* Error - distinct from "empty", with a real retry affordance */}
220
+ {!isLoading && error && (
221
+ <div
222
+ style={{
223
+ padding: "12px",
224
+ background: "rgba(220,38,38,0.04)",
225
+ border: "1px solid rgba(220,38,38,0.18)",
226
+ borderRadius: 10,
227
+ textAlign: "center",
228
+ }}
229
+ >
230
+ <div style={{ fontSize: 11.5, color: "#b91c1c", fontWeight: 600, marginBottom: 2 }}>
231
+ Couldn't load suggestions
232
+ </div>
233
+ <div style={{ fontSize: 10.5, color: "#4b5563", marginBottom: 8 }}>{error}</div>
234
+ <button
235
+ onClick={onRefresh}
236
+ style={{
237
+ padding: "5px 14px",
238
+ fontSize: 11,
239
+ fontWeight: 700,
240
+ color: "#ffffff",
241
+ background: "#dc2626",
242
+ border: "none",
243
+ borderRadius: 6,
244
+ cursor: "pointer",
245
+ }}
246
+ >
247
+ Try again
248
+ </button>
249
+ </div>
250
+ )}
251
+
252
+ {/* Empty - no error, just nothing matched. */}
253
+ {!isLoading && !error && suggestions.length === 0 && (
254
+ <div style={{ fontSize: 11, color: "#1f2937", padding: "10px 12px", lineHeight: 1.55, background: "rgba(0,93,172,0.04)", borderRadius: 8, border: "1px solid rgba(0,93,172,0.12)" }}>
255
+ <div style={{ fontWeight: 700, marginBottom: 4, color: "#1f2937" }}>No related articles found yet.</div>
256
+ <div style={{ color: "#4b5563" }}>
257
+ Linking suggests published articles on closely related topics. As more related content is published, matches will appear here.
258
+ </div>
259
+ </div>
260
+ )}
261
+
262
+ {suggestions.length > 0 && (
263
+ <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
264
+ {suggestions.map((s) => (
265
+ <div
266
+ key={s._id}
267
+ style={{
268
+ padding: "10px 12px",
269
+ background: "rgba(0,93,172,0.03)",
270
+ borderRadius: 10,
271
+ borderLeft: `2px solid ${MJH_BLUE}`,
272
+ display: "flex",
273
+ gap: 12,
274
+ alignItems: "flex-start",
275
+ }}
276
+ >
277
+ <ScoreDonut
278
+ size={64}
279
+ topic={s.topicScore}
280
+ freshness={s.freshnessScore}
281
+ sectionFit={s.sectionFitScore}
282
+ composite={s.compositeScore}
283
+ />
284
+ <div style={{ flex: 1, minWidth: 0 }}>
285
+ <div style={{ display: "flex", alignItems: "center", gap: 6, marginBottom: 3, flexWrap: "wrap" }}>
286
+ {freshnessChip(s.freshnessLabel, s.freshnessScore)}
287
+ {s.publishedAt && (
288
+ <span style={{ fontSize: 10, color: "#4b5563" }}>{formatDate(s.publishedAt)}</span>
289
+ )}
290
+ </div>
291
+ <div style={{ fontSize: 12, fontWeight: 700, color: "#1f2937", lineHeight: 1.4, marginBottom: 2 }}>
292
+ {s.title}
293
+ </div>
294
+ <div style={{ fontSize: 10, color: "#4b5563", fontFamily: "ui-monospace, SFMono-Regular, monospace", marginBottom: 6 }}>
295
+ /{s.slug}
296
+ </div>
297
+ {s.relevanceReason && (
298
+ <div style={{ fontSize: 11, color: "#1f2937", marginBottom: 6, lineHeight: 1.45 }}>
299
+ <span style={{ fontWeight: 700, color: "#4b5563" }}>Why it fits:</span>{" "}
300
+ <span style={{ fontStyle: "italic" }}>{s.relevanceReason}</span>
301
+ </div>
302
+ )}
303
+ {s.anchorQuote && (
304
+ <div
305
+ style={{
306
+ marginTop: 4,
307
+ padding: "6px 9px",
308
+ background: "rgba(0,0,0,0.025)",
309
+ borderRadius: 6,
310
+ borderLeft: "2px solid #cbd5e1",
311
+ fontSize: 11,
312
+ color: "#1f2937",
313
+ lineHeight: 1.5,
314
+ }}
315
+ >
316
+ <div style={{ fontSize: 9, fontWeight: 700, color: "#4b5563", textTransform: "uppercase", letterSpacing: "0.05em", marginBottom: 3 }}>
317
+ Where to link from - paragraph {s.anchorParagraph}
318
+ </div>
319
+ <div style={{ fontStyle: "italic" }}>"{s.anchorQuote}"</div>
320
+ {s.anchorReason && (
321
+ <div style={{ fontSize: 10, color: "#4b5563", marginTop: 3, lineHeight: 1.45 }}>
322
+ {s.anchorReason}
323
+ </div>
324
+ )}
325
+ </div>
326
+ )}
327
+ </div>
328
+ <div style={{ flexShrink: 0 }}>
329
+ <FeedbackButton
330
+ suggestionType="internal_link"
331
+ suggestionText={s.title}
332
+ publication={publication}
333
+ documentId={documentId}
334
+ />
335
+ </div>
336
+ </div>
337
+ ))}
338
+ </div>
339
+ )}
340
+ </div>
341
+ </div>
342
+ );
343
+ }