import React, { useEffect, useState } from 'react'; import { __, _n, sprintf } from '@wordpress/i18n'; import { api } from '../api'; import { Card, Button, Badge, Notice, Field, LineChart, Delta } from './ui'; import Explain from './Explain'; /** * The business as the directories know it. * * A "near me" answer is assembled from the directories, not from the site: * Google Business Profile first, Bing Places for Copilot, Apple Business * Connect for Siri and Maps. This tab connects the Google listing through the * Places API — the part of the profile the public sees, on a key from Google * Cloud that works the same afternoon — compares it field by field with the * record on the Profile tab, imports it, and keeps a weekly rating history. * Bing and Apple publish no listing API; they are attached by URL, published * as sameAs, and kept aligned by Bing's own sync from the Google profile. * * @param {object} props * @param {object} props.record The business record (settings). * @param {Function} props.onImported Called after an import so the form reloads. */ export default function LocalListings({ record, onImported }) { const [data, setData] = useState(null); const [error, setError] = useState(null); const [key, setKey] = useState(''); const [query, setQuery] = useState(''); const [found, setFound] = useState(null); const [busy, setBusy] = useState(''); const [imported, setImported] = useState(null); const load = () => api.get('/local-seo/listings').then(setData).catch((e) => setError(e.message)); useEffect(() => { load(); }, []); const run = async (name, fn) => { setBusy(name); setError(null); try { await fn(); } catch (e) { setError(e.message); } finally { setBusy(''); } }; const saveKey = () => run('key', async () => { setData(await api.post('/local-seo/listings', { key })); setKey(''); }); const search = () => run('search', async () => setFound((await api.post('/local-seo/listings/search', { query })).places || [])); const fetch = (placeId) => run('fetch', async () => { setData(await api.post('/local-seo/listings/fetch', { place_id: placeId })); setFound(null); }); const doImport = (overwrite) => run(overwrite ? 'overwrite' : 'import', async () => { const r = await api.post('/local-seo/listings/import', { overwrite }); setData(r); setImported(r.imported || []); onImported && onImported(); }); const forget = () => run('forget', async () => setData(await api.del('/local-seo/listings/google'))); if (!data) return null; const g = data.google; const mismatches = (data.compare || []).filter((c) => c.match === false); const history = (data.history || []).map((h) => ({ label: h.day.slice(5), value: h.reviews, rating: h.rating })); const firstR = data.history?.[0]; const lastR = data.history?.[data.history.length - 1]; return (
{error && {error}} {/* Verdict first: connected and consistent, connected with differences, or not connected. */} {g ? ( mismatches.length === 0 ? ( {__('Google Business Profile matches your record', 'ai-rank-suite')} — {__('name, address, phone and website agree with what Google shows.', 'ai-rank-suite')} {g.reviews ? sprintf(__('%1$s ★ from %2$s reviews.', 'ai-rank-suite'), g.rating, g.reviews.toLocaleString()) : ''} ) : ( {sprintf(_n('%d field differs between your record and Google', '%d fields differ between your record and Google', mismatches.length, 'ai-rank-suite'), mismatches.length)} {' '} {/* * The labels as written, again. * Lowercasing them reads naturally mid-sentence in * English — "street and number differs" — and is the * same spelling mistake in German, where every noun is * capitalised. This is the second instance of it; the * button above was the first. */} — {mismatches.map((m) => m.label).join(', ')}. {__('To a matching engine a different street spelling is a different business. Fix one side: import Google’s values below, or edit the Profile tab and then correct the Google listing to match.', 'ai-rank-suite')} ) ) : ( {__('No directory is connected yet.', 'ai-rank-suite')} {__('Google Business Profile is where a “near me” answer starts; connect it to check that your record and Google’s agree character for character.', 'ai-rank-suite')} )}
{/* ---- Google ------------------------------------------------ */} ) : null } > {!data.has_key ? (
setKey(e.target.value)} placeholder={__('AIza…', 'ai-rank-suite')} />
{__('Enable Places API (New) ↗', 'ai-rank-suite')}

{__('Google’s Business Profile management API needs a project reviewed and approved by Google — weeks, and quota zero until then. The Places API answers with a plain key the same afternoon and returns the part of the profile that matters for consistency: name, address, phone, website, hours, rating and review count. Editing the profile stays on Google’s side; this checks and imports.', 'ai-rank-suite')}

) : !g ? (
{sprintf(__('Key saved (%s).', 'ai-rank-suite'), data.key_hint)}
{ e.preventDefault(); search(); }} > setQuery(e.target.value)} placeholder={`${record.name || __('Business name', 'ai-rank-suite')}, ${record.city || __('city', 'ai-rank-suite')}`} style={{ flex: 1 }} />
{found && found.length === 0 && {__('Nothing found — try the name as it appears on Google Maps.', 'ai-rank-suite')}} {found && found.length > 0 && (
{found.map((p) => ( ))}
)}
) : (
{g.name} {g.address} {g.type} {g.status && g.status !== 'OPERATIONAL' ? ` · ${g.status.toLowerCase().replace(/_/g, ' ')}` : ''}
{g.rating ?? '–'} ★ · {sprintf(_n('%s review', '%s reviews', g.reviews || 0, 'ai-rank-suite'), (g.reviews || 0).toLocaleString())}
{g.hours && g.hours.length > 0 && (
{__('Hours as Google shows them', 'ai-rank-suite')}
    {g.hours.map((h) => (
  • {h}
  • ))}
)}
{g.maps_url && ( {__('Open on Google Maps ↗', 'ai-rank-suite')} )} {__('Edit in Business Profile ↗', 'ai-rank-suite')} {sprintf(__('Fetched %s · refreshed weekly', 'ai-rank-suite'), new Date(g.fetched * 1000).toLocaleString())}
)}
{/* ---- Consistency ---------------------------------------- */} ) } > {!g ? (
{__('Connect the Google listing and this becomes a field-by-field comparison with one-click import.', 'ai-rank-suite')}
) : (
{data.compare.map((c) => ( ))}
{__('Field', 'ai-rank-suite')} {__('Your record', 'ai-rank-suite')} {__('Google', 'ai-rank-suite')}
{c.label} {c.ours || '—'} {c.theirs || '—'} {c.match === true ? {__('match', 'ai-rank-suite')} : c.match === false ? {__('differs', 'ai-rank-suite')} : {__('n/a', 'ai-rank-suite')}}
{imported && (

{imported.length ? sprintf(__('Imported: %s. Save the profile to publish.', 'ai-rank-suite'), imported.join(', ')) : __('Nothing to import — every field was already filled.', 'ai-rank-suite')}

)}
)}
{/* ---- Reviews trend -------------------------------------------- */} {history.length > 0 && ( 1 && lastR && firstR ? : null} > `${v}`} /> {lastR && lastR.rating !== null && (

{sprintf(__('Rating now %s ★', 'ai-rank-suite'), lastR.rating)}{firstR && firstR.rating !== null && firstR !== lastR ? ` ${sprintf(__('(was %1$s ★ on %2$s)', 'ai-rank-suite'), firstR.rating, firstR.day)}` : ''}.

)}
)} {/* ---- Bing + Apple ------------------------------------------- */}
onImported && onImported({ bing_url: e.target.value })} placeholder="https://www.bing.com/maps?…" />
{__('Open Bing Places ↗', 'ai-rank-suite')} {record.bing_url ? {__('attached', 'ai-rank-suite')} : {__('not attached', 'ai-rank-suite')}}

{__('Bing Places has an “Import from Google My Business” option in its dashboard that syncs name, address, phone, hours and photos from your Google profile — and can keep syncing. Use it, and the consistency check above covers Bing too.', 'ai-rank-suite')}

onImported && onImported({ apple_url: e.target.value })} placeholder="https://maps.apple.com/place?…" />
{__('Open Business Connect ↗', 'ai-rank-suite')} {record.apple_url ? {__('attached', 'ai-rank-suite')} : {__('not attached', 'ai-rank-suite')}}
); }