flotilla/src/routes/spaces/[relay]/goals/+page.svelte

98 lines
2.8 KiB
Svelte
Raw Normal View History

2025-07-07 22:28:36 +00:00
<script lang="ts">
import {onMount} from "svelte"
2025-10-06 18:23:19 +00:00
import {readable} from "svelte/store"
import type {Readable} from "svelte/store"
2025-07-07 22:28:36 +00:00
import {page} from "$app/stores"
2025-10-06 18:23:19 +00:00
import {sortBy, partition, spec, pushToMapKey, max} from "@welshman/lib"
2025-07-07 22:28:36 +00:00
import type {TrustedEvent} from "@welshman/util"
2025-10-06 18:23:19 +00:00
import {ZAP_GOAL, getTagValue} from "@welshman/util"
2025-07-07 22:28:36 +00:00
import {fly} from "@lib/transition"
import StarFallMinimalistic from "@assets/icons/star-fall-minimalistic.svg?dataurl"
import Add from "@assets/icons/add.svg?dataurl"
2025-07-07 22:28:36 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
import PageContent from "@lib/components/PageContent.svelte"
import Spinner from "@lib/components/Spinner.svelte"
import SpaceBar from "@app/components/SpaceBar.svelte"
2025-07-07 22:28:36 +00:00
import GoalItem from "@app/components/GoalItem.svelte"
import GoalCreate from "@app/components/GoalCreate.svelte"
import {decodeRelay} from "@app/relays"
import {makeCommentFilter} from "@app/content"
import {makeFeed} from "@app/feeds"
2026-06-08 21:14:40 +00:00
import {pushModal} from "@app/modal"
2025-07-07 22:28:36 +00:00
2025-10-13 20:35:02 +00:00
const url = decodeRelay($page.params.relay!)
2025-07-07 22:28:36 +00:00
let loading = $state(true)
let element: HTMLElement | undefined = $state()
2025-10-06 18:23:19 +00:00
let events: Readable<TrustedEvent[]> = $state(readable([]))
2025-07-07 22:28:36 +00:00
const createGoal = () => pushModal(GoalCreate, {url})
2025-10-06 18:23:19 +00:00
const items = $derived.by(() => {
const scores = new Map<string, number[]>()
const [goals, comments] = partition(spec({kind: ZAP_GOAL}), $events)
2025-07-07 22:28:36 +00:00
for (const comment of comments) {
2025-10-06 18:23:19 +00:00
const id = getTagValue("E", comment.tags)
2025-07-07 22:28:36 +00:00
if (id) {
2025-10-06 18:23:19 +00:00
pushToMapKey(scores, id, comment.created_at)
2025-07-07 22:28:36 +00:00
}
}
2025-10-06 18:23:19 +00:00
return sortBy(e => -max([...(scores.get(e.id) || []), e.created_at]), goals)
2025-07-07 22:28:36 +00:00
})
onMount(() => {
2025-10-06 18:23:19 +00:00
const feed = makeFeed({
url,
2025-07-07 22:28:36 +00:00
element: element!,
2025-10-06 18:23:19 +00:00
filters: [{kinds: [ZAP_GOAL]}, makeCommentFilter([ZAP_GOAL])],
2026-02-16 21:31:43 +00:00
onBackwardExhausted: () => {
2025-07-07 22:28:36 +00:00
loading = false
},
})
2025-10-06 18:23:19 +00:00
events = feed.events
2025-07-07 22:28:36 +00:00
return () => {
2025-10-06 18:23:19 +00:00
feed.cleanup()
2025-07-07 22:28:36 +00:00
}
})
</script>
<SpaceBar>
{#snippet leading()}
<Icon icon={StarFallMinimalistic} />
{/snippet}
{#snippet title()}
2025-07-17 22:40:07 +00:00
<strong>Goals</strong>
2025-07-07 22:28:36 +00:00
{/snippet}
{#snippet action()}
<Button class="btn btn-primary btn-sm" onclick={createGoal}>
<Icon icon={Add} />
Create
</Button>
2025-07-07 22:28:36 +00:00
{/snippet}
</SpaceBar>
2025-07-07 22:28:36 +00:00
2026-06-04 21:31:07 +00:00
<PageContent bind:element class="flex flex-col gap-2 p-2">
2025-10-06 18:23:19 +00:00
{#each items as event (event.id)}
2025-07-07 22:28:36 +00:00
<div in:fly>
<GoalItem {url} event={$state.snapshot(event)} />
</div>
{/each}
<p class="flex h-10 items-center justify-center py-20">
<Spinner {loading}>
{#if loading}
Looking for goals...
2025-10-06 18:23:19 +00:00
{:else if items.length === 0}
2025-07-07 22:28:36 +00:00
No goals found.
{:else}
That's all!
{/if}
</Spinner>
</p>
</PageContent>