summaryrefslogtreecommitdiff
path: root/packages/frontend/src/widgets/WidgetActivity.calendar.vue
blob: c2eab92309e23f01c9d4eb7d0afd48d44c8bc785 (plain)
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
<!--
SPDX-FileCopyrightText: syuilo and misskey-project
SPDX-License-Identifier: AGPL-3.0-only
-->

<template>
<svg viewBox="0 0 21 7">
	<rect
		v-for="record in activity" class="day"
		width="1" height="1"
		:x="record.x" :y="record.date.weekday"
		rx="1" ry="1"
		fill="transparent"
	>
		<title>{{ record.date.year }}/{{ record.date.month + 1 }}/{{ record.date.day }}</title>
	</rect>
	<rect
		v-for="record in activity" class="day"
		:width="record.v" :height="record.v"
		:x="record.x + ((1 - record.v) / 2)" :y="record.date.weekday + ((1 - record.v) / 2)"
		rx="1" ry="1"
		:fill="record.color"
		style="pointer-events: none;"
	/>
	<rect
		class="today"
		width="1" height="1"
		:x="activity[0].x" :y="activity[0].date.weekday"
		rx="1" ry="1"
		fill="none"
		stroke-width="0.1"
		stroke="#f73520"
	/>
</svg>
</template>

<script lang="ts" setup>
import { deepClone } from '@/utility/clone.js';

const props = defineProps<{
	activity: {
		total: number;
		notes: number;
		replies: number;
		renotes: number;
	}[]
}>();

const activity = deepClone(props.activity).map(d => ({
	...d,
	total: d.notes + d.replies + d.renotes,
	x: 0,
	date: {
		year: 0,
		month: 0,
		day: 0,
		weekday: 0,
	},
	v: 0,
	color: '',
}));

const peak = Math.max(...activity.map(d => d.total));

const now = new Date();
const year = now.getFullYear();
const month = now.getMonth();
const day = now.getDate();

let x = 20;
activity.slice().forEach((d, i) => {
	d.x = x;

	const date = new Date(year, month, day - i);
	d.date = {
		year: date.getFullYear(),
		month: date.getMonth(),
		day: date.getDate(),
		weekday: date.getDay(),
	};

	d.v = peak === 0 ? 0 : d.total / (peak / 2);
	if (d.v > 1) d.v = 1;
	const ch = d.date.weekday === 0 || d.date.weekday === 6 ? 275 : 170;
	const cs = d.v * 100;
	const cl = 15 + ((1 - d.v) * 80);
	d.color = `hsl(${ch}, ${cs}%, ${cl}%)`;

	if (d.date.weekday === 0) x--;
});
</script>

<style lang="scss" scoped>
svg {
	display: block;
	padding: 16px;
	width: 100%;
	box-sizing: border-box;

	> rect {
		transform-origin: center;

		&.day {
			&:hover {
				fill: rgba(#000, 0.05);
			}
		}
	}
}
</style>