import React, { useEffect, useMemo, useState } from 'react';
import { __ } from '@wordpress/i18n';
import { api } from '../api';
import { Card, Button, Badge, Notice, ScoreRing, MediaPicker, PageState } from './ui';
import Explain from './Explain';
/**
* Comma-separated list editor.
*
* A repeater with add/remove buttons per row looks more serious and is slower
* to use for exactly the fields people paste from somewhere else. One field,
* commas, and a live chip preview so nothing is silently mangled.
*/
function ListField({ label, hint, value, onChange, placeholder }) {
const [text, setText] = useState((value || []).join(', '));
useEffect(() => {
setText((value || []).join(', '));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [JSON.stringify(value || [])]);
const commit = (raw) => {
setText(raw);
onChange(
raw
.split(',')
.map((s) => s.trim())
.filter(Boolean)
);
};
const chips = text
.split(',')
.map((s) => s.trim())
.filter(Boolean);
return (
{label}
commit(e.target.value)} />
{hint && (
{hint}
)}
{chips.length > 0 && (
{chips.map((c, i) => (
{c}
))}
)}
);
}
export default function Identity() {
const [data, setData] = useState(null);
const [id, setId] = useState(null);
const [busy, setBusy] = useState(false);
const [saved, setSaved] = useState(false);
const [error, setError] = useState(null);
const [showJson, setShowJson] = useState(false);
useEffect(() => {
api
.get('/settings/schema')
.then((d) => {
setData(d);
setId(d.settings.identity || {});
})
.catch((e) => setError(e.message));
}, []);
const set = (patch) => setId((prev) => ({ ...prev, ...patch }));
const save = async () => {
setBusy(true);
setError(null);
try {
// Only the fields this screen owns. Spreading the whole settings
// snapshot would revert anything changed on the Overview tab since
// this component mounted — the same clobber, in the other direction.
const next = await api.put('/settings/schema', {
identity: id,
org_type: data.settings.org_type,
org_name: data.settings.org_name,
});
setData(next);
setId(next.settings.identity || {});
setSaved(true);
setTimeout(() => setSaved(false), 2400);
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
}
};
const typeGroups = useMemo(() => Object.entries(data?.types || {}), [data]);
if (!data || !id) return ;
const entity = data.entity || { score: 0, missing: [] };
const identifiers = Object.entries(data.identifiers || {});
const policies = Object.entries(data.policies || {});
const contactTypes = data.contactTypes || {};
const contacts = id.contacts || [];
const setContact = (i, patch) =>
set({ contacts: contacts.map((c, j) => (i === j ? { ...c, ...patch } : c)) });
return (
{__('Save identity', 'ai-rank-suite')}
}
>
{__('A search engine ranks pages. An assistant answers a question, and to answer it, it has to decide whose answer to repeat. That decision runs on entities — a resolved, cross-referenced thing in a knowledge graph — not on keywords.', 'ai-rank-suite')}
{__('Organization + name + logo', 'ai-rank-suite')} {__('does not resolve to anything; it is a label. Registration identifiers, links to profiles the graph already holds, and an explicit list of the subjects you claim expertise in are what turn a label into an entity that can be cited by name.', 'ai-rank-suite')}
{entity.missing.length > 0 && (
{__('Worth adding, strongest first', 'ai-rank-suite')}
{entity.missing.slice(0, 4).map((m) => (
))}
)}
{entity.missing.length === 0 &&
{__('Every high-value entity field is filled in.', 'ai-rank-suite')} }
{error && {error} }
{saved && ✓ Saved }
set({ logo: url })}
hint={__('What Google shows beside your name in a Knowledge Panel. Square, at least 112×112px. Without one, the site icon (favicon) is used — rarely how a brand wants to appear.', 'ai-rank-suite')}
/>
{__('Description', 'ai-rank-suite')}
set({ same_as: v })}
/>
set({ knows_about: v })}
/>
set({ area_served: v })}
/>
set({ languages: v })}
/>
set({ currencies: v })}
/>
set({ payments: v })}
/>
set({ awards: v })}
/>
set({ certifications: v })}
/>
{identifiers.map(([key, def]) => (
{def.label}
{def.region}
set({ identifiers: { ...(id.identifiers || {}), [key]: e.target.value } })}
/>
))}
{contacts.length === 0 && (
{__('No contact points yet. One per role beats a single switchboard number.', 'ai-rank-suite')}
)}
{contacts.map((c, i) => (
setContact(i, { type: e.target.value })}>
{Object.entries(contactTypes).map(([value, label]) => (
{label}
))}
setContact(i, { phone: e.target.value })}
/>
setContact(i, { email: e.target.value })}
/>
setContact(i, {
languages: e.target.value
.split(',')
.map((s) => s.trim())
.filter(Boolean),
})
}
/>
setContact(i, {
areas: e.target.value
.split(',')
.map((s) => s.trim())
.filter(Boolean),
})
}
/>
{/* Bare red text was the only control on the page with no shape.
A chip, like every other in-row action. */}
set({ contacts: contacts.filter((_, j) => j !== i) })}
>{__('Remove', 'ai-rank-suite')}
))}
set({ contacts: [...contacts, { type: 'customer support', phone: '', email: '' }] })}
>{__('Add a contact point', 'ai-rank-suite')}
{policies.map(([key, def]) => (
))}
setShowJson(!showJson)}>
{showJson ? 'Hide' : 'Show'} JSON-LD
}
>
{showJson ? (
{JSON.stringify(data.preview || {}, null, 2)}
) : (
{Object.keys(data.preview || {}).length} properties are currently attached to your identity node. Save to
refresh.
)}
{__('Save identity', 'ai-rank-suite')}
{saved && ✓ Saved }
);
}