Add a calendar heatmap to personal listening activity
This commit is contained in:
1 parent
576e802b7a
commit
3f5fbdd05a
8 files changed
+355
-30
No files matched your search
@@ -10,6 +10,12 @@
|
||||
- Desktop/mobile lyrics view following playback, individual track previews,
|
||||
availability icons, and local version editing in the admin track editor.
|
||||
|
||||
## v0.15.3 — 2026-11-24
|
||||
|
||||
- Listening by day can display a calendar heatmap shaded by daily play count.
|
||||
Select a day to open its history, or select the last 365 days for a full year.
|
||||
The player remembers the chart style in the browser.
|
||||
|
||||
## v0.15.2 — 2026-11-24
|
||||
|
||||
- Reorder pinned sidebar shortcuts by dragging or using the arrow keys. The
|
||||
|
||||
Generated
+1
-1
@@ -2062,7 +2062,7 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
|
||||
|
||||
[[package]]
|
||||
name = "furumusic"
|
||||
version = "0.15.2"
|
||||
version = "0.15.3"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-stream",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "furumusic"
|
||||
version = "0.15.2"
|
||||
version = "0.15.3"
|
||||
edition = "2024"
|
||||
description = "Reusable web-app boilerplate: auth, OIDC/SSO, admin panel, user management, i18n, PostgreSQL"
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
<option value="7" x-text="listens.text('week')"></option>
|
||||
<option value="30" x-text="listens.text('month')"></option>
|
||||
<option value="90" x-text="listens.text('quarter')"></option>
|
||||
<option value="365" x-text="listens.text('year')"></option>
|
||||
</select>
|
||||
</header>
|
||||
<nav class="listening-tabs" :aria-label="listens.text('title')">
|
||||
@@ -37,30 +38,79 @@
|
||||
<div><dt x-text="listens.text('tracksMetric')"></dt><dd x-text="listens.number(listens.overview.tracks)"></dd></div>
|
||||
</dl>
|
||||
<p class="listening-notice" x-show="!listens.overview.plays" x-text="listens.text('emptyOverview')"></p>
|
||||
<div class="listening-panels" x-show="listens.overview.plays">
|
||||
<section class="listening-panel">
|
||||
<h2 x-text="listens.text('daily')"></h2>
|
||||
<div class="listening-chart-scroll" :aria-label="listens.text('daily')">
|
||||
<div class="listening-chart" :style="'--days:' + listens.chart.length">
|
||||
<template x-for="(point, index) in listens.chart" :key="point.day">
|
||||
<button type="button" class="listening-day" :disabled="!point.plays"
|
||||
:aria-label="listens.date(point.day) + ' · ' + listens.duration(point.seconds)"
|
||||
:title="listens.date(point.day) + ' · ' + listens.duration(point.seconds)"
|
||||
@click="$store.library.openListening('history', {day:point.day})">
|
||||
<span class="listening-bar" :style="{height: point.height + '%'}" :class="{'has-plays':point.plays}"></span>
|
||||
<span class="listening-tick" aria-hidden="true"
|
||||
x-show="index === 0 || listens.chart.length === 7 || point.day.endsWith('-01') || (index % 5 === 0 && index !== listens.chart.length-1)"
|
||||
x-text="Number(point.day.slice(-2))"></span>
|
||||
</button>
|
||||
</template>
|
||||
<div class="listening-panels" :class="{'listening-panels-year':listens.chartMode === 'heatmap' && listens.period === 365}">
|
||||
<section class="listening-panel listening-daily-panel">
|
||||
<header class="listening-chart-header">
|
||||
<h2 x-text="listens.text('daily')"></h2>
|
||||
<div class="listening-chart-switch" role="group" :aria-label="listens.text('chartView')">
|
||||
<button type="button" data-mode="bars" :aria-label="listens.text('bars')" :title="listens.text('bars')"
|
||||
:aria-pressed="listens.chartMode === 'bars'" @click="listens.setChartMode('bars')">
|
||||
<svg viewBox="0 0 16 16" aria-hidden="true" fill="currentColor"><path d="M1 8h3v6H1zm5-6h3v12H6zm5 3h3v9h-3z"/></svg>
|
||||
<span x-text="listens.text('bars')"></span>
|
||||
</button>
|
||||
<button type="button" data-mode="heatmap" :aria-label="listens.text('heatmap')" :title="listens.text('heatmap')"
|
||||
:aria-pressed="listens.chartMode === 'heatmap'" @click="listens.setChartMode('heatmap')">
|
||||
<svg viewBox="0 0 16 16" aria-hidden="true" fill="currentColor"><path d="M1 1h4v4H1zm5 0h4v4H6zm5 0h4v4h-4zM1 6h4v4H1zm5 0h4v4H6zm5 0h4v4h-4zM1 11h4v4H1zm5 0h4v4H6zm5 0h4v4h-4z"/></svg>
|
||||
<span x-text="listens.text('heatmap')"></span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="listening-chart-dates">
|
||||
</header>
|
||||
<template x-if="listens.chartMode === 'bars' && listens.overview.plays">
|
||||
<div class="listening-chart-scroll" :aria-label="listens.text('daily')"
|
||||
x-init="if (listens.chart.length > 90) $nextTick(() => { $el.scrollLeft = $el.scrollWidth })">
|
||||
<div class="listening-chart" :style="'--days:' + listens.chart.length" :class="{'listening-chart-year':listens.chart.length > 90}">
|
||||
<template x-for="(point, index) in listens.chart" :key="point.day">
|
||||
<button type="button" class="listening-day" :disabled="!point.plays"
|
||||
:aria-label="listens.date(point.day) + ' · ' + listens.duration(point.seconds)"
|
||||
:title="listens.date(point.day) + ' · ' + listens.duration(point.seconds)"
|
||||
@click="$store.library.openListening('history', {day:point.day})">
|
||||
<span class="listening-bar" :style="{height: point.height + '%'}" :class="{'has-plays':point.plays}"></span>
|
||||
<template x-if="listens.chart.length > 90 ? point.day.endsWith('-01') && index < listens.chart.length - 12
|
||||
: index === 0 || listens.chart.length === 7 || point.day.endsWith('-01') || (index % 5 === 0 && index !== listens.chart.length-1)">
|
||||
<span class="listening-tick" aria-hidden="true" x-text="listens.chart.length > 90 ? listens.month(point.day) : Number(point.day.slice(-2))"></span>
|
||||
</template>
|
||||
</button>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<template x-if="listens.chartMode === 'heatmap'">
|
||||
<div class="listening-heatmap" :class="{'listening-heatmap-year':listens.heatmap.weeks > 20}">
|
||||
<div class="listening-heatmap-scroll" x-init="$nextTick(() => { $el.scrollLeft = $el.scrollWidth })">
|
||||
<div class="listening-heatmap-grid" :style="{'--weeks':listens.heatmap.weeks}"
|
||||
@keydown="listens.moveHeatFocus($event)">
|
||||
<template x-for="month in listens.heatmap.months" :key="month.week">
|
||||
<span aria-hidden="true" class="listening-heatmap-month" :style="{gridColumn:month.week+2}" x-text="month.label"></span>
|
||||
</template>
|
||||
<template x-for="(label, weekday) in listens.heatmap.weekdays" :key="weekday">
|
||||
<span aria-hidden="true" class="listening-heatmap-weekday" :style="{gridRow:weekday+2}"
|
||||
x-text="weekday === 0 || weekday === 2 || weekday === 4 ? label : ''"></span>
|
||||
</template>
|
||||
<template x-for="point in listens.heatmap.days" :key="point.day">
|
||||
<a class="listening-heatmap-day" :data-day="point.day" :data-level="point.level"
|
||||
:style="{gridColumn:point.week+2, gridRow:point.weekday+2}"
|
||||
:href="'#listening/history?day=' + point.day"
|
||||
:aria-label="listens.heatDayLabel(point)" :title="listens.heatDayLabel(point)"
|
||||
:tabindex="point.day === listens.heatFocusDay ? 0 : -1" @focus="listens.heatFocusDay = point.day"></a>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
<div class="listening-heatmap-legend">
|
||||
<span x-text="listens.text('less')"></span>
|
||||
<template x-for="level in [0,1,2,3,4]" :key="level">
|
||||
<i aria-hidden="true" :data-level="level"></i>
|
||||
</template>
|
||||
<span x-text="listens.text('moreActivity')"></span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<div class="listening-chart-dates" :class="{'compact':listens.chartMode === 'heatmap' && listens.period < 365}"
|
||||
x-show="listens.overview.plays || listens.chartMode === 'heatmap'">
|
||||
<span x-text="listens.date(listens.chart[0].day)"></span>
|
||||
<span x-text="listens.date(listens.chart[listens.chart.length-1].day)"></span>
|
||||
</div>
|
||||
</section>
|
||||
<section class="listening-panel listening-rank-panel">
|
||||
<section class="listening-panel listening-rank-panel" x-show="listens.overview.plays">
|
||||
<h2 x-text="listens.text('top')"></h2>
|
||||
<div class="listening-rank-kinds" role="group" :aria-label="listens.text('rankBy')">
|
||||
<template x-for="kind in ['artists','tracks','releases']" :key="kind">
|
||||
|
||||
@@ -4,9 +4,12 @@ document.addEventListener('alpine:init', () => {
|
||||
title: ['My listening', 'Мои прослушивания'], overview: ['Overview', 'Обзор'],
|
||||
history: ['History', 'История'], period: ['Period', 'Период'], date: ['Date', 'Дата'],
|
||||
week: ['Last 7 days', 'Последние 7 дней'], month: ['Last 30 days', 'Последние 30 дней'],
|
||||
year: ['Last 365 days', 'Последние 365 дней'],
|
||||
quarter: ['Last 90 days', 'Последние 90 дней'], allDates: ['All dates', 'Все даты'],
|
||||
time: ['Listening time', 'Время прослушивания'], plays: ['Plays', 'Прослушивания'],
|
||||
tracksMetric: ['Different tracks', 'Разных треков'], daily: ['Listening by day', 'По дням'],
|
||||
chartView: ['Chart view', 'Вид графика'], bars: ['Bars', 'Столбцы'], heatmap: ['Heatmap', 'Календарь'],
|
||||
less: ['Less', 'Меньше'], moreActivity: ['More', 'Больше'],
|
||||
top: ['In heavy rotation', 'Чаще всего'], artists: ['Artists', 'Артисты'],
|
||||
tracks: ['Tracks', 'Треки'], releases: ['Albums', 'Альбомы'], rankBy: ['Rank by', 'Рейтинг'],
|
||||
emptyOverview: ['No plays in this period.', 'За этот период прослушиваний нет.'],
|
||||
@@ -22,6 +25,18 @@ document.addEventListener('alpine:init', () => {
|
||||
const numbers = new Intl.NumberFormat(locale);
|
||||
const dates = new Intl.DateTimeFormat(locale, {day:'numeric', month:'long', year:'numeric'});
|
||||
const times = new Intl.DateTimeFormat(locale, {hour:'2-digit', minute:'2-digit'});
|
||||
const months = new Intl.DateTimeFormat(locale, {month:'short'});
|
||||
const weekdays = new Intl.DateTimeFormat(locale, {weekday:'short'});
|
||||
const weekLabels = Array.from({length:7}, (_, i) => weekdays.format(new Date(2024, 0, 1 + i, 12))); // Jan 1 was a Monday.
|
||||
const plurals = new Intl.PluralRules(locale);
|
||||
const playWords = locale.startsWith('ru')
|
||||
? {one:'прослушивание', few:'прослушивания', many:'прослушиваний', other:'прослушивания'}
|
||||
: {one:'play', other:'plays'};
|
||||
const chartModeKey = 'furumusic.listening.chartMode';
|
||||
let chartMode = 'bars';
|
||||
try {
|
||||
if (localStorage.getItem(chartModeKey) === 'heatmap') chartMode = 'heatmap';
|
||||
} catch {}
|
||||
const calendar = date => [date.getFullYear(), String(date.getMonth() + 1).padStart(2, '0'), String(date.getDate()).padStart(2, '0')].join('-');
|
||||
const localDate = day => new Date(day + 'T12:00:00');
|
||||
const validDay = day => /^\d{4}-\d{2}-\d{2}$/.test(day || '') && calendar(localDate(day)) === day;
|
||||
@@ -29,12 +44,14 @@ document.addEventListener('alpine:init', () => {
|
||||
let playRequest = 0;
|
||||
let pending;
|
||||
Alpine.store('listening', {
|
||||
tab: 'overview', period: 30, kind: 'artists', day: '', page: 1,
|
||||
tab: 'overview', period: 30, kind: 'artists', day: '', page: 1, chartMode,
|
||||
overview: null, overviewPeriod: 0, rankedKind: '', history: null, chart: [], groups: [],
|
||||
heatmap: {weeks:0, days:[], months:[], weekdays:weekLabels}, heatFocusDay: '',
|
||||
loading: false, error: '', actionError: '',
|
||||
text(key) { return labels[key][locale.startsWith('ru') ? 1 : 0]; },
|
||||
number(value) { return numbers.format(value || 0); },
|
||||
date(day) { return dates.format(localDate(day)); },
|
||||
month(day) { return months.format(localDate(day)); },
|
||||
clock(ms) { return times.format(new Date(ms)); },
|
||||
coverFailed(image) {
|
||||
image.setAttribute('data-unavailable', '');
|
||||
@@ -78,6 +95,56 @@ document.addEventListener('alpine:init', () => {
|
||||
}
|
||||
return points;
|
||||
},
|
||||
setChartMode(mode) {
|
||||
if (mode !== 'bars' && mode !== 'heatmap') return;
|
||||
this.chartMode = mode;
|
||||
try { localStorage.setItem(chartModeKey, mode); } catch {}
|
||||
},
|
||||
buildHeatmap(points) {
|
||||
const result = {weeks:0, days:[], months:[], weekdays:weekLabels};
|
||||
if (!points.length) return result;
|
||||
const offset = (localDate(points[0].day).getDay() + 6) % 7;
|
||||
result.weeks = Math.ceil((offset + points.length) / 7);
|
||||
// Compare active days in this period. A single unusually busy day
|
||||
// should not give every other active day the same colour.
|
||||
const counts = points.map(p => p.plays).filter(n => n > 0).sort((a, b) => a - b);
|
||||
const cutoffs = [0.25, 0.5, 0.75].map(q => counts[Math.ceil(q * counts.length) - 1]);
|
||||
for (const [index, point] of points.entries()) {
|
||||
const slot = offset + index;
|
||||
const week = Math.floor(slot / 7);
|
||||
let level = 0;
|
||||
if (point.plays > 0) {
|
||||
level = point.plays === counts.at(-1) ? 4 : 1 + cutoffs.filter(c => point.plays > c).length;
|
||||
}
|
||||
result.days.push({day:point.day, plays:point.plays, week, weekday:slot % 7, level});
|
||||
if (index === 0 || point.day.endsWith('-01')) {
|
||||
// Drop a sliver of the first month if the labels would overlap.
|
||||
if (result.months.length && week - result.months.at(-1).week < 3) result.months.pop();
|
||||
result.months.push({week, label:this.month(point.day)});
|
||||
}
|
||||
}
|
||||
return result;
|
||||
},
|
||||
heatDayLabel(point) {
|
||||
const unit = playWords[plurals.select(point.plays)] || playWords.other;
|
||||
return this.date(point.day) + ' · ' + this.number(point.plays) + ' ' + unit;
|
||||
},
|
||||
moveHeatFocus(event) {
|
||||
const delta = {ArrowUp:-1, ArrowDown:1, ArrowLeft:-7, ArrowRight:7};
|
||||
if (!(event.key in delta) && event.key !== 'Home' && event.key !== 'End') return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
const days = this.heatmap.days;
|
||||
const button = event.target.closest('[data-day]');
|
||||
if (!button) return;
|
||||
const index = days.findIndex(p => p.day === button.dataset.day);
|
||||
if (index < 0) return;
|
||||
let next = index + delta[event.key];
|
||||
if (event.key === 'Home') next = 0;
|
||||
if (event.key === 'End') next = days.length - 1;
|
||||
if (!days[next]) return;
|
||||
button.closest('.listening-heatmap-grid').querySelector('[data-day="' + days[next].day + '"]').focus();
|
||||
},
|
||||
historyDays(items) {
|
||||
const groups = [];
|
||||
for (const item of items) {
|
||||
@@ -106,6 +173,10 @@ document.addEventListener('alpine:init', () => {
|
||||
this.overviewPeriod = this.period;
|
||||
this.rankedKind = this.kind;
|
||||
this.chart = this.dailyPoints(data.daily, range);
|
||||
this.heatmap = this.buildHeatmap(this.chart);
|
||||
if (!this.chart.some(point => point.day === this.heatFocusDay)) {
|
||||
this.heatFocusDay = range.to;
|
||||
}
|
||||
} else {
|
||||
this.history = data;
|
||||
this.page = data.page;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<style>
|
||||
.my-listening { max-width: 1150px; margin: 0 auto; padding: 0 0 20px; }
|
||||
.my-listening { max-width: 1150px; margin: 0 auto; padding: 0 0 20px; container: listening-page / inline-size; }
|
||||
.my-listening button, .my-listening select, .my-listening input { font: inherit; color: inherit; }
|
||||
.my-listening button, .my-listening summary { cursor: pointer; }
|
||||
.my-listening button:disabled { cursor: default; }
|
||||
@@ -20,14 +20,56 @@
|
||||
.listening-panels { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(240px, 1fr); gap: 16px; }
|
||||
.listening-panel { min-width: 0; padding: clamp(12px, 2vw, 20px); background: var(--bg-secondary); border: 1px solid var(--border-color); border-radius: 8px; }
|
||||
.listening-panel h2, .listening-history-group h2 { margin: 0 0 12px; font-size: 18px; letter-spacing: -.015em; }
|
||||
.listening-daily-panel { container-type: inline-size; }
|
||||
.listening-chart-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
|
||||
.listening-chart-header h2 { margin: 0; }
|
||||
.listening-chart-switch { display: inline-flex; flex: none; padding: 2px; gap: 2px; border: 1px solid var(--border-color); border-radius: 7px; }
|
||||
.listening-chart-switch button { display: inline-flex; align-items: center; justify-content: center; gap: 5px; padding: 5px 8px; border: 0; border-radius: 4px; background: transparent; color: var(--text-secondary); font-size: 12px; }
|
||||
.listening-chart-switch button svg { width: 13px; height: 13px; }
|
||||
.listening-chart-switch button:hover, .listening-chart-switch button:focus-visible { color: var(--text-primary); }
|
||||
.listening-chart-switch button[aria-pressed=true] { background: var(--bg-active); color: var(--text-primary); }
|
||||
@container (max-width: 340px) {
|
||||
.listening-chart-switch button { min-width: 30px; min-height: 30px; }
|
||||
.listening-chart-switch button span { display: none; }
|
||||
}
|
||||
.listening-heatmap { --heat-cell: 18px; --heat-gap: 3px; width: fit-content; max-width: 100%; }
|
||||
.listening-heatmap-year { --heat-cell: clamp(8px, calc((100cqi - 56px) / 53 - 2px), 12px); --heat-gap: 2px; width: 100%; }
|
||||
.listening-heatmap-scroll { overflow-x: auto; overscroll-behavior-x: contain; scrollbar-width: thin; }
|
||||
.listening-heatmap-grid {
|
||||
display: grid; grid-template-columns: max-content repeat(var(--weeks), var(--heat-cell));
|
||||
grid-template-rows: 1.4em repeat(7, var(--heat-cell)); gap: var(--heat-gap); align-items: center;
|
||||
width: max-content; padding: 4px 22px 5px 2px; color: var(--text-secondary); font-size: 11px;
|
||||
}
|
||||
.listening-heatmap-grid::before { content: ''; grid-area: 1 / 1; position: sticky; left: 0; z-index: 1; align-self: stretch; background: var(--bg-secondary); }
|
||||
.listening-heatmap-month { grid-row: 1; white-space: nowrap; }
|
||||
.listening-heatmap-weekday { grid-column: 1; position: sticky; left: 0; z-index: 1; align-self: stretch; display: flex; align-items: center; margin-block: calc(-.5 * var(--heat-gap)); padding-right: 5px; background: var(--bg-secondary); }
|
||||
.listening-heatmap [data-level] { display: block; border-radius: 2px; background: #292e2b; box-shadow: inset 0 0 0 1px #ffffff0c; }
|
||||
.listening-heatmap [data-level="1"] { background: #16422a; }
|
||||
.listening-heatmap [data-level="2"] { background: #19753e; }
|
||||
.listening-heatmap [data-level="3"] { background: #25a650; }
|
||||
.listening-heatmap [data-level="4"] { background: #62d57d; }
|
||||
.listening-heatmap-day { width: var(--heat-cell); height: var(--heat-cell); }
|
||||
.listening-heatmap-day:hover, .my-listening .listening-heatmap-day:focus-visible { outline: 2px solid var(--text-primary); outline-offset: 0; z-index: 2; }
|
||||
.listening-heatmap-legend { display: flex; justify-content: flex-end; align-items: center; gap: 3px; margin-top: 8px; font-size: 11px; color: var(--text-secondary); }
|
||||
.listening-heatmap-legend span { padding-inline: 2px; }
|
||||
.listening-heatmap-legend i { width: 10px; height: 10px; }
|
||||
@media (pointer: coarse), (max-width: 540px) {
|
||||
.listening-heatmap, .listening-heatmap-year { --heat-cell: 20px; --heat-gap: 4px; }
|
||||
.listening-chart-switch button { min-height: 34px; min-width: 34px; }
|
||||
}
|
||||
.listening-chart-scroll { overflow-x: auto; overscroll-behavior-x: contain; scrollbar-width: thin; }
|
||||
.listening-chart { display: grid; grid-template-columns: repeat(var(--days), minmax(11px, 1fr)); width: 100%; min-width: calc(var(--days) * 11px); height: 180px; margin: 12px 0 24px; border-bottom: 1px solid var(--border-color); background: repeating-linear-gradient(to top, transparent 0, transparent calc(25% - 1px), var(--border-color) calc(25% - 1px), var(--border-color) 25%); }
|
||||
.listening-chart-year { grid-template-columns: repeat(var(--days), minmax(1.5px, 1fr)); min-width: calc(var(--days) * 1.5px); }
|
||||
.listening-chart-year .listening-day { min-width: 0; padding: 0; }
|
||||
.listening-chart-year .listening-tick { left: 0; }
|
||||
.listening-day { display: flex; align-items: flex-end; justify-content: center; position: relative; border: 0; background: transparent; }
|
||||
.listening-day:hover:not(:disabled) .listening-bar, .listening-day:focus-visible .listening-bar { background: var(--accent-hover); }
|
||||
.listening-bar { display: block; width: 75%; max-width: 32px; border-radius: 3px 3px 0 0; background: var(--accent); }
|
||||
.listening-bar.has-plays { min-height: 3px; }
|
||||
.listening-tick { position: absolute; top: calc(100% + 4px); color: var(--text-secondary); font-size: 10px; }
|
||||
.listening-chart-dates { justify-content: space-between; margin-top: 8px; font-size: 12px; }
|
||||
.listening-chart-dates.compact { justify-content: flex-start; flex-wrap: wrap; gap: 4px; }
|
||||
.listening-chart-dates.compact span + span::before { content: '– '; }
|
||||
.listening-rank-kinds { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 10px; }
|
||||
.listening-rank-kinds button, .listening-pager button { padding: 6px 10px; border: 1px solid var(--border-color); border-radius: 5px; background: transparent; color: var(--text-secondary); font-size: 13px; }
|
||||
.listening-rank-kinds button[aria-pressed=true] { background: var(--bg-active); color: var(--text-primary); }
|
||||
@@ -71,6 +113,12 @@
|
||||
@media (max-width: 1080px) {
|
||||
.listening-panels { grid-template-columns: minmax(0, 1fr); }
|
||||
}
|
||||
@container listening-page (min-width: 980px) {
|
||||
.listening-panels-year { grid-template-columns: minmax(730px, 2fr) minmax(0, 1fr); }
|
||||
}
|
||||
@container listening-page (max-width: 979px) {
|
||||
.listening-panels-year { grid-template-columns: minmax(0, 1fr); }
|
||||
}
|
||||
@media (max-width: 540px) {
|
||||
.my-listening { width: 100%; }
|
||||
.listening-header { gap: 8px; }
|
||||
|
||||
+102
-5
@@ -12,15 +12,109 @@ const overview = {
|
||||
tracks: { ...seeder, plays:5, detail:'Singer' },
|
||||
releases: { ...seeder, title:'Dawn', plays:5, detail:'Singer' },
|
||||
};
|
||||
const daily = [
|
||||
{day:'2024-02-29',plays:1,listened_seconds:6000},
|
||||
{day:'2024-03-30',plays:1,listened_seconds:4000},
|
||||
{day:'2024-03-31',plays:8,listened_seconds:1800},
|
||||
{day:'2024-04-01',plays:16,listened_seconds:900},
|
||||
{day:'2024-04-02',plays:1,listened_seconds:80},
|
||||
];
|
||||
|
||||
async function checkCalendar(page, queries, width, showEmpty) {
|
||||
const content = page.locator('.my-listening');
|
||||
const mode = name => content.locator('.listening-chart-switch [data-mode="' + name + '"]');
|
||||
const cell = day => content.locator('.listening-heatmap-day[data-day="' + day + '"]');
|
||||
const waitDays = count => page.waitForFunction(count =>
|
||||
document.querySelectorAll('.listening-heatmap-day').length === count, count);
|
||||
const waitFocused = day => page.waitForFunction(day => document.activeElement?.dataset.day === day, day);
|
||||
assert.equal(await mode('bars').getAttribute('aria-pressed'), 'true');
|
||||
const requests = queries.length;
|
||||
if (width < 600) await mode('heatmap').tap();
|
||||
else await mode('heatmap').click();
|
||||
await waitDays(7);
|
||||
assert.equal(await mode('heatmap').getAttribute('aria-pressed'), 'true');
|
||||
assert.equal(await content.locator('.listening-chart:visible').count(), 0);
|
||||
assert.equal(queries.length, requests, 'switching charts uses the loaded daily totals');
|
||||
assert.equal(await cell('2024-04-01').getAttribute('data-level'), '4');
|
||||
assert.equal(await cell('2024-03-28').getAttribute('data-level'), '0');
|
||||
assert.match(await cell('2024-04-01').getAttribute('aria-label'), /\b16\b/);
|
||||
const saturday = await cell('2024-03-30').boundingBox();
|
||||
const sunday = await cell('2024-03-31').boundingBox();
|
||||
const monday = await cell('2024-04-01').boundingBox();
|
||||
assert.ok(Math.abs(saturday.x - sunday.x) < 1 && sunday.y > saturday.y);
|
||||
assert.ok(monday.x > sunday.x && monday.y < saturday.y, 'the new week starts in the next column');
|
||||
assert.equal(queries.at(-1).to, '2024-04-02', 'period ends on local time after the DST transition');
|
||||
|
||||
await content.locator('select').selectOption('365');
|
||||
await waitDays(365);
|
||||
assert.equal(queries.at(-1).from, '2023-04-04');
|
||||
assert.equal(await cell('2024-02-29').count(), 1);
|
||||
const viewport = await content.locator('.listening-heatmap-scroll').boundingBox();
|
||||
await page.waitForFunction(() => {
|
||||
const last = document.querySelector('.listening-heatmap-day[data-day="2024-04-02"]').getBoundingClientRect();
|
||||
const scroll = document.querySelector('.listening-heatmap-scroll').getBoundingClientRect();
|
||||
return last.left >= scroll.left - 1 && last.right <= scroll.right + 1;
|
||||
});
|
||||
assert.ok(viewport.x >= 0 && viewport.x + viewport.width <= width + 1);
|
||||
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false);
|
||||
if (width >= 1366) {
|
||||
assert.ok(await content.locator('.listening-heatmap-scroll').evaluate(el => el.scrollWidth <= el.clientWidth + 1),
|
||||
'the whole year fits the desktop panel');
|
||||
}
|
||||
const tabbable = content.locator('.listening-heatmap-day[tabindex="0"]');
|
||||
assert.equal(await tabbable.count(), 1);
|
||||
await tabbable.focus();
|
||||
await page.keyboard.press('End');
|
||||
await waitFocused('2024-04-02');
|
||||
await page.keyboard.press('ArrowLeft');
|
||||
await waitFocused('2024-03-26');
|
||||
await page.keyboard.press('ArrowUp');
|
||||
await waitFocused('2024-03-25');
|
||||
await page.keyboard.press('Home');
|
||||
await page.keyboard.press('ArrowUp');
|
||||
await waitFocused('2023-04-04');
|
||||
|
||||
const yearRequests = queries.length;
|
||||
await mode('bars').click();
|
||||
await page.waitForFunction(() => document.querySelectorAll('.listening-day').length === 365);
|
||||
assert.ok(await content.locator('.listening-chart').isVisible());
|
||||
await mode('heatmap').click();
|
||||
await waitDays(365);
|
||||
assert.equal(queries.length, yearRequests);
|
||||
await page.reload();
|
||||
await waitDays(30);
|
||||
assert.equal(await mode('heatmap').getAttribute('aria-pressed'), 'true');
|
||||
if (width < 600) await cell('2024-04-01').tap();
|
||||
else await cell('2024-04-01').click();
|
||||
await page.waitForURL('**/#listening/history?day=2024-04-01');
|
||||
await content.locator('.listening-history-row').waitFor();
|
||||
assert.equal(queries.at(-1).to, '2024-04-01');
|
||||
await page.goBack();
|
||||
await waitDays(30);
|
||||
await cell('2024-03-29').click();
|
||||
await page.waitForURL('**/#listening/history?day=2024-03-29');
|
||||
await content.locator('.listening-notice:visible').filter({hasText:/No plays on this day|В этот день/}).waitFor();
|
||||
|
||||
showEmpty();
|
||||
await content.locator('.listening-tabs a').first().click();
|
||||
await page.waitForFunction(() => document.querySelectorAll('.listening-heatmap-day[data-level="0"]').length === 30);
|
||||
assert.equal(await mode('bars').isVisible(), true);
|
||||
await mode('bars').click();
|
||||
await page.reload();
|
||||
assert.equal(await mode('bars').getAttribute('aria-pressed'), 'true');
|
||||
}
|
||||
|
||||
async function run(browser, width, locale) {
|
||||
const page = await browser.newPage({viewport:{width, height:850}, timezoneId:'Europe/Madrid', locale});
|
||||
const page = await browser.newPage({viewport:{width, height:850}, timezoneId:'Europe/Madrid', locale,
|
||||
hasTouch:width < 600, isMobile:width < 600});
|
||||
page.setDefaultTimeout(6000);
|
||||
await page.clock.setFixedTime(new Date('2024-04-01T22:30:00Z'));
|
||||
const errors = [];
|
||||
const queries = [];
|
||||
const coverRequests = [];
|
||||
let artistCover = '/api/player/cover/10/small';
|
||||
let fail = false;
|
||||
let empty = false;
|
||||
page.on('pageerror', error => errors.push(error.message));
|
||||
await page.route('**/*', async route => {
|
||||
const url = new URL(route.request().url());
|
||||
@@ -44,11 +138,13 @@ async function run(browser, width, locale) {
|
||||
if (fail) return route.fulfill({status:503,json:{}});
|
||||
if (query.view === 'overview') {
|
||||
const item = query.kind === 'artists' ? {...overview.artists, cover_url:artistCover} : overview[query.kind];
|
||||
return send({plays:5, tracks:1, listened_seconds:900, ranking:[item],
|
||||
daily:[{day:query.to,plays:5,listened_seconds:900}]});
|
||||
const rows = empty ? [] : daily.filter(row => row.day >= query.from && row.day <= query.to);
|
||||
return send({plays:rows.reduce((sum,row)=>sum + row.plays, 0), tracks:rows.length ? 1 : 0,
|
||||
listened_seconds:rows.reduce((sum,row)=>sum + row.listened_seconds, 0), ranking:rows.length ? [item] : [], daily:rows});
|
||||
}
|
||||
const selected = query.from !== 'all';
|
||||
const date = selected ? query.from : '2026-03-28';
|
||||
if (selected && !daily.some(row => row.day === date)) return send({page:1,limit:25,total:0,items:[]});
|
||||
return send({page:+query.page, limit:25, total:selected ? 1 : 26, items:[{...seeder,
|
||||
id:'event-'+query.page, title:'Hours '+query.page, started_at_ms: Date.parse(date+'T18:05:00Z')}]});
|
||||
}
|
||||
@@ -93,7 +189,7 @@ async function run(browser, width, locale) {
|
||||
await page.waitForFunction(() => window.lastPlayed === 3);
|
||||
await content.locator('.listening-rank-kinds button').nth(2).click();
|
||||
assert.equal(await content.getByRole('link',{name:'Dawn',exact:true}).getAttribute('href'), '#release/7');
|
||||
await content.locator('.listening-day:not(:disabled)').click();
|
||||
await content.locator('.listening-day:not(:disabled)').last().click();
|
||||
await page.waitForURL('**/#listening/history?day=' + expectedDay);
|
||||
await content.locator('.listening-history-row').waitFor();
|
||||
assert.equal(queries.at(-1).from, expectedDay);
|
||||
@@ -112,6 +208,7 @@ async function run(browser, width, locale) {
|
||||
fail = false;
|
||||
await content.locator('[role=alert] button').click();
|
||||
await content.locator('.listening-totals').waitFor();
|
||||
await checkCalendar(page, queries, width, () => {empty = true;});
|
||||
assert.deepEqual(errors, []);
|
||||
await page.close();
|
||||
}
|
||||
@@ -121,6 +218,6 @@ async function run(browser, width, locale) {
|
||||
try {
|
||||
await run(browser, 1366, 'en');
|
||||
await run(browser, 320, 'ru');
|
||||
console.log('Personal listening overview, playback, day drill-down, history and retry passed on desktop and mobile.');
|
||||
console.log('Personal listening overview, calendar heatmap, playback, day drill-down, history and retry passed on desktop and mobile.');
|
||||
} finally { await browser.close(); }
|
||||
})().catch(error => { console.error(error); process.exitCode = 1; });
|
||||
@@ -4,9 +4,9 @@ const {test} = require('node:test');
|
||||
const vm = require('node:vm');
|
||||
const script = readFileSync(__dirname + '/../templates/player/listening_scripts.html', 'utf8').replace(/<\/?script>/g, '');
|
||||
|
||||
function setup(fetch, stores = {}) {
|
||||
function setup(fetch, stores = {}, localStorage) {
|
||||
const Alpine = {store(name, state) {if (state) stores[name] = state; return stores[name];}};
|
||||
vm.runInNewContext(script, {Alpine, fetch, URLSearchParams, AbortController,
|
||||
vm.runInNewContext(script, {Alpine, fetch, URLSearchParams, AbortController, localStorage,
|
||||
document:{documentElement:{lang:'en'}, addEventListener(evt, fn) {fn();}}});
|
||||
return stores.listening;
|
||||
}
|
||||
@@ -63,3 +63,56 @@ test('the chart fills quiet calendar days including the leap day', () => {
|
||||
['2024-02-28',0], ['2024-02-29',0], ['2024-03-01',100],
|
||||
]);
|
||||
});
|
||||
|
||||
test('the listening calendar aligns Monday weeks across leap day and month boundaries', () => {
|
||||
const listening = setup(() => assert.fail('Unexpected request'));
|
||||
const points = listening.dailyPoints([], {from:'2024-02-29',to:'2024-04-02'});
|
||||
const calendar = listening.buildHeatmap(points);
|
||||
const position = day => {
|
||||
const cell = calendar.days.find(point => point.day === day);
|
||||
return [cell.week, cell.weekday];
|
||||
};
|
||||
assert.equal(calendar.days.length, 34);
|
||||
assert.equal(calendar.weeks, 6);
|
||||
assert.deepEqual(position('2024-02-29'), [0,3]);
|
||||
assert.deepEqual(position('2024-03-01'), [0,4]);
|
||||
assert.deepEqual(position('2024-03-31'), [4,6]);
|
||||
assert.deepEqual(position('2024-04-01'), [5,0]);
|
||||
assert.equal(calendar.weekdays.length, 7);
|
||||
assert.ok(calendar.months.some(month => month.week === 5 && month.label.startsWith('Apr')));
|
||||
assert.ok(calendar.days.every(cell => cell.level === 0));
|
||||
});
|
||||
|
||||
test('calendar intensity follows play count even when longer sessions have fewer plays', () => {
|
||||
const listening = setup(() => assert.fail('Unexpected request'));
|
||||
const rows = [
|
||||
{day:'2024-04-01', plays:1, listened_seconds:10000},
|
||||
{day:'2024-04-02', plays:2, listened_seconds:9000},
|
||||
{day:'2024-04-03', plays:4, listened_seconds:4000},
|
||||
{day:'2024-04-04', plays:8, listened_seconds:1500},
|
||||
{day:'2024-04-05', plays:16, listened_seconds:600},
|
||||
{day:'2024-04-06', plays:1, listened_seconds:10},
|
||||
];
|
||||
const points = listening.dailyPoints(rows, {from:'2024-04-01',to:'2024-04-07'});
|
||||
const {days} = listening.buildHeatmap(points);
|
||||
const levels = Array.from(days, cell => cell.level);
|
||||
assert.ok(levels.every(level => Number.isInteger(level) && level >= 0 && level <= 4));
|
||||
assert.deepEqual(levels.slice(0,5), levels.slice(0,5).sort((a,b) => a-b));
|
||||
assert.ok(levels[0] > 0 && levels[0] < levels[4]);
|
||||
assert.equal(levels[4], 4);
|
||||
assert.equal(levels[0], levels[5]);
|
||||
assert.equal(levels[6], 0);
|
||||
});
|
||||
|
||||
test('an invalid or inaccessible chart preference falls back to bars and can still be switched', () => {
|
||||
const noRequest = () => assert.fail('Changing the chart should not request new listening data');
|
||||
for (const storage of [
|
||||
{getItem() { return 'unknown'; }, setItem() {}},
|
||||
{getItem() { throw new Error('Storage blocked'); }, setItem() { throw new Error('Storage blocked'); }},
|
||||
]) {
|
||||
const listening = setup(noRequest, {}, storage);
|
||||
assert.equal(listening.chartMode, 'bars');
|
||||
listening.setChartMode('heatmap');
|
||||
assert.equal(listening.chartMode, 'heatmap');
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user