Skip to content

Commit 67c2f4c

Browse files
committed
refactor(ExpirationPicker): remove ZonedDateTime dep, use native Temporal and datetime-local input
1 parent d428d0c commit 67c2f4c

3 files changed

Lines changed: 27 additions & 34 deletions

File tree

src/lib/components/ExpirationPicker.svelte

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
<script lang="ts">
2-
import type { ZonedDateTime } from '@internationalized/date';
3-
import DateTimePicker from '$lib/components/datetime-picker/date-time-picker.svelte';
42
import * as Select from '$lib/components/ui/select';
53
import { GetValResColor } from '$lib/types/ValidationResult';
6-
import { durationBetween, formatElapsed, instantFromDate } from '$lib/utils';
4+
import { durationBetween, formatElapsed } from '$lib/utils';
75
86
interface Props {
97
option?: string;
10-
customDate?: ZonedDateTime | undefined;
118
instant?: Temporal.Instant | null;
129
}
1310
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();
1913
2014
const inDays = (days: number) => () => Temporal.Now.instant().add({ hours: days * 24 });
2115
@@ -25,11 +19,7 @@
2519
getInstant: () => Temporal.Instant | null;
2620
}[] = [
2721
{ 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 },
3323
{ value: '1days', label: '1 Day', getInstant: inDays(1) },
3424
{ value: '7days', label: '7 Days', getInstant: inDays(7) },
3525
{ value: '30days', label: '30 Days', getInstant: inDays(30) },
@@ -38,14 +28,25 @@
3828
{ value: '365days', label: '365 Days', getInstant: inDays(365) },
3929
];
4030
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+
4143
let selectedExpiration = $derived(expirationOptions.find((o) => o.value === option));
42-
let _instant = $derived(selectedExpiration?.getInstant() ?? null);
4344
$effect(() => {
44-
instant = _instant;
45+
instant = selectedExpiration?.getInstant() ?? null;
4546
});
4647
4748
let validationResult = $derived(
48-
option === 'custom' && !customDate
49+
option === 'custom' && !customInstant
4950
? { valid: false, message: 'Expire date is required' }
5051
: { valid: true }
5152
);
@@ -67,8 +68,12 @@
6768
</Select.Root>
6869

6970
{#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+
/>
7277
</div>
7378
{/if}
7479
{#if 'message' in validationResult}

src/routes/(app)/settings/api-tokens/dialog-token-create.svelte

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<script lang="ts" module>
22
import { PermissionType, tokensCreateToken } from '$lib/api';
33
import type { TokenCreatedResponse } from '$lib/api';
4-
import type { ZonedDateTime } from '@internationalized/date';
54
import type { ValidationResult } from '$lib/types/ValidationResult';
65
76
type PermissionCategory = {
@@ -56,7 +55,6 @@
5655
5756
let name = $state<string>('');
5857
let expire = $state('never');
59-
let expireCustom = $state<ZonedDateTime | undefined>(undefined);
6058
let expireInstant = $state<Temporal.Instant | null>(null);
6159
let permissions = $state<PermissionType[]>([PermissionType.ShockersUse]);
6260
@@ -65,7 +63,7 @@
6563
if (!o) {
6664
name = '';
6765
expire = 'never';
68-
expireCustom = undefined;
66+
6967
permissions = [PermissionType.ShockersUse];
7068
}
7169
open = o;
@@ -103,11 +101,7 @@
103101
validationResult={nameValidationResult}
104102
/>
105103

106-
<ExpirationPicker
107-
bind:option={expire}
108-
bind:customDate={expireCustom}
109-
bind:instant={expireInstant}
110-
/>
104+
<ExpirationPicker bind:option={expire} bind:instant={expireInstant} />
111105

112106
<div class="mt-4">
113107
<h2>Permissions</h2>

src/routes/(app)/shares/public/dialog-publicshare-create.svelte

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script lang="ts">
2-
import type { ZonedDateTime } from '@internationalized/date';
32
import { shareLinksCreatePublicShare } from '$lib/api';
43
import ExpirationPicker from '$lib/components/ExpirationPicker.svelte';
54
import TextInput from '$lib/components/input/TextInput.svelte';
@@ -16,7 +15,6 @@
1615
1716
let name = $state('');
1817
let expireOption = $state('never');
19-
let customExpire = $state<ZonedDateTime | undefined>();
2018
let expireInstant = $state<Temporal.Instant | null>(null);
2119
2220
function createShareLink() {
@@ -39,11 +37,7 @@
3937
<Dialog.Title>Create Public Share</Dialog.Title>
4038
</Dialog.Header>
4139
<TextInput label="Name" bind:value={name} />
42-
<ExpirationPicker
43-
bind:option={expireOption}
44-
bind:customDate={customExpire}
45-
bind:instant={expireInstant}
46-
/>
40+
<ExpirationPicker bind:option={expireOption} bind:instant={expireInstant} />
4741

4842
<Button onclick={createShareLink}>Create</Button>
4943
</Dialog.Content>

0 commit comments

Comments
 (0)