Subir proyecto
This commit is contained in:
+160
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* BrandsTable — brands list table for the Dashboard tab.
|
||||
*
|
||||
* @since 1.0.273
|
||||
*/
|
||||
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { get } from 'lodash'
|
||||
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n'
|
||||
import { useMemo, memo } from '@wordpress/element'
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { ListTable as Table, TableSkeleton } from '@rank-math/components'
|
||||
import { formatLongDate } from '../utils/formatDate'
|
||||
import { getAnalysisState } from '../utils/analysisState'
|
||||
import {
|
||||
RankBadge,
|
||||
SentimentBadge,
|
||||
CountryFlag,
|
||||
ActionButtons,
|
||||
ScoreBadge,
|
||||
CountBadge,
|
||||
StatusIcon,
|
||||
} from '../shared/components'
|
||||
import './BrandsTable.scss'
|
||||
|
||||
/**
|
||||
* @param {Object} props
|
||||
* @param {Array} [props.brands=[]]
|
||||
* @param {boolean} [props.loading=false]
|
||||
* @param {Object} [props.pagination={}] `{ page, perPage, total, pages }`
|
||||
* @param {Function} [props.onView]
|
||||
* @param {Function} [props.onEdit]
|
||||
* @param {Function} [props.onDisable]
|
||||
* @param {Function} [props.onPageChange]
|
||||
* @param {Function} [props.onPerPageChange]
|
||||
* @return {JSX.Element} Rendered component.
|
||||
*/
|
||||
const BrandsTable = ( {
|
||||
brands = [],
|
||||
loading = false,
|
||||
pagination = {},
|
||||
onView = () => {},
|
||||
onEdit = () => {},
|
||||
onDisable = () => {},
|
||||
onPageChange = () => {},
|
||||
onPerPageChange = () => {},
|
||||
} ) => {
|
||||
const ns = 'rank-math-ai-visibility-brands-table'
|
||||
|
||||
const headers = useMemo( () => [
|
||||
{
|
||||
key: 'name',
|
||||
label: __( 'Brand', 'seo-by-rank-math' ),
|
||||
render: ( value, row ) => (
|
||||
<div className={ `${ ns }__brand-cell` }>
|
||||
{ /* eslint-disable-next-line jsx-a11y/anchor-is-valid */ }
|
||||
<a href="#" className={ `${ ns }__brand-name` } onClick={ ( event ) => {
|
||||
event.preventDefault()
|
||||
onView( row )
|
||||
} }>
|
||||
{ value }
|
||||
</a>
|
||||
<div className={ `${ ns }__brand-link` }>
|
||||
<a
|
||||
className={ `${ ns }__brand-url` }
|
||||
href={ row.url }
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{ row.url }
|
||||
</a>
|
||||
{ row.locale && <CountryFlag locale={ row.locale } /> }
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: 'score',
|
||||
label: __( 'AI Visibility Score', 'seo-by-rank-math' ),
|
||||
render: ( value ) => <ScoreBadge score={ value ?? null } />,
|
||||
},
|
||||
{
|
||||
key: 'rank',
|
||||
label: __( 'Avg Rank', 'seo-by-rank-math' ),
|
||||
render: ( value ) => <RankBadge rank={ value } />,
|
||||
},
|
||||
{
|
||||
key: 'avg_sentiment',
|
||||
label: __( 'Avg sentiment', 'seo-by-rank-math' ),
|
||||
render: ( value ) => <SentimentBadge score={ value } />,
|
||||
},
|
||||
{
|
||||
key: 'mentions',
|
||||
label: __( 'Mentions', 'seo-by-rank-math' ),
|
||||
render: ( value ) => <CountBadge value={ value } variant="mentions" />,
|
||||
},
|
||||
{
|
||||
key: 'citations',
|
||||
label: __( 'Citations', 'seo-by-rank-math' ),
|
||||
render: ( value ) => <CountBadge value={ value } variant="citations" />,
|
||||
},
|
||||
{
|
||||
key: 'last_analyzed',
|
||||
label: __( 'Last analyzed', 'seo-by-rank-math' ),
|
||||
render: ( value, row ) => {
|
||||
const state = getAnalysisState( row )
|
||||
return (
|
||||
<span className={ `${ ns }__last-analyzed` }>
|
||||
{ value ? formatLongDate( value ) : ( ! state && '-' ) }
|
||||
{ state && <StatusIcon variant={ state } /> }
|
||||
</span>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
key: 'id',
|
||||
label: __( 'Actions', 'seo-by-rank-math' ),
|
||||
render: ( _, row ) => (
|
||||
<ActionButtons
|
||||
status={ row?.status }
|
||||
onView={ () => onView( row ) }
|
||||
onEdit={ () => onEdit( row ) }
|
||||
onDisable={ () => onDisable( row ) }
|
||||
/>
|
||||
),
|
||||
},
|
||||
], [ onView, onEdit, onDisable ] )
|
||||
|
||||
if ( loading && ! brands.length ) {
|
||||
return <TableSkeleton columns={ 7 } rows={ 10 } />
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={ ns }>
|
||||
<Table
|
||||
storageKey="ai-visibility-brands"
|
||||
headers={ headers }
|
||||
rows={ brands }
|
||||
currentPage={ get( pagination, 'page', 1 ) }
|
||||
rowsPerPage={ get( pagination, 'perPage', 10 ) }
|
||||
totalItems={ get( pagination, 'total', brands.length ) }
|
||||
totalPages={ get( pagination, 'pages', 1 ) }
|
||||
onPageChange={ onPageChange }
|
||||
onRowsPerPageChange={ onPerPageChange }
|
||||
getRowClass={ ( row ) => row?.status === 'inactive' ? 'is-disabled' : '' }
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo( BrandsTable )
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* BrandsTable — brands list table column overrides.
|
||||
*/
|
||||
|
||||
@import '../scss/variables';
|
||||
|
||||
.rank-math-ai-visibility-brands-table {
|
||||
&__last-analyzed {
|
||||
color: #868686;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
tbody .rank-math-table-row .rank-math-table-primary-column a {
|
||||
color: #017CBA;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
&__brand-cell {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 5px;
|
||||
}
|
||||
|
||||
tbody .rank-math-table-row .rank-math-table-primary-column .rank-math-ai-visibility-brands-table__brand-name {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
color: $color-text-primary;
|
||||
line-height: 1.4;
|
||||
|
||||
&:hover {
|
||||
color: $color-text-primary;
|
||||
text-decoration: none;
|
||||
}
|
||||
}
|
||||
|
||||
&__brand-url {
|
||||
font-size: 12px;
|
||||
color: $color-btn-primary;
|
||||
line-height: 1.4;
|
||||
word-break: break-all;
|
||||
|
||||
&:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
}
|
||||
|
||||
&__brand-link {
|
||||
display: inline-flex;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.rank-math-table {
|
||||
&-cell-content {
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
&-header-cell {
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
thead {
|
||||
background-color: #FCFCFC;
|
||||
|
||||
th {
|
||||
color: #1E1E1E;
|
||||
font-size: 14px;
|
||||
|
||||
&:first-child {
|
||||
padding-left: 25px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tbody {
|
||||
tr td:first-child {
|
||||
padding-left: 25px;
|
||||
}
|
||||
|
||||
.rank-math-table-row {
|
||||
&:hover,
|
||||
&:nth-child(odd) {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
// Disabled (inactive) brand — replace all colors with flat gray.
|
||||
&.is-disabled {
|
||||
// All plain text cells (mentions, citations, date).
|
||||
td {
|
||||
color: #c4c4c4;
|
||||
}
|
||||
|
||||
// Brand name, URL, last-analyzed.
|
||||
.rank-math-ai-visibility-brands-table {
|
||||
&__brand-name {
|
||||
color: #c4c4c4;
|
||||
}
|
||||
|
||||
&__brand-url {
|
||||
color: #c4c4c4;
|
||||
|
||||
&:hover {
|
||||
text-decoration: none;
|
||||
}
|
||||
}
|
||||
|
||||
&__last-analyzed {
|
||||
color: #c4c4c4;
|
||||
}
|
||||
}
|
||||
|
||||
// Country flag locale code.
|
||||
.rank-math-ai-visibility-country-flag__code {
|
||||
color: #c4c4c4;
|
||||
}
|
||||
|
||||
// Score badge — strip variant color, replace with gray.
|
||||
.rank-math-ai-visibility-score-badge {
|
||||
color: #c4c4c4 !important;
|
||||
border-color: #c4c4c4 !important;
|
||||
background-color: rgba(196, 196, 196, 0.1) !important;
|
||||
}
|
||||
|
||||
// Rank badge — strip variant color, replace with gray.
|
||||
.rank-math-ai-visibility-rank-badge {
|
||||
color: #c4c4c4 !important;
|
||||
border-color: #c4c4c4 !important;
|
||||
background-color: rgba(196, 196, 196, 0.2) !important;
|
||||
}
|
||||
|
||||
// Sentiment badge — strip variant color, replace with gray.
|
||||
// Also covers the SVG smiley icon via currentColor.
|
||||
.rank-math-ai-visibility-sentiment-badge {
|
||||
color: #c4c4c4 !important;
|
||||
border-color: #c4c4c4 !important;
|
||||
background-color: rgba(196, 196, 196, 0.08) !important;
|
||||
|
||||
svg {
|
||||
color: #c4c4c4;
|
||||
|
||||
path {
|
||||
fill: #c4c4c4 !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* BrandsTableToolbar — search + add-brand controls above the brands table.
|
||||
*
|
||||
* @since 1.0.273
|
||||
*/
|
||||
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n'
|
||||
import { SearchControl } from '@wordpress/components'
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { Button } from '../shared/components'
|
||||
import './BrandsTableToolbar.scss'
|
||||
|
||||
/**
|
||||
* @param {Object} props
|
||||
* @param {string} [props.searchValue='']
|
||||
* @param {Function} [props.onSearchChange]
|
||||
* @param {Function} [props.onAdd]
|
||||
* @return {JSX.Element} Rendered component.
|
||||
*/
|
||||
const BrandsTableToolbar = ( {
|
||||
searchValue = '',
|
||||
onSearchChange = () => {},
|
||||
onAdd = () => {},
|
||||
} ) => {
|
||||
const ns = 'rank-math-ai-visibility-brands-toolbar'
|
||||
|
||||
return (
|
||||
<div className={ ns }>
|
||||
<Button
|
||||
variant="primary"
|
||||
onClick={ onAdd }
|
||||
>
|
||||
{ __( '+ Add Brand / Product', 'seo-by-rank-math' ) }
|
||||
</Button>
|
||||
|
||||
<SearchControl
|
||||
className={ `${ ns }__search` }
|
||||
placeholder={ __( 'Search name or URL', 'seo-by-rank-math' ) }
|
||||
value={ searchValue }
|
||||
onChange={ onSearchChange }
|
||||
__nextHasNoMarginBottom={ true }
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
BrandsTableToolbar.displayName = 'BrandsTableToolbar'
|
||||
|
||||
export default BrandsTableToolbar
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* BrandsTableToolbar — search + add-brand bar above the brands table.
|
||||
*/
|
||||
|
||||
@import '../scss/variables';
|
||||
|
||||
.rank-math-ai-visibility-brands-toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: $space-md;
|
||||
|
||||
&__search.components-base-control {
|
||||
flex: 0 0 auto;
|
||||
margin: 0;
|
||||
width: 200px;
|
||||
|
||||
input[type='text'],
|
||||
input[type='search'] {
|
||||
height: 36px;
|
||||
border-radius: 2px;
|
||||
border-color: #949494;
|
||||
font-size: 13px;
|
||||
|
||||
&:focus {
|
||||
border-color: $color-btn-primary;
|
||||
box-shadow: 0 0 0 1px $color-btn-primary;
|
||||
}
|
||||
}
|
||||
|
||||
input[type='text'] {
|
||||
padding: 0 12px;
|
||||
}
|
||||
|
||||
input[type='search'] {
|
||||
padding: 0 5px;
|
||||
}
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Dashboard tab — layout styles.
|
||||
*/
|
||||
|
||||
@import '../scss/variables';
|
||||
|
||||
.rank-math-ai-visibility-dashboard {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 26px;
|
||||
|
||||
&__error {
|
||||
padding: $space-sm $space-md;
|
||||
background: #fef0f0;
|
||||
border: 1px solid $color-trend-down;
|
||||
border-radius: $radius-sm;
|
||||
color: $color-trend-down;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
&__stats {
|
||||
display: flex;
|
||||
gap: $card-row-gap;
|
||||
margin-top: 16px;
|
||||
|
||||
// On narrow viewports stack cards vertically.
|
||||
@media ( max-width: 782px ) {
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
|
||||
&__empty {
|
||||
background: $color-surface;
|
||||
border: $border-width solid $border-color;
|
||||
border-radius: $radius-sm;
|
||||
}
|
||||
|
||||
&__table-card {
|
||||
background: #fff;
|
||||
border: 1px solid $color-border;
|
||||
border-radius: $radius-sm;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
|
||||
// Toolbar padding inside the card.
|
||||
.rank-math-ai-visibility-brands-toolbar {
|
||||
padding: 14px 25px;
|
||||
border-bottom: 1px solid $color-border;
|
||||
gap: 8px;
|
||||
|
||||
&__search.components-base-control {
|
||||
width: 260px;
|
||||
}
|
||||
}
|
||||
|
||||
.rank-math-table-header {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.rank-math-table-card {
|
||||
border: none;
|
||||
border-radius: 0;
|
||||
background: transparent;
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
// Center the table pagination row.
|
||||
.rank-math-table-footer,
|
||||
.rank-math-table__pagination {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
> svg {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
z-index: 999;
|
||||
opacity: .5;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* GatedPreview — blurred decorative dashboard shown behind access-gate modals.
|
||||
* Uses static sample data; no API calls are made.
|
||||
*
|
||||
* @since 1.0.281
|
||||
*/
|
||||
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n'
|
||||
import { trendingUp } from '@wordpress/icons'
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { StatCard } from '../shared/components'
|
||||
import BrandsTableToolbar from './BrandsTableToolbar'
|
||||
import BrandsTable from './BrandsTable'
|
||||
import './GatedPreview.scss'
|
||||
|
||||
const noop = () => {}
|
||||
|
||||
// Display-only sample data — intentionally untranslated.
|
||||
const SAMPLE_BRANDS = [
|
||||
{ id: 'preview-1', name: 'Playstation 5', url: 'sony.com', locale: 'US', score: 94, rank: 1, avg_sentiment: 81, mentions: 4, citations: 2, last_analyzed: '2026-03-16', status: 'active' },
|
||||
{ id: 'preview-2', name: 'iPad', url: 'apple.com', locale: 'US', score: 94, rank: 1, avg_sentiment: 76, mentions: 9, citations: 16, last_analyzed: '2026-03-16', status: 'active' },
|
||||
{ id: 'preview-3', name: 'MiniCRM', url: 'https://minicrm.hu/', locale: 'HU', score: null, rank: null, avg_sentiment: null, mentions: null, citations: null, last_analyzed: '2026-03-16', status: 'active' },
|
||||
{ id: 'preview-4', name: 'BackWPup', url: 'https://backwpup.com', locale: '', score: null, rank: null, avg_sentiment: null, mentions: null, citations: null, last_analyzed: '2026-03-16', status: 'active' },
|
||||
{ id: 'preview-5', name: 'GTmetrix', url: 'https://gtmetrix.com', locale: '', score: null, rank: null, avg_sentiment: null, mentions: null, citations: null, last_analyzed: '2026-03-16', status: 'active' },
|
||||
{ id: 'preview-6', name: 'Rank Math', url: 'https://rankmath.com/', locale: '', score: 23, rank: 3, avg_sentiment: 40, mentions: 2, citations: 1, last_analyzed: '2026-03-16', status: 'inactive' },
|
||||
{ id: 'preview-7', name: 'WP Rocket', url: 'https://wp-rocket.me', locale: '', score: 23, rank: 3, avg_sentiment: 40, mentions: 2, citations: 1, last_analyzed: '2026-03-16', status: 'active' },
|
||||
]
|
||||
|
||||
// Reuses the Dashboard BEM namespace so Dashboard.scss layout applies.
|
||||
|
||||
const GatedPreview = () => {
|
||||
const ns = 'rank-math-ai-visibility-dashboard'
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ `${ ns } rank-math-ai-visibility-gated-preview` }
|
||||
aria-hidden="true"
|
||||
>
|
||||
<div className={ `${ ns }__stats` }>
|
||||
<StatCard
|
||||
className="rank-math-ai-visibility-stat-card--active-brands"
|
||||
icon=" rm-icon rm-icon-ai-visibility"
|
||||
label={ __( 'Global AI Visibility Score', 'seo-by-rank-math' ) }
|
||||
value="70/100"
|
||||
/>
|
||||
<StatCard
|
||||
className="rank-math-ai-visibility-stat-card--analyses"
|
||||
icon={ trendingUp }
|
||||
label={ __( 'Analyses in the last 7 days', 'seo-by-rank-math' ) }
|
||||
value={ 0 }
|
||||
/>
|
||||
<StatCard
|
||||
className="rank-math-ai-visibility-stat-card--mentions"
|
||||
icon="rm-icon rm-icon-comments"
|
||||
label={ __( 'Avg mentions per brand', 'seo-by-rank-math' ) }
|
||||
value={ 3.2 }
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className={ `${ ns }__table-card` }>
|
||||
<BrandsTableToolbar
|
||||
searchValue=""
|
||||
onSearchChange={ noop }
|
||||
onAdd={ noop }
|
||||
/>
|
||||
|
||||
<BrandsTable
|
||||
brands={ SAMPLE_BRANDS }
|
||||
loading={ false }
|
||||
onView={ noop }
|
||||
onEdit={ noop }
|
||||
onDisable={ noop }
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
GatedPreview.displayName = 'GatedPreview'
|
||||
|
||||
export default GatedPreview
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* GatedPreview — blurred decorative dashboard behind the access-gate modals.
|
||||
*/
|
||||
|
||||
.rank-math-ai-visibility-gated-preview {
|
||||
filter: blur( 4px );
|
||||
pointer-events: none;
|
||||
user-select: none;
|
||||
}
|
||||
+380
@@ -0,0 +1,380 @@
|
||||
/**
|
||||
* Dashboard tab
|
||||
*
|
||||
* @since 1.0.273
|
||||
*/
|
||||
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { get, toLower, isNil } from 'lodash'
|
||||
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
import { __, sprintf } from '@wordpress/i18n'
|
||||
import { Notice } from '@wordpress/components'
|
||||
import { useState, useEffect, useRef } from '@wordpress/element'
|
||||
import { trendingUp, store } from '@wordpress/icons'
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import useDashboard from '../shared/hooks/useDashboard'
|
||||
import usePagination from '../shared/hooks/usePagination'
|
||||
import { getBrand } from '../shared/services/api/aiVisibilityApi'
|
||||
import { StatCard, EmptyState } from '../shared/components'
|
||||
import { ConfirmModal, AddBrandModal, UpgradePlanModal } from '../shared/Modals'
|
||||
import BrandsTableToolbar from './BrandsTableToolbar'
|
||||
import BrandsTable from './BrandsTable'
|
||||
import BrandDetail from '../BrandDetail'
|
||||
import { getActiveBrandId, navigateToBrand, navigateBackToDashboard } from '../utils/urlState'
|
||||
import './Dashboard.scss'
|
||||
|
||||
// Plans not listed here are treated as unlimited.
|
||||
const BRAND_LIMITS = {
|
||||
starter: 1,
|
||||
creator: 10,
|
||||
expert: 50,
|
||||
}
|
||||
|
||||
const TOP_TIER_PLAN = 'expert' // no upgrade path — show limit-reached messaging
|
||||
|
||||
/**
|
||||
* @param {Object|null} confirmBrand
|
||||
* @param {string} confirmAction 'disable' | 'enable'
|
||||
* @return {string} Localised confirmation message.
|
||||
*/
|
||||
const getConfirmMessage = ( confirmBrand, confirmAction ) => {
|
||||
if ( ! confirmBrand ) {
|
||||
return ''
|
||||
}
|
||||
|
||||
if ( confirmAction === 'enable' ) {
|
||||
return sprintf(
|
||||
/* translators: %s is the brand name, e.g. "Nike". */
|
||||
__( `Resume tracking "%s"? Analysis will run on the next scheduled cycle.`, 'seo-by-rank-math' ),
|
||||
confirmBrand.name
|
||||
)
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
/* translators: %s is the brand name, e.g. "Nike". */
|
||||
__( `Stop tracking "%s"? You can re-enable it later.`, 'seo-by-rank-math' ),
|
||||
confirmBrand.name
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the display value for a summary StatCard.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @param {boolean} options.loading Whether the dashboard is still loading.
|
||||
* @param {boolean} options.hasBrands Whether any brands are tracked.
|
||||
* @param {*} options.raw Raw summary value (may be null/undefined).
|
||||
* @param {Function} [options.format] Optional formatter for a present value.
|
||||
* @return {string|number|null} Placeholder, null (empty), 0 (missing) or formatted value.
|
||||
*/
|
||||
const getStatValue = ( { loading, hasBrands, raw, format } ) => {
|
||||
if ( loading ) {
|
||||
return '—'
|
||||
}
|
||||
if ( ! hasBrands ) {
|
||||
return null
|
||||
}
|
||||
if ( isNil( raw ) ) {
|
||||
return 0
|
||||
}
|
||||
return format ? format( raw ) : raw
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} props
|
||||
* @param {Array} [props.locales] Locale options for the brand modals.
|
||||
* @param {Array} [props.languages] Output language options for the brand modals.
|
||||
* @param {string} [props.defaultLanguage] Default language for new brands (site language).
|
||||
* @param {Object} [props.platforms] AI platform registry from PHP.
|
||||
* @param {number} [props.maxPlatforms] Platforms selectable on the current plan.
|
||||
* @param {Function} [props.onBrandCreated] Callback when a new brand is successfully created.
|
||||
* @param {Array} [props.intervals] Interval options for the brand modals.
|
||||
* @return {JSX.Element} Rendered component.
|
||||
*/
|
||||
const Dashboard = ( { locales = [], languages = [], defaultLanguage = '', platforms = {}, maxPlatforms = 1, onBrandCreated = () => {}, intervals = [] } ) => {
|
||||
const [ activeBrandId, setActiveBrandId ] = useState( getActiveBrandId )
|
||||
|
||||
useEffect( () => {
|
||||
const handlePopState = () => setActiveBrandId( getActiveBrandId() )
|
||||
window.addEventListener( 'popstate', handlePopState )
|
||||
return () => window.removeEventListener( 'popstate', handlePopState )
|
||||
}, [] )
|
||||
|
||||
const {
|
||||
brands,
|
||||
summary,
|
||||
loading,
|
||||
error,
|
||||
handleSearch,
|
||||
handleAddBrand,
|
||||
handleUpdateBrand,
|
||||
handleDisableBrand,
|
||||
handleEnableBrand,
|
||||
} = useDashboard()
|
||||
|
||||
const [ isAddModalOpen, setAddModalOpen ] = useState( false )
|
||||
const [ editBrand, setEditBrand ] = useState( null )
|
||||
const [ isSaving, setIsSaving ] = useState( false )
|
||||
const [ saveError, setSaveError ] = useState( null )
|
||||
|
||||
const [ confirmBrand, setConfirmBrand ] = useState( null )
|
||||
const [ confirmAction, setConfirmAction ] = useState( 'delete' )
|
||||
const [ isProcessing, setIsProcessing ] = useState( false )
|
||||
const [ actionError, setActionError ] = useState( null )
|
||||
|
||||
const [ isUpgradeModalOpen, setUpgradeModalOpen ] = useState( false )
|
||||
|
||||
// Search is debounced 300ms.
|
||||
const [ searchQuery, setSearchQuery ] = useState( '' )
|
||||
const isFirstSearchRender = useRef( true )
|
||||
|
||||
const { pagination, pageItems, total, pages, onPageChange, onPerPageChange, resetPage } =
|
||||
usePagination( brands, { perPage: 10, syncUrl: true } )
|
||||
|
||||
useEffect( () => {
|
||||
if ( isFirstSearchRender.current ) {
|
||||
isFirstSearchRender.current = false
|
||||
return
|
||||
}
|
||||
|
||||
const timer = setTimeout( () => {
|
||||
handleSearch( searchQuery.trim() )
|
||||
resetPage()
|
||||
}, 300 )
|
||||
return () => clearTimeout( timer )
|
||||
}, [ searchQuery, handleSearch, resetPage ] )
|
||||
|
||||
const hasActiveSearch = !! searchQuery.trim()
|
||||
const isEmpty = ! loading && brands.length === 0 && ! hasActiveSearch
|
||||
|
||||
const openDisableConfirm = ( brand ) => {
|
||||
setConfirmBrand( brand )
|
||||
setConfirmAction( brand?.status === 'inactive' ? 'enable' : 'disable' )
|
||||
}
|
||||
const closeConfirm = () => {
|
||||
setConfirmBrand( null )
|
||||
setActionError( null )
|
||||
}
|
||||
|
||||
const confirmTitle = confirmAction === 'enable'
|
||||
? __( 'Activate brand?', 'seo-by-rank-math' )
|
||||
: __( 'Deactivate brand?', 'seo-by-rank-math' )
|
||||
|
||||
const confirmMessage = getConfirmMessage( confirmBrand, confirmAction )
|
||||
|
||||
const closeAddModal = () => {
|
||||
setAddModalOpen( false )
|
||||
setEditBrand( null )
|
||||
setSaveError( null )
|
||||
}
|
||||
|
||||
const plan = toLower( get( window, 'rankMath.aiVisibility.plan', '' ) )
|
||||
const brandLimit = get( BRAND_LIMITS, plan, null )
|
||||
|
||||
const openAddBrand = () => {
|
||||
if ( null !== brandLimit && brands.length >= brandLimit ) {
|
||||
setUpgradeModalOpen( true )
|
||||
return
|
||||
}
|
||||
setEditBrand( null )
|
||||
setAddModalOpen( true )
|
||||
}
|
||||
|
||||
const handleSaveBrand = async ( data ) => {
|
||||
setSaveError( null )
|
||||
setIsSaving( true )
|
||||
try {
|
||||
if ( editBrand ) {
|
||||
await handleUpdateBrand( editBrand.id, data )
|
||||
} else {
|
||||
await handleAddBrand( data )
|
||||
onBrandCreated( __( 'Brand added! The first analysis is running — data will appear in approximately 10 minutes.', 'seo-by-rank-math' ) )
|
||||
}
|
||||
closeAddModal()
|
||||
} catch ( err ) {
|
||||
setSaveError( err?.message || __( 'Something went wrong. Please try again.', 'seo-by-rank-math' ) )
|
||||
} finally {
|
||||
setIsSaving( false )
|
||||
}
|
||||
}
|
||||
|
||||
const onConfirmAction = async () => {
|
||||
if ( ! confirmBrand ) {
|
||||
return
|
||||
}
|
||||
setIsProcessing( true )
|
||||
setActionError( null )
|
||||
try {
|
||||
if ( confirmAction === 'disable' ) {
|
||||
await handleDisableBrand( confirmBrand.id )
|
||||
} else if ( confirmAction === 'enable' ) {
|
||||
await handleEnableBrand( confirmBrand.id )
|
||||
}
|
||||
closeConfirm()
|
||||
} catch ( err ) {
|
||||
setActionError( err?.message ?? __( 'Action failed. Please try again.', 'seo-by-rank-math' ) )
|
||||
} finally {
|
||||
setIsProcessing( false )
|
||||
}
|
||||
}
|
||||
|
||||
const handleViewBrand = ( brand ) => {
|
||||
navigateToBrand( brand.id )
|
||||
setActiveBrandId( brand.id )
|
||||
}
|
||||
|
||||
const handleBackToDashboard = () => {
|
||||
navigateBackToDashboard()
|
||||
setActiveBrandId( null )
|
||||
}
|
||||
|
||||
if ( activeBrandId ) {
|
||||
return (
|
||||
<BrandDetail
|
||||
brandId={ activeBrandId }
|
||||
onBack={ handleBackToDashboard }
|
||||
locales={ locales }
|
||||
languages={ languages }
|
||||
defaultLanguage={ defaultLanguage }
|
||||
intervals={ intervals }
|
||||
platforms={ platforms }
|
||||
maxPlatforms={ maxPlatforms }
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const ns = 'rank-math-ai-visibility-dashboard'
|
||||
|
||||
return (
|
||||
<div className={ ns }>
|
||||
|
||||
{ error && (
|
||||
<Notice status="error" isDismissible={ false }>
|
||||
{ error }
|
||||
</Notice>
|
||||
) }
|
||||
|
||||
<div className={ `${ ns }__stats` }>
|
||||
<StatCard
|
||||
className="rank-math-ai-visibility-stat-card--active-brands"
|
||||
icon=" rm-icon rm-icon-ai-visibility"
|
||||
label={ __( 'Global AI Visibility Score', 'seo-by-rank-math' ) }
|
||||
value={ getStatValue( { loading, hasBrands: brands.length > 0, raw: summary?.ai_visibility_score, format: ( v ) => v + '/100' } ) }
|
||||
tooltip={ __( 'Average rank score across all tracked brands.', 'seo-by-rank-math' ) }
|
||||
/>
|
||||
<StatCard
|
||||
className="rank-math-ai-visibility-stat-card--analyses"
|
||||
icon={ trendingUp }
|
||||
label={ __( 'Analyses in the last 7 days', 'seo-by-rank-math' ) }
|
||||
value={ getStatValue( { loading, hasBrands: brands.length > 0, raw: summary?.analyses_last_7d } ) }
|
||||
tooltip={ __( 'Total analysis runs completed in the past 24 hours.', 'seo-by-rank-math' ) }
|
||||
/>
|
||||
<StatCard
|
||||
className="rank-math-ai-visibility-stat-card--mentions"
|
||||
icon="rm-icon rm-icon-comments"
|
||||
label={ __( 'Avg mentions per brand', 'seo-by-rank-math' ) }
|
||||
value={ getStatValue( { loading, hasBrands: brands.length > 0, raw: summary?.avg_mentions_per_brand } ) }
|
||||
tooltip={ __( 'Average number of AI-generated mentions across all tracked brands.', 'seo-by-rank-math' ) }
|
||||
/>
|
||||
</div>
|
||||
|
||||
{ isEmpty && (
|
||||
<div className={ `${ ns }__empty` }>
|
||||
<EmptyState
|
||||
icon={ store }
|
||||
heading={ __( 'No brands or products tracked yet', 'seo-by-rank-math' ) }
|
||||
description={ __(
|
||||
'Add a brand or a specific product to see how you rank against competitors. Monitor your AI visibility, discover where you stand, and uncover opportunities to grow.',
|
||||
'seo-by-rank-math'
|
||||
) }
|
||||
ctaLabel={ __( '+ Add Your First Brand', 'seo-by-rank-math' ) }
|
||||
onCta={ openAddBrand }
|
||||
/>
|
||||
</div>
|
||||
) }
|
||||
|
||||
{ ! isEmpty && (
|
||||
<div className={ `${ ns }__table-card` }>
|
||||
<BrandsTableToolbar
|
||||
searchValue={ searchQuery }
|
||||
onSearchChange={ setSearchQuery }
|
||||
onAdd={ openAddBrand }
|
||||
/>
|
||||
|
||||
<BrandsTable
|
||||
brands={ pageItems }
|
||||
loading={ loading }
|
||||
pagination={ { ...pagination, total, pages } }
|
||||
onView={ handleViewBrand }
|
||||
onEdit={ async ( brand ) => {
|
||||
setEditBrand( brand )
|
||||
setAddModalOpen( true )
|
||||
try {
|
||||
const result = await getBrand( brand.id )
|
||||
if ( result?.brand ) {
|
||||
setEditBrand( result.brand )
|
||||
}
|
||||
} catch {
|
||||
// keep the partial brand already set
|
||||
}
|
||||
} }
|
||||
onDisable={ ( brand ) => openDisableConfirm( brand ) }
|
||||
onPageChange={ onPageChange }
|
||||
onPerPageChange={ onPerPageChange }
|
||||
/>
|
||||
</div>
|
||||
) }
|
||||
|
||||
{ isAddModalOpen && (
|
||||
<AddBrandModal
|
||||
brand={ editBrand }
|
||||
onSave={ handleSaveBrand }
|
||||
onClose={ closeAddModal }
|
||||
isSaving={ isSaving }
|
||||
apiError={ saveError }
|
||||
locales={ locales }
|
||||
languages={ languages }
|
||||
defaultLanguage={ defaultLanguage }
|
||||
intervals={ intervals }
|
||||
platforms={ platforms }
|
||||
maxPlatforms={ maxPlatforms }
|
||||
/>
|
||||
) }
|
||||
|
||||
{ isUpgradeModalOpen && (
|
||||
<UpgradePlanModal
|
||||
plan={ plan }
|
||||
isTopTier={ TOP_TIER_PLAN === plan }
|
||||
onClose={ () => setUpgradeModalOpen( false ) }
|
||||
/>
|
||||
) }
|
||||
|
||||
{ confirmBrand && (
|
||||
<ConfirmModal
|
||||
title={ confirmTitle }
|
||||
message={ confirmMessage }
|
||||
confirmLabel={ confirmAction === 'enable'
|
||||
? __( 'Activate', 'seo-by-rank-math' )
|
||||
: __( 'Deactivate', 'seo-by-rank-math' )
|
||||
}
|
||||
isDestructive={ confirmAction === 'disable' }
|
||||
isProcessing={ isProcessing }
|
||||
error={ actionError }
|
||||
onConfirm={ onConfirmAction }
|
||||
onCancel={ closeConfirm }
|
||||
/>
|
||||
) }
|
||||
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Dashboard
|
||||
Reference in New Issue
Block a user