A platform for community discussion. Free, open, simple.
UX: distinguish incomplete period in admin charts (#37277)
Krisa63d1e94e608
diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex bc48a429411..752899db53a 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -6405,6 +6405,7 @@ en:daily: Dailymonthly: Monthlyweekly: Weekly+ value_so_far: "%{value} (so far)"dates: "Dates (UTC)"groups: "All groups"disabled: "This report is disabled"diff --git a/frontend/discourse/admin/components/admin-report-chart.gjs b/frontend/discourse/admin/components/admin-report-chart.gjsindex fe78fc6c0b3..c18fb05ba89 100644--- a/frontend/discourse/admin/components/admin-report-chart.gjs+++ b/frontend/discourse/admin/components/admin-report-chart.gjs@@ -2,8 +2,38 @@ import Component from "@glimmer/component";import Report from "discourse/admin/models/report";import { number } from "discourse/lib/formatter";import { makeArray } from "discourse/lib/helpers";+import { i18n } from "discourse-i18n";import Chart from "./chart";+const DOTTED_LINE = [5, 5];++function getCSSColor(varName) {+ return getComputedStyle(document.documentElement)+ .getPropertyValue(varName)+ .trim();+}++export function isInCurrentPeriod(timestamp, grouping) {+ const date = moment(timestamp);+ const now = moment();++ switch (grouping) {+ case "weekly":+ return date.isSame(now, "week");+ case "monthly":+ return date.isSame(now, "month");+ default:+ return date.isSame(now, "day");+ }+}++export function hasIncompleteData(lastPoint, grouping) {+ if (!lastPoint) {+ return false;+ }+ return isInCurrentPeriod(lastPoint.x, grouping);+}+export default class AdminReportChart extends Component {get chartConfig() {const { model, options } = this.args;@@ -16,6 +46,30 @@ export default class AdminReportChart extends Component {const prevChartData = makeArray(model.prevChartData || model.prev_data);const labels = chartData.map((d) => d.x);+ const lastDataPointIndex = chartData.length - 1;+ const lastDataPoint = chartData[lastDataPointIndex];+ const isLastPointInCurrentPeriod = hasIncompleteData(+ lastDataPoint,+ options.chartGrouping+ );++ const incompleteColor = getCSSColor("--primary-medium");+ let pointColors = model.primary_color;+ let segment;++ if (isLastPointInCurrentPeriod) {+ pointColors = Array(chartData.length).fill(model.primary_color);+ pointColors[lastDataPointIndex] = incompleteColor;++ const isIncompleteSegment = (ctx) =>+ ctx.p1DataIndex === lastDataPointIndex;+ segment = {+ borderDash: (ctx) => (isIncompleteSegment(ctx) ? DOTTED_LINE : []),+ borderColor: (ctx) =>+ isIncompleteSegment(ctx) ? incompleteColor : model.primary_color,+ };+ }+const data = {labels,datasets: [@@ -27,8 +81,9 @@ export default class AdminReportChart extends Component {borderColor: model.primary_color,pointRadius: 3,borderWidth: 1,- pointBackgroundColor: model.primary_color,- pointBorderColor: model.primary_color,+ pointBackgroundColor: pointColors,+ pointBorderColor: pointColors,+ segment,},],};@@ -37,7 +92,7 @@ export default class AdminReportChart extends Component {data.datasets.push({data: prevChartData.map((d) => Math.round(parseFloat(d.y))),borderColor: model.primary_color,- borderDash: [5, 5],+ borderDash: DOTTED_LINE,backgroundColor: "transparent",borderWidth: 1,pointRadius: 0,@@ -53,6 +108,18 @@ export default class AdminReportChart extends Component {callbacks: {title: (tooltipItem) =>moment(tooltipItem[0].parsed.x).format("LL"),+ label: (tooltipItem) => {+ const value = tooltipItem.formattedValue;+ if (+ isLastPointInCurrentPeriod &&+ tooltipItem.dataIndex === lastDataPointIndex+ ) {+ return i18n("admin.dashboard.reports.value_so_far", {+ value,+ });+ }+ return value;+ },},},legend: {diff --git a/frontend/discourse/tests/integration/components/admin-report-chart-test.gjs b/frontend/discourse/tests/integration/components/admin-report-chart-test.gjsnew file mode 100644index 00000000000..b3b61cca6d1--- /dev/null+++ b/frontend/discourse/tests/integration/components/admin-report-chart-test.gjs@@ -0,0 +1,85 @@+import { module, test } from "qunit";+import { hasIncompleteData } from "discourse/admin/components/admin-report-chart";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";++module("Integration | Component | admin-report-chart", function (hooks) {+ setupRenderingTest(hooks);++ module("hasIncompleteData", function () {+ module("daily grouping", function () {+ test("returns true when last data point is today", function (assert) {+ const today = moment().format("YYYY-MM-DD");++ assert.true(+ hasIncompleteData({ x: today, y: 5 }, "daily"),+ "should show incomplete styling when last point is today"+ );+ });++ test("returns false when last data point is yesterday", function (assert) {+ const yesterday = moment().subtract(1, "day").format("YYYY-MM-DD");++ assert.false(+ hasIncompleteData({ x: yesterday, y: 5 }, "daily"),+ "should not show incomplete styling when last point is yesterday"+ );+ });+ });++ module("weekly grouping", function () {+ test("returns true when last data point is this week", function (assert) {+ const today = moment().format("YYYY-MM-DD");++ assert.true(+ hasIncompleteData({ x: today, y: 5 }, "weekly"),+ "should show incomplete styling when last point is this week"+ );+ });++ test("returns false when last data point is last week", function (assert) {+ const lastWeek = moment().subtract(1, "week").format("YYYY-MM-DD");++ assert.false(+ hasIncompleteData({ x: lastWeek, y: 5 }, "weekly"),+ "should not show incomplete styling when last point is last week"+ );+ });+ });++ module("monthly grouping", function () {+ test("returns true when last data point is this month", function (assert) {+ const today = moment().format("YYYY-MM-DD");++ assert.true(+ hasIncompleteData({ x: today, y: 5 }, "monthly"),+ "should show incomplete styling when last point is this month"+ );+ });++ test("returns false when last data point is last month", function (assert) {+ const lastMonth = moment().subtract(1, "month").format("YYYY-MM-DD");++ assert.false(+ hasIncompleteData({ x: lastMonth, y: 5 }, "monthly"),+ "should not show incomplete styling when last point is last month"+ );+ });+ });++ module("edge cases", function () {+ test("returns false for null point", function (assert) {+ assert.false(+ hasIncompleteData(null, "daily"),+ "should not show incomplete styling for null point"+ );+ });++ test("returns false for undefined point", function (assert) {+ assert.false(+ hasIncompleteData(undefined, "daily"),+ "should not show incomplete styling for undefined point"+ );+ });+ });+ });+});