Rework datetime input
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {writable} from "svelte/store"
|
import {writable} from "svelte/store"
|
||||||
import {randomId} from "@welshman/lib"
|
import {randomId, HOUR} from "@welshman/lib"
|
||||||
import {createEvent, EVENT_TIME} from "@welshman/util"
|
import {createEvent, EVENT_TIME} from "@welshman/util"
|
||||||
import {publishThunk, dateToSeconds} from "@welshman/app"
|
import {publishThunk} from "@welshman/app"
|
||||||
import {preventDefault} from "@lib/html"
|
import {preventDefault} from "@lib/html"
|
||||||
import {timeHashesBetween} from "@lib/util"
|
import {timeHashesBetween} from "@lib/util"
|
||||||
import Icon from "@lib/components/Icon.svelte"
|
import Icon from "@lib/components/Icon.svelte"
|
||||||
@@ -39,18 +39,15 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const startTs = dateToSeconds(start)
|
|
||||||
const endTs = dateToSeconds(end)
|
|
||||||
|
|
||||||
const event = createEvent(EVENT_TIME, {
|
const event = createEvent(EVENT_TIME, {
|
||||||
content: editor.getText({blockSeparator: "\n"}).trim(),
|
content: editor.getText({blockSeparator: "\n"}).trim(),
|
||||||
tags: [
|
tags: [
|
||||||
["d", randomId()],
|
["d", randomId()],
|
||||||
["title", title],
|
["title", title],
|
||||||
["location", location],
|
["location", location],
|
||||||
["start", startTs.toString()],
|
["start", start.toString()],
|
||||||
["end", endTs.toString()],
|
["end", end.toString()],
|
||||||
...timeHashesBetween(startTs, endTs).map(T => ["T", T]),
|
...timeHashesBetween(start, end).map(T => ["T", T]),
|
||||||
...editor.storage.nostr.getEditorTags(),
|
...editor.storage.nostr.getEditorTags(),
|
||||||
PROTECTED,
|
PROTECTED,
|
||||||
],
|
],
|
||||||
@@ -64,8 +61,8 @@
|
|||||||
|
|
||||||
let title = $state("")
|
let title = $state("")
|
||||||
let location = $state("")
|
let location = $state("")
|
||||||
let start: Date | undefined = $state()
|
let start: number | undefined = $state()
|
||||||
let end: Date | undefined = $state()
|
let end: number | undefined = $state()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<form class="column gap-4" onsubmit={preventDefault(submit)}>
|
<form class="column gap-4" onsubmit={preventDefault(submit)}>
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {fromPairs} from "@welshman/lib"
|
import {fromPairs} from "@welshman/lib"
|
||||||
import {
|
import {formatTimestamp, formatTimestampAsDate, formatTimestampAsTime} from "@welshman/app"
|
||||||
formatTimestamp,
|
|
||||||
formatTimestampAsDate,
|
|
||||||
formatTimestampAsTime,
|
|
||||||
} from "@welshman/app"
|
|
||||||
import {preventDefault} from "@lib/html"
|
import {preventDefault} from "@lib/html"
|
||||||
import Icon from "@lib/components/Icon.svelte"
|
import Icon from "@lib/components/Icon.svelte"
|
||||||
import Button from "@lib/components/Button.svelte"
|
import Button from "@lib/components/Button.svelte"
|
||||||
@@ -24,8 +20,6 @@
|
|||||||
const isSingleDay = $derived(startDateDisplay === endDateDisplay)
|
const isSingleDay = $derived(startDateDisplay === endDateDisplay)
|
||||||
|
|
||||||
const openProfile = () => pushModal(ProfileDetail, {pubkey: event.pubkey})
|
const openProfile = () => pushModal(ProfileDetail, {pubkey: event.pubkey})
|
||||||
|
|
||||||
$inspect(event)
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="card2 bg-alt col-2">
|
<div class="card2 bg-alt col-2">
|
||||||
|
|||||||
@@ -1,58 +1,74 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {DateInput} from "date-picker-svelte"
|
import {DateInput} from "date-picker-svelte"
|
||||||
|
import {secondsToDate, dateToSeconds} from "@welshman/app"
|
||||||
import Icon from "@lib/components/Icon.svelte"
|
import Icon from "@lib/components/Icon.svelte"
|
||||||
import Button from "@lib/components/Button.svelte"
|
import Button from "@lib/components/Button.svelte"
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
value?: Date | undefined
|
value?: number | undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
let {value = $bindable()}: Props = $props()
|
let {value = $bindable()}: Props = $props()
|
||||||
|
|
||||||
const pad = (n: number) => ("00" + String(n)).slice(-2)
|
const pad = (n: number) => ("00" + String(n)).slice(-2)
|
||||||
|
|
||||||
const defaultTime = `${pad(new Date().getHours())}:00`
|
const getTime = (d: Date, inheritMinutes: boolean) => {
|
||||||
|
const minutes = inheritMinutes ? pad(d.getMinutes()) : "00"
|
||||||
|
|
||||||
|
return `${pad(d.getHours())}:${minutes}`
|
||||||
|
}
|
||||||
|
|
||||||
|
const setTime = (d: Date, time: string) => {
|
||||||
|
const [hours, minutes] = time.split(":").map(x => parseInt(x))
|
||||||
|
const newDate = new Date(d)
|
||||||
|
|
||||||
|
newDate.setHours(hours, minutes, 0, 0)
|
||||||
|
|
||||||
|
return newDate
|
||||||
|
}
|
||||||
|
|
||||||
|
const onTimeChange = () => {
|
||||||
|
if (time) {
|
||||||
|
date = setTime(date || new Date(), time)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const focusDate = () => element.querySelector("input")?.focus()
|
const focusDate = () => element.querySelector("input")?.focus()
|
||||||
|
|
||||||
const syncTime = (d: Date) => {
|
|
||||||
if (!time) {
|
|
||||||
time = defaultTime
|
|
||||||
}
|
|
||||||
|
|
||||||
const [hours, minutes] = time.split(":").map(x => parseInt(x))
|
|
||||||
|
|
||||||
d.setHours(hours, minutes, 0, 0)
|
|
||||||
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
|
|
||||||
const onchange = () => {
|
|
||||||
if (value) {
|
|
||||||
value = syncTime(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const clear = () => {
|
const clear = () => {
|
||||||
value = undefined
|
date = undefined
|
||||||
time = ""
|
time = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
let time = $state("")
|
let date: Date | undefined = $state()
|
||||||
|
let time: string | undefined = $state()
|
||||||
let element: HTMLElement
|
let element: HTMLElement
|
||||||
|
|
||||||
|
// Sync date to time and value
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (value) {
|
if (date) {
|
||||||
value = syncTime(value)
|
time = getTime(date, false)
|
||||||
|
value = dateToSeconds(date)
|
||||||
|
} else {
|
||||||
|
value = undefined
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Sync updates to value to date/time
|
||||||
|
$effect(() => {
|
||||||
|
const derivedDate = value ? secondsToDate(value) : undefined
|
||||||
|
const derivedTime = derivedDate ? getTime(derivedDate, true) : undefined
|
||||||
|
|
||||||
|
date = derivedDate
|
||||||
|
time = derivedTime
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="relative grid grid-cols-2 gap-2" bind:this={element}>
|
<div class="relative grid grid-cols-2 gap-2" bind:this={element}>
|
||||||
<div class="relative">
|
<div class="relative">
|
||||||
<DateInput format="yyyy-MM-dd" placeholder="" bind:value on:change={onchange} />
|
<DateInput format="yyyy-MM-dd" placeholder="" bind:value={date} />
|
||||||
<div class="absolute right-2 top-0 flex h-12 cursor-pointer items-center gap-2">
|
<div class="absolute right-2 top-0 flex h-12 cursor-pointer items-center gap-2">
|
||||||
{#if value}
|
{#if date}
|
||||||
<Button onclick={clear} class="h-5">
|
<Button onclick={clear} class="h-5">
|
||||||
<Icon icon="close-circle" />
|
<Icon icon="close-circle" />
|
||||||
</Button>
|
</Button>
|
||||||
@@ -64,6 +80,12 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<label class="input input-bordered flex items-center">
|
<label class="input input-bordered flex items-center">
|
||||||
<input list="time-options" class="grow" type="time" step="300" bind:value={time} {onchange} />
|
<input
|
||||||
|
list="time-options"
|
||||||
|
class="grow"
|
||||||
|
type="time"
|
||||||
|
step="300"
|
||||||
|
bind:value={time}
|
||||||
|
onchange={onTimeChange} />
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -8,8 +8,6 @@
|
|||||||
let index = $state(0)
|
let index = $state(0)
|
||||||
let items: string[] = $state([])
|
let items: string[] = $state([])
|
||||||
|
|
||||||
$inspect(items)
|
|
||||||
|
|
||||||
const populateItems = throttle(300, term => {
|
const populateItems = throttle(300, term => {
|
||||||
items = search(term).slice(0, 5)
|
items = search(term).slice(0, 5)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ export type ScrollerOpts = {
|
|||||||
onScroll: () => any
|
onScroll: () => any
|
||||||
element: Element
|
element: Element
|
||||||
threshold?: number
|
threshold?: number
|
||||||
reverse?: boolean
|
|
||||||
delay?: number
|
delay?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +34,6 @@ export const createScroller = ({
|
|||||||
element,
|
element,
|
||||||
delay = 1000,
|
delay = 1000,
|
||||||
threshold = 2000,
|
threshold = 2000,
|
||||||
reverse = false,
|
|
||||||
}: ScrollerOpts) => {
|
}: ScrollerOpts) => {
|
||||||
let done = false
|
let done = false
|
||||||
|
|
||||||
|
|||||||
+11
-5
@@ -1,6 +1,6 @@
|
|||||||
import {hexToBytes, bytesToHex} from "@noble/hashes/utils"
|
import {hexToBytes, bytesToHex} from "@noble/hashes/utils"
|
||||||
import * as nip19 from "nostr-tools/nip19"
|
import * as nip19 from "nostr-tools/nip19"
|
||||||
import {HOUR} from "@welshman/lib"
|
import {HOUR, MONTH, DAY} from "@welshman/lib"
|
||||||
|
|
||||||
export const displayList = <T>(xs: T[], conj = "and", n = 6, locale = "en-US") => {
|
export const displayList = <T>(xs: T[], conj = "and", n = 6, locale = "en-US") => {
|
||||||
const stringItems = xs.map(String)
|
const stringItems = xs.map(String)
|
||||||
@@ -44,10 +44,7 @@ export const timeHash = (seconds: number, precision = 32) => {
|
|||||||
return hash
|
return hash
|
||||||
}
|
}
|
||||||
|
|
||||||
export const timeHashesBetween = (a: number, b: number) => {
|
export const timeHashesBetween = (start: number, end: number, precisions = [10, 15, 20]) => {
|
||||||
const precisions = [10, 15, 20]
|
|
||||||
const start = Math.min(a, b) >>> 0
|
|
||||||
const end = Math.min(Math.max(a, b) >>> 0, 0xffffffff)
|
|
||||||
const hashes = new Set<string>()
|
const hashes = new Set<string>()
|
||||||
|
|
||||||
for (let seconds = start; seconds <= end; seconds += HOUR) {
|
for (let seconds = start; seconds <= end; seconds += HOUR) {
|
||||||
@@ -58,3 +55,12 @@ export const timeHashesBetween = (a: number, b: number) => {
|
|||||||
|
|
||||||
return Array.from(hashes).sort()
|
return Array.from(hashes).sort()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const timeHashesForFilter = (start: number, end: number) => {
|
||||||
|
const diff = end - start
|
||||||
|
|
||||||
|
if (diff < 3 * DAY) return timeHashesBetween(start, end, [20])
|
||||||
|
if (diff < 3 * MONTH) return timeHashesBetween(start, end, [15])
|
||||||
|
|
||||||
|
return timeHashesBetween(start, end, [10])
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {onMount, onDestroy} from "svelte"
|
import {onMount, onDestroy} from "svelte"
|
||||||
import {page} from "$app/stores"
|
import {page} from "$app/stores"
|
||||||
import {sortBy, last, ago} from "@welshman/lib"
|
import {sortBy, now, int, MONTH, last} from "@welshman/lib"
|
||||||
import type {TrustedEvent} from "@welshman/util"
|
import type {TrustedEvent} from "@welshman/util"
|
||||||
import {EVENT_DATE, EVENT_TIME} from "@welshman/util"
|
import {EVENT_DATE, EVENT_TIME, getTagValue} from "@welshman/util"
|
||||||
import {subscribe, formatTimestampAsDate} from "@welshman/app"
|
import {subscribe, formatTimestampAsDate} from "@welshman/app"
|
||||||
|
import {timeHashesForFilter} from "@lib/util"
|
||||||
import Icon from "@lib/components/Icon.svelte"
|
import Icon from "@lib/components/Icon.svelte"
|
||||||
import Button from "@lib/components/Button.svelte"
|
import Button from "@lib/components/Button.svelte"
|
||||||
import Spinner from "@lib/components/Spinner.svelte"
|
import Spinner from "@lib/components/Spinner.svelte"
|
||||||
@@ -24,10 +25,9 @@
|
|||||||
|
|
||||||
const createEvent = () => pushModal(EventCreate, {url})
|
const createEvent = () => pushModal(EventCreate, {url})
|
||||||
|
|
||||||
const getEnd = (event: TrustedEvent) => parseInt(event.tags.find(t => t[0] === "end")?.[1] || "")
|
const getEnd = (event: TrustedEvent) => parseInt(getTagValue("end", event.tags) || "")
|
||||||
|
|
||||||
const getStart = (event: TrustedEvent) =>
|
const getStart = (event: TrustedEvent) => parseInt(getTagValue("start", event.tags) || "")
|
||||||
parseInt(event.tags.find(t => t[0] === "start")?.[1] || "")
|
|
||||||
|
|
||||||
const limit = 5
|
const limit = 5
|
||||||
let loading = $state(true)
|
let loading = $state(true)
|
||||||
@@ -38,7 +38,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const items = $derived(
|
const items = $derived(
|
||||||
sortBy(e => -getStart(e), $events)
|
sortBy(e => getStart(e), $events)
|
||||||
.reduce<Item[]>((r, event) => {
|
.reduce<Item[]>((r, event) => {
|
||||||
const end = getEnd(event)
|
const end = getEnd(event)
|
||||||
const start = getStart(event)
|
const start = getStart(event)
|
||||||
@@ -56,9 +56,10 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
const sub = subscribe({filters: [{kinds, since: ago(30)}]})
|
const sub = subscribe({filters: [{kinds, since: now()}]})
|
||||||
|
const hashes = timeHashesForFilter(now() - int(3, MONTH), now() + int(3, MONTH))
|
||||||
|
|
||||||
pullConservatively({filters: [{kinds}], relays: [url]})
|
pullConservatively({filters: [{kinds, "#T": hashes}], relays: [url]})
|
||||||
|
|
||||||
return () => sub.close()
|
return () => sub.close()
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user