flotilla/src/app/components/PinboardEdit.svelte

107 lines
3.2 KiB
Svelte
Raw Normal View History

2026-06-23 23:30:54 +00:00
<script lang="ts">
import {navigate} from "@app/modal"
import {getAddress} from "@welshman/util"
import {publish} from "@welshman/app"
2026-07-28 16:16:26 +00:00
import {Pinboard} from "@welshman/domain"
import type {PinboardReader} from "@welshman/domain"
2026-06-23 23:30:54 +00:00
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte"
import Field from "@lib/components/Field.svelte"
import Icon from "@lib/components/Icon.svelte"
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalTitle from "@lib/components/ModalTitle.svelte"
import ModalSubtitle from "@lib/components/ModalSubtitle.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
import RelayName from "@app/components/RelayName.svelte"
import {command, writer} from "@app/core"
2026-09-28 17:59:33 +00:00
import {addressWriterToSpace} from "@app/rooms"
import {makeLibraryPath} from "@app/routes"
2026-06-23 23:30:54 +00:00
import {pushToast} from "@app/toast"
type Props = {
url: string
2026-07-28 16:16:26 +00:00
board?: PinboardReader
2026-06-23 23:30:54 +00:00
}
2026-07-28 16:16:26 +00:00
const {url, board}: Props = $props()
2026-06-23 23:30:54 +00:00
const back = () => history.back()
const submit = async () => {
loading = true
try {
2026-09-28 17:59:33 +00:00
const eventWriter = await addressWriterToSpace(
writer(Pinboard, board).setTitle(title).setDescription(description),
url,
)
2026-07-28 16:16:26 +00:00
if (!board) {
eventWriter.setIdentifier().setCollaborative(true)
2026-07-28 16:16:26 +00:00
}
const publication = await command(eventWriter).then(publish)
const error = (await publication.settled()).getError()
2026-06-23 23:30:54 +00:00
if (error) {
pushToast({theme: "error", message: error})
2026-07-28 16:16:26 +00:00
} else if (board) {
pushToast({message: "Shelf updated!"})
back()
2026-06-23 23:30:54 +00:00
} else {
2026-07-28 16:16:26 +00:00
pushToast({message: "Shelf created!"})
navigate(makeLibraryPath(url, getAddress(publication.event)))
2026-06-23 23:30:54 +00:00
}
2026-07-28 16:16:26 +00:00
} catch (e) {
console.error(e)
pushToast({theme: "error", message: "This space refused to save the shelf."})
2026-06-23 23:30:54 +00:00
} finally {
loading = false
}
}
2026-07-28 16:16:26 +00:00
let title = $state(board?.title() ?? "")
let description = $state(board?.description() ?? "")
let loading = $state(false)
2026-06-23 23:30:54 +00:00
</script>
<Modal>
<ModalBody>
<ModalHeader>
2026-07-28 16:16:26 +00:00
<ModalTitle>{board ? "Edit Shelf" : "Create Shelf"}</ModalTitle>
2026-06-23 23:30:54 +00:00
<ModalSubtitle>on <RelayName {url} class="text-primary" /></ModalSubtitle>
</ModalHeader>
<Field>
{#snippet label()}
Title
{/snippet}
{#snippet input()}
<input bind:value={title} class="input w-full" placeholder="Shelf title" />
2026-06-23 23:30:54 +00:00
{/snippet}
</Field>
<Field>
{#snippet label()}
Description
{/snippet}
{#snippet input()}
<input
bind:value={description}
class="input w-full"
2026-06-23 23:30:54 +00:00
placeholder="What's this shelf about?" />
{/snippet}
</Field>
</ModalBody>
<ModalFooter>
<Button class="button button-link" onclick={back}>
<Icon icon={AltArrowLeft} />
Go back
</Button>
<Button class="button button-primary" onclick={submit} disabled={loading || !title.trim()}>
<Spinner {loading}>Save changes</Spinner>
</Button>
</ModalFooter>
</Modal>