File size: 2,003 Bytes
9e15897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { AppTabs } from "../AppTabs";
import { AppDashboard } from "../AppDashboard";
import { Kpi, KpiStrip, TrendChart } from "../../viz/kit";
import type { OperatorDashboardAdapter, OperatorDashboardProps, OperatorTabsProps } from "./AdsOverview";

function OperatorTabs({ tabs, value, onChange }: OperatorTabsProps) {
  return <AppTabs tabs={tabs.map(([id, label, count]) => ({ id, label, count }))} value={value}
    ariaLabel="Amazon operator" onChange={id => {
      const tab = tabs.find(([key]) => key === id);
      if (tab) onChange(tab[0]);
    }} />;
}

function OperatorDashboard({ figures, comparison, groups, start, end, comparisonLabel }: OperatorDashboardProps) {
  return <AppDashboard figures={figures} charts={[]} start={start} end={end}>
    {comparison.length > 0 && <section aria-label={comparisonLabel}>
      <h3>{comparisonLabel}</h3>
      <KpiStrip>{comparison.map(figure => <Kpi key={figure.id} label={figure.label} unit={figure.unit}
        value={figure.value} validated={figure.validated === true} reason={figure.reason} />)}</KpiStrip>
    </section>}
    {groups.map(group => {
      const current = figures.filter(figure => group.figures.includes(figure.id));
      const previous = comparison.filter(figure => group.figures.includes(figure.id));
      const series = [...current, ...previous].filter(figure => figure.validated === true).map(figure => ({
        id: `${previous.includes(figure) ? "prior" : "current"}:${figure.id}`,
        label: `${figure.label}${previous.includes(figure) ? ` (${comparisonLabel})` : ""}`,
        points: figure.series,
      }));
      return <section key={group.id} aria-label={group.label}>
        <TrendChart label={group.label} unit={current[0]?.unit} series={series}
          period={`${start} to ${end}`} comparisonLabel={comparisonLabel} />
      </section>;
    })}
  </AppDashboard>;
}

export const operatorDashboardAdapter: OperatorDashboardAdapter = {
  AppTabs: OperatorTabs, AppDashboard: OperatorDashboard,
};