Skip to content

Commit a83552b

Browse files
committed
Fix the title of calendar heatmap on the dashboard (sheepzh#323)
1 parent 06b87d2 commit a83552b

File tree

8 files changed

+101
-158
lines changed

8 files changed

+101
-158
lines changed

src/app/components/Analysis/components/Summary/Calendar/Wrapper.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import { t } from "@app/locale"
88
import { periodFormatter } from "@app/util/time"
99
import { EchartsWrapper } from "@hooks/useEcharts"
10-
import { locale } from "@i18n"
10+
import weekHelper from "@service/components/week-helper"
1111
import { groupBy, rotate } from "@util/array"
1212
import { getRegularTextColor, getSecondaryTextColor } from "@util/style"
13-
import { formatTime, getAllDatesBetween, getWeeksAgo, parseTime } from "@util/time"
13+
import { formatTime, getAllDatesBetween, MILL_PER_WEEK, parseTime } from "@util/time"
1414
import {
1515
ComposeOption,
1616
EffectScatterSeriesOption,
@@ -148,11 +148,11 @@ export type BizOption = {
148148
class Wrapper extends EchartsWrapper<BizOption, EcOption> {
149149
isSizeSensitize = true
150150

151-
protected generateOption({ rows = [], timeFormat }: BizOption): EcOption | Promise<EcOption> {
151+
protected async generateOption({ rows = [], timeFormat }: BizOption): Promise<EcOption> {
152152
const width = this.getDomWidth()
153-
const weekNum = getWeekNum(width)
153+
const colNum = getWeekNum(width)
154154
const endTime = new Date()
155-
const startTime = getWeeksAgo(endTime, locale === "zh_CN", weekNum)
155+
const [startTime,] = await weekHelper.getWeekDate(endTime.getTime() - MILL_PER_WEEK * (colNum - 1))
156156
const allDates = getAllDatesBetween(startTime, endTime)
157157
const value = groupBy(rows, r => r.date, l => l?.[0]?.focus)
158158
const data: _Value[] = []
@@ -163,12 +163,9 @@ class Wrapper extends EchartsWrapper<BizOption, EcOption> {
163163
const x = colIndex, y = 7 - (1 + weekDay)
164164
data.push([x, y, dailyMills, date])
165165
})
166+
const weekStart = await weekHelper.getRealWeekStart()
166167
const weekDays = (t(msg => msg.calendar.weekDays)?.split?.('|') || []).reverse()
167-
if (locale !== "zh_CN") {
168-
// Let Sunday last
169-
// Saturday to Sunday
170-
rotate(weekDays, 1)
171-
}
168+
rotate(weekDays, weekStart, true)
172169
return optionOf(data, weekDays, timeFormat, width)
173170
}
174171
}

src/app/components/Dashboard/components/Calendar/Wrapper.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@
66
*/
77
import type {
88
ComposeOption,
9-
TooltipComponentOption, GridComponentOption, VisualMapComponentOption,
10-
ScatterSeriesOption, HeatmapSeriesOption,
9+
GridComponentOption,
10+
HeatmapSeriesOption,
11+
ScatterSeriesOption,
12+
TooltipComponentOption,
13+
VisualMapComponentOption,
1114
} from "echarts"
1215

13-
import { EchartsWrapper } from "@hooks/useEcharts"
14-
import { formatPeriodCommon, getAllDatesBetween, MILL_PER_HOUR, MILL_PER_MINUTE } from "@util/time"
15-
import { groupBy, rotate } from "@util/array"
16+
import { createTabAfterCurrent } from "@api/chrome/tab"
1617
import { t } from "@app/locale"
17-
import { getPrimaryTextColor } from "@util/style"
18-
import { getAppPageUrl } from "@util/constant/url"
1918
import { REPORT_ROUTE } from "@app/router/constants"
20-
import { createTabAfterCurrent } from "@api/chrome/tab"
2119
import { getStepColors } from "@app/util/echarts"
22-
import { locale } from "@i18n"
2320
import { cvt2LocaleTime } from "@app/util/time"
21+
import { EchartsWrapper } from "@hooks/useEcharts"
22+
import weekHelper from "@service/components/week-helper"
23+
import { groupBy, rotate } from "@util/array"
24+
import { getAppPageUrl } from "@util/constant/url"
25+
import { getPrimaryTextColor } from "@util/style"
26+
import { formatPeriodCommon, getAllDatesBetween, MILL_PER_HOUR, MILL_PER_MINUTE } from "@util/time"
2427

2528
type _Value = [
2629
x: number,
@@ -197,7 +200,7 @@ function handleClick(value: _Value): void {
197200
class Wrapper extends EchartsWrapper<BizOption, EcOption> {
198201
protected isSizeSensitize: boolean = true
199202

200-
protected generateOption(option: BizOption): EcOption | Promise<EcOption> {
203+
protected async generateOption(option: BizOption): Promise<EcOption> {
201204
if (!option) return {}
202205

203206
const { startTime, endTime, value } = option
@@ -211,11 +214,8 @@ class Wrapper extends EchartsWrapper<BizOption, EcOption> {
211214
data.push([x, y, dailyMills, date])
212215
})
213216
const weekDays = (t(msg => msg.calendar.weekDays)?.split?.('|') || []).reverse()
214-
if (locale !== "zh_CN") {
215-
// Let Sunday last
216-
// Saturday to Sunday
217-
rotate(weekDays, 1)
218-
}
217+
const weekStart = await weekHelper.getRealWeekStart()
218+
weekStart && rotate(weekDays, weekStart, true)
219219
return optionOf(data, weekDays, this.getDom())
220220
}
221221

src/app/components/Dashboard/components/Calendar/index.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
* https://opensource.org/licenses/MIT
66
*/
77

8+
import { t } from "@app/locale"
89
import { useRequest } from "@hooks"
910
import { useEcharts } from "@hooks/useEcharts"
10-
import { locale } from "@i18n"
11+
import weekHelper from "@service/components/week-helper"
1112
import statService from "@service/stat-service"
12-
import { getWeeksAgo, MILL_PER_HOUR } from "@util/time"
13-
import { defineComponent } from "vue"
14-
import Wrapper, { BizOption } from "./Wrapper"
1513
import { groupBy, sum } from "@util/array"
16-
import { t } from "@app/locale"
14+
import { formatTimeYMD, MILL_PER_DAY, MILL_PER_HOUR } from "@util/time"
15+
import { computed, defineComponent } from "vue"
1716
import ChartTitle from "../../ChartTitle"
17+
import Wrapper, { BizOption } from "./Wrapper"
1818

19-
const WEEK_NUM = 53
20-
21-
const titleText = (option: BizOption) => {
22-
const { value } = option || {}
23-
const totalMills = sum(Object.values(value || {}))
19+
const titleText = (option: Result) => {
20+
const { value, yearAgo } = option || {}
21+
const start = formatTimeYMD(yearAgo)
22+
const statValues = Object.entries(value || {}).filter(([date]) => date.localeCompare(start) >= 0).map(([, v]) => v)
23+
const totalMills = sum(statValues)
2424
const totalHours = Math.floor(totalMills / MILL_PER_HOUR)
2525
return t(msg => totalHours
2626
? msg.dashboard.heatMap.title0
@@ -29,21 +29,25 @@ const titleText = (option: BizOption) => {
2929
)
3030
}
3131

32-
const fetchData = async (): Promise<BizOption> => {
32+
type Result = BizOption & { yearAgo: Date }
33+
34+
const fetchData = async (): Promise<Result> => {
3335
const endTime = new Date()
34-
const startTime: Date = getWeeksAgo(endTime, locale === "zh_CN", WEEK_NUM)
36+
const yearAgo = new Date(endTime.getTime() - MILL_PER_DAY * 365)
37+
const [startTime] = await weekHelper.getWeekDate(yearAgo)
3538
const items = await statService.select({ date: [startTime, endTime], sort: "date" })
3639
const value = groupBy(
3740
items,
3841
i => i.date,
3942
list => sum(list?.map(i => i.focus ?? 0))
4043
)
41-
return { value, startTime, endTime }
44+
return { value, startTime, endTime, yearAgo }
4245
}
4346

4447
const _default = defineComponent(() => {
4548
const { data } = useRequest(fetchData)
46-
const { elRef } = useEcharts(Wrapper, data)
49+
const biz = computed(() => (data.value as BizOption))
50+
const { elRef } = useEcharts(Wrapper, biz)
4751

4852
return () => (
4953
<div class="calendar-container">

src/popup/components/footer/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import type { PopupQueryResult, PopupRow } from "@popup/common"
99
import type { StatQueryParam } from "@service/stat-service"
1010

11-
import { locale } from "@i18n"
1211
import { t } from "@popup/locale"
12+
import weekHelper from "@service/components/week-helper"
1313
import optionService from "@service/option-service"
1414
import statService from "@service/stat-service"
15-
import { getDayLength, getMonthTime, getWeekTime, MILL_PER_DAY } from "@util/time"
15+
import { getDayLength, getMonthTime, MILL_PER_DAY } from "@util/time"
1616
import initAllFunction from './all-function'
1717
import MergeHostWrapper from "./merge-host"
1818
import TimeSelectWrapper from "./select/time-select"
@@ -26,11 +26,15 @@ type FooterParam = StatQueryParam & {
2626

2727
type QueryResultHandler = (result: PopupQueryResult) => void
2828

29-
type DateRangeCalculator = (now: Date, weekStart: timer.option.WeekStartOption) => Date | [Date, Date]
29+
type DateRangeCalculator = (now: Date) => Promise<Date | [Date, Date]> | Date | [Date, Date]
3030

3131
const dateRangeCalculators: { [duration in timer.option.PopupDuration]: DateRangeCalculator } = {
3232
today: now => now,
33-
thisWeek: (now, weekStart) => getWeekTime(now, weekStart, locale),
33+
thisWeek: async now => {
34+
const [start] = await weekHelper.getWeekDate(now)
35+
console.log(start)
36+
return [start, now]
37+
},
3438
thisMonth: now => [getMonthTime(now)[0], now],
3539
last30Days: now => [new Date(now.getTime() - MILL_PER_DAY * 29), now],
3640
allTime: () => null,
@@ -79,7 +83,7 @@ class FooterWrapper {
7983
async query() {
8084
const option = await optionService.getAllOption()
8185
const itemCount = option.popupMax
82-
const queryParam = this.getQueryParam(option.weekStart)
86+
const queryParam = await this.getQueryParam()
8387
const rows = await statService.select(queryParam, true)
8488
const popupRows: PopupRow[] = []
8589
const other = otherPopupRow()
@@ -117,10 +121,10 @@ class FooterWrapper {
117121
this.afterQuery?.(queryResult)
118122
}
119123

120-
getQueryParam(weekStart: timer.option.WeekStartOption): FooterParam {
124+
async getQueryParam(): Promise<FooterParam> {
121125
const duration = this.timeSelectWrapper.getSelectedTime()
122126
const param: FooterParam = {
123-
date: dateRangeCalculators[duration]?.(new Date(), weekStart),
127+
date: await dateRangeCalculators[duration]?.(new Date()),
124128
mergeHost: this.mergeHostWrapper.mergedHost(),
125129
sort: this.typeSelectWrapper.getSelectedType(),
126130
sortOrder: 'DESC',

src/service/components/week-helper.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
import OptionDatabase from "@db/option-database"
22
import { locale } from "@i18n"
3-
import { formatTimeYMD, getRealWeekStart, getWeekTime } from "@util/time"
3+
import { formatTimeYMD, getWeekDay, MILL_PER_DAY } from "@util/time"
4+
5+
function getRealWeekStart(weekStart: timer.option.WeekStartOption, locale: timer.Locale): number {
6+
weekStart = weekStart ?? 'default'
7+
if (weekStart === 'default') {
8+
return locale === 'zh_CN' ? 0 : 6
9+
} else {
10+
return weekStart - 1
11+
}
12+
}
13+
14+
/**
15+
* Get the start time and end time of this week
16+
* @param now the specific time
17+
* @param weekStart 0-6
18+
* @returns [startTime, endTime]
19+
*
20+
* @since 0.6.0
21+
*/
22+
function getWeekTime(now: Date, weekStart: number): [Date, Date] {
23+
// Returns 0 - 6 means Monday to Sunday
24+
const weekDayNow = getWeekDay(now)
25+
let start: Date = undefined
26+
if (weekDayNow === weekStart) {
27+
start = now
28+
} else if (weekDayNow < weekStart) {
29+
const millDelta = (weekDayNow + 7 - weekStart) * MILL_PER_DAY
30+
start = new Date(now.getTime() - millDelta)
31+
} else {
32+
const millDelta = (weekDayNow - weekStart) * MILL_PER_DAY
33+
start = new Date(now.getTime() - millDelta)
34+
}
35+
return [start, now]
36+
}
437

538
class WeekHelper {
639
private optionDb = new OptionDatabase(chrome.storage.local)
@@ -15,12 +48,16 @@ class WeekHelper {
1548
}
1649

1750
async getWeekDateRange(now: Date): Promise<[startDate: string, endDateOrToday: string]> {
18-
const weekStart = await this.getWeekStart()
19-
const [start, end] = getWeekTime(now, weekStart, locale)
51+
const [start, end] = await this.getWeekDate(now)
2052
return [formatTimeYMD(start), formatTimeYMD(end)]
2153
}
2254

23-
private async getWeekStart(): Promise<timer.option.WeekStartOption> {
55+
async getWeekDate(now: Date | number): Promise<[start: Date, end: Date]> {
56+
const weekStart = await this.getRealWeekStart()
57+
return getWeekTime(typeof now === 'number' ? new Date(now) : now, weekStart)
58+
}
59+
60+
private async getWeekStartOpt(): Promise<timer.option.WeekStartOption> {
2461
if (!this.initialized) {
2562
await this.init()
2663
}
@@ -33,8 +70,8 @@ class WeekHelper {
3370
* @returns 0-6
3471
*/
3572
async getRealWeekStart(): Promise<number> {
36-
const weekStart = await this.getWeekStart()
37-
return getRealWeekStart(weekStart, locale) - 1
73+
const weekStart = await this.getWeekStartOpt()
74+
return getRealWeekStart(weekStart, locale)
3875
}
3976
}
4077

src/util/limit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function skipToday(item: timer.limit.Item): boolean {
2828
const weekdays = item?.weekdays
2929
const weekdayLen = weekdays?.length
3030
if (weekdayLen && weekdayLen !== 7) {
31-
const weekday = getWeekDay(new Date(), true)
31+
const weekday = getWeekDay(new Date())
3232
if (!weekdays.includes(weekday)) return true
3333
}
3434
return false

src/util/time.ts

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export const MILL_PER_HOUR = MILL_PER_MINUTE * 60
118118
*/
119119
export const MILL_PER_DAY = MILL_PER_HOUR * 24
120120

121+
export const MILL_PER_WEEK = MILL_PER_DAY * 7
122+
121123
/**
122124
* Date range between {start} days ago and {end} days ago
123125
*/
@@ -133,64 +135,10 @@ export function isSameDay(a: Date, b: Date): boolean {
133135
}
134136

135137
/**
136-
* Get the start time and end time of this week
137-
* @param now the specific time
138-
* @returns [startTime, endTime]
139-
*
140-
* @since 0.6.0
141-
*/
142-
export function getWeekTime(now: Date, weekStart: timer.option.WeekStartOption, locale: timer.Locale): [Date, Date] {
143-
weekStart = getRealWeekStart(weekStart, locale)
144-
// Returns 0 - 6 means Monday to Sunday
145-
const weekDayNow = getWeekDay(now, true)
146-
const optionWeekDay = weekDayNow + 1
147-
let start: Date = undefined
148-
if (optionWeekDay === weekStart) {
149-
start = now
150-
} else if (optionWeekDay < weekStart) {
151-
const millDelta = (optionWeekDay + 7 - weekStart) * MILL_PER_DAY
152-
start = new Date(now.getTime() - millDelta)
153-
} else {
154-
const millDelta = (optionWeekDay - weekStart) * MILL_PER_DAY
155-
start = new Date(now.getTime() - millDelta)
156-
}
157-
return [start, now]
158-
}
159-
160-
/**
161-
* return 1-7
162-
*/
163-
export function getRealWeekStart(weekStart: timer.option.WeekStartOption, locale: timer.Locale): number {
164-
weekStart = weekStart ?? 'default'
165-
if (weekStart === 'default') {
166-
return locale === 'zh_CN' ? 1 : 7
167-
} else {
168-
return weekStart
169-
}
170-
}
171-
172-
/**
173-
* Get the start time {@param weekCount} weeks ago
174-
*
175-
* @param now the specific time
176-
* @param weekCount weekCount
177-
* @since 1.0.0
178-
*/
179-
export function getWeeksAgo(now: Date, isChinese: boolean, weekCount: number): Date {
180-
const date = new Date(now)
181-
const nowWeekday = getWeekDay(date, isChinese)
182-
return new Date(date.getFullYear(), date.getMonth(), date.getDate() - nowWeekday - weekCount * 7)
183-
}
184-
185-
/**
186-
* @returns 0 to 6, means Monday to Sunday if Chinese, or Sunday to Saturday
138+
* @returns 0 to 6, means Monday to Sunday
187139
*/
188-
export function getWeekDay(now: Date, isChinese: boolean): number {
189-
const date = new Date(now)
190-
return isChinese
191-
// Trans 2 chinese weekday
192-
? (date.getDay() + 6) % 7
193-
: date.getDay()
140+
export function getWeekDay(date: Date): number {
141+
return (date.getDay() + 6) % 7
194142
}
195143

196144
/**

0 commit comments

Comments
 (0)