Heatmap: follow the profile's week-start setting
The GitHub-style activity heatmap hardcoded Monday-first weeks — both the column anchoring and the Mon/Wed/Fri row labels — while every other "this week" surface (Plan, streaks, muscle balance, effort chart) already reads S.weekStart. Anchor the grid with weekDayOffset() and derive the row labels from it too, so a Sunday-first profile gets a Sunday-first grid.
This commit is contained in:
2 files changed
+62
-3
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { fmtVol, isoOf, todayISO, MONTHS } from '../lib/format.js'
|
||||
import { fmtVol, isoOf, todayISO, MONTHS, weekStartOf, weekDayOffset } from '../lib/format.js'
|
||||
import { t } from '../lib/i18n.js'
|
||||
import { tappable } from '../lib/use-sheet-keyboard.js'
|
||||
|
||||
@@ -20,9 +20,15 @@ export default function Heatmap({ S, onDay }) {
|
||||
const level = a => !a ? 0 : !a.min ? 1 : a.min >= t3 ? 4 : a.min >= t2 ? 3 : a.min >= t1 ? 2 : 1
|
||||
|
||||
const today = new Date(); today.setHours(12, 0, 0, 0)
|
||||
const end = new Date(today); end.setDate(today.getDate() - ((today.getDay() + 6) % 7))
|
||||
const ws = weekStartOf(S)
|
||||
const end = new Date(today); end.setDate(today.getDate() - weekDayOffset(today.getDay(), ws))
|
||||
const start = new Date(end); start.setDate(end.getDate() - 52 * 7)
|
||||
|
||||
const dayLabels = Array(7).fill(null)
|
||||
dayLabels[weekDayOffset(1, ws)] = 'Mon'
|
||||
dayLabels[weekDayOffset(3, ws)] = 'Wed'
|
||||
dayLabels[weekDayOffset(5, ws)] = 'Fri'
|
||||
|
||||
const months = [], cols = []
|
||||
let lastMonth = -1
|
||||
for (let wk = 0; wk <= 52; wk++) {
|
||||
@@ -48,7 +54,7 @@ export default function Heatmap({ S, onDay }) {
|
||||
<div className="hm-wrap" ref={wrapRef}>
|
||||
<div className="hm-months" style={{ marginLeft: 30 }}>{months}</div>
|
||||
<div className="hm-body">
|
||||
<div className="hm-days"><span>{t('Mon')}</span><span /><span>{t('Wed')}</span><span /><span>{t('Fri')}</span><span /><span /></div>
|
||||
<div className="hm-days">{dayLabels.map((lbl, i) => <span key={i}>{lbl ? t(lbl) : ''}</span>)}</div>
|
||||
<div className="hm-grid">{cols}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// @vitest-environment happy-dom
|
||||
// The heatmap used to hardcode Monday-first weeks. It should follow S.weekStart like every
|
||||
// other "this week" surface (see lib/week-start.test.js and views/week-start.test.jsx).
|
||||
import React, { act } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import Heatmap from './Heatmap.jsx'
|
||||
import { isoOf } from '../lib/format.js'
|
||||
|
||||
globalThis.IS_REACT_ACT_ENVIRONMENT = true
|
||||
|
||||
let container, root
|
||||
beforeEach(() => {
|
||||
container = document.createElement('div')
|
||||
document.body.appendChild(container)
|
||||
root = createRoot(container)
|
||||
})
|
||||
afterEach(() => {
|
||||
act(() => root.unmount())
|
||||
container.remove()
|
||||
})
|
||||
|
||||
const baseS = { workouts: [], unit: 'kg' }
|
||||
const render = weekStart => act(() => root.render(<Heatmap S={{ ...baseS, weekStart }} onDay={() => {}} />))
|
||||
const dayLabels = () => [...container.querySelectorAll('.hm-days span')].map(s => s.textContent)
|
||||
const lastColumn = () => {
|
||||
const cols = container.querySelectorAll('.hm-grid .hm-col')
|
||||
return [...cols[cols.length - 1].querySelectorAll('.hm-c')]
|
||||
}
|
||||
|
||||
describe('Heatmap — week start', () => {
|
||||
it('rows read Mon/Wed/Fri for a Monday-first week (default)', () => {
|
||||
render(undefined)
|
||||
expect(dayLabels()).toEqual(['Mon', '', 'Wed', '', 'Fri', '', ''])
|
||||
})
|
||||
|
||||
it('shifts the same three labels down a row for a Sunday-first week', () => {
|
||||
render(0)
|
||||
expect(dayLabels()).toEqual(['', 'Mon', '', 'Wed', '', 'Fri', ''])
|
||||
})
|
||||
|
||||
it('the current (rightmost) column starts on the chosen week-start day and includes today', () => {
|
||||
render(1)
|
||||
let cells = lastColumn()
|
||||
expect(new Date(cells[0].title.slice(0, 10) + 'T12:00:00').getDay()).toBe(1)
|
||||
expect(cells.map(c => c.title.slice(0, 10))).toContain(isoOf(new Date()))
|
||||
|
||||
render(0)
|
||||
cells = lastColumn()
|
||||
expect(new Date(cells[0].title.slice(0, 10) + 'T12:00:00').getDay()).toBe(0)
|
||||
expect(cells.map(c => c.title.slice(0, 10))).toContain(isoOf(new Date()))
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user