loopable / web /src /apps /amazon-profit /pdp-text /CompetitorText.tsx
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
3e19b1e verified
Raw History Blame Contribute Delete
5.87 kB
import { ArrowSquareOut } from "@phosphor-icons/react";
import type { CompetitorTextPayload, MarkedToken, PageWord, ResearchPayload, Resource, TextColumn, TextPart } from "./api";
import { amazonListingUrl, ResearchInfo, ResearchSection } from "./ResearchSummary";
import { searchValue } from "./WhoTargets";
function WordChip({ word, theirs }: { word: PageWord; theirs: boolean }) {
const money = searchValue(word.mkt, "money");
const tip = theirs ? `On ${word.n} of ${word.N} competitors' pages. ${money} spent on searches with it.${word.fits ? "" : " Nothing shows it fits this product."}`
: `${word.n ? `On ${word.n} of ${word.N} competitors' pages` : "On no competitor's page"}. ${word.mkt == null ? "Search money not reported." : word.mkt ? `${money} spent on searches with it, ${searchValue(word.our, "money")} with you.` : "No search in the report uses it."}`;
return <span className={`pdp-word-chip ${theirs ? "is-theirs" : "is-yours"}`} title={tip}><b>{word.word}</b>
{theirs && <span>{word.n} of {word.N}</span>}<span>{word.mkt === 0 ? "no searches" : money}</span></span>;
}
export function WordGap({ data }: { data: CompetitorTextPayload }) {
const theirs = data.theirs84, yours = data.yours84;
return <section className="pdp-text-card pdp-word-gap" aria-label="Words: theirs and yours">
<header><b>Words: theirs and yours</b><ResearchInfo text={`Title and bullets on every page, your live page against the competitors from Setup, word by word. Money is what shoppers spent on searches that use the word${data.periodLabel ? `, ${data.periodLabel}` : ""}. They have, you don't: on most competitors' pages and not on yours. Only you: on your page and on no competitor's, or one of four or more.`} /></header>
<div className="pdp-word-groups"><div><div className="pdp-word-heading"><span className="pdp-word-label is-act">Gap</span>
<b>They have, you don't</b><span className="pdp-text-muted">{theirs.length} words</span></div>
<div className="pdp-word-chips">{theirs.filter(word => word.fits).map(word => <WordChip key={word.k} word={word} theirs />)}
{!theirs.some(word => word.fits) && <span className="pdp-text-muted">None that fit this product</span>}</div>
{theirs.some(word => !word.fits) && <p className="pdp-text-muted pdp-word-note">Also on their pages, not fitting this product: {theirs.filter(word => !word.fits).map(word => word.word).join(", ")}</p>}</div>
<div><div className="pdp-word-heading"><span className="pdp-word-label is-good">Only you</span><b>You have, they don't</b>
<span className="pdp-text-muted">{yours.length} words</span></div>
<div className="pdp-word-chips">{yours.filter(word => word.mkt !== 0).map(word => <WordChip key={word.k} word={word} theirs={false} />)}</div>
{yours.some(word => word.mkt === 0) && <p className="pdp-text-muted pdp-word-note">No search uses: {yours.filter(word => word.mkt === 0).map(word => word.word).join(", ")}. Keep them only if they help the shopper decide.</p>}</div>
</div>
</section>;
}
export function markedWords(tokens: MarkedToken[]) {
return tokens.map((token, index) => typeof token === "string" ? token
: <mark key={index} className="pdp-gap-mark">{token.mark}</mark>);
}
function TextCard({ column, descriptionUnavailable }: { column: TextColumn; descriptionUnavailable: boolean }) {
return <article className={`pdp-competitor-card${column.id === "you" ? " is-you" : ""}`}>
<header><b>{column.id === "you" ? "You, live" : column.name}</b>
{column.m.group_rank != null && <span className="pdp-text-muted" title="Best seller rank">#{column.m.group_rank.toLocaleString("en-US")}</span>}
{column.id !== "you" && <a className="pdp-competitor-open" href={amazonListingUrl(column.id)} target="_blank" rel="noreferrer"
aria-label={`Open ${column.name} on Amazon`} title="Open on Amazon"><ArrowSquareOut size={14} aria-hidden="true" /></a>}</header>
<div className="pdp-competitor-body">{column.bullets ? <ul>{column.bullets.map((bullet, index) =>
<li key={index}>{markedWords(column.marks[index] ?? [bullet])}</li>)}</ul>
: <p>{column.text ? markedWords(column.marks[0] ?? [column.text]) : <span className="pdp-text-muted">
{descriptionUnavailable ? "Description not available from this source" : "Not on this listing"}</span>}</p>}</div>
</article>;
}
export function CompetitorTextContent({ data, part }: { data?: CompetitorTextPayload; part: TextPart }) {
return <div className="pdp-competitor-text">
<div className="pdp-text-muted pdp-competitors-picked">Competitors from Setup: {data?.picks.map(pick => pick.brand || pick.a).join(", ") || (data?.hasCompetitors ? "Not available" : "none")}</div>
{data?.hasCompetitors ? <WordGap data={data} /> : <p className="pdp-text-empty">No competitors selected in Setup</p>}
{part !== "terms" && <div className="pdp-competitor-cards">{data?.cols.map(column =>
<TextCard key={column.id} column={column} descriptionUnavailable={part === "desc" && column.id !== "you"} />)}</div>}
{[...new Set([data?.note, ...(data?.constraints ?? []),
part === "terms" ? "Backend search terms are hidden on every listing, so there is no text to compare. The keywords below still work."
: part === "highlights" ? "Competitors' item highlights are not in Amazon's API. Their titles are shown." : null])]
.filter((note): note is string => !!note).map(note => <p className="pdp-text-muted" key={note}>{note}</p>)}
</div>;
}
export function CompetitorText({ resource, retry, part }: { resource: Resource<ResearchPayload>; retry: () => void; part: TextPart }) {
return <ResearchSection name="Competitor text" field="competitorText" resource={resource} retry={retry}>
{data => <CompetitorTextContent data={data.competitorText} part={part} />}</ResearchSection>;
}