Chart.js
1 month ago
ChartAreaChart.js
1 month ago
ChartContext.js
1 month ago
ChartDateFilter.js
1 month ago
ChartKPI.js
1 month ago
ChartSummary.js
1 month ago
ChartTabs.js
1 month ago
index.js
1 month ago
ChartSummary.js
71 lines
| 1 | import { memo } from "@wordpress/element"; |
| 2 | import { Container, Text } from "@bsf/force-ui"; |
| 3 | import { __ } from "@wordpress/i18n"; |
| 4 | import { humanSeconds } from "../../utils"; |
| 5 | import { CHART_COLOR, ALL_TIME_CHART_COLOR } from "../charts"; |
| 6 | import { useChartContext } from "./ChartContext"; |
| 7 | |
| 8 | const StatCard = memo(({ label, value, indicatorColor }) => ( |
| 9 | <Container.Item className="flex flex-col items-start justify-center flex-1 p-4 text-left rounded-md shadow-sm bg-background-primary"> |
| 10 | <div className="flex items-center gap-2"> |
| 11 | {indicatorColor && ( |
| 12 | <div |
| 13 | className="w-3 h-3 rounded-sm" |
| 14 | style={{ backgroundColor: indicatorColor }} |
| 15 | /> |
| 16 | )} |
| 17 | <Text className="text-sm font-medium text-text-primary"> |
| 18 | {label} |
| 19 | </Text> |
| 20 | </div> |
| 21 | <Text className="mt-2 text-2xl font-semibold text-text-primary tabular-nums"> |
| 22 | {value} |
| 23 | </Text> |
| 24 | </Container.Item> |
| 25 | )); |
| 26 | |
| 27 | const ChartSummary = () => { |
| 28 | const { total, allTimeViews, allTimeWatchTime, isWatchTime, chartData } = |
| 29 | useChartContext(); |
| 30 | |
| 31 | // Only render when there is chart data and all-time data available |
| 32 | const hasAllTimeData = allTimeViews !== null || allTimeWatchTime !== null; |
| 33 | if (!chartData.length || !hasAllTimeData) return null; |
| 34 | |
| 35 | const formatCount = (n) => |
| 36 | typeof n === "number" ? n.toLocaleString() : n; |
| 37 | |
| 38 | const currentLabel = isWatchTime |
| 39 | ? __("Watch Time", "presto-player") |
| 40 | : __("Views", "presto-player"); |
| 41 | const currentValue = isWatchTime ? humanSeconds(total) : formatCount(total); |
| 42 | |
| 43 | const allTimeLabel = isWatchTime |
| 44 | ? __("All Time Watch Time", "presto-player") |
| 45 | : __("All Time Views", "presto-player"); |
| 46 | const allTimeValue = isWatchTime |
| 47 | ? humanSeconds(allTimeWatchTime) |
| 48 | : formatCount(allTimeViews); |
| 49 | |
| 50 | return ( |
| 51 | <Container |
| 52 | containerType="flex" |
| 53 | direction="column" |
| 54 | className="w-[30%] gap-1 bg-background-secondary rounded-lg" |
| 55 | > |
| 56 | <StatCard |
| 57 | label={currentLabel} |
| 58 | value={currentValue} |
| 59 | indicatorColor={CHART_COLOR} |
| 60 | /> |
| 61 | <StatCard |
| 62 | label={allTimeLabel} |
| 63 | value={allTimeValue} |
| 64 | indicatorColor={ALL_TIME_CHART_COLOR} |
| 65 | /> |
| 66 | </Container> |
| 67 | ); |
| 68 | }; |
| 69 | |
| 70 | export default ChartSummary; |
| 71 |