sanity-plugin-seo-copilot 0.1.2 → 0.2.1

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,384 @@
1
+ import { useMemo, useState } from "react";
2
+ import { LoadingBars } from "../SEOCopilotPanel";
3
+ import { ShimmerLoader, DEFAULT_GENERATE_ALL_MESSAGES } from "../ShimmerLoader";
4
+ import { calculateContentScoreDetailed, type ContentScore } from "../../lib/scoring";
5
+ import { buildSummaryItems } from "../../lib/summary";
6
+ import { MJH_BLUE, MJH_GOLD } from "../styles";
7
+ import type {
8
+ SEOAnalysis,
9
+ DeepAnalysis,
10
+ DocumentFields,
11
+ LinkingSuggestion,
12
+ LinkCheckResult,
13
+ SummaryItem,
14
+ SummarySeverity,
15
+ } from "../../lib/types";
16
+
17
+ interface SummaryTabProps {
18
+ text: string;
19
+ analysis: SEOAnalysis | null;
20
+ deepAnalysis: DeepAnalysis | null;
21
+ documentFields?: DocumentFields;
22
+ linkingSuggestions: LinkingSuggestion[];
23
+ linkCheckResults?: LinkCheckResult[];
24
+ isAnyLoading: boolean;
25
+ onGenerateAll: () => void;
26
+ onItemClick: (item: SummaryItem) => void;
27
+ }
28
+
29
+ type FilterKey = "all" | "error" | "warning" | "opportunity";
30
+
31
+ const cardStyle: React.CSSProperties = {
32
+ borderRadius: 14,
33
+ background: "rgba(255,255,255,0.55)",
34
+ backdropFilter: "blur(16px) saturate(170%)",
35
+ WebkitBackdropFilter: "blur(16px) saturate(170%)",
36
+ 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)",
37
+ padding: "14px",
38
+ };
39
+
40
+ // Display labels for the "Go to ..." jump buttons. MUST stay in sync with the tab
41
+ // labels in SEOCopilotPanel's tabDefs - notably the internal id "technical" is
42
+ // shown to editors as "Other Recs", and "aeo" as "AEO/GEO".
43
+ const TAB_LABEL: Record<string, string> = {
44
+ summary: "Summary",
45
+ keywords: "Keywords",
46
+ aeo: "AEO/GEO",
47
+ linking: "Linking",
48
+ meta: "Meta",
49
+ technical: "Other Recs",
50
+ };
51
+
52
+ function scoreColors(score: number): { bg: string; text: string; label: string } {
53
+ if (score >= 70) return { bg: "rgba(22,163,74,0.1)", text: "#16a34a", label: "Strong" };
54
+ if (score >= 40) return { bg: "rgba(230,192,27,0.15)", text: "#8B7310", label: "Decent" };
55
+ return { bg: "rgba(220,38,38,0.08)", text: "#dc2626", label: "Needs work" };
56
+ }
57
+
58
+ // The score card. The breakdown is ALWAYS visible - the score is just a roll-up
59
+ // of the six visible components below it. No hidden state, no "click to see why".
60
+ function ScoreBadge({ score, breakdown }: { score: number; breakdown: ContentScore["components"] }) {
61
+ const c = scoreColors(score);
62
+ return (
63
+ <div style={cardStyle}>
64
+ {/* Top row: number + label */}
65
+ <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
66
+ <div
67
+ style={{
68
+ width: 56,
69
+ height: 56,
70
+ borderRadius: "50%",
71
+ background: c.bg,
72
+ display: "flex",
73
+ flexDirection: "column",
74
+ alignItems: "center",
75
+ justifyContent: "center",
76
+ flexShrink: 0,
77
+ lineHeight: 1,
78
+ }}
79
+ >
80
+ <span style={{ fontSize: 20, fontWeight: 700, color: c.text }}>{score}</span>
81
+ <span style={{ fontSize: 8, fontWeight: 700, color: c.text, opacity: 0.7, letterSpacing: "0.06em", marginTop: 2 }}>
82
+ / 100
83
+ </span>
84
+ </div>
85
+ <div style={{ flex: 1, minWidth: 0 }}>
86
+ <div style={{ fontSize: 10.5, textTransform: "uppercase", letterSpacing: "0.06em", color: "#4b5563", fontWeight: 600, marginBottom: 2 }}>
87
+ Content Score
88
+ </div>
89
+ <div style={{ fontSize: 14, fontWeight: 700, color: c.text }}>{c.label}</div>
90
+ </div>
91
+ </div>
92
+
93
+ {/* Always-visible breakdown - each row shows what's driving the score */}
94
+ {breakdown.length > 0 && (
95
+ <div style={{ marginTop: 12, display: "flex", flexDirection: "column", gap: 8 }}>
96
+ {breakdown.map((comp, i) => {
97
+ const pct = comp.max > 0 ? (comp.points / comp.max) * 100 : 0;
98
+ const barColor = pct >= 75 ? "#16a34a" : pct >= 40 ? "#E6C01B" : "#dc2626";
99
+ return (
100
+ <div key={i}>
101
+ <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 3 }}>
102
+ <span style={{ fontSize: 11, fontWeight: 600, color: "#1f2937" }}>{comp.label}</span>
103
+ <span style={{ fontSize: 11, color: "#4b5563", fontVariantNumeric: "tabular-nums" }}>
104
+ {comp.points} / {comp.max}
105
+ </span>
106
+ </div>
107
+ <div style={{ height: 4, background: "#f1f5f9", borderRadius: 99, overflow: "hidden", marginBottom: 4 }}>
108
+ <div style={{ width: `${pct}%`, height: "100%", background: barColor, transition: "width 200ms" }} />
109
+ </div>
110
+ <div style={{ fontSize: 10.5, color: "#4b5563", lineHeight: 1.4 }}>{comp.detail}</div>
111
+ </div>
112
+ );
113
+ })}
114
+ </div>
115
+ )}
116
+ </div>
117
+ );
118
+ }
119
+
120
+ function severityVisual(s: SummarySeverity) {
121
+ if (s === "error") return { dot: "#dc2626", bg: "rgba(220,38,38,0.08)" };
122
+ if (s === "warning") return { dot: "#E6C01B", bg: "rgba(230,192,27,0.15)" };
123
+ return { dot: MJH_BLUE, bg: "rgba(0,93,172,0.06)" };
124
+ }
125
+
126
+ // One issue / warning / opportunity. Everything is visible up front - no expand
127
+ // state, no "click to see why". Clicking the card jumps to the source tab.
128
+ function ItemCard({ item, onClick }: { item: SummaryItem; onClick: () => void }) {
129
+ const v = severityVisual(item.severity);
130
+ const tab = item.jumpTo.tab;
131
+ const tabLabel = TAB_LABEL[tab] ?? tab.charAt(0).toUpperCase() + tab.slice(1);
132
+ return (
133
+ <div
134
+ onClick={onClick}
135
+ style={{
136
+ padding: "10px 12px",
137
+ background: "rgba(255,255,255,0.55)",
138
+ backdropFilter: "blur(16px) saturate(170%)",
139
+ WebkitBackdropFilter: "blur(16px) saturate(170%)",
140
+ borderRadius: 10,
141
+ boxShadow: "inset 0 0 0 1px rgba(255,255,255,0.5), inset 1.2px 1.5px 0 -1px rgba(255,255,255,0.85), 0 1px 3px rgba(0,0,0,0.05), 0 4px 12px rgba(0,0,0,0.05)",
142
+ display: "flex",
143
+ flexDirection: "column",
144
+ gap: 6,
145
+ cursor: "pointer",
146
+ }}
147
+ >
148
+ <div style={{ display: "flex", alignItems: "flex-start", gap: 9 }}>
149
+ <div
150
+ style={{
151
+ width: 8,
152
+ height: 8,
153
+ borderRadius: "50%",
154
+ background: v.dot,
155
+ marginTop: 5,
156
+ flexShrink: 0,
157
+ }}
158
+ />
159
+ <div style={{ flex: 1, minWidth: 0 }}>
160
+ <div style={{ fontSize: 12, fontWeight: 700, color: "#1f2937", marginBottom: 2 }}>{item.label}</div>
161
+ <div style={{ fontSize: 11, color: "#374151", lineHeight: 1.5 }}>{item.description}</div>
162
+ </div>
163
+ </div>
164
+ {/* How to fix - always visible, inline, severity-tinted */}
165
+ <div
166
+ style={{
167
+ padding: "7px 10px",
168
+ background: v.bg,
169
+ borderRadius: 6,
170
+ fontSize: 11,
171
+ color: "#1f2937",
172
+ lineHeight: 1.55,
173
+ }}
174
+ >
175
+ <span style={{ fontWeight: 700, color: "#4b5563", letterSpacing: "0.04em", fontSize: 9.5, textTransform: "uppercase", marginRight: 6 }}>
176
+ Fix
177
+ </span>
178
+ {item.howToFix}
179
+ </div>
180
+ {item.jumpTo.tab !== "summary" && (
181
+ <div style={{ display: "flex", justifyContent: "flex-end" }}>
182
+ <span style={{ fontSize: 10, fontWeight: 700, color: MJH_BLUE }}>
183
+ Go to {tabLabel} ›
184
+ </span>
185
+ </div>
186
+ )}
187
+ </div>
188
+ );
189
+ }
190
+
191
+ function FilterChip({
192
+ active,
193
+ onClick,
194
+ label,
195
+ count,
196
+ dotColor,
197
+ }: {
198
+ active: boolean;
199
+ onClick: () => void;
200
+ label: string;
201
+ count: number;
202
+ dotColor?: string;
203
+ }) {
204
+ return (
205
+ <button
206
+ onClick={onClick}
207
+ style={{
208
+ padding: "4px 9px",
209
+ background: active ? "#1f2937" : "#ffffff",
210
+ color: active ? "#ffffff" : "#4b5563",
211
+ border: active ? "1px solid #1f2937" : "1px solid rgba(0,0,0,0.08)",
212
+ borderRadius: 99,
213
+ cursor: "pointer",
214
+ fontSize: 11,
215
+ fontWeight: 600,
216
+ display: "flex",
217
+ alignItems: "center",
218
+ gap: 5,
219
+ transition: "all 150ms",
220
+ }}
221
+ >
222
+ {dotColor && (
223
+ <span style={{ width: 6, height: 6, borderRadius: "50%", background: dotColor, display: "inline-block" }} />
224
+ )}
225
+ <span>{label}</span>
226
+ <span style={{ fontSize: 10, opacity: 0.8 }}>{count}</span>
227
+ </button>
228
+ );
229
+ }
230
+
231
+ export function SummaryTab({
232
+ text,
233
+ analysis,
234
+ deepAnalysis,
235
+ documentFields,
236
+ linkingSuggestions,
237
+ linkCheckResults,
238
+ isAnyLoading,
239
+ onGenerateAll,
240
+ onItemClick,
241
+ }: SummaryTabProps) {
242
+ const scoreDetail = useMemo(() => {
243
+ if (!analysis) return null;
244
+ return calculateContentScoreDetailed(text, analysis);
245
+ }, [text, analysis]);
246
+ const score = scoreDetail?.total ?? null;
247
+
248
+ const items = useMemo(
249
+ () =>
250
+ buildSummaryItems({
251
+ analysis,
252
+ deepAnalysis,
253
+ documentFields,
254
+ contentScore: score,
255
+ linkingSuggestions,
256
+ linkCheckResults,
257
+ text,
258
+ }),
259
+ [analysis, deepAnalysis, documentFields, score, linkingSuggestions, linkCheckResults, text]
260
+ );
261
+
262
+ const [filter, setFilter] = useState<FilterKey>("all");
263
+ const counts = useMemo(
264
+ () => ({
265
+ all: items.length,
266
+ error: items.filter((i) => i.severity === "error").length,
267
+ warning: items.filter((i) => i.severity === "warning").length,
268
+ opportunity: items.filter((i) => i.severity === "opportunity").length,
269
+ }),
270
+ [items]
271
+ );
272
+ const filtered = filter === "all" ? items : items.filter((i) => i.severity === filter);
273
+
274
+ if (!analysis) {
275
+ return (
276
+ <div style={{ padding: 24, textAlign: "center" }}>
277
+ <div style={{ fontSize: 13, color: "#4b5563", fontWeight: 500 }}>Start with the Keywords tab</div>
278
+ <div style={{ fontSize: 11, color: "#4b5563", marginTop: 4 }}>
279
+ Once keywords are analyzed, the summary appears here.
280
+ </div>
281
+ </div>
282
+ );
283
+ }
284
+
285
+ return (
286
+ <div style={{ padding: 12, display: "flex", flexDirection: "column", gap: 12 }}>
287
+ {/* Content Score badge */}
288
+ {score != null && scoreDetail && <ScoreBadge score={score} breakdown={scoreDetail.components} />}
289
+
290
+ {/* Generate everything - runs the analyses that fill the AEO/GEO, Linking,
291
+ and Other Recs tabs in parallel. Their results flow back into this Summary
292
+ via buildSummaryItems, so editors see new issues here as each one finishes. */}
293
+ <div style={cardStyle}>
294
+ {isAnyLoading ? (
295
+ <div style={{ display: "flex", alignItems: "center", justifyContent: "center", padding: "4px 0" }}>
296
+ <ShimmerLoader messages={DEFAULT_GENERATE_ALL_MESSAGES} />
297
+ </div>
298
+ ) : (
299
+ <button
300
+ onClick={onGenerateAll}
301
+ disabled={!text || text.trim().length < 100}
302
+ style={{
303
+ width: "100%",
304
+ padding: "10px 14px",
305
+ fontSize: 12,
306
+ fontWeight: 700,
307
+ color: !text || text.trim().length < 100 ? "#9ca3af" : "#1f2937",
308
+ background: !text || text.trim().length < 100 ? "#e5e7eb" : "linear-gradient(135deg, rgba(230,192,27,0.82), rgba(230,192,27,0.98))",
309
+ backdropFilter: "blur(10px) saturate(160%)",
310
+ WebkitBackdropFilter: "blur(10px) saturate(160%)",
311
+ boxShadow: !text || text.trim().length < 100 ? "none" : "inset 0 1px 1px rgba(255,255,255,0.6), inset 0 -1px 2px rgba(0,0,0,0.12), 0 2px 8px rgba(230,192,27,0.32)",
312
+ border: "none",
313
+ borderRadius: 8,
314
+ cursor: !text || text.trim().length < 100 ? "not-allowed" : "pointer",
315
+ display: "flex",
316
+ alignItems: "center",
317
+ justifyContent: "center",
318
+ gap: 8,
319
+ transition: "all 150ms",
320
+ letterSpacing: "0.01em",
321
+ }}
322
+ >
323
+ Generate everything
324
+ </button>
325
+ )}
326
+ <div style={{ fontSize: 10.5, color: "#4b5563", marginTop: 8, lineHeight: 1.5 }}>
327
+ Runs the analyses that fill the <strong>AEO/GEO</strong>, <strong>Linking</strong>, and <strong>Other Recs</strong> tabs. New issues they find appear in this Summary as each one finishes.
328
+ </div>
329
+ </div>
330
+
331
+ {/* Filter chips */}
332
+ <div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
333
+ <FilterChip active={filter === "all"} onClick={() => setFilter("all")} label="All" count={counts.all} />
334
+ <FilterChip active={filter === "error"} onClick={() => setFilter("error")} label="Errors" count={counts.error} dotColor="#dc2626" />
335
+ <FilterChip active={filter === "warning"} onClick={() => setFilter("warning")} label="Warnings" count={counts.warning} dotColor="#E6C01B" />
336
+ <FilterChip active={filter === "opportunity"} onClick={() => setFilter("opportunity")} label="Opportunities" count={counts.opportunity} dotColor={MJH_BLUE} />
337
+ </div>
338
+
339
+ {/* Items list */}
340
+ <div id="anchor-summary-issues" style={{ display: "flex", flexDirection: "column", gap: 8 }}>
341
+ {filtered.length === 0 ? (
342
+ <div style={{ ...cardStyle, textAlign: "center", padding: "20px 14px" }}>
343
+ {items.length === 0 ? (
344
+ <>
345
+ <div
346
+ style={{
347
+ width: 40,
348
+ height: 40,
349
+ borderRadius: "50%",
350
+ background: "rgba(22,163,74,0.1)",
351
+ display: "flex",
352
+ alignItems: "center",
353
+ justifyContent: "center",
354
+ margin: "0 auto 8px",
355
+ }}
356
+ >
357
+ <svg width="20" height="20" viewBox="0 0 20 20" fill="#16a34a">
358
+ <path fillRule="evenodd" d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z" clipRule="evenodd" />
359
+ </svg>
360
+ </div>
361
+ <div style={{ fontSize: 13, color: "#16a34a", fontWeight: 700 }}>Looking good</div>
362
+ <div style={{ fontSize: 11, color: "#4b5563", marginTop: 2 }}>
363
+ No issues found for this article.
364
+ </div>
365
+ </>
366
+ ) : (
367
+ <div style={{ fontSize: 12, color: "#4b5563" }}>Nothing in this category.</div>
368
+ )}
369
+ </div>
370
+ ) : (
371
+ filtered.map((item) => <ItemCard key={item.id} item={item} onClick={() => onItemClick(item)} />)
372
+ )}
373
+ </div>
374
+
375
+ {/* Loading hint when something's still running and we have partial data */}
376
+ {isAnyLoading && items.length > 0 && (
377
+ <div style={{ display: "flex", alignItems: "center", justifyContent: "center", gap: 6, padding: "4px 0" }}>
378
+ <LoadingBars size="xs" color={MJH_BLUE} />
379
+ <span style={{ fontSize: 10, color: "#4b5563" }}>Still checking...</span>
380
+ </div>
381
+ )}
382
+ </div>
383
+ );
384
+ }