|
1 | 1 | <script lang="ts">
|
2 | 2 | import { onMount } from 'svelte';
|
3 | 3 | import ActivityBubble from './ActivityBubble.svelte';
|
| 4 | + import dayjs from 'dayjs'; |
| 5 | + import isoWeek from 'dayjs/plugin/isoWeek'; |
| 6 | +
|
| 7 | + dayjs.extend(isoWeek); |
4 | 8 |
|
5 | 9 | let { dates, showWeeks } = $props();
|
6 | 10 | const activeDateSet = $derived(new Set(dates));
|
7 | 11 |
|
8 |
| - const days: Date[] = $state([]); |
9 |
| -
|
10 |
| - const months = [ |
11 |
| - 'Jan', |
12 |
| - 'Feb', |
13 |
| - 'Mar', |
14 |
| - 'Apr', |
15 |
| - 'May', |
16 |
| - 'Jun', |
17 |
| - 'Jul', |
18 |
| - 'Aug', |
19 |
| - 'Sep', |
20 |
| - 'Oct', |
21 |
| - 'Nov', |
22 |
| - 'Dec' |
23 |
| - ]; |
| 12 | + const days: dayjs.Dayjs[] = $state([]); |
24 | 13 |
|
25 |
| - const today = new Date(); |
26 |
| - const startDate = new Date(today); |
27 |
| - startDate.setDate(today.getDate() - today.getDay() - (7 * showWeeks - 1)); |
| 14 | + const today = dayjs(); |
| 15 | + const startDate = today.startOf('isoWeek').subtract(showWeeks, 'weeks'); |
28 | 16 |
|
29 | 17 | onMount(() => {
|
30 |
| - let current = new Date(startDate); |
31 |
| - while (current <= today) { |
32 |
| - days.push(new Date(current)); |
33 |
| - current.setDate(current.getDate() + 1); |
| 18 | + let current = startDate.clone(); |
| 19 | + while (current.isBefore(today.add(1, 'day'), 'day')) { |
| 20 | + days.push(current.clone()); |
| 21 | + current = current.add(1, 'day'); |
34 | 22 | }
|
35 | 23 | });
|
36 | 24 |
|
37 |
| - const formatDate = (date: Date) => date.toISOString().split('T')[0]; |
| 25 | + const formatDate = (date: dayjs.Dayjs) => date.format('YYYY-MM-DD'); |
38 | 26 | </script>
|
39 | 27 |
|
40 | 28 | <div class="grid grid-flow-col grid-rows-8 items-center justify-center gap-1 text-[0.5rem]">
|
|
47 | 35 | <div></div>
|
48 | 36 | <div class="sticky left-0 pr-1">Sun</div>
|
49 | 37 | {#each days as day, i}
|
50 |
| - {#if i % 7 === 0 && day.getDate() <= 7} |
| 38 | + {#if i % 7 === 0 && day.date() <= 7} |
51 | 39 | <div class="-mr-2">
|
52 |
| - {months[days[i + 6]?.getMonth()]} |
| 40 | + {days[i + 6]?.format('MMM')} |
53 | 41 | </div>
|
54 | 42 | {:else if i % 7 === 0}
|
55 | 43 | <div></div>
|
|
0 commit comments