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,76 @@
1
+ import { useState } from "react";
2
+ import type { KeywordPlacement } from "../lib/types";
3
+
4
+ interface PlacementIndicatorsProps {
5
+ placement?: KeywordPlacement;
6
+ }
7
+
8
+ const indicators = [
9
+ { key: "inBody" as const, letter: "B", tooltip: "Keyword in body" },
10
+ { key: "inTitle" as const, letter: "T", tooltip: "Keyword in title" },
11
+ { key: "inMetaDescription" as const, letter: "D", tooltip: "Keyword in meta description" },
12
+ { key: "inImages" as const, letter: "I", tooltip: "Keyword in image names" },
13
+ { key: "inUrl" as const, letter: "U", tooltip: "Keyword in URL" },
14
+ ];
15
+
16
+ function IndicatorDot({ letter, active, tooltip }: { letter: string; active: boolean; tooltip: string }) {
17
+ const [hovered, setHovered] = useState(false);
18
+
19
+ return (
20
+ <span
21
+ onMouseEnter={() => setHovered(true)}
22
+ onMouseLeave={() => setHovered(false)}
23
+ style={{
24
+ position: "relative",
25
+ display: "inline-flex",
26
+ alignItems: "center",
27
+ justifyContent: "center",
28
+ width: 16,
29
+ height: 16,
30
+ borderRadius: "50%",
31
+ fontSize: 8,
32
+ fontWeight: 700,
33
+ lineHeight: 1,
34
+ cursor: "default",
35
+ flexShrink: 0,
36
+ background: active ? "#16a34a" : "transparent",
37
+ color: active ? "#ffffff" : "#d1d5db",
38
+ border: active ? "none" : "1.5px solid #d1d5db",
39
+ transition: "all 150ms",
40
+ }}
41
+ >
42
+ {letter}
43
+ {hovered && (
44
+ <span style={{
45
+ position: "absolute",
46
+ bottom: "100%",
47
+ left: "50%",
48
+ transform: "translateX(-50%)",
49
+ marginBottom: 4,
50
+ padding: "2px 6px",
51
+ borderRadius: 4,
52
+ background: "#1f2937",
53
+ color: "#ffffff",
54
+ fontSize: 10,
55
+ whiteSpace: "nowrap",
56
+ pointerEvents: "none",
57
+ zIndex: 10,
58
+ }}>
59
+ {tooltip}
60
+ </span>
61
+ )}
62
+ </span>
63
+ );
64
+ }
65
+
66
+ export function PlacementIndicators({ placement }: PlacementIndicatorsProps) {
67
+ if (!placement) return null;
68
+
69
+ return (
70
+ <div style={{ display: "flex", alignItems: "center", gap: 2 }}>
71
+ {indicators.map(({ key, letter, tooltip }) => (
72
+ <IndicatorDot key={key} letter={letter} active={placement[key]} tooltip={tooltip} />
73
+ ))}
74
+ </div>
75
+ );
76
+ }
@@ -0,0 +1,550 @@
1
+ import { useState, useRef, useCallback, useEffect, useMemo, Suspense } from "react";
2
+ import { createPortal } from "react-dom";
3
+ import { useDocumentContent } from "../hooks/useDocumentContent";
4
+ import { useKeywordAnalysis } from "../hooks/useKeywordAnalysis";
5
+ import { useConfig, ConfigProvider } from "./ConfigContext";
6
+ import { KeywordPanel } from "./KeywordPanel";
7
+ import { ChatPanel } from "./ChatPanel";
8
+ import { INJECTED_CSS, MJH_GOLD, MJH_BLUE, MJH_SLATE } from "./styles";
9
+ import type { SEOCopilotConfig } from "../index";
10
+ import type { SEOAnalysis, DeepAnalysis } from "../lib/types";
11
+
12
+ interface SEOCopilotPanelProps {
13
+ config: SEOCopilotConfig;
14
+ onClose?: () => void;
15
+ }
16
+
17
+ function LogoIcon() {
18
+ return (
19
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
20
+ <rect x="1" y="3" width="4" height="10" rx="1" fill={MJH_GOLD} opacity="1" />
21
+ <rect x="6" y="1" width="4" height="14" rx="1" fill={MJH_GOLD} opacity="0.85" />
22
+ <rect x="11" y="5" width="4" height="8" rx="1" fill={MJH_GOLD} opacity="0.6" />
23
+ </svg>
24
+ );
25
+ }
26
+
27
+ interface PanelContentProps {
28
+ initialPublication?: string;
29
+ isFloating?: boolean;
30
+ isFullscreen?: boolean;
31
+ onToggleFloat?: () => void;
32
+ onToggleFullscreen?: () => void;
33
+ onClose?: () => void;
34
+ floatPos?: { x: number; y: number };
35
+ onDragStart?: (e: React.MouseEvent) => void;
36
+ seedKeywords: string[];
37
+ onSeedsChange: (seeds: string[]) => void;
38
+ text: string;
39
+ documentFields: import("../lib/types").DocumentFields;
40
+ onSelectPrimary?: (term: string) => void;
41
+ externalAnalysis: {
42
+ analysis: SEOAnalysis | null;
43
+ isLoading: boolean;
44
+ isVolumesLoading: boolean;
45
+ error: string | null;
46
+ deepAnalysis: DeepAnalysis | null;
47
+ isDeepLoading: boolean;
48
+ runDeepAnalysis: () => void;
49
+ refreshVolumes: () => void;
50
+ aeoContentChanged: boolean;
51
+ };
52
+ }
53
+
54
+ function PanelContent({ initialPublication, isFloating, isFullscreen, onToggleFloat, onToggleFullscreen, onClose, floatPos: _floatPos, onDragStart, seedKeywords, onSeedsChange, text, documentFields, onSelectPrimary, externalAnalysis }: PanelContentProps) {
55
+ const publication = initialPublication;
56
+ const [keywordsOpen, setKeywordsOpen] = useState(true);
57
+ const [chatOpen, setChatOpen] = useState(true);
58
+ const isLoading = externalAnalysis.isLoading;
59
+ const keywordAnalysisState = externalAnalysis;
60
+
61
+ // Build keyword panel data for the chat
62
+ const keywordPanelData = useMemo(() => {
63
+ const { analysis } = keywordAnalysisState;
64
+ if (!analysis) return undefined;
65
+ return {
66
+ primaryKeyword: { term: analysis.primaryKeyword.term, volume: analysis.primaryKeyword.volume },
67
+ supportingKeywords: (analysis.supportingKeywords || []).map((kw) => ({ term: kw.term, volume: kw.volume })),
68
+ missingKeywords: analysis.missingKeywords.map((kw) => ({ term: kw.term, volume: kw.volume })),
69
+ aeoData: keywordAnalysisState.deepAnalysis?.aeo ? {
70
+ questionHeadings: (keywordAnalysisState.deepAnalysis.aeo.questionHeadings || []).map((h) => ({
71
+ suggestedHeading: h.suggestedHeading,
72
+ rationale: h.rationale,
73
+ })),
74
+ faqSuggestions: (keywordAnalysisState.deepAnalysis.aeo.faqSuggestions || []).map((f) => ({
75
+ question: f.question,
76
+ answer: f.answer,
77
+ })),
78
+ } : undefined,
79
+ };
80
+ }, [keywordAnalysisState.analysis, keywordAnalysisState.deepAnalysis]);
81
+
82
+ // Draggable divider
83
+ const [splitPercent, setSplitPercent] = useState(75);
84
+ const containerRef = useRef<HTMLDivElement>(null);
85
+ const dragging = useRef(false);
86
+
87
+ const onMouseDown = useCallback(() => {
88
+ dragging.current = true;
89
+ document.body.style.cursor = "row-resize";
90
+ document.body.style.userSelect = "none";
91
+ }, []);
92
+
93
+ useEffect(() => {
94
+ const onMouseMove = (e: MouseEvent) => {
95
+ if (!dragging.current || !containerRef.current) return;
96
+ const rect = containerRef.current.getBoundingClientRect();
97
+ const pct = ((e.clientY - rect.top) / rect.height) * 100;
98
+ setSplitPercent(Math.min(85, Math.max(15, pct)));
99
+ };
100
+ const onMouseUp = () => {
101
+ dragging.current = false;
102
+ document.body.style.cursor = "";
103
+ document.body.style.userSelect = "";
104
+ };
105
+ window.addEventListener("mousemove", onMouseMove);
106
+ window.addEventListener("mouseup", onMouseUp);
107
+ return () => {
108
+ window.removeEventListener("mousemove", onMouseMove);
109
+ window.removeEventListener("mouseup", onMouseUp);
110
+ };
111
+ }, []);
112
+
113
+ // Header cursor: grab when floating (draggable), default otherwise
114
+ const headerCursor = isFloating ? "grab" : "default";
115
+
116
+ return (
117
+ <div className="seo-copilot-panel" style={{ height: "100%", display: "flex", flexDirection: "column", background: "#FBFAF7", fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif", opacity: 1 }}>
118
+ <style dangerouslySetInnerHTML={{ __html: INJECTED_CSS }} />
119
+
120
+ {/* Header */}
121
+ <div
122
+ onMouseDown={isFloating ? onDragStart : undefined}
123
+ style={{
124
+ position: "relative",
125
+ display: "flex",
126
+ alignItems: "center",
127
+ justifyContent: "space-between",
128
+ padding: "14px 16px",
129
+ background: "linear-gradient(180deg, #FFFFFF 0%, #FBFAF7 100%)",
130
+ boxShadow: "0 1px 0 rgba(0,0,0,0.04)",
131
+ zIndex: 10,
132
+ cursor: headerCursor,
133
+ userSelect: "none",
134
+ flexShrink: 0,
135
+ }}
136
+ >
137
+ {/* Logo - vertically centered in header */}
138
+ <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
139
+ <LogoIcon />
140
+ <span style={{ fontWeight: 700, fontSize: 15, letterSpacing: "-0.01em" }}>
141
+ <span style={{ color: MJH_GOLD }}>SEO</span>
142
+ <span style={{ color: MJH_SLATE }}> copilot</span>
143
+ </span>
144
+ </div>
145
+ <div style={{ display: "flex", alignItems: "center", gap: isFloating ? 4 : 6 }}>
146
+ {/* When floating: labeled action buttons */}
147
+ {isFloating && onToggleFullscreen && (
148
+ <button
149
+ onClick={(e) => { e.stopPropagation(); onToggleFullscreen(); }}
150
+ onMouseDown={(e) => e.stopPropagation()}
151
+ style={{
152
+ background: isFullscreen ? "rgba(230,192,27,0.15)" : "rgba(0,0,0,0.04)",
153
+ border: "none",
154
+ borderRadius: 6,
155
+ cursor: "pointer",
156
+ padding: "4px 8px",
157
+ display: "flex",
158
+ alignItems: "center",
159
+ gap: 4,
160
+ color: isFullscreen ? "#8B7310" : "#6b7280",
161
+ fontSize: 10,
162
+ fontWeight: 600,
163
+ transition: "all 150ms",
164
+ }}
165
+ >
166
+ {isFullscreen ? (
167
+ <>
168
+ <svg width="12" height="12" viewBox="0 0 20 20" fill="currentColor">
169
+ <path d="M3.28 2.22a.75.75 0 00-1.06 1.06L5.44 6.5H3.75a.75.75 0 000 1.5h4.5A.75.75 0 009 7.25v-4.5a.75.75 0 00-1.5 0v1.69L4.28 1.22a.75.75 0 00-1 1zM16.72 17.78a.75.75 0 001.06-1.06L14.56 13.5h1.69a.75.75 0 000-1.5h-4.5a.75.75 0 00-.75.75v4.5a.75.75 0 001.5 0v-1.69l3.22 3.22z" />
170
+ </svg>
171
+ Exit fullscreen
172
+ </>
173
+ ) : (
174
+ <>
175
+ <svg width="12" height="12" viewBox="0 0 20 20" fill="currentColor">
176
+ <path d="M13.28 7.78l3.22-3.22v1.69a.75.75 0 001.5 0v-4.5a.75.75 0 00-.75-.75h-4.5a.75.75 0 000 1.5h1.69l-3.22 3.22a.75.75 0 001.06 1.06zM2 17.25a.75.75 0 001.5 0v-1.69l3.22 3.22a.75.75 0 001.06-1.06L4.56 14.5h1.69a.75.75 0 000-1.5h-4.5a.75.75 0 00-.75.75v4.5z" />
177
+ </svg>
178
+ Fullscreen
179
+ </>
180
+ )}
181
+ </button>
182
+ )}
183
+ {isFloating && onToggleFloat && (
184
+ <button
185
+ onClick={(e) => { e.stopPropagation(); onToggleFloat(); }}
186
+ onMouseDown={(e) => e.stopPropagation()}
187
+ style={{
188
+ background: "rgba(0,93,172,0.08)",
189
+ border: "none",
190
+ borderRadius: 6,
191
+ cursor: "pointer",
192
+ padding: "4px 8px",
193
+ display: "flex",
194
+ alignItems: "center",
195
+ gap: 4,
196
+ color: MJH_BLUE,
197
+ fontSize: 10,
198
+ fontWeight: 600,
199
+ transition: "all 150ms",
200
+ }}
201
+ >
202
+ <svg width="12" height="12" viewBox="0 0 20 20" fill="currentColor">
203
+ <path fillRule="evenodd" d="M4.25 2A2.25 2.25 0 002 4.25v11.5A2.25 2.25 0 004.25 18h11.5A2.25 2.25 0 0018 15.75V4.25A2.25 2.25 0 0015.75 2H4.25zM3.5 4.25a.75.75 0 01.75-.75h11.5a.75.75 0 01.75.75v11.5a.75.75 0 01-.75.75H4.25a.75.75 0 01-.75-.75V4.25z" clipRule="evenodd" />
204
+ </svg>
205
+ Dock
206
+ </button>
207
+ )}
208
+ {/* When docked: small icon buttons */}
209
+ {!isFloating && onToggleFloat && (
210
+ <button
211
+ onClick={(e) => { e.stopPropagation(); onToggleFloat(); }}
212
+ onMouseDown={(e) => e.stopPropagation()}
213
+ style={{
214
+ background: "none",
215
+ border: "none",
216
+ cursor: "pointer",
217
+ padding: 2,
218
+ display: "flex",
219
+ alignItems: "center",
220
+ color: "#9ca3af",
221
+ transition: "color 150ms",
222
+ }}
223
+ title="Pop out"
224
+ >
225
+ <svg width="14" height="14" viewBox="0 0 20 20" fill="currentColor">
226
+ <path d="M4.25 5.5a.75.75 0 00-.75.75v8.5c0 .414.336.75.75.75h8.5a.75.75 0 00.75-.75v-4a.75.75 0 011.5 0v4A2.25 2.25 0 0112.75 17h-8.5A2.25 2.25 0 012 14.75v-8.5A2.25 2.25 0 014.25 4h5a.75.75 0 010 1.5h-5z" />
227
+ <path d="M6.194 12.753a.75.75 0 001.06.053L16.5 4.44v2.81a.75.75 0 001.5 0v-4.5a.75.75 0 00-.75-.75h-4.5a.75.75 0 000 1.5h2.553l-9.056 8.194a.75.75 0 00-.053 1.06z" />
228
+ </svg>
229
+ </button>
230
+ )}
231
+ {isLoading && (
232
+ <div style={{ width: 8, height: 8, borderRadius: "50%", background: MJH_GOLD, animation: "content-pulse 2s ease-in-out infinite" }} />
233
+ )}
234
+ {/* Close button - always visible */}
235
+ {onClose && (
236
+ <button
237
+ onClick={(e) => { e.stopPropagation(); onClose(); }}
238
+ onMouseDown={(e) => e.stopPropagation()}
239
+ style={{
240
+ background: "none",
241
+ border: "none",
242
+ cursor: "pointer",
243
+ padding: 2,
244
+ display: "flex",
245
+ alignItems: "center",
246
+ color: "#9ca3af",
247
+ transition: "color 150ms",
248
+ }}
249
+ title="Close"
250
+ >
251
+ <svg width="14" height="14" viewBox="0 0 20 20" fill="currentColor">
252
+ <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />
253
+ </svg>
254
+ </button>
255
+ )}
256
+ </div>
257
+ {/* Tagline - absolute positioned, not affecting vertical centering */}
258
+ <div style={{
259
+ position: "absolute",
260
+ bottom: 3,
261
+ left: 38,
262
+ fontSize: 9,
263
+ color: "#b0b8c4",
264
+ fontWeight: 400,
265
+ fontStyle: "italic",
266
+ letterSpacing: "0.02em",
267
+ }}>
268
+ More eyes on your expertise
269
+ </div>
270
+ </div>
271
+
272
+ {/* Split View */}
273
+ <div ref={containerRef} style={{ flex: 1, display: "flex", flexDirection: "column", overflow: "hidden" }}>
274
+ {/* Keywords Section */}
275
+ <div style={{ height: keywordsOpen ? (chatOpen ? `${splitPercent}%` : "100%") : "auto", overflow: keywordsOpen ? "auto" : "hidden", flexShrink: keywordsOpen ? undefined : 0 }}>
276
+ <button
277
+ onClick={() => setKeywordsOpen(!keywordsOpen)}
278
+ style={{ width: "100%", padding: "4px 12px", display: "flex", alignItems: "center", justifyContent: "space-between", background: "transparent", border: "none", borderBottom: "1px solid rgba(0,0,0,0.06)", cursor: "pointer" }}
279
+ >
280
+ <span style={{ display: "flex", alignItems: "center", gap: 6, flex: 1 }}>
281
+ <span style={{ fontSize: 10, fontWeight: 600, textTransform: "uppercase", letterSpacing: "0.05em", color: "#9ca3af" }}>Keywords & AEO Insights</span>
282
+ {(keywordAnalysisState.isLoading || keywordAnalysisState.isVolumesLoading || keywordAnalysisState.isDeepLoading) && (
283
+ <LoadingBars size="xs" color="#9ca3af" />
284
+ )}
285
+ </span>
286
+ <svg width="12" height="12" viewBox="0 0 20 20" fill="#9ca3af" style={{ transform: keywordsOpen ? "rotate(0deg)" : "rotate(-90deg)", transition: "transform 200ms" }}>
287
+ <path fillRule="evenodd" d="M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z" clipRule="evenodd" />
288
+ </svg>
289
+ </button>
290
+ {/* Always mounted to preserve state; hidden when collapsed. key=publication forces reload on brand change */}
291
+ <div style={{ display: keywordsOpen ? "block" : "none" }}>
292
+ <KeywordPanel
293
+ text={text}
294
+ publication={publication}
295
+ seedKeywords={seedKeywords}
296
+ onSeedsChange={onSeedsChange}
297
+ onSelectPrimary={onSelectPrimary}
298
+ documentFields={documentFields}
299
+ externalAnalysis={keywordAnalysisState}
300
+ />
301
+ </div>
302
+ </div>
303
+
304
+ {/* Draggable Divider - only when both open */}
305
+ {keywordsOpen && chatOpen && (
306
+ <div
307
+ onMouseDown={onMouseDown}
308
+ style={{
309
+ height: 6,
310
+ cursor: "row-resize",
311
+ background: "linear-gradient(180deg, rgba(0,0,0,0.04) 0%, rgba(0,0,0,0.08) 50%, rgba(0,0,0,0.04) 100%)",
312
+ display: "flex",
313
+ alignItems: "center",
314
+ justifyContent: "center",
315
+ flexShrink: 0,
316
+ }}
317
+ >
318
+ <div style={{ width: 32, height: 2, borderRadius: 1, background: "#d1d5db" }} />
319
+ </div>
320
+ )}
321
+
322
+ {/* Chat Section - fills entire panel when keywords collapsed */}
323
+ <div style={{
324
+ flex: chatOpen && !keywordsOpen ? 1 : undefined,
325
+ height: chatOpen ? (keywordsOpen ? `${100 - splitPercent}%` : undefined) : "auto",
326
+ overflow: "hidden",
327
+ display: "flex",
328
+ flexDirection: "column",
329
+ flexShrink: chatOpen ? undefined : 0,
330
+ minHeight: 0,
331
+ }}>
332
+ <button
333
+ onClick={() => setChatOpen(!chatOpen)}
334
+ style={{ width: "100%", padding: "4px 12px", display: "flex", alignItems: "center", justifyContent: "space-between", background: "transparent", border: "none", borderBottom: "1px solid rgba(0,0,0,0.06)", cursor: "pointer" }}
335
+ >
336
+ <span style={{ fontSize: 10, fontWeight: 600, textTransform: "uppercase", letterSpacing: "0.05em", color: "#9ca3af" }}>Chat</span>
337
+ <svg width="12" height="12" viewBox="0 0 20 20" fill="#9ca3af" style={{ transform: chatOpen ? "rotate(0deg)" : "rotate(-90deg)", transition: "transform 200ms" }}>
338
+ <path fillRule="evenodd" d="M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z" clipRule="evenodd" />
339
+ </svg>
340
+ </button>
341
+ {/* Always mounted to preserve chat state; hidden when collapsed. key=publication forces reload on brand change */}
342
+ <div style={{ flex: chatOpen ? 1 : 0, overflow: "hidden", display: chatOpen ? "flex" : "none", flexDirection: "column", minHeight: 0 }}>
343
+ <ChatPanel text={text} publication={publication} keywordPanelData={keywordPanelData} />
344
+ </div>
345
+ </div>
346
+ </div>
347
+ </div>
348
+ );
349
+ }
350
+
351
+ // Inner component that hosts lifted state and renders PanelContent.
352
+ // By keeping this wrapper stable in the React tree, PanelContent never remounts on float toggle.
353
+ function SEOCopilotInner({ config, onClose }: { config: SEOCopilotConfig; onClose?: () => void }) {
354
+ const [isFloating, setIsFloating] = useState(false);
355
+ const [isFullscreen, setIsFullscreen] = useState(false);
356
+ const [floatPos, setFloatPos] = useState({ x: typeof window !== "undefined" ? window.innerWidth - 420 : 400, y: 80 });
357
+ const [floatSize, setFloatSize] = useState({ width: 380, height: typeof window !== "undefined" ? Math.round(window.innerHeight * 0.8) : 600 });
358
+ const dragOffset = useRef<{ dx: number; dy: number } | null>(null);
359
+ const resizeStart = useRef<{ startX: number; startY: number; startW: number; startH: number } | null>(null);
360
+
361
+ const { text, isLoading: docLoading, documentFields, documentId } = useDocumentContent();
362
+ const { apiUrl } = useConfig();
363
+
364
+ // Lifted state - survives float/dock toggles. Seeds cached per-document in sessionStorage.
365
+ const seedCacheKey = documentId ? `seo-copilot-cache-${documentId}` : "seo-copilot-cache-v2";
366
+ const [seedKeywords, setSeedKeywordsRaw] = useState<string[]>(() => {
367
+ try { const c = sessionStorage.getItem(seedCacheKey); if (c) return JSON.parse(c).seeds || []; } catch {} return [];
368
+ });
369
+ const setSeedKeywords = useCallback((seeds: string[]) => {
370
+ setSeedKeywordsRaw(seeds);
371
+ try { const c = JSON.parse(sessionStorage.getItem(seedCacheKey) || "{}"); sessionStorage.setItem(seedCacheKey, JSON.stringify({ ...c, seeds })); } catch {}
372
+ }, [seedCacheKey]);
373
+ const [selectedPrimary, setSelectedPrimary] = useState<string | undefined>();
374
+ const keywordAnalysisState = useKeywordAnalysis(text, apiUrl, config.publication, seedKeywords, documentFields, selectedPrimary, documentId);
375
+
376
+ const handleDragStart = useCallback((e: React.MouseEvent) => {
377
+ dragOffset.current = {
378
+ dx: e.clientX - floatPos.x,
379
+ dy: e.clientY - floatPos.y,
380
+ };
381
+
382
+ const onMouseMove = (ev: MouseEvent) => {
383
+ if (!dragOffset.current) return;
384
+ setFloatPos({
385
+ x: ev.clientX - dragOffset.current.dx,
386
+ y: ev.clientY - dragOffset.current.dy,
387
+ });
388
+ };
389
+ const onMouseUp = () => {
390
+ dragOffset.current = null;
391
+ window.removeEventListener("mousemove", onMouseMove);
392
+ window.removeEventListener("mouseup", onMouseUp);
393
+ document.body.style.cursor = "";
394
+ document.body.style.userSelect = "";
395
+ };
396
+ document.body.style.cursor = "grabbing";
397
+ document.body.style.userSelect = "none";
398
+ window.addEventListener("mousemove", onMouseMove);
399
+ window.addEventListener("mouseup", onMouseUp);
400
+ }, [floatPos]);
401
+
402
+ const handleResizeStart = useCallback((e: React.MouseEvent) => {
403
+ e.preventDefault();
404
+ e.stopPropagation();
405
+ resizeStart.current = { startX: e.clientX, startY: e.clientY, startW: floatSize.width, startH: floatSize.height };
406
+ document.body.style.cursor = "nwse-resize";
407
+ document.body.style.userSelect = "none";
408
+ const onMouseMove = (ev: MouseEvent) => {
409
+ if (!resizeStart.current) return;
410
+ const newW = Math.max(320, resizeStart.current.startW + (ev.clientX - resizeStart.current.startX));
411
+ const newH = Math.max(300, resizeStart.current.startH + (ev.clientY - resizeStart.current.startY));
412
+ setFloatSize({ width: newW, height: newH });
413
+ };
414
+ const onMouseUp = () => {
415
+ resizeStart.current = null;
416
+ document.body.style.cursor = "";
417
+ document.body.style.userSelect = "";
418
+ window.removeEventListener("mousemove", onMouseMove);
419
+ window.removeEventListener("mouseup", onMouseUp);
420
+ };
421
+ window.addEventListener("mousemove", onMouseMove);
422
+ window.addEventListener("mouseup", onMouseUp);
423
+ }, [floatSize]);
424
+
425
+ // Unused var appeasement
426
+ void docLoading;
427
+
428
+ const panelContent = (
429
+ <PanelContent
430
+ initialPublication={config.publication}
431
+ isFloating={isFloating}
432
+ isFullscreen={isFullscreen}
433
+ onToggleFloat={() => { setIsFloating((v) => !v); setIsFullscreen(false); }}
434
+ onToggleFullscreen={() => setIsFullscreen((v) => !v)}
435
+ onClose={onClose}
436
+ floatPos={floatPos}
437
+ onDragStart={handleDragStart}
438
+ seedKeywords={seedKeywords}
439
+ onSeedsChange={setSeedKeywords}
440
+ text={text}
441
+ documentFields={documentFields}
442
+ onSelectPrimary={setSelectedPrimary}
443
+ externalAnalysis={{
444
+ analysis: keywordAnalysisState.analysis,
445
+ isLoading: keywordAnalysisState.isLoading,
446
+ isVolumesLoading: keywordAnalysisState.isVolumesLoading,
447
+ error: keywordAnalysisState.error,
448
+ deepAnalysis: keywordAnalysisState.deepAnalysis,
449
+ isDeepLoading: keywordAnalysisState.isDeepLoading,
450
+ runDeepAnalysis: keywordAnalysisState.runDeepAnalysis,
451
+ refreshVolumes: keywordAnalysisState.refreshVolumes,
452
+ aeoContentChanged: keywordAnalysisState.aeoContentChanged,
453
+ }}
454
+ />
455
+ );
456
+
457
+ // When floating, render via portal to document.body to escape Sanity's
458
+ // stacking contexts (transforms/will-change trap position:fixed elements).
459
+ // When docked, render inline in the inspector.
460
+ const floatingPanel = (
461
+ <div
462
+ style={{
463
+ position: "fixed",
464
+ left: isFullscreen ? 0 : floatPos.x,
465
+ top: isFullscreen ? 0 : floatPos.y,
466
+ width: isFullscreen ? "100vw" : floatSize.width,
467
+ height: isFullscreen ? "100vh" : floatSize.height,
468
+ zIndex: 2147483647,
469
+ borderRadius: isFullscreen ? 0 : 12,
470
+ overflow: "hidden",
471
+ boxShadow: isFullscreen ? "none" : "0 8px 32px rgba(0,0,0,0.18), 0 2px 8px rgba(0,0,0,0.10)",
472
+ border: isFullscreen ? "none" : "1px solid rgba(0,0,0,0.08)",
473
+ background: "#FBFAF7",
474
+ }}
475
+ >
476
+ {panelContent}
477
+ {!isFullscreen && (
478
+ <div
479
+ onMouseDown={handleResizeStart}
480
+ style={{
481
+ position: "absolute", bottom: 0, right: 0, width: 16, height: 16,
482
+ cursor: "nwse-resize", display: "flex", alignItems: "center", justifyContent: "center",
483
+ }}
484
+ >
485
+ <svg width="10" height="10" viewBox="0 0 10 10" fill="#9ca3af">
486
+ <path d="M9 1v8H1" fill="none" stroke="#9ca3af" strokeWidth="1.5" strokeLinecap="round" />
487
+ <path d="M9 5v4H5" fill="none" stroke="#9ca3af" strokeWidth="1.5" strokeLinecap="round" />
488
+ </svg>
489
+ </div>
490
+ )}
491
+ </div>
492
+ );
493
+
494
+ if (isFloating) {
495
+ return (
496
+ <>
497
+ {/* Empty placeholder in inspector so Sanity doesn't collapse it */}
498
+ <div style={{ height: "100%", background: "#FBFAF7", display: "flex", alignItems: "center", justifyContent: "center" }}>
499
+ <span style={{ fontSize: 12, color: "#9ca3af" }}>SEO Copilot is floating</span>
500
+ </div>
501
+ {typeof document !== "undefined" && createPortal(floatingPanel, document.body)}
502
+ </>
503
+ );
504
+ }
505
+
506
+ return (
507
+ <div style={{ height: "100%", background: "#FBFAF7", position: "relative" as const, zIndex: 1 }}>
508
+ {panelContent}
509
+ </div>
510
+ );
511
+ }
512
+
513
+ export default function SEOCopilotPanel({ config, onClose }: SEOCopilotPanelProps) {
514
+ return (
515
+ <Suspense
516
+ fallback={
517
+ <div style={{ padding: 16, display: "flex", alignItems: "center", gap: 12 }}>
518
+ <LoadingBars />
519
+ <span style={{ fontSize: 12, color: "#9ca3af" }}>Loading SEO Copilot...</span>
520
+ </div>
521
+ }
522
+ >
523
+ <ConfigProvider config={config}>
524
+ <SEOCopilotInner config={config} onClose={onClose} />
525
+ </ConfigProvider>
526
+ </Suspense>
527
+ );
528
+ }
529
+
530
+ export function LoadingBars({ size = "sm", color = MJH_GOLD }: { size?: "xs" | "sm"; color?: string }) {
531
+ const barWidth = size === "xs" ? 4 : 6;
532
+ const containerHeight = size === "xs" ? 10 : 14;
533
+
534
+ return (
535
+ <div style={{ display: "inline-flex", alignItems: "flex-end", gap: 2, height: containerHeight }}>
536
+ {[1, 2, 3].map((i) => (
537
+ <div
538
+ key={i}
539
+ style={{
540
+ width: barWidth,
541
+ height: 8,
542
+ borderRadius: 2,
543
+ background: color,
544
+ animation: `bar-shift-${i} 1.4s ease-in-out infinite`,
545
+ }}
546
+ />
547
+ ))}
548
+ </div>
549
+ );
550
+ }