sanity-plugin-seo-copilot 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.
@@ -0,0 +1,291 @@
1
+ import { useState, useRef, useEffect } from "react";
2
+ import ReactMarkdown from "react-markdown";
3
+ import remarkGfm from "remark-gfm";
4
+ import { useDocumentPane } from "sanity/structure";
5
+ import { useSEOChat } from "../hooks/useSEOChat";
6
+ import { useConfig } from "./ConfigContext";
7
+ import { LoadingBars } from "./SEOCopilotPanel";
8
+ import { MJH_GOLD, MJH_BLUE, USER_BLUE, USER_BLUE_BORDER, MJH_SLATE } from "./styles";
9
+
10
+ interface KeywordPanelData {
11
+ primaryKeyword?: { term: string; volume: number | null };
12
+ supportingKeywords?: { term: string; volume: number | null }[];
13
+ missingKeywords?: { term: string; volume: number | null }[];
14
+ aeoData?: {
15
+ questionHeadings: { suggestedHeading: string; rationale: string }[];
16
+ faqSuggestions: { question: string; answer: string }[];
17
+ };
18
+ }
19
+
20
+ interface ChatPanelProps {
21
+ text: string;
22
+ publication?: string;
23
+ keywordPanelData?: KeywordPanelData;
24
+ }
25
+
26
+ const SUGGESTIONS = [
27
+ "What keywords am I missing?",
28
+ "How do I optimize for AEO?",
29
+ "Chat about primary and supporting keywords",
30
+ "What semantically related terms should I include?",
31
+ ];
32
+
33
+ function LogoIcon() {
34
+ return (
35
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
36
+ <rect x="1" y="3" width="4" height="10" rx="1" fill={MJH_GOLD} opacity="1" />
37
+ <rect x="6" y="1" width="4" height="14" rx="1" fill={MJH_GOLD} opacity="0.85" />
38
+ <rect x="11" y="5" width="4" height="8" rx="1" fill={MJH_GOLD} opacity="0.6" />
39
+ </svg>
40
+ );
41
+ }
42
+
43
+ export function ChatPanel({ text, publication, keywordPanelData }: ChatPanelProps) {
44
+ const { apiUrl } = useConfig();
45
+ const { value } = useDocumentPane();
46
+ const documentId = (value?._id as string | undefined) || undefined;
47
+ const { messages, isStreaming, sendMessage, clearChat } = useSEOChat(apiUrl, text, publication, keywordPanelData, documentId);
48
+ const [input, setInput] = useState("");
49
+ const [inputFocused, setInputFocused] = useState(false);
50
+ const [hoveredSuggestion, setHoveredSuggestion] = useState<number | null>(null);
51
+ const [clearHovered, setClearHovered] = useState(false);
52
+ const messagesEndRef = useRef<HTMLDivElement>(null);
53
+
54
+ useEffect(() => {
55
+ messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
56
+ }, [messages]);
57
+
58
+ const handleSend = () => {
59
+ const trimmed = input.trim();
60
+ if (!trimmed || isStreaming) return;
61
+ setInput("");
62
+ sendMessage(trimmed);
63
+ };
64
+
65
+ const handleKeyDown = (e: React.KeyboardEvent) => {
66
+ if (e.key === "Enter" && !e.shiftKey) {
67
+ e.preventDefault();
68
+ handleSend();
69
+ }
70
+ };
71
+
72
+ const showTypingBars = isStreaming && (
73
+ messages.length === 0 ||
74
+ messages[messages.length - 1].role === "user" ||
75
+ (messages[messages.length - 1].role === "assistant" && !messages[messages.length - 1].content)
76
+ );
77
+
78
+ return (
79
+ <div style={{ position: "relative", flex: 1, minHeight: 0 }}>
80
+ {/* Absolute inner creates a definite-height context so flex children respect bounds */}
81
+ <div style={{ position: "absolute", top: 0, left: 0, right: 0, bottom: 0, display: "flex", flexDirection: "column" }}>
82
+ {/* Messages Area - scrollable */}
83
+ <div style={{ flex: 1, overflowY: "auto", padding: 12, minHeight: 0 }}>
84
+ {messages.length === 0 && !isStreaming ? (
85
+ <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
86
+ {/* Welcome */}
87
+ <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 4 }}>
88
+ <LogoIcon />
89
+ <span style={{ fontSize: 12, fontWeight: 600, color: "#6b7280" }}>
90
+ Welcome to SEO Copilot! Ask anything about your content.
91
+ </span>
92
+ </div>
93
+ {/* Suggestion prompts */}
94
+ {SUGGESTIONS.map((s, i) => (
95
+ <button
96
+ key={i}
97
+ onClick={() => sendMessage(s)}
98
+ onMouseEnter={() => setHoveredSuggestion(i)}
99
+ onMouseLeave={() => setHoveredSuggestion(null)}
100
+ style={{
101
+ width: "100%",
102
+ textAlign: "left",
103
+ padding: "10px 14px",
104
+ borderRadius: 12,
105
+ fontSize: 12.5,
106
+ color: "#475569",
107
+ background: hoveredSuggestion === i ? USER_BLUE : "#ffffff",
108
+ border: `1px solid ${hoveredSuggestion === i ? USER_BLUE_BORDER : "#f1f5f9"}`,
109
+ boxShadow: "0 1px 2px rgba(0,0,0,0.03)",
110
+ cursor: "pointer",
111
+ transition: "all 200ms ease",
112
+ }}
113
+ >
114
+ {s}
115
+ </button>
116
+ ))}
117
+ </div>
118
+ ) : (
119
+ <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
120
+ {messages.map((msg, i) => {
121
+ // Skip empty assistant messages (typing bars show instead)
122
+ if (msg.role === "assistant" && !msg.content) return null;
123
+ return (
124
+ <div
125
+ key={i}
126
+ style={{
127
+ display: "flex",
128
+ justifyContent: msg.role === "user" ? "flex-end" : "flex-start",
129
+ animation: "fade-up 0.2s ease-out forwards",
130
+ }}
131
+ >
132
+ <div
133
+ style={{
134
+ maxWidth: "88%",
135
+ padding: "10px 14px",
136
+ fontSize: 12.5,
137
+ lineHeight: 1.65,
138
+ borderRadius: 18,
139
+ ...(msg.role === "user"
140
+ ? {
141
+ background: USER_BLUE,
142
+ border: `1px solid ${USER_BLUE_BORDER}`,
143
+ boxShadow: "0 1px 3px rgba(0,93,172,0.08)",
144
+ borderBottomRightRadius: 6,
145
+ color: "#1e293b",
146
+ }
147
+ : {
148
+ background: "#ffffff",
149
+ boxShadow: "0 1px 2px rgba(0,0,0,0.04)",
150
+ borderBottomLeftRadius: 6,
151
+ color: "#475569",
152
+ }),
153
+ }}
154
+ >
155
+ {msg.role === "assistant" ? (
156
+ <div className="prose-chat">
157
+ <ReactMarkdown remarkPlugins={[remarkGfm]}>{msg.content}</ReactMarkdown>
158
+ </div>
159
+ ) : (
160
+ <span style={{ whiteSpace: "pre-wrap" }}>{msg.content}</span>
161
+ )}
162
+ {isStreaming && i === messages.length - 1 && msg.role === "assistant" && msg.content && (
163
+ <span style={{ display: "inline-flex", alignItems: "flex-end", gap: 2, height: 10, marginLeft: 4, verticalAlign: "middle" }}>
164
+ {[1, 2, 3].map((n) => (
165
+ <span
166
+ key={n}
167
+ style={{
168
+ width: 2,
169
+ height: 6,
170
+ borderRadius: 1,
171
+ background: MJH_GOLD,
172
+ animation: `bar-shift-${n} 1.4s ease-in-out infinite`,
173
+ }}
174
+ />
175
+ ))}
176
+ </span>
177
+ )}
178
+ </div>
179
+ </div>
180
+ );
181
+ })}
182
+
183
+ {/* Typing bars */}
184
+ {showTypingBars && (
185
+ <div style={{ display: "flex", justifyContent: "flex-start" }}>
186
+ <div
187
+ style={{
188
+ padding: 12,
189
+ borderRadius: 16,
190
+ borderBottomLeftRadius: 4,
191
+ background: "#ffffff",
192
+ boxShadow: "0 1px 3px rgba(0,0,0,0.06)",
193
+ }}
194
+ >
195
+ <LoadingBars size="xs" />
196
+ </div>
197
+ </div>
198
+ )}
199
+
200
+ <div ref={messagesEndRef} />
201
+ </div>
202
+ )}
203
+ </div>
204
+
205
+ {/* Input Bar - always at bottom */}
206
+ <div
207
+ style={{
208
+ display: "flex",
209
+ gap: 8,
210
+ padding: 12,
211
+ borderTop: "1px solid #f3f4f6",
212
+ background: "rgba(255,255,255,0.9)",
213
+ backdropFilter: "blur(8px)",
214
+ flexShrink: 0,
215
+ }}
216
+ >
217
+ <input
218
+ type="text"
219
+ value={input}
220
+ onChange={(e) => setInput(e.target.value)}
221
+ onKeyDown={handleKeyDown}
222
+ onFocus={() => setInputFocused(true)}
223
+ onBlur={() => setInputFocused(false)}
224
+ placeholder="Ask about SEO..."
225
+ disabled={isStreaming}
226
+ style={{
227
+ flex: 1,
228
+ padding: "8px 12px",
229
+ fontSize: 12,
230
+ borderRadius: 12,
231
+ border: `1px solid ${inputFocused ? MJH_BLUE : "#e5e7eb"}`,
232
+ background: "#ffffff",
233
+ boxShadow: inputFocused ? "0 0 0 3px rgba(0,93,172,0.1)" : "0 1px 2px rgba(0,0,0,0.04)",
234
+ outline: "none",
235
+ opacity: isStreaming ? 0.4 : 1,
236
+ transition: "border-color 150ms, box-shadow 150ms",
237
+ }}
238
+ />
239
+ <button
240
+ onClick={handleSend}
241
+ disabled={isStreaming || !input.trim()}
242
+ style={{
243
+ padding: "8px 12px",
244
+ borderRadius: 12,
245
+ border: "none",
246
+ color: "#ffffff",
247
+ background: `linear-gradient(135deg, ${MJH_BLUE}, #004A8A)`,
248
+ boxShadow: "0 2px 6px rgba(0,93,172,0.3)",
249
+ cursor: isStreaming || !input.trim() ? "not-allowed" : "pointer",
250
+ opacity: isStreaming || !input.trim() ? 0.4 : 1,
251
+ transition: "all 150ms",
252
+ display: "flex",
253
+ alignItems: "center",
254
+ }}
255
+ >
256
+ <svg width="16" height="16" viewBox="0 0 20 20" fill="currentColor">
257
+ <path d="M3.105 2.289a.75.75 0 00-.826.95l1.414 4.925A1.5 1.5 0 005.135 9.25h6.115a.75.75 0 010 1.5H5.135a1.5 1.5 0 00-1.442 1.086l-1.414 4.926a.75.75 0 00.826.95 28.896 28.896 0 0015.293-7.154.75.75 0 000-1.115A28.897 28.897 0 003.105 2.289z" />
258
+ </svg>
259
+ </button>
260
+ </div>
261
+
262
+ {/* Clear */}
263
+ {messages.length > 0 && (
264
+ <div style={{ display: "flex", justifyContent: "flex-end", padding: "0 12px 8px", flexShrink: 0 }}>
265
+ <button
266
+ onClick={clearChat}
267
+ onMouseEnter={() => setClearHovered(true)}
268
+ onMouseLeave={() => setClearHovered(false)}
269
+ style={{
270
+ background: "none",
271
+ border: "none",
272
+ fontSize: 10,
273
+ color: clearHovered ? "#ef4444" : "#d1d5db",
274
+ cursor: "pointer",
275
+ display: "flex",
276
+ alignItems: "center",
277
+ gap: 4,
278
+ transition: "color 150ms",
279
+ }}
280
+ >
281
+ <svg width="12" height="12" viewBox="0 0 20 20" fill="currentColor">
282
+ <path fillRule="evenodd" d="M8.75 1A2.75 2.75 0 006 3.75v.443c-.795.077-1.584.176-2.365.298a.75.75 0 10.23 1.482l.149-.022.841 10.518A2.75 2.75 0 007.596 19h4.807a2.75 2.75 0 002.742-2.53l.841-10.519.149.023a.75.75 0 00.23-1.482A41.03 41.03 0 0014 4.193V3.75A2.75 2.75 0 0011.25 1h-2.5zM10 4c.84 0 1.673.025 2.5.075V3.75c0-.69-.56-1.25-1.25-1.25h-2.5c-.69 0-1.25.56-1.25 1.25v.325C8.327 4.025 9.16 4 10 4zM8.58 7.72a.75.75 0 00-1.5.06l.3 7.5a.75.75 0 101.5-.06l-.3-7.5zm4.34.06a.75.75 0 10-1.5-.06l-.3 7.5a.75.75 0 101.5.06l.3-7.5z" clipRule="evenodd" />
283
+ </svg>
284
+ Clear
285
+ </button>
286
+ </div>
287
+ )}
288
+ </div>
289
+ </div>
290
+ );
291
+ }
@@ -0,0 +1,22 @@
1
+ import { createContext, useContext, type ReactNode } from "react";
2
+ import type { SEOCopilotConfig } from "../index";
3
+
4
+ const ConfigContext = createContext<SEOCopilotConfig>({
5
+ apiUrl: "http://localhost:3000",
6
+ });
7
+
8
+ export function ConfigProvider({
9
+ config,
10
+ children,
11
+ }: {
12
+ config: SEOCopilotConfig;
13
+ children: ReactNode;
14
+ }) {
15
+ return (
16
+ <ConfigContext.Provider value={config}>{children}</ConfigContext.Provider>
17
+ );
18
+ }
19
+
20
+ export function useConfig(): SEOCopilotConfig {
21
+ return useContext(ConfigContext);
22
+ }
@@ -0,0 +1,208 @@
1
+ import { useState, useRef, useEffect } from "react";
2
+ import { useConfig } from "./ConfigContext";
3
+ import { submitFeedback } from "../lib/api";
4
+
5
+ interface FeedbackButtonProps {
6
+ suggestionType: string;
7
+ suggestionText: string;
8
+ documentId?: string;
9
+ publication?: string;
10
+ }
11
+
12
+ export function FeedbackButton({ suggestionType, suggestionText, documentId, publication }: FeedbackButtonProps) {
13
+ const { apiUrl } = useConfig();
14
+ const [vote, setVote] = useState<"up" | "down" | null>(null);
15
+ const [showReasonPopup, setShowReasonPopup] = useState(false);
16
+ const [reason, setReason] = useState("");
17
+ const [submitted, setSubmitted] = useState(false);
18
+ const popupRef = useRef<HTMLDivElement>(null);
19
+ const inputRef = useRef<HTMLInputElement>(null);
20
+
21
+ // Close popup on outside click
22
+ useEffect(() => {
23
+ if (!showReasonPopup) return;
24
+ const handler = (e: MouseEvent) => {
25
+ if (popupRef.current && !popupRef.current.contains(e.target as Node)) {
26
+ setShowReasonPopup(false);
27
+ }
28
+ };
29
+ document.addEventListener("mousedown", handler);
30
+ return () => document.removeEventListener("mousedown", handler);
31
+ }, [showReasonPopup]);
32
+
33
+ // Focus input when popup opens
34
+ useEffect(() => {
35
+ if (showReasonPopup) {
36
+ setTimeout(() => inputRef.current?.focus(), 50);
37
+ }
38
+ }, [showReasonPopup]);
39
+
40
+ const handleVote = async (v: "up" | "down") => {
41
+ if (v === "down" && vote !== "down") {
42
+ setVote("down");
43
+ setShowReasonPopup(true);
44
+ setSubmitted(false);
45
+ // Submit the vote immediately
46
+ try {
47
+ await submitFeedback(apiUrl, {
48
+ suggestion_type: suggestionType,
49
+ suggestion_text: suggestionText,
50
+ vote: "down",
51
+ document_id: documentId,
52
+ publication,
53
+ });
54
+ } catch { /* silent */ }
55
+ return;
56
+ }
57
+
58
+ const newVote = vote === v ? null : v;
59
+ setVote(newVote);
60
+ setShowReasonPopup(false);
61
+ if (newVote) {
62
+ try {
63
+ await submitFeedback(apiUrl, {
64
+ suggestion_type: suggestionType,
65
+ suggestion_text: suggestionText,
66
+ vote: newVote,
67
+ document_id: documentId,
68
+ publication,
69
+ });
70
+ } catch { /* silent */ }
71
+ }
72
+ };
73
+
74
+ const handleSubmitReason = async () => {
75
+ const trimmed = reason.trim();
76
+ if (trimmed) {
77
+ try {
78
+ await submitFeedback(apiUrl, {
79
+ suggestion_type: suggestionType + "_reason",
80
+ suggestion_text: suggestionText + " | REASON: " + trimmed,
81
+ vote: "down",
82
+ document_id: documentId,
83
+ publication,
84
+ });
85
+ } catch { /* silent */ }
86
+ }
87
+ setSubmitted(true);
88
+ setTimeout(() => {
89
+ setShowReasonPopup(false);
90
+ setReason("");
91
+ }, 1000);
92
+ };
93
+
94
+ return (
95
+ <div style={{ display: "flex", alignItems: "center", gap: 2, flexShrink: 0, position: "relative" }}>
96
+ <button
97
+ onClick={() => handleVote("up")}
98
+ style={{
99
+ background: "none",
100
+ border: "none",
101
+ cursor: "pointer",
102
+ padding: 1,
103
+ fontSize: 12,
104
+ color: vote === "up" ? "#16a34a" : "#d1d5db",
105
+ transition: "color 150ms",
106
+ lineHeight: 1,
107
+ display: "flex",
108
+ }}
109
+ title="Good suggestion"
110
+ >
111
+ <svg width="12" height="12" viewBox="0 0 20 20" fill="currentColor">
112
+ <path d="M1 8.25a1.25 1.25 0 112.5 0v7.5a1.25 1.25 0 11-2.5 0v-7.5zM11 3V1.7c0-.268.14-.526.395-.607A2 2 0 0114 3c0 .995-.182 1.948-.514 2.826-.204.54.166 1.174.744 1.174h2.52c1.243 0 2.261 1.01 2.146 2.247a23.864 23.864 0 01-1.341 5.974 1.637 1.637 0 01-1.555 1.029H6.5V7.5L11 3z" />
113
+ </svg>
114
+ </button>
115
+ <button
116
+ onClick={() => handleVote("down")}
117
+ style={{
118
+ background: "none",
119
+ border: "none",
120
+ cursor: "pointer",
121
+ padding: 1,
122
+ fontSize: 12,
123
+ color: vote === "down" ? "#dc2626" : "#d1d5db",
124
+ transition: "color 150ms",
125
+ lineHeight: 1,
126
+ display: "flex",
127
+ }}
128
+ title="Not helpful"
129
+ >
130
+ <svg width="12" height="12" viewBox="0 0 20 20" fill="currentColor">
131
+ <path d="M19 11.75a1.25 1.25 0 11-2.5 0v-7.5a1.25 1.25 0 112.5 0v7.5zM9 17v1.3c0 .268-.14.526-.395.607A2 2 0 016 17c0-.995.182-1.948.514-2.826.204-.54-.166-1.174-.744-1.174H3.25c-1.243 0-2.261-1.01-2.146-2.247a23.864 23.864 0 011.341-5.974A1.637 1.637 0 014 3.75h9.5V12.5L9 17z" />
132
+ </svg>
133
+ </button>
134
+
135
+ {/* Thumbs down reason popup */}
136
+ {showReasonPopup && (
137
+ <div
138
+ ref={popupRef}
139
+ style={{
140
+ position: "absolute",
141
+ top: "100%",
142
+ right: 0,
143
+ marginTop: 4,
144
+ width: 220,
145
+ padding: 10,
146
+ borderRadius: 10,
147
+ background: "#ffffff",
148
+ boxShadow: "0 4px 16px rgba(0,0,0,0.15), 0 1px 4px rgba(0,0,0,0.1)",
149
+ zIndex: 100,
150
+ border: "1px solid #e5e7eb",
151
+ }}
152
+ >
153
+ {submitted ? (
154
+ <div style={{ fontSize: 11, color: "#16a34a", textAlign: "center", padding: "4px 0" }}>
155
+ Thanks for the feedback!
156
+ </div>
157
+ ) : (
158
+ <>
159
+ <div style={{ fontSize: 11, fontWeight: 600, color: "#374151", marginBottom: 6 }}>
160
+ Sorry! What went wrong?
161
+ </div>
162
+ <input
163
+ ref={inputRef}
164
+ type="text"
165
+ value={reason}
166
+ onChange={(e) => setReason(e.target.value)}
167
+ onKeyDown={(e) => { if (e.key === "Enter") handleSubmitReason(); }}
168
+ placeholder="Tell us what to improve..."
169
+ style={{
170
+ width: "100%",
171
+ padding: "5px 8px",
172
+ fontSize: 11,
173
+ borderRadius: 6,
174
+ border: "1px solid #e5e7eb",
175
+ outline: "none",
176
+ fontFamily: "inherit",
177
+ boxSizing: "border-box",
178
+ }}
179
+ />
180
+ <div style={{ display: "flex", justifyContent: "flex-end", marginTop: 6, gap: 4 }}>
181
+ <button
182
+ onClick={() => { setShowReasonPopup(false); setReason(""); }}
183
+ style={{
184
+ padding: "3px 8px", fontSize: 10, borderRadius: 4,
185
+ border: "1px solid #e5e7eb", background: "#ffffff", color: "#6b7280",
186
+ cursor: "pointer",
187
+ }}
188
+ >
189
+ Skip
190
+ </button>
191
+ <button
192
+ onClick={handleSubmitReason}
193
+ style={{
194
+ padding: "3px 8px", fontSize: 10, borderRadius: 4,
195
+ border: "none", background: "#dc2626", color: "#ffffff",
196
+ cursor: "pointer", fontWeight: 600,
197
+ }}
198
+ >
199
+ Submit
200
+ </button>
201
+ </div>
202
+ </>
203
+ )}
204
+ </div>
205
+ )}
206
+ </div>
207
+ );
208
+ }
@@ -0,0 +1,50 @@
1
+ import { MJH_BLUE } from "./styles";
2
+ import { PlacementIndicators } from "./PlacementIndicators";
3
+ import type { KeywordPlacement } from "../lib/types";
4
+
5
+ interface KeywordBadgeProps {
6
+ term: string;
7
+ volume?: number | null;
8
+ type: "primary" | "supporting" | "missing";
9
+ inContent?: boolean;
10
+ placement?: KeywordPlacement;
11
+ }
12
+
13
+ const colorMap = {
14
+ primary: { bg: "rgba(220,252,231,0.8)", text: "#166534" },
15
+ supporting: { bg: "rgba(0,93,172,0.1)", text: MJH_BLUE },
16
+ missing: { bg: "rgba(230,192,27,0.15)", text: "#8B7310" },
17
+ };
18
+
19
+ function formatVolume(volume: number | null | undefined): string {
20
+ if (volume === null || volume === undefined) return "";
21
+ if (volume === 0) return "N/A";
22
+ return volume.toLocaleString();
23
+ }
24
+
25
+ export function KeywordBadge({ term, volume, type, inContent, placement }: KeywordBadgeProps) {
26
+ const colors = colorMap[type];
27
+ const volumeStr = formatVolume(volume);
28
+
29
+ return (
30
+ <div style={{ display: "flex", alignItems: "center", gap: 6, padding: "2px 0", flexWrap: "wrap" }}>
31
+ <span
32
+ style={{
33
+ borderRadius: 99,
34
+ padding: "2px 10px",
35
+ fontSize: 12,
36
+ fontWeight: 500,
37
+ whiteSpace: "nowrap",
38
+ background: colors.bg,
39
+ color: colors.text,
40
+ }}
41
+ >
42
+ {term}
43
+ </span>
44
+ {volumeStr && (
45
+ <span style={{ fontSize: 12, color: "#9ca3af" }}>{volumeStr === "N/A" ? volumeStr : `${volumeStr}/mo`}</span>
46
+ )}
47
+ <PlacementIndicators placement={placement} />
48
+ </div>
49
+ );
50
+ }