-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathDueDateSelector.tsx
More file actions
367 lines (338 loc) · 10.2 KB
/
DueDateSelector.tsx
File metadata and controls
367 lines (338 loc) · 10.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import type { DueDate as ApiDueDate } from "@/api/domain/dueDate";
import type { Duration as ApiDuration } from "@/api/domain/task";
import { DueDate as DataDueDate } from "@/data/dueDate";
import { t } from "@/i18n";
import { now, timezone } from "@/infra/time";
import { ObsidianIcon } from "@/ui/components/obsidian-icon";
import { Popover } from "@/ui/createTaskModal/Popover";
import {
type CalendarDate,
DateFormatter,
Time,
endOfWeek,
toCalendarDateTime,
toZoned,
today,
} from "@internationalized/date";
import type React from "react";
import { useState } from "react";
import {
Button,
Calendar,
CalendarCell,
CalendarGrid,
DateInput,
DateSegment,
Dialog,
DialogTrigger,
Heading,
type Key,
Label,
ListBox,
ListBoxItem,
Menu,
MenuItem,
Section,
Select,
SelectValue,
TimeField,
} from "react-aria-components";
const weekdayFormatter = new DateFormatter("en-US", {
weekday: "short",
});
export type DueDate = {
date: CalendarDate;
timeInfo:
| {
time: Time;
duration: ApiDuration | undefined;
}
| undefined;
};
type Props = {
selected: DueDate | undefined;
setSelected: (selected: DueDate | undefined) => void;
};
export const DueDateSelector: React.FC<Props> = ({ selected, setSelected }) => {
const label = getLabel(selected);
const suggestions = getSuggestions();
const selectDate = (date: CalendarDate) => {
if (selected === undefined) {
setSelected({ date, timeInfo: undefined });
} else {
setSelected({ date, timeInfo: selected.timeInfo });
}
};
const selectedDateSuggestion = (key: Key) => {
const suggestion = suggestions.find((s) => s.id === key);
if (suggestion === undefined) {
return;
}
if (suggestion.target === undefined) {
setSelected(undefined);
} else {
setSelected({ date: suggestion.target, timeInfo: selected?.timeInfo });
}
};
const setTimeInfo = (timeInfo: DueDate["timeInfo"]) => {
if (selected === undefined) {
if (timeInfo !== undefined) {
setSelected({
date: today(timezone()),
timeInfo,
});
}
} else {
setSelected({
date: selected.date,
timeInfo,
});
}
};
const i18n = t().createTaskModal.dateSelector;
return (
<DialogTrigger>
<Button className="due-date-selector" aria-label={i18n.buttonLabel}>
<ObsidianIcon size="s" id="calendar" />
{label}
</Button>
<Popover maxHeight={600}>
<Dialog className="task-option-dialog task-date-menu" aria-label={i18n.dialogLabel}>
{({ close }) => (
<>
<Menu
onAction={(key: Key) => {
selectedDateSuggestion(key);
close();
}}
aria-label={i18n.suggestionsLabel}
>
<Section>
{suggestions.map((props) => (
<DateSuggestion key={props.id} {...props} />
))}
</Section>
</Menu>
<hr />
<Calendar
aria-label={i18n.datePickerLabel}
className="date-picker"
value={selected?.date ?? null}
onChange={(date) => {
selectDate(date);
close();
}}
minValue={today(timezone())}
>
<header>
<Heading level={4} />
<div className="date-picker-controls">
<Button slot="previous">◀</Button>
<Button slot="next">▶</Button>
</div>
</header>
<CalendarGrid>{(date) => <CalendarCell date={date} />}</CalendarGrid>
</Calendar>
<hr />
<div className="time-picker-container">
<DialogTrigger>
<Button className="time-picker-button" aria-label="Set time">
<ObsidianIcon size="xs" id="clock" />
Time
</Button>
<Popover defaultPlacement="top">
<TimeDialog selectedTimeInfo={selected?.timeInfo} setTimeInfo={setTimeInfo} />
</Popover>
</DialogTrigger>
{selected?.timeInfo !== undefined && (
<Button
className="time-picker-clear-button"
onPress={() => setTimeInfo(undefined)}
aria-label="Clear time"
>
<ObsidianIcon size="xs" id="cross" />
</Button>
)}
</div>
</>
)}
</Dialog>
</Popover>
</DialogTrigger>
);
};
const getLabel = (selected: DueDate | undefined) => {
if (selected === undefined) {
return t().createTaskModal.dateSelector.emptyDate;
}
const apiDueDate: ApiDueDate = {
date: selected.date.toString(),
datetime:
selected.timeInfo !== undefined
? toZoned(
toCalendarDateTime(selected.date, selected.timeInfo.time),
timezone(),
).toAbsoluteString()
: undefined,
isRecurring: false,
};
const dueDate = DataDueDate.parse(apiDueDate, selected?.timeInfo?.duration);
return DataDueDate.format(dueDate);
};
type DateSuggestionProps = {
id: string;
icon: string;
label: string;
target: CalendarDate | undefined;
};
const DateSuggestion: React.FC<DateSuggestionProps> = ({ id, icon, label, target }) => {
const dayOfWeek = target !== undefined ? weekdayFormatter.format(target.toDate(timezone())) : "";
return (
<MenuItem id={id} aria-label={label}>
<div className="date-suggestion-elem">
<div className="date-suggestion-label">
<ObsidianIcon id={icon} size="s" />
{label}
</div>
<div className="date-suggestion-day">{dayOfWeek}</div>
</div>
</MenuItem>
);
};
const getSuggestions = (): DateSuggestionProps[] => {
const i18n = t().createTaskModal.dateSelector;
const startOfNextWeek = endOfWeek(today(timezone()), "en-US").add({ days: 1 });
const suggestions = [
{
id: "today",
icon: "calendar",
label: i18n.today,
target: today(timezone()),
},
{
id: "tomorrow",
icon: "sun",
label: i18n.tomorrow,
target: today(timezone()).add({ days: 1 }),
},
{
id: "next-week",
icon: "calendar-clock",
label: i18n.nextWeek,
target: startOfNextWeek,
},
{
id: "no-date",
icon: "ban",
label: i18n.noDate,
target: undefined,
},
];
return suggestions;
};
type TimeDialogProps = {
selectedTimeInfo: DueDate["timeInfo"] | undefined;
setTimeInfo: (timeInfo: DueDate["timeInfo"] | undefined) => void;
};
// We want enough options to get to 23h 45m.
const MAX_DURATION_SEGMENTS = (24 * 60 - 15) / 15;
const TimeDialog: React.FC<TimeDialogProps> = ({ selectedTimeInfo, setTimeInfo }) => {
const i18n = t().createTaskModal.dateSelector.timeDialog;
const durationOptions = [
undefined,
...Array.from({ length: MAX_DURATION_SEGMENTS }, (_, i) => ({
amount: (i + 1) * 15,
unit: "minute" as const,
})),
].map((option) => ({
label: option === undefined ? i18n.noDuration : i18n.duration(option.amount),
value: option,
}));
const initialDurationIndex = durationOptions.findIndex(
(o) => o.value?.amount === selectedTimeInfo?.duration?.amount,
);
const [selectedDurationIndex, setSelectedDurationIndex] = useState<number>(
initialDurationIndex === -1 ? 0 : initialDurationIndex,
);
const [taskTimeInfo, setTaskTimeInfo] = useState(selectedTimeInfo);
const onDurationChange = (key: Key) => {
const idx = Number(key);
setSelectedDurationIndex(idx);
const option = durationOptions[idx];
if (taskTimeInfo?.time) {
setTaskTimeInfo({
time: taskTimeInfo.time,
duration: option.value,
});
} else {
setTaskTimeInfo({
time: new Time(now().hour, now().minute, 0),
duration: option.value,
});
}
};
return (
<Dialog className="task-option-dialog task-time-menu" aria-label="Time selector">
{({ close }) => (
<>
<TimeField
className="task-time-picker"
value={taskTimeInfo?.time ?? null}
onChange={(time) => {
setTaskTimeInfo({
time,
duration: taskTimeInfo?.duration,
});
}}
>
<Label className="task-time-picker-label">{i18n.timeLabel}</Label>
<DateInput className="task-time-picker-input">
{(segment) => (
<DateSegment className="task-time-picker-input-segment" segment={segment} />
)}
</DateInput>
</TimeField>
<Select
className="task-duration-select"
selectedKey={selectedDurationIndex}
onSelectionChange={onDurationChange}
>
<Label className="task-duration-picker-label">{i18n.durationLabel}</Label>
<Button className="task-duration-button">
<SelectValue />
</Button>
<Popover defaultPlacement="top" maxHeight={150}>
<ListBox
className="task-option-dialog task-duration-menu"
aria-label={i18n.durationLabel}
>
{durationOptions.map((option, index) => (
<ListBoxItem
key={String(index)}
id={index}
className="duration-option"
textValue={option.label}
>
{option.label}
</ListBoxItem>
))}
</ListBox>
</Popover>
</Select>
<div className="task-time-controls">
<Button onPress={close}>{i18n.cancelButtonLabel}</Button>
<Button
className="mod-cta"
onPress={() => {
close();
setTimeInfo(taskTimeInfo);
}}
>
{i18n.saveButtonLabel}
</Button>
</div>
</>
)}
</Dialog>
);
};