flotilla/src/routes/spaces/[nrelay]/calendar/+page.svelte

79 lines
2.5 KiB
Svelte
Raw Normal View History

2024-09-11 22:21:25 +00:00
<script lang="ts">
import {page} from "$app/stores"
2024-09-23 20:58:01 +00:00
import {sortBy, last} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util"
2024-09-20 18:00:41 +00:00
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-09-20 18:00:41 +00:00
import Divider from "@lib/components/Divider.svelte"
import EventItem from "@app/components/EventItem.svelte"
2024-09-11 22:21:25 +00:00
import EventCreate from "@app/components/EventCreate.svelte"
import {pushModal} from "@app/modal"
import {eventsByUrl, decodeNRelay} from "@app/state"
const url = decodeNRelay($page.params.nrelay)
2024-09-20 18:00:41 +00:00
const createEvent = () => pushModal(EventCreate, {url})
2024-09-11 22:21:25 +00:00
2024-09-20 18:00:41 +00:00
const getStart = (event: TrustedEvent) =>
2024-09-23 20:58:01 +00:00
parseInt(event.tags.find(t => t[0] === "start")?.[1] || "0")
2024-09-11 22:21:25 +00:00
2024-09-20 18:00:41 +00:00
let loading = true
type Item = {
event: TrustedEvent
dateDisplay?: string
2024-09-11 22:21:25 +00:00
}
2024-09-23 20:58:01 +00:00
$: items = sortBy(getStart, $eventsByUrl.get(url) || []).reduce<Item[]>((r, event) => {
const prevDateDisplay =
r.length > 0 ? formatTimestampAsDate(getStart(last(r).event)) : undefined
const newDateDisplay = formatTimestampAsDate(getStart(event))
const dateDisplay = prevDateDisplay === newDateDisplay ? undefined : newDateDisplay
2024-09-11 22:21:25 +00:00
2024-09-23 20:58:01 +00:00
return [...r, {event, dateDisplay}]
}, [])
2024-09-11 22:21:25 +00:00
setTimeout(() => {
loading = false
}, 3000)
</script>
<div class="relative flex h-screen flex-col">
<div class="relative z-feature mx-2 rounded-xl pt-4">
<div
class="flex min-h-12 items-center justify-between gap-4 rounded-xl bg-base-100 px-4 shadow-xl">
<div class="flex items-center gap-2">
<Icon icon="calendar-minimalistic" />
<strong>Calendar</strong>
</div>
</div>
</div>
2024-09-23 20:58:01 +00:00
<div class="flex flex-grow flex-col gap-2 overflow-auto p-2">
{#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}
<EventItem {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
class="tooltip tooltip-left fixed bottom-4 right-4 p-1"
data-tip="Create an Event"
on:click={createEvent}>
<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>