import React, { useEffect, useState } from 'react'; import { BarChart3, ExternalLink } from 'lucide-react'; import { apiJson } from '../lib/api'; const fmtNum = (n) => new Intl.NumberFormat('en-US', { notation: (n || 0) >= 10000 ? 'compact' : 'standard', maximumFractionDigits: 1, }).format(n || 0); // Upload-Post metric shapes vary per platform; read the first count that exists. const postViews = (p) => { const m = p.post_metrics || p.metrics || p; return Number(m.views || m.impressions || m.plays || 0) || 0; }; const postTitle = (p) => p.title || p.caption || p.youtube_title || p.tiktok_title || 'untitled post'; const postUrl = (p) => p.post_url || p.url || p.share_url || null; // Post-publication analytics: what the clips actually did out there. // Paid-only by nature (social posting itself is paid in cloud): a 402 or any // other failure simply hides the card — the account page works without it. export default function SocialAnalyticsCard() { const [data, setData] = useState(null); useEffect(() => { let alive = true; (async () => { try { const [imp, postsResp] = await Promise.all([ apiJson('/api/social/analytics/impressions?period=last_month&breakdown=true'), apiJson('/api/social/analytics/posts?limit=50'), ]); if (!alive) return; const posts = (postsResp.posts || postsResp.data || postsResp.items || []) .slice() .sort((a, b) => postViews(b) - postViews(a)); setData({ imp, posts }); } catch (_) { /* free plan, nothing connected, or vendor hiccup — stay hidden */ } })(); return () => { alive = false; }; }, []); if (!data) return null; const total = data.imp?.total_impressions || data.posts.reduce((s, p) => s + postViews(p), 0); const perPlatform = Object.entries(data.imp?.per_platform || {}) .filter(([, v]) => Number(v) > 0) .sort((a, b) => Number(b[1]) - Number(a[1])); const top = data.posts.slice(0, 3); return (

Your posts

last 30 days
{data.posts.length === 0 && !total ? (

Nothing published yet. Post a clip from your results and its views show up here.

) : ( <>
{fmtNum(total)} impressions
{perPlatform.length > 0 && (
{perPlatform.map(([platform, v]) => ( {platform} {fmtNum(Number(v))} ))}
)} {top.length > 0 && (
{top.map((p, i) => (
{postTitle(p)} {postUrl(p) && ( )} {fmtNum(postViews(p))} views
))}
)} )}
); }