feat(dashboard): post analytics card + connect entry in profile selector

Account page gains a 'Your posts' card: last-30-days impressions with
per-platform split and the top 3 posts by views, backed by the new
analytics endpoints. Hidden for free users (402) and until data loads.

The header profile selector was a no-op for managed users (exactly one
profile, nothing to switch) while displaying greyed-out networks with
no way to connect them — it now ends in a Connect / manage accounts
action that opens the branded Upload-Post connection page.
This commit is contained in:
juancarlos.cavero 2026-08-21 19:16:53 +02:00
parent 6f76e875cd
commit cb1eecaa34
3 changed files with 120 additions and 1 deletions

View file

@ -96,7 +96,7 @@ const ProfileNetworkIcons = ({ profile, size = 12 }) => (
</span> </span>
); );
const UserProfileSelector = ({ profiles, selectedUserId, onSelect }) => { const UserProfileSelector = ({ profiles, selectedUserId, onSelect, onConnect }) => {
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
if (!profiles || profiles.length === 0) return null; if (!profiles || profiles.length === 0) return null;
@ -163,6 +163,17 @@ const UserProfileSelector = ({ profiles, selectedUserId, onSelect }) => {
</button> </button>
))} ))}
</div> </div>
{/* For managed users this dropdown otherwise does nothing (one profile,
nothing to switch) — its real job is being the door to connecting
the greyed-out networks it displays. */}
{onConnect && (
<button
onClick={() => { setIsOpen(false); onConnect(); }}
className="w-full flex items-center gap-2 px-4 py-3 text-sm text-brass hover:bg-paper3 transition-colors text-left border-t border-rule"
>
<Share2 size={14} /> Connect / manage accounts
</button>
)}
</div> </div>
)} )}
</div> </div>
@ -881,6 +892,7 @@ function App() {
profiles={userProfiles} profiles={userProfiles}
selectedUserId={uploadUserId} selectedUserId={uploadUserId}
onSelect={setUploadUserId} onSelect={setUploadUserId}
onConnect={isManaged ? handleConnectSocials : undefined}
/> />
)} )}

View file

@ -4,6 +4,7 @@ import { useAuth } from '../contexts/AuthContext';
import { apiJson } from '../lib/api'; import { apiJson } from '../lib/api';
import { track } from '../lib/analytics'; import { track } from '../lib/analytics';
import ApiKeysCard from './ApiKeysCard'; import ApiKeysCard from './ApiKeysCard';
import SocialAnalyticsCard from './SocialAnalyticsCard';
const fmt1 = (n) => Math.round((n || 0) * 10) / 10; const fmt1 = (n) => Math.round((n || 0) * 10) / 10;
@ -181,6 +182,8 @@ export default function AccountPage() {
</div> </div>
</div> </div>
<SocialAnalyticsCard />
{topups.length > 0 && ( {topups.length > 0 && (
<div className="card p-6"> <div className="card p-6">
<h3 className="font-display lowercase text-lg text-ink mb-1 flex items-center gap-2"><Plus size={16} className="text-brass" /> Buy more minutes</h3> <h3 className="font-display lowercase text-lg text-ink mb-1 flex items-center gap-2"><Plus size={16} className="text-brass" /> Buy more minutes</h3>

View file

@ -0,0 +1,104 @@
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 (
<div className="card p-6">
<div className="flex items-baseline justify-between gap-4 mb-1">
<h3 className="font-display lowercase text-lg text-ink flex items-center gap-2">
<BarChart3 size={16} className="text-brass" /> Your posts
</h3>
<span className="text-muted text-xs lowercase">last 30 days</span>
</div>
{data.posts.length === 0 && !total ? (
<p className="text-muted text-sm lowercase">
Nothing published yet. Post a clip from your results and its views show up here.
</p>
) : (
<>
<div className="flex items-end gap-3 mb-3">
<span className="readout text-2xl text-ink">{fmtNum(total)}</span>
<span className="text-muted text-sm lowercase mb-0.5">impressions</span>
</div>
{perPlatform.length > 0 && (
<div className="flex flex-wrap gap-x-4 gap-y-1 mb-4 text-sm">
{perPlatform.map(([platform, v]) => (
<span key={platform} className="text-ink2 lowercase">
{platform} <span className="text-brass">{fmtNum(Number(v))}</span>
</span>
))}
</div>
)}
{top.length > 0 && (
<div className="space-y-2 border-t border-rule pt-3">
{top.map((p, i) => (
<div key={p.request_id || p.platform_post_id || i}
className="flex items-center justify-between gap-3 text-sm">
<span className="text-ink2 truncate">
{postTitle(p)}
{postUrl(p) && (
<a href={postUrl(p)} target="_blank" rel="noreferrer"
className="inline-flex align-middle ml-1.5 text-muted hover:text-ink">
<ExternalLink size={12} />
</a>
)}
</span>
<span className="text-ink shrink-0">{fmtNum(postViews(p))} views</span>
</div>
))}
</div>
)}
</>
)}
</div>
);
}