|
1 | 1 | <script lang="ts"> |
2 | | - import type { ZonedDateTime } from '@internationalized/date'; |
3 | | - import DateTimePicker from '$lib/components/datetime-picker/date-time-picker.svelte'; |
4 | 2 | import * as Select from '$lib/components/ui/select'; |
5 | 3 | import { GetValResColor } from '$lib/types/ValidationResult'; |
6 | | - import { durationBetween, formatElapsed, instantFromDate } from '$lib/utils'; |
| 4 | + import { durationBetween, formatElapsed } from '$lib/utils'; |
7 | 5 |
|
8 | 6 | interface Props { |
9 | 7 | option?: string; |
10 | | - customDate?: ZonedDateTime | undefined; |
11 | 8 | instant?: Temporal.Instant | null; |
12 | 9 | } |
13 | 10 |
|
14 | | - let { |
15 | | - option = $bindable('never'), |
16 | | - customDate = $bindable<ZonedDateTime | undefined>(undefined), |
17 | | - instant = $bindable<Temporal.Instant | null>(null), |
18 | | - }: Props = $props(); |
| 11 | + let { option = $bindable('never'), instant = $bindable<Temporal.Instant | null>(null) }: Props = |
| 12 | + $props(); |
19 | 13 |
|
20 | 14 | const inDays = (days: number) => () => Temporal.Now.instant().add({ hours: days * 24 }); |
21 | 15 |
|
|
25 | 19 | getInstant: () => Temporal.Instant | null; |
26 | 20 | }[] = [ |
27 | 21 | { value: 'never', label: 'Never', getInstant: () => null }, |
28 | | - { |
29 | | - value: 'custom', |
30 | | - label: 'Custom', |
31 | | - getInstant: () => (customDate !== undefined ? instantFromDate(customDate.toDate()) : null), |
32 | | - }, |
| 22 | + { value: 'custom', label: 'Custom', getInstant: () => customInstant }, |
33 | 23 | { value: '1days', label: '1 Day', getInstant: inDays(1) }, |
34 | 24 | { value: '7days', label: '7 Days', getInstant: inDays(7) }, |
35 | 25 | { value: '30days', label: '30 Days', getInstant: inDays(30) }, |
|
38 | 28 | { value: '365days', label: '365 Days', getInstant: inDays(365) }, |
39 | 29 | ]; |
40 | 30 |
|
| 31 | + let customValue = $state(''); |
| 32 | + let customInstant = $derived.by<Temporal.Instant | null>(() => { |
| 33 | + if (!customValue) return null; |
| 34 | + try { |
| 35 | + return Temporal.PlainDateTime.from(customValue) |
| 36 | + .toZonedDateTime(Temporal.Now.timeZoneId()) |
| 37 | + .toInstant(); |
| 38 | + } catch { |
| 39 | + return null; |
| 40 | + } |
| 41 | + }); |
| 42 | +
|
41 | 43 | let selectedExpiration = $derived(expirationOptions.find((o) => o.value === option)); |
42 | | - let _instant = $derived(selectedExpiration?.getInstant() ?? null); |
43 | 44 | $effect(() => { |
44 | | - instant = _instant; |
| 45 | + instant = selectedExpiration?.getInstant() ?? null; |
45 | 46 | }); |
46 | 47 |
|
47 | 48 | let validationResult = $derived( |
48 | | - option === 'custom' && !customDate |
| 49 | + option === 'custom' && !customInstant |
49 | 50 | ? { valid: false, message: 'Expire date is required' } |
50 | 51 | : { valid: true } |
51 | 52 | ); |
|
67 | 68 | </Select.Root> |
68 | 69 |
|
69 | 70 | {#if option === 'custom'} |
70 | | - <div class="my-2 w-1/2"> |
71 | | - <DateTimePicker bind:date={customDate} /> |
| 71 | + <div class="my-2"> |
| 72 | + <input |
| 73 | + type="datetime-local" |
| 74 | + bind:value={customValue} |
| 75 | + class="rounded border px-2 py-1 text-sm" |
| 76 | + /> |
72 | 77 | </div> |
73 | 78 | {/if} |
74 | 79 | {#if 'message' in validationResult} |
|
0 commit comments