import React, { useEffect, useCallback } from 'react'
import FadeIn from '../../fade-in'
import Bar from '../bar'
import MoreLink from '../more-link'
import { numberShortFormatter } from '../../util/number-formatter'
import RocketIcon from '../modals/rocket-icon'
import * as api from '../../api'
import LazyLoader from '../../components/lazy-loader'
import { referrersGoogleRoute } from '../../router'
import { useQueryContext } from '../../query-context'
import { PlausibleSite, useSiteContext } from '../../site-context'
interface SearchTerm {
name: string
visitors: number
}
type ErrorCode = 'not_configured' | 'unsupported_filters' | 'period_too_recent'
interface ErrorPayload {
error_code: ErrorCode
is_admin: boolean
}
function ErrorMessage({ code }: { code: ErrorCode }): JSX.Element {
if (code === 'not_configured') {
return
The site is not connected to Google Search Keywords
} else if (code === 'unsupported_filters') {
return (
Unable to fetch keyword data from Search Console because it does not
support the current set of filters
)
} else if (code === 'period_too_recent') {
return (
No search terms were found for this period. Please adjust or extend your
time range. Check{' '}
our documentation
{' '}
for more details.
)
} else {
return Unable to fetch keyword data from Search Console
}
}
function ConfigureSearchTermsCTA({
site
}: {
site: PlausibleSite
}): JSX.Element {
return (
<>
Configure the integration to view search terms
Connect with Google
>
)
}
export function SearchTerms() {
const site = useSiteContext()
const { query } = useQueryContext()
const [loading, setLoading] = React.useState(true)
const [errorPayload, setErrorPayload] = React.useState(
null
)
const [searchTerms, setSearchTerms] = React.useState(
null
)
const [visible, setVisible] = React.useState(false)
const fetchSearchTerms = useCallback(() => {
api
.get(
`/api/stats/${encodeURIComponent(site.domain)}/referrers/Google`,
query
)
.then((res) => {
setLoading(false)
setSearchTerms(res.results)
setErrorPayload(null)
})
.catch((error) => {
setLoading(false)
setSearchTerms(null)
setErrorPayload(error.payload)
})
}, [query, site.domain])
useEffect(() => {
if (visible) {
setLoading(true)
setSearchTerms([])
fetchSearchTerms()
}
}, [query, fetchSearchTerms, visible])
const onVisible = () => {
setVisible(true)
}
const renderList = () => {
if (searchTerms && searchTerms.length > 0) {
return (
Search term
Visitors
{searchTerms &&
searchTerms.map((term: SearchTerm) => (
{term.name}
{numberShortFormatter(term.visitors)}
))}
) => search
}}
className="w-full mt-3"
onClick={undefined}
/>
)
}
}
const renderNoDataYet = () => {
if (searchTerms && searchTerms.length === 0) {
return (
)
}
}
const renderError = () => {
if (errorPayload) {
const { is_admin, error_code } = errorPayload
return (
{error_code === 'not_configured' && is_admin && (
)}
)
}
}
return (
Search Terms
{loading && (
)}
{searchTerms && searchTerms.length > 0 && renderList()}
{searchTerms && searchTerms.length === 0 && renderNoDataYet()}
{errorPayload && renderError()}
)
}