import { lazy, Suspense, useEffect, useMemo, useRef, useState } from "react"; import { CommentIcon, ImageIcon, VideoIcon } from "@/components/icons"; import { addUp, combined, cumulativeSeries, dailySeries, metricSeries, metricValue, platformColor, reportingPlatforms, retentionByPlatform, timeAgo, watchTime, type DailyChange, type Metric, type Metrics, type RetentionReading, type Sample, type SeriesPoint, type VideoMark, } from "@/lib/publisher/metrics"; import { kindNames, platformNames, postLabel, type Platform, type PostKind } from "@/lib/publisher/presets"; import { previewSources, storedThumbnail } from "@/lib/publisher/preview"; import { saveThumbnail, thumbnailFrom } from "@/lib/publisher/thumbnail"; import { kindsOf, periods, postTypes, readView, typeNames, writeView, type AnalyticsView, type PostType, } from "@/lib/publisher/route"; // Only the Analytics tab downloads the chart library, and the Worker never loads it. const TrendChart = lazy(async () => { if (import.meta.env.SSR) throw new Error("The chart renders in the browser"); return import("./PublisherTrendChart"); }); type Target = { id: string; platform: Platform; state: string; url: string | null; metrics: Metrics | null; /** The latest reading with a watch time, which can be older than `metrics`. */ retention: Metrics | null; observed_at: number | null; checked_at: number | null; analytics_error: string | null; deletion_state: string | null; /** 1 when the owner published it themselves and linked it to the post. */ linked: number; /** 1 when the helper published it before the platform's sign-in last changed. */ earlier_sign_in: number; }; type Post = { id: string; created_at: number; post: { title: string; caption: string; kind: PostKind; media: string[]; mediaKinds?: ("photo" | "video")[]; cover?: string; }; /** When its kept thumbnail was saved, or null before it has one. */ thumbnail: number | null; targets: Target[]; }; type Page = { posts: Post[]; next: string | null }; type Refresh = { job: string; at: number; queued: string[] }; type Request = (path: string, body?: unknown) => Promise; type Props = { request: Request; online: boolean; /** The Analytics tab's query, which holds what it shows. */ search: string; /** Opens another view: pushed for a post or page, replaced for a filter. */ onNavigate: (search: string, replace?: boolean) => void; }; const IDLE_POLL = 60_000; const REFRESH_POLL = 5_000; const REFRESH_TIMEOUT = 3 * 60_000; const number = (value: number | null) => (value === null ? "—" : new Intl.NumberFormat().format(value)); const dateTime = (at: number) => new Date(at).toLocaleString(undefined, { dateStyle: "medium", timeStyle: "short" }); function shortDate(at: number) { const date = new Date(at); const year = date.getFullYear() === new Date().getFullYear() ? undefined : "numeric"; return date.toLocaleDateString(undefined, { day: "numeric", month: "short", year }); } const metricNames: Record = { exposure: "Views / impressions", views: "Views", impressions: "Impressions", reach: "Reach", likes: "Likes / favorites", comments: "Comments / replies", shares: "Shares / reposts", saves: "Saves", }; /** Whether every kind of post is listed; otherwise at most two of the three are. */ const allTypes = (types: PostType[]) => types.length === postTypes.length; function Spinner() { return