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

104 lines
3 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"
2025-09-10 22:26:39 +00:00
import NotesMinimalistic from "@assets/icons/notes-minimalistic.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 PageBar from "@lib/components/PageBar.svelte"
import PageContent from "@lib/components/PageContent.svelte"
import Spinner from "@lib/components/Spinner.svelte"
import MenuSpaceButton from "@app/components/MenuSpaceButton.svelte"
import GoalItem from "@app/components/GoalItem.svelte"
import GoalCreate from "@app/components/GoalCreate.svelte"
2025-10-06 18:23:19 +00:00
import {decodeRelay, makeCommentFilter} from "@app/core/state"
import {setChecked} from "@app/util/notifications"
import {makeFeed} from "@app/core/requests"
import {pushModal} from "@app/util/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])],
2025-07-07 22:28:36 +00:00
onExhausted: () => {
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
setChecked($page.url.pathname)
}
})
</script>
<PageBar>
{#snippet icon()}
<div class="center">
2025-09-10 22:26:39 +00:00
<Icon icon={NotesMinimalistic} />
2025-07-07 22:28:36 +00:00
</div>
{/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()}
<div class="row-2">
<Button class="btn btn-primary btn-sm" onclick={createGoal}>
2025-09-10 22:26:39 +00:00
<Icon icon={NotesMinimalistic} />
2025-07-07 22:28:36 +00:00
Create a Goal
</Button>
<MenuSpaceButton {url} />
</div>
{/snippet}
</PageBar>
<PageContent bind:element class="flex flex-col gap-2 p-2 pt-4">
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>