summaryrefslogtreecommitdiffstats
path: root/apps/files_reminders/src/shared/utils.ts
blob: 605b429a378a3876bc2565f0e2375c67892c5bc8 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
 * @copyright 2023 Christopher Ng <chrng8@gmail.com>
 *
 * @author Christopher Ng <chrng8@gmail.com>
 *
 * @license AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import moment from '@nextcloud/moment'
import { getCanonicalLocale } from '@nextcloud/l10n'

export enum DateTimePreset {
	LaterToday,
	Tomorrow,
	ThisWeekend,
	NextWeek,
}

export const getDateTime = (dateTime: DateTimePreset): null | Date => {
	const matchPreset: Record<DateTimePreset, () => null | Date> = {
		[DateTimePreset.LaterToday]: () => {
			const now = moment()
			const evening = moment()
				.startOf('day')
				.add(18, 'hour')
			const cutoff = evening
				.clone()
				.subtract(1, 'hour')
			if (now.isSameOrAfter(cutoff)) {
				return null
			}
			return evening.toDate()
		},

		[DateTimePreset.Tomorrow]: () => {
			const day = moment()
				.add(1, 'day')
				.startOf('day')
				.add(8, 'hour')
			return day.toDate()
		},

		[DateTimePreset.ThisWeekend]: () => {
			const today = moment()
			if (
				[
					5, // Friday
					6, // Saturday
					7, // Sunday
				].includes(today.isoWeekday())
			) {
				return null
			}
			const saturday = moment()
				.startOf('isoWeek')
				.add(5, 'day')
				.add(8, 'hour')
			return saturday.toDate()
		},

		[DateTimePreset.NextWeek]: () => {
			const today = moment()
			if (today.isoWeekday() === 7) { // Sunday
				return null
			}
			const workday = moment()
				.startOf('isoWeek')
				.add(1, 'week')
				.add(8, 'hour')
			return workday.toDate()
		},
	}

	return matchPreset[dateTime]()
}

export const getInitialCustomDueDate = (): Date => {
	const hour = moment().get('hour')
	const dueDate = moment()
		.startOf('day')
		.add(hour + 2, 'hour')
	return dueDate.toDate()
}

export const getDateString = (dueDate: Date): string => {
	let formatOptions: Intl.DateTimeFormatOptions = {
		hour: 'numeric',
		minute: '2-digit',
	}

	const dueDateMoment = moment(dueDate)
	const today = moment()

	if (!dueDateMoment.isSame(today, 'date')) {
		formatOptions = {
			...formatOptions,
			weekday: 'short',
		}
	}

	if (!dueDateMoment.isSame(today, 'week')) {
		formatOptions = {
			...formatOptions,
			month: 'short',
			day: 'numeric',
		}
	}

	return dueDate.toLocaleString(
		getCanonicalLocale(),
		formatOptions,
	)
}

export const getVerboseDateString = (dueDate: Date): string => {
	const formatOptions: Intl.DateTimeFormatOptions = {
		weekday: 'long',
		hour: 'numeric',
		minute: '2-digit',
		month: 'long',
		day: 'numeric',
	}

	return dueDate.toLocaleString(
		getCanonicalLocale(),
		formatOptions,
	)
}