sanity-plugin-seo-copilot 0.1.0 → 0.1.2
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/README.md +50 -50
- package/dist/index.js +77 -48
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +75 -46
- package/dist/index.mjs.map +1 -1
- package/package.json +56 -56
- package/sanity.json +3 -3
- package/src/components/FeedbackButton.tsx +213 -208
- package/src/components/KeywordPanel.tsx +1173 -1165
- package/src/hooks/useKeywordAnalysis.ts +226 -225
- package/src/hooks/useSEOChat.ts +178 -169
- package/v2-incompatible.js +10 -10
package/dist/index.mjs
CHANGED
|
@@ -176,6 +176,7 @@ async function saveChatHistory(apiUrl, userId, documentId, messages, publication
|
|
|
176
176
|
|
|
177
177
|
// src/hooks/useKeywordAnalysis.ts
|
|
178
178
|
function mergeVolumes(analysis, volumeMap) {
|
|
179
|
+
if (volumeMap.size === 0) return analysis;
|
|
179
180
|
const updated = { ...analysis };
|
|
180
181
|
if (updated.primaryKeywordCandidates && updated.primaryKeywordCandidates.length > 0) {
|
|
181
182
|
updated.primaryKeywordCandidates = updated.primaryKeywordCandidates.map((candidate) => {
|
|
@@ -224,11 +225,13 @@ function useKeywordAnalysis(text, apiUrl, publication, seedKeywords, documentFie
|
|
|
224
225
|
const [isDeepLoading, setIsDeepLoading] = useState2(false);
|
|
225
226
|
const [aeoTextSnapshot, setAeoTextSnapshot] = useState2("");
|
|
226
227
|
const [aeoContentChanged, setAeoContentChanged] = useState2(false);
|
|
228
|
+
const volumeMapRef = useRef2(/* @__PURE__ */ new Map());
|
|
227
229
|
const setAnalysis = useCallback((val) => {
|
|
228
|
-
|
|
230
|
+
const withVolumes = val ? mergeVolumes(val, volumeMapRef.current) : null;
|
|
231
|
+
setAnalysisRaw(withVolumes);
|
|
229
232
|
try {
|
|
230
233
|
const prev = loadCached();
|
|
231
|
-
sessionStorage.setItem(cacheKey, JSON.stringify({ ...prev, analysis:
|
|
234
|
+
sessionStorage.setItem(cacheKey, JSON.stringify({ ...prev, analysis: withVolumes }));
|
|
232
235
|
} catch {
|
|
233
236
|
}
|
|
234
237
|
}, [cacheKey]);
|
|
@@ -246,21 +249,10 @@ function useKeywordAnalysis(text, apiUrl, publication, seedKeywords, documentFie
|
|
|
246
249
|
setAeoContentChanged(true);
|
|
247
250
|
}
|
|
248
251
|
}, [text, deepAnalysis, aeoTextSnapshot]);
|
|
249
|
-
const volumesLoadedRef = useRef2(false);
|
|
250
252
|
const lastAnalysisRef = useRef2(null);
|
|
251
|
-
const prevSeedsRef = useRef2(effectiveSeeds?.join(",") || "");
|
|
252
|
-
const prevFocusRef = useRef2(focusPrimary || "");
|
|
253
|
-
const currentSeedsKey = effectiveSeeds?.join(",") || "";
|
|
254
|
-
const currentFocusKey = focusPrimary || "";
|
|
255
|
-
if (currentSeedsKey !== prevSeedsRef.current || currentFocusKey !== prevFocusRef.current) {
|
|
256
|
-
volumesLoadedRef.current = false;
|
|
257
|
-
prevSeedsRef.current = currentSeedsKey;
|
|
258
|
-
prevFocusRef.current = currentFocusKey;
|
|
259
|
-
}
|
|
260
253
|
useEffect2(() => {
|
|
261
254
|
if ((!text || text.trim().length < 20) && (!effectiveSeeds || effectiveSeeds.length === 0)) {
|
|
262
255
|
setAnalysis(null);
|
|
263
|
-
volumesLoadedRef.current = false;
|
|
264
256
|
return;
|
|
265
257
|
}
|
|
266
258
|
let cancelled = false;
|
|
@@ -272,9 +264,18 @@ function useKeywordAnalysis(text, apiUrl, publication, seedKeywords, documentFie
|
|
|
272
264
|
lastAnalysisRef.current = analysisResult;
|
|
273
265
|
setAnalysis(analysisResult);
|
|
274
266
|
setIsLoading(false);
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
267
|
+
const candidateTerms = (analysisResult.primaryKeywordCandidates || []).map((c) => c.term);
|
|
268
|
+
const allTerms = [
|
|
269
|
+
.../* @__PURE__ */ new Set([
|
|
270
|
+
...candidateTerms,
|
|
271
|
+
analysisResult.primaryKeyword.term,
|
|
272
|
+
...(analysisResult.supportingKeywords || []).map((kw) => kw.term),
|
|
273
|
+
...analysisResult.missingKeywords.map((kw) => kw.term)
|
|
274
|
+
])
|
|
275
|
+
];
|
|
276
|
+
const uncachedTerms = allTerms.filter((t) => !volumeMapRef.current.has(t.toLowerCase()));
|
|
277
|
+
if (uncachedTerms.length > 0) {
|
|
278
|
+
doVolumeLookup(analysisResult, uncachedTerms);
|
|
278
279
|
}
|
|
279
280
|
}).catch((err) => {
|
|
280
281
|
if (!cancelled) {
|
|
@@ -286,30 +287,39 @@ function useKeywordAnalysis(text, apiUrl, publication, seedKeywords, documentFie
|
|
|
286
287
|
cancelled = true;
|
|
287
288
|
};
|
|
288
289
|
}, [text, apiUrl, publication, effectiveSeeds?.join(","), focusPrimary]);
|
|
289
|
-
const doVolumeLookup = useCallback((analysisResult,
|
|
290
|
+
const doVolumeLookup = useCallback((analysisResult, terms) => {
|
|
290
291
|
setIsVolumesLoading(true);
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
results.map((r) => [r.keyword.toLowerCase(), r])
|
|
304
|
-
);
|
|
305
|
-
setAnalysis(mergeVolumes(analysisResult, volumeMap));
|
|
292
|
+
const lookupTerms = terms || (() => {
|
|
293
|
+
const candidateTerms = (analysisResult.primaryKeywordCandidates || []).map((c) => c.term);
|
|
294
|
+
return [
|
|
295
|
+
.../* @__PURE__ */ new Set([
|
|
296
|
+
...candidateTerms,
|
|
297
|
+
analysisResult.primaryKeyword.term,
|
|
298
|
+
...(analysisResult.supportingKeywords || []).map((kw) => kw.term),
|
|
299
|
+
...analysisResult.missingKeywords.map((kw) => kw.term)
|
|
300
|
+
])
|
|
301
|
+
];
|
|
302
|
+
})();
|
|
303
|
+
if (lookupTerms.length === 0) {
|
|
306
304
|
setIsVolumesLoading(false);
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
lookupKeywords(apiUrl, lookupTerms).then(({ results }) => {
|
|
308
|
+
results.forEach((r) => volumeMapRef.current.set(r.keyword.toLowerCase(), r));
|
|
309
|
+
const current = lastAnalysisRef.current || analysisResult;
|
|
310
|
+
const merged = mergeVolumes(current, volumeMapRef.current);
|
|
311
|
+
lastAnalysisRef.current = merged;
|
|
312
|
+
setAnalysisRaw(merged);
|
|
313
|
+
try {
|
|
314
|
+
const prev = loadCached();
|
|
315
|
+
sessionStorage.setItem(cacheKey, JSON.stringify({ ...prev, analysis: merged }));
|
|
316
|
+
} catch {
|
|
310
317
|
}
|
|
318
|
+
setIsVolumesLoading(false);
|
|
319
|
+
}).catch(() => {
|
|
320
|
+
setIsVolumesLoading(false);
|
|
311
321
|
});
|
|
312
|
-
}, [apiUrl]);
|
|
322
|
+
}, [apiUrl, cacheKey]);
|
|
313
323
|
const refreshVolumes = useCallback(() => {
|
|
314
324
|
const current = lastAnalysisRef.current || analysis;
|
|
315
325
|
if (!current) return;
|
|
@@ -605,9 +615,11 @@ function SeedKeywordInput({ seeds, onSeedsChange }) {
|
|
|
605
615
|
|
|
606
616
|
// src/components/FeedbackButton.tsx
|
|
607
617
|
import { useState as useState5, useRef as useRef4, useEffect as useEffect3 } from "react";
|
|
618
|
+
import { useCurrentUser } from "sanity";
|
|
608
619
|
import { Fragment, jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
609
620
|
function FeedbackButton({ suggestionType, suggestionText, documentId, publication }) {
|
|
610
621
|
const { apiUrl } = useConfig();
|
|
622
|
+
const sanityUser = useCurrentUser();
|
|
611
623
|
const [vote, setVote] = useState5(null);
|
|
612
624
|
const [showReasonPopup, setShowReasonPopup] = useState5(false);
|
|
613
625
|
const [reason, setReason] = useState5("");
|
|
@@ -640,7 +652,8 @@ function FeedbackButton({ suggestionType, suggestionText, documentId, publicatio
|
|
|
640
652
|
suggestion_text: suggestionText,
|
|
641
653
|
vote: "down",
|
|
642
654
|
document_id: documentId,
|
|
643
|
-
publication
|
|
655
|
+
publication,
|
|
656
|
+
user_id: sanityUser?.id
|
|
644
657
|
});
|
|
645
658
|
} catch {
|
|
646
659
|
}
|
|
@@ -656,7 +669,8 @@ function FeedbackButton({ suggestionType, suggestionText, documentId, publicatio
|
|
|
656
669
|
suggestion_text: suggestionText,
|
|
657
670
|
vote: newVote,
|
|
658
671
|
document_id: documentId,
|
|
659
|
-
publication
|
|
672
|
+
publication,
|
|
673
|
+
user_id: sanityUser?.id
|
|
660
674
|
});
|
|
661
675
|
} catch {
|
|
662
676
|
}
|
|
@@ -671,7 +685,8 @@ function FeedbackButton({ suggestionType, suggestionText, documentId, publicatio
|
|
|
671
685
|
suggestion_text: suggestionText + " | REASON: " + trimmed,
|
|
672
686
|
vote: "down",
|
|
673
687
|
document_id: documentId,
|
|
674
|
-
publication
|
|
688
|
+
publication,
|
|
689
|
+
user_id: sanityUser?.id
|
|
675
690
|
});
|
|
676
691
|
} catch {
|
|
677
692
|
}
|
|
@@ -834,16 +849,18 @@ function placementCount(p) {
|
|
|
834
849
|
if (!p) return 0;
|
|
835
850
|
return [p.inBody, p.inTitle, p.inMetaDescription, p.inImages, p.inUrl].filter(Boolean).length;
|
|
836
851
|
}
|
|
837
|
-
function buildSuggestedKeywords(supporting, missing, documentText) {
|
|
852
|
+
function buildSuggestedKeywords(supporting, missing, documentText, documentFields) {
|
|
838
853
|
const merged = [];
|
|
839
854
|
const lowerText = documentText.toLowerCase();
|
|
840
855
|
for (const kw of supporting) {
|
|
841
856
|
const actuallyInContent = lowerText.includes(kw.term.toLowerCase());
|
|
842
|
-
|
|
857
|
+
const livePlacement = computePlacementClient(kw.term, documentText, documentFields);
|
|
858
|
+
merged.push({ term: kw.term, volume: kw.volume ?? 0, inContent: actuallyInContent, placement: livePlacement });
|
|
843
859
|
}
|
|
844
860
|
for (const kw of missing) {
|
|
845
861
|
const actuallyInContent = lowerText.includes(kw.term.toLowerCase());
|
|
846
|
-
|
|
862
|
+
const livePlacement = computePlacementClient(kw.term, documentText, documentFields);
|
|
863
|
+
merged.push({ term: kw.term, volume: kw.volume ?? 0, inContent: actuallyInContent, reason: kw.reason, placement: livePlacement });
|
|
847
864
|
}
|
|
848
865
|
merged.sort((a, b) => {
|
|
849
866
|
const pDiff = placementCount(b.placement) - placementCount(a.placement);
|
|
@@ -1621,9 +1638,14 @@ function KeywordPanel({
|
|
|
1621
1638
|
const suggestedKeywords = buildSuggestedKeywords(
|
|
1622
1639
|
analysis.supportingKeywords || [],
|
|
1623
1640
|
analysis.missingKeywords,
|
|
1624
|
-
text
|
|
1641
|
+
text,
|
|
1642
|
+
documentFields
|
|
1625
1643
|
);
|
|
1626
|
-
const
|
|
1644
|
+
const rawCandidates = analysis.primaryKeywordCandidates && analysis.primaryKeywordCandidates.length > 0 ? analysis.primaryKeywordCandidates : [analysis.primaryKeyword];
|
|
1645
|
+
const candidates = rawCandidates.map((c) => ({
|
|
1646
|
+
...c,
|
|
1647
|
+
placement: computePlacementClient(c.term, text, documentFields)
|
|
1648
|
+
}));
|
|
1627
1649
|
const activeTerm = candidates.find((c) => c.term === selectedPrimaryTerm) ? selectedPrimaryTerm : candidates[0]?.term ?? "";
|
|
1628
1650
|
return /* @__PURE__ */ jsxs4("div", { style: { padding: 12, display: "flex", flexDirection: "column", gap: 12 }, children: [
|
|
1629
1651
|
onSeedsChange && /* @__PURE__ */ jsx5(SeedKeywordInput, { seeds: seedKeywords, onSeedsChange }),
|
|
@@ -1777,7 +1799,8 @@ import { useDocumentPane as useDocumentPane2 } from "sanity/structure";
|
|
|
1777
1799
|
|
|
1778
1800
|
// src/hooks/useSEOChat.ts
|
|
1779
1801
|
import { useState as useState7, useCallback as useCallback3, useEffect as useEffect5, useRef as useRef6 } from "react";
|
|
1780
|
-
|
|
1802
|
+
import { useCurrentUser as useCurrentUser2 } from "sanity";
|
|
1803
|
+
function getLocalUserId() {
|
|
1781
1804
|
const key = "seo-copilot-user-id";
|
|
1782
1805
|
try {
|
|
1783
1806
|
let id = localStorage.getItem(key);
|
|
@@ -1812,7 +1835,13 @@ function useSEOChat(apiUrl, documentText, publication, keywordPanelData, documen
|
|
|
1812
1835
|
}, [chatCacheKey]);
|
|
1813
1836
|
const [isStreaming, setIsStreaming] = useState7(false);
|
|
1814
1837
|
const [historyLoaded, setHistoryLoaded] = useState7(false);
|
|
1815
|
-
const
|
|
1838
|
+
const sanityUser = useCurrentUser2();
|
|
1839
|
+
const userId = useRef6(sanityUser?.id || getLocalUserId());
|
|
1840
|
+
useEffect5(() => {
|
|
1841
|
+
if (sanityUser?.id && userId.current !== sanityUser.id) {
|
|
1842
|
+
userId.current = sanityUser.id;
|
|
1843
|
+
}
|
|
1844
|
+
}, [sanityUser?.id]);
|
|
1816
1845
|
const saveTimerRef = useRef6(null);
|
|
1817
1846
|
useEffect5(() => {
|
|
1818
1847
|
if (!documentId || historyLoaded) return;
|