fix: 修复使用了较新的api node版本不支持的问题
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
---
|
||||
import {dayjs} from "@/lib/format.ts";
|
||||
import type {HeatmapPoint} from "@/types/stats.ts";
|
||||
|
||||
interface Props {
|
||||
@@ -14,7 +15,6 @@ const GAP = 3;
|
||||
const STEP = CELL + GAP;
|
||||
const TOP = 20;
|
||||
const LEFT = 26;
|
||||
const DAY_MS = 86_400_000;
|
||||
|
||||
type GridCell = {
|
||||
key: string
|
||||
@@ -24,26 +24,13 @@ type GridCell = {
|
||||
level: number
|
||||
}
|
||||
|
||||
const dayFormatter = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit"
|
||||
});
|
||||
const monthFormatter = new Intl.DateTimeFormat("zh-CN", {timeZone: "UTC", month: "numeric"});
|
||||
|
||||
const toDayKey = (ms: number) => dayFormatter.format(new Date(ms));
|
||||
const toDayMs = (key: string) => {
|
||||
const [year, month, day] = key.split("-").map(Number);
|
||||
return Date.UTC(year, month - 1, day);
|
||||
};
|
||||
const toDateText = (key: string) => key.replaceAll("-", ".");
|
||||
|
||||
// 按天聚合:接口按分钟返回,同一天可能有多条
|
||||
const byDay = new Map<string, number>();
|
||||
for (const {timestamp, contributions} of points) {
|
||||
if (!Number.isFinite(timestamp) || !Number.isFinite(contributions)) continue;
|
||||
const key = toDayKey(timestamp * 1000);
|
||||
const key = dayjs(timestamp * 1000).tz(timeZone).format("YYYY-MM-DD");
|
||||
byDay.set(key, (byDay.get(key) ?? 0) + Math.max(0, contributions));
|
||||
}
|
||||
|
||||
@@ -65,19 +52,18 @@ let lastDate = "";
|
||||
const firstKey = days.at(0);
|
||||
|
||||
if (firstKey) {
|
||||
const todayKey = toDayKey(Date.now());
|
||||
const lastDataKey = days.at(-1) ?? firstKey;
|
||||
const first = dayjs.tz(firstKey, timeZone).startOf("day");
|
||||
const today = dayjs().tz(timeZone).startOf("day");
|
||||
const lastData = dayjs.tz(days.at(-1) ?? firstKey, timeZone).startOf("day");
|
||||
// 末尾补到今天,保证最后一列是完整的自然周
|
||||
const endDayMs = Math.max(toDayMs(lastDataKey), toDayMs(todayKey));
|
||||
|
||||
const startMs = toDayMs(firstKey);
|
||||
const start = startMs - ((new Date(startMs).getUTCDay() + 6) % 7) * DAY_MS;
|
||||
const end = endDayMs + (6 - ((new Date(endDayMs).getUTCDay() + 6) % 7)) * DAY_MS;
|
||||
weeks = Math.floor((end - start) / (7 * DAY_MS)) + 1;
|
||||
const endDay = lastData.isAfter(today) ? lastData : today;
|
||||
// 列从周一开始、到周日结束
|
||||
const start = first.subtract((first.day() + 6) % 7, "day");
|
||||
const end = endDay.add(6 - ((endDay.day() + 6) % 7), "day");
|
||||
|
||||
total = [...byDay.values()].reduce((sum, value) => sum + value, 0);
|
||||
firstDate = firstKey;
|
||||
lastDate = toDayKey(endDayMs);
|
||||
lastDate = endDay.format("YYYY-MM-DD");
|
||||
|
||||
// 用非零数据的分位数划分 4 档,避免个别高峰压扁其余格子
|
||||
const nonZero = [...byDay.values()].filter((value) => value > 0).sort((a, b) => a - b);
|
||||
@@ -88,10 +74,10 @@ if (firstKey) {
|
||||
const levelOf = (value: number) =>
|
||||
value <= 0 ? 0 : value <= t1 ? 1 : value <= t2 ? 2 : value <= t3 ? 3 : 4;
|
||||
|
||||
for (let time = start; time <= end; time += DAY_MS) {
|
||||
const key = new Date(time).toISOString().slice(0, 10);
|
||||
const week = Math.floor((time - start) / (7 * DAY_MS));
|
||||
const weekday = (new Date(time).getUTCDay() + 6) % 7;
|
||||
let week = 0;
|
||||
let weekday = 0;
|
||||
for (let cursor = start; !cursor.isAfter(end); cursor = cursor.add(1, "day")) {
|
||||
const key = cursor.format("YYYY-MM-DD");
|
||||
const value = byDay.get(key) ?? 0;
|
||||
|
||||
cells.push({
|
||||
@@ -101,15 +87,21 @@ if (firstKey) {
|
||||
value,
|
||||
level: levelOf(value)
|
||||
});
|
||||
|
||||
weekday += 1;
|
||||
if (weekday === 7) {
|
||||
weekday = 0;
|
||||
week += 1;
|
||||
}
|
||||
}
|
||||
weeks = week + (weekday > 0 ? 1 : 0);
|
||||
|
||||
// 每月第一次出现的列打上月份标签
|
||||
let lastMonth = "";
|
||||
for (let week = 0; week < weeks; week++) {
|
||||
const monday = new Date(start + week * 7 * DAY_MS);
|
||||
const month = monthFormatter.format(monday);
|
||||
let lastMonth = 0;
|
||||
for (let i = 0; i < weeks; i++) {
|
||||
const month = start.add(i, "week").month() + 1;
|
||||
if (month !== lastMonth) {
|
||||
monthLabels.push({x: LEFT + week * STEP, label: month});
|
||||
monthLabels.push({x: LEFT + i * STEP, label: `${month}月`});
|
||||
lastMonth = month;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user