import type { ScanHistoryPoint, WeeklyReportResponse, } from '../../service/visibility/visibility.interface'; export interface PulseNumbers { score: number; shareOfVoice: number; point: ScanHistoryPoint | null; /** The week the numbers come from when it is not the one picked. */ scanWeek: string | null; } /** * The headline numbers of a week. The weekly report is written a while * after the scan, so a week without its report (this week, most days) * reads the scan history instead: the latest scan when the week picked has * none either. Score, share of voice and the "mentioned in" footnote then * come from the same scan and never contradict each other. * * @param {WeeklyReportResponse | null} report - The week's report, or null. * @param {ScanHistoryPoint[]} history - The scans, oldest first. * @param {string} weekIso - The week picked. * @returns {PulseNumbers} */ export const pulseNumbers = ( report: WeeklyReportResponse | null, history: ScanHistoryPoint[], weekIso: string ): PulseNumbers => { const point = history.find(scanPoint => scanPoint?.week_iso === weekIso) ?? history[history.length - 1] ?? null; if (report) { return { score: report.visibility_score ?? 0, shareOfVoice: report.share_of_voice ?? 0, point, scanWeek: null, }; } return { score: point?.visibility_score ?? 0, shareOfVoice: point?.share_of_voice ?? 0, point, scanWeek: point?.week_iso && point.week_iso !== weekIso ? point.week_iso : null, }; };