import { useEffect, useState } from 'react'; import { Badge } from '../ui/badge'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '../ui/card'; import { getShopLinkStatus } from '../../service/shop-link/shop-link.service'; import type { IShopLinkStatus, TShopLinkAnswer, } from '../../service/shop-link/shop-link.interface'; import { formatDateLabel } from '../../utils/dateUtils'; type TBadgeVariant = 'default' | 'secondary' | 'destructive' | 'outline'; /** How each answer of Recomaze reads on the card. */ const ANSWER_LABELS: Record< TShopLinkAnswer, { label: string; variant: TBadgeVariant; hint: string } > = { ok: { label: 'Answering', variant: 'default', hint: 'Recomaze answers the shop link for this store.', }, off: { label: 'Off in Recomaze', variant: 'secondary', hint: 'Set the shop address in Recomaze (CRM settings) to turn the shop link on.', }, unauthorized: { label: 'Not signed in', variant: 'destructive', hint: "Recomaze turned down this site's sign-in. Open the Recomaze page to reconnect.", }, failing: { label: 'Not answering', variant: 'destructive', hint: 'Recomaze did not answer the last call. The plugin tries again on its own.', }, not_connected: { label: 'Not connected', variant: 'outline', hint: 'Connect this site to Recomaze first.', }, unknown: { label: 'Not checked yet', variant: 'outline', hint: 'The first check runs within 15 minutes.', }, }; type CountRowProps = { label: string; value: string; }; /** * One label and its value. * * @param {CountRowProps} props * @return {JSX.Element} */ const CountRow = ({ label, value }: CountRowProps): JSX.Element => (
{label}
{value}
); /** * A date label, or "not yet" when there is none. * * @param {string | null} iso * @return {string} */ const dateOrNotYet = (iso: string | null): string => formatDateLabel(iso) ?? 'not yet'; /** * The rows of the card: the invited clients and the order sends. * * @param {IShopLinkStatus} status * @return {CountRowProps[]} */ const statusRows = (status: IShopLinkStatus): CountRowProps[] => [ { label: 'Invited clients', value: `${status.clients.count}, read ${dateOrNotYet(status.clients.read_at)}`, }, { label: 'Clients whose earlier orders are still being sent', value: String(status.clients.history_pending), }, { label: 'Order sends waiting', value: String(status.sends.pending) }, { label: 'Orders whose sends are failing (retried)', value: String(status.sends.failing), }, { label: 'Orders Recomaze refused', value: String(status.sends.refused) }, ]; /** * The shop link's state, read only: whether Recomaze answers, the invited * clients last read and the order sends waiting or failing. No email or * token is ever shown. * * @return {JSX.Element} */ const ShopLinkCard = (): JSX.Element => { const [status, setStatus] = useState(null); const [hasFailed, setHasFailed] = useState(false); useEffect(() => { let isActive = true; getShopLinkStatus() .then((fetchedStatus: IShopLinkStatus) => { if (isActive) setStatus(fetchedStatus); }) .catch(() => { if (isActive) setHasFailed(true); }); return () => { isActive = false; }; }, []); const isLoading: boolean = !hasFailed && status === null; const answer = status ? (ANSWER_LABELS[status.answer] ?? ANSWER_LABELS.unknown) : null; const lastAnswer: string = status?.answered_at ? ` Last answer: ${dateOrNotYet(status.answered_at)}.` : ''; const rows: CountRowProps[] = status ? statusRows(status) : []; return (
Shop link {answer && {answer.label}}
Clients this firm invited in Recomaze prepare orders in ChatGPT or Claude, finish them in this shop and follow them from the chat. The plugin does it with its own Recomaze sign-in: there is nothing to set up here.
{hasFailed && (

The shop link's state could not be read. Reload the page to try again.

)} {isLoading && (

Loading...

)} {answer && ( <>

{answer.hint} {lastAnswer}

{rows.map((row: CountRowProps) => ( ))}
)}
); }; export default ShopLinkCard;