import React, { useState } from 'react'; import { __, _n, sprintf } from '@wordpress/i18n'; import { Badge, Tabs } from './ui'; import KeywordSuggest from './KeywordSuggest'; import { IconX } from './icons'; const GROUPS = [ { id: 'shorttail', label: __('Short-tail', 'ai-rank-suite'), hint: __('Broad terms, 1–2 words. The first one is your primary focus keyword.', 'ai-rank-suite'), placeholder: __('geo optimization', 'ai-rank-suite'), }, { id: 'longtail', label: __('Long-tail', 'ai-rank-suite'), hint: __('Specific phrases, 3+ words. These win the most AI citations.', 'ai-rank-suite'), placeholder: __('how to optimize a website for ChatGPT', 'ai-rank-suite'), }, { id: 'geo', label: __('AI search prompts', 'ai-rank-suite'), hint: __('Questions people actually type into ChatGPT, Claude or Perplexity.', 'ai-rank-suite'), placeholder: __('What is GEO optimization and how do I start?', 'ai-rank-suite'), }, ]; function ScoreDot({ score }) { const color = score >= 70 ? 'var(--ars-good)' : score >= 40 ? 'var(--ars-warning)' : 'var(--ars-serious)'; return ( ); } export default function KeywordEditor({ keywords, coverage, max = 100, onChange, postId = 0 }) { const [group, setGroup] = useState('shorttail'); const [draft, setDraft] = useState(''); const [bulk, setBulk] = useState(false); const [editing, setEditing] = useState(null); const [editText, setEditText] = useState(''); /** * Commit an in-place edit of one keyword. * * An edit that empties the field is a delete by another name, and an edit * onto a phrase already in the list is a duplicate — both resolve to * removing the row rather than to a silent no-op that would leave the old * text on screen and make the edit look like it failed. */ const commitEdit = (index, save) => { if (save) { const next = editText.trim(); const clash = list.some((other, j) => j !== index && other.toLowerCase() === next.toLowerCase()); set(next && !clash ? list.map((k, j) => (j === index ? next : k)) : list.filter((_, j) => j !== index)); } setEditing(null); setEditText(''); }; const list = keywords?.[group] || []; const meta = GROUPS.find((g) => g.id === group); const rows = (coverage?.rows || []).filter((r) => r.group === group); const summary = coverage?.summary?.[group]; const set = (next) => onChange({ ...keywords, [group]: next.slice(0, max) }); const addFromText = (text) => { const parts = text .split(/[\n,]/) .map((s) => s.trim()) .filter(Boolean); if (!parts.length) return; const existing = new Set(list.map((k) => k.toLowerCase())); const merged = [...list]; parts.forEach((p) => { if (!existing.has(p.toLowerCase())) { existing.add(p.toLowerCase()); merged.push(p); } }); set(merged); setDraft(''); }; const scoreFor = (keyword) => rows.find((r) => r.keyword === keyword)?.score ?? null; const draftCount = draft.split(/[\n,]/).filter((s) => s.trim()).length; return (
{meta.hint}
{bulk ? ({__('No keywords in this group yet.', 'ai-rank-suite')}
) : (