-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdaily-overview.ts
More file actions
121 lines (104 loc) · 4.41 KB
/
Copy pathdaily-overview.ts
File metadata and controls
121 lines (104 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { Translator } from './locales';
import { Entry } from './entry-types';
// Daily overview interface
export interface DailyOverview {
hasEntries: boolean;
hasToday: boolean;
streak: number;
lastMissing: string | null;
}
const normalizeDateKey = (value: string | Date): string => {
const date = value instanceof Date ? value : new Date(value);
if (Number.isNaN(date.getTime())) return '';
const local = new Date(date.getTime() - date.getTimezoneOffset() * 60000);
return local.toISOString().slice(0, 10);
}
const dateFromKey = (key: string): Date => {
const [year, month, day] = key.split('-').map(Number);
return new Date(year, (month || 1) - 1, day || 1);
}
export const computeDailyOverview = (entries: Entry[]): DailyOverview => {
const dates = new Set<string>();
let earliestKey: string | null = null;
for (const entry of entries) {
const key = normalizeDateKey(entry.date);
if (!key) continue;
dates.add(key);
if (!earliestKey || key < earliestKey) {
earliestKey = key;
}
}
const todayKey = normalizeDateKey(new Date());
const hasEntries = dates.size > 0;
const hasToday = dates.has(todayKey);
let streak = 0;
if (hasEntries) {
const cursor = new Date();
if (!hasToday) {
cursor.setDate(cursor.getDate() - 1);
}
while (dates.has(normalizeDateKey(cursor))) {
streak += 1;
cursor.setDate(cursor.getDate() - 1);
}
}
let lastMissing: string | null = null;
if (hasEntries && earliestKey) {
const earliestDate = dateFromKey(earliestKey);
const cursor = new Date();
while (cursor >= earliestDate) {
const key = normalizeDateKey(cursor);
if (!dates.has(key)) {
lastMissing = key;
break;
}
cursor.setDate(cursor.getDate() - 1);
}
}
return { hasEntries, hasToday, streak, lastMissing };
}
export const buildDailyOverview = (container: HTMLElement, overview: DailyOverview, t: Translator): void => {
container.addClass('easy-tracker-card');
container.setAttr('id', 'easy-tracker-daily-overview');
container.createEl('div', { cls: 'easy-tracker-card-title', text: t('overview.title') });
const metrics: Array<{ label: string; value: string; hint?: string; modifier?: string }> = [
{
label: t('overview.statusLabel'),
value: overview.hasToday ? t('overview.statusValue.checkedIn') : t('overview.statusValue.missed'),
hint: overview.hasToday ? t('overview.statusHint.checkedIn') : t('overview.statusHint.missed'),
modifier: overview.hasToday ? 'easy-tracker-daily-overview__value--positive' : 'easy-tracker-daily-overview__value--warning',
},
{
label: t('overview.streakLabel'),
value: t('overview.streakValue', { count: String(overview.hasEntries ? overview.streak : 0) }),
hint: overview.hasEntries && overview.streak > 0 ? t('overview.streakHint.active') : t('overview.streakHint.inactive'),
},
{
label: t('overview.gapLabel'),
value: overview.hasEntries ? overview.lastMissing ?? t('overview.gapValue.none') : t('overview.noData'),
hint: overview.lastMissing ? t('overview.gapHint.present') : t('overview.gapHint.none'),
},
];
const grid = container.createDiv({ cls: 'easy-tracker-daily-overview__grid' });
for (const metric of metrics) {
const card = grid.createDiv({ cls: 'easy-tracker-daily-overview__item' });
card.createEl('div', { cls: 'easy-tracker-daily-overview__label', text: metric.label });
const valueEl = card.createEl('div', { cls: 'easy-tracker-daily-overview__value', text: metric.value });
if (metric.modifier) valueEl.addClass(metric.modifier);
if (metric.hint) {
card.createEl('div', { cls: 'easy-tracker-daily-overview__hint', text: metric.hint });
}
}
};
export const renderDailyOverview = (el: HTMLElement, overview: DailyOverview, t: Translator): void => {
const container = el.createDiv();
buildDailyOverview(container, overview, t);
};
export const updateDailyOverview = (el: HTMLElement, overview: DailyOverview, t: Translator): void => {
const container = el.querySelector('#easy-tracker-daily-overview');
if (container instanceof HTMLElement) {
buildDailyOverview(container, overview, t);
} else {
renderDailyOverview(el, overview, t);
}
};