2024-09-11 22:21:25 +00:00
|
|
|
<script lang="ts">
|
2025-02-06 00:26:22 +00:00
|
|
|
import {onMount} from "svelte"
|
|
|
|
|
import type {Readable} from "svelte/store"
|
|
|
|
|
import {readable} from "svelte/store"
|
2024-09-11 22:21:25 +00:00
|
|
|
import {page} from "$app/stores"
|
2025-02-06 00:26:22 +00:00
|
|
|
import {sortBy, now, last} from "@welshman/lib"
|
2024-09-23 20:58:01 +00:00
|
|
|
import type {TrustedEvent} from "@welshman/util"
|
2025-02-06 00:26:22 +00:00
|
|
|
import {REACTION, DELETE, EVENT_TIME, getTagValue} from "@welshman/util"
|
|
|
|
|
import {formatTimestampAsDate} from "@welshman/app"
|
2024-09-11 22:21:25 +00:00
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
|
|
|
|
import Button from "@lib/components/Button.svelte"
|
|
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
2024-10-09 23:11:23 +00:00
|
|
|
import PageBar from "@lib/components/PageBar.svelte"
|
2024-09-20 18:00:41 +00:00
|
|
|
import Divider from "@lib/components/Divider.svelte"
|
2024-11-15 22:03:54 +00:00
|
|
|
import MenuSpaceButton from "@app/components/MenuSpaceButton.svelte"
|
2024-09-24 17:56:42 +00:00
|
|
|
import EventItem from "@app/components/EventItem.svelte"
|
2024-09-11 22:21:25 +00:00
|
|
|
import EventCreate from "@app/components/EventCreate.svelte"
|
2024-11-15 22:03:54 +00:00
|
|
|
import {pushModal} from "@app/modal"
|
2025-02-06 00:26:22 +00:00
|
|
|
import {GENERAL, getEventsForUrl, decodeRelay} from "@app/state"
|
|
|
|
|
import {makeCalendarFeed} from "@app/requests"
|
2024-11-14 18:53:13 +00:00
|
|
|
import {setChecked} from "@app/notifications"
|
2024-09-11 22:21:25 +00:00
|
|
|
|
2024-10-21 17:30:12 +00:00
|
|
|
const url = decodeRelay($page.params.relay)
|
2024-09-11 22:21:25 +00:00
|
|
|
|
2024-09-20 18:00:41 +00:00
|
|
|
const createEvent = () => pushModal(EventCreate, {url})
|
2024-09-11 22:21:25 +00:00
|
|
|
|
2025-02-05 23:05:58 +00:00
|
|
|
const getEnd = (event: TrustedEvent) => parseInt(getTagValue("end", event.tags) || "")
|
2024-10-18 16:53:35 +00:00
|
|
|
|
2025-02-05 23:05:58 +00:00
|
|
|
const getStart = (event: TrustedEvent) => parseInt(getTagValue("start", event.tags) || "")
|
2024-09-11 22:21:25 +00:00
|
|
|
|
2025-02-06 00:26:22 +00:00
|
|
|
let element: HTMLElement
|
2025-02-04 00:37:14 +00:00
|
|
|
let loading = $state(true)
|
2025-02-06 00:26:22 +00:00
|
|
|
let cleanup: () => void
|
|
|
|
|
let events: Readable<TrustedEvent[]> = $state(readable([]))
|
2024-09-20 18:00:41 +00:00
|
|
|
|
|
|
|
|
type Item = {
|
|
|
|
|
event: TrustedEvent
|
|
|
|
|
dateDisplay?: string
|
2024-09-11 22:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-04 01:21:46 +00:00
|
|
|
const items = $derived(
|
2025-02-06 00:26:22 +00:00
|
|
|
sortBy(e => getStart(e), $events).reduce<Item[]>((r, event) => {
|
|
|
|
|
const end = getEnd(event)
|
|
|
|
|
const start = getStart(event)
|
2024-10-18 16:53:35 +00:00
|
|
|
|
2025-02-06 00:26:22 +00:00
|
|
|
if (isNaN(start) || isNaN(end)) return r
|
2024-10-18 16:53:35 +00:00
|
|
|
|
2025-02-06 00:26:22 +00:00
|
|
|
const prevDateDisplay =
|
|
|
|
|
r.length > 0 ? formatTimestampAsDate(getStart(last(r).event)) : undefined
|
|
|
|
|
const newDateDisplay = formatTimestampAsDate(start)
|
|
|
|
|
const dateDisplay = prevDateDisplay === newDateDisplay ? undefined : newDateDisplay
|
2024-09-11 22:21:25 +00:00
|
|
|
|
2025-02-06 00:26:22 +00:00
|
|
|
return [...r, {event, dateDisplay}]
|
|
|
|
|
}, []),
|
2025-02-04 00:37:14 +00:00
|
|
|
)
|
2024-09-11 22:21:25 +00:00
|
|
|
|
2024-10-11 00:09:01 +00:00
|
|
|
onMount(() => {
|
2025-02-06 00:26:22 +00:00
|
|
|
const feedFilters = [{kinds: [EVENT_TIME], "#h": [GENERAL]}]
|
|
|
|
|
const subscriptionFilters = [
|
|
|
|
|
{kinds: [DELETE, REACTION, EVENT_TIME], "#h": [GENERAL], since: now()},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
;({events, cleanup} = makeCalendarFeed({
|
|
|
|
|
element,
|
|
|
|
|
relays: [url],
|
|
|
|
|
feedFilters,
|
|
|
|
|
subscriptionFilters,
|
|
|
|
|
initialEvents: getEventsForUrl(url, feedFilters),
|
|
|
|
|
onExhausted: () => {
|
|
|
|
|
loading = false
|
|
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
setChecked($page.url.pathname)
|
|
|
|
|
cleanup()
|
|
|
|
|
}
|
2024-10-11 00:09:01 +00:00
|
|
|
})
|
2024-09-11 22:21:25 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<div class="relative flex h-screen flex-col">
|
2024-10-09 23:11:23 +00:00
|
|
|
<PageBar>
|
2025-02-04 00:37:14 +00:00
|
|
|
{#snippet icon()}
|
|
|
|
|
<div class="center">
|
|
|
|
|
<Icon icon="calendar-minimalistic" />
|
|
|
|
|
</div>
|
|
|
|
|
{/snippet}
|
|
|
|
|
{#snippet title()}
|
|
|
|
|
<strong>Calendar</strong>
|
|
|
|
|
{/snippet}
|
|
|
|
|
{#snippet action()}
|
|
|
|
|
<div class="md:hidden">
|
|
|
|
|
<MenuSpaceButton {url} />
|
|
|
|
|
</div>
|
|
|
|
|
{/snippet}
|
2024-10-09 23:11:23 +00:00
|
|
|
</PageBar>
|
2025-02-06 00:26:22 +00:00
|
|
|
<div class="scroll-container flex flex-grow flex-col gap-2 overflow-auto p-2" bind:this={element}>
|
2024-09-23 20:58:01 +00:00
|
|
|
{#each items as { event, dateDisplay }, i (event.id)}
|
2024-09-11 22:21:25 +00:00
|
|
|
{#if dateDisplay}
|
2024-09-20 18:00:41 +00:00
|
|
|
<Divider>{dateDisplay}</Divider>
|
2024-09-11 22:21:25 +00:00
|
|
|
{/if}
|
2025-02-05 21:02:51 +00:00
|
|
|
<EventItem {url} {event} />
|
2024-09-11 22:21:25 +00:00
|
|
|
{/each}
|
|
|
|
|
<p class="flex h-10 items-center justify-center py-20">
|
|
|
|
|
<Spinner {loading}>
|
|
|
|
|
{#if loading}
|
|
|
|
|
Looking for events...
|
2024-09-20 18:00:41 +00:00
|
|
|
{:else if items.length === 0}
|
2024-09-11 22:21:25 +00:00
|
|
|
No events found.
|
|
|
|
|
{/if}
|
|
|
|
|
</Spinner>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2024-09-23 20:58:01 +00:00
|
|
|
<Button
|
2024-10-18 16:53:35 +00:00
|
|
|
class="tooltip tooltip-left fixed bottom-16 right-2 z-feature p-1 md:bottom-4 md:right-4"
|
2024-09-23 20:58:01 +00:00
|
|
|
data-tip="Create an Event"
|
2025-02-04 01:21:46 +00:00
|
|
|
onclick={createEvent}>
|
2024-09-23 20:58:01 +00:00
|
|
|
<div class="btn btn-circle btn-primary flex h-12 w-12 items-center justify-center">
|
2024-09-23 20:10:15 +00:00
|
|
|
<Icon icon="calendar-add" />
|
2024-09-11 22:21:25 +00:00
|
|
|
</div>
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|