flotilla/src/app/components/CalendarEventForm.svelte

212 lines
5.8 KiB
Svelte
Raw Normal View History

2025-03-18 22:36:52 +00:00
<script lang="ts">
import type {Snippet} from "svelte"
import {writable} from "svelte/store"
2026-07-03 21:43:00 +00:00
import {randomId} from "@welshman/lib"
2026-07-28 16:16:26 +00:00
import {publish} from "@welshman/app"
import {TimeEvent} from "@welshman/domain"
2025-03-18 22:36:52 +00:00
import {preventDefault} from "@lib/html"
2025-09-10 22:26:39 +00:00
import GallerySend from "@assets/icons/gallery-send.svg?dataurl"
import MapPoint from "@assets/icons/map-point.svg?dataurl"
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
2025-03-18 22:36:52 +00:00
import Icon from "@lib/components/Icon.svelte"
import Field from "@lib/components/Field.svelte"
import Button from "@lib/components/Button.svelte"
import Spinner from "@lib/components/Spinner.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
2026-01-30 22:14:12 +00:00
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.svelte"
2026-07-03 21:43:00 +00:00
import DateTimeRangeInput from "@lib/components/DateTimeRangeInput.svelte"
2025-03-18 22:36:52 +00:00
import EditorContent from "@app/editor/EditorContent.svelte"
2026-07-28 16:16:26 +00:00
import {publishRoomQuote} from "@app/rooms"
2025-03-18 22:36:52 +00:00
import {makeEditor} from "@app/editor"
2026-06-08 21:14:40 +00:00
import {DraftKey} from "@app/drafts"
import {pushToast} from "@app/toast"
2026-07-28 16:16:26 +00:00
import {command, relays, writer} from "@app/core"
2025-03-18 22:36:52 +00:00
2026-04-07 13:06:29 +00:00
type Values = {
d: string
title: string
2026-04-07 13:06:29 +00:00
content: string | object
location: string
2026-04-07 13:06:29 +00:00
start?: number
end?: number
}
2025-03-18 22:36:52 +00:00
type Props = {
url: string
2025-10-30 22:33:34 +00:00
h?: string
shareToChat?: boolean
2025-03-18 22:36:52 +00:00
header: Snippet
2026-04-07 13:06:29 +00:00
initialValues?: Values
2025-03-18 22:36:52 +00:00
}
let {url, h, shareToChat = false, header, initialValues}: Props = $props()
2026-04-07 13:06:29 +00:00
const draftKey = new DraftKey<Values>(`calendar:${url}:${h ?? ""}`)
if (!initialValues) {
2026-04-07 13:06:29 +00:00
initialValues = draftKey.get()
}
2025-03-18 22:36:52 +00:00
2026-07-28 16:16:26 +00:00
const shouldProtect = $relays.hasNip(url, 70)
2025-03-18 22:36:52 +00:00
const uploading = writable(false)
const back = () => history.back()
const selectFiles = () => editor.then(ed => ed.chain().selectFiles().run())
const submit = async () => {
if ($uploading || loading) return
2025-03-18 22:36:52 +00:00
if (!title) {
return pushToast({
theme: "error",
message: "Please provide a title.",
})
}
if (!start || !end) {
return pushToast({
theme: "error",
message: "Please provide start and end times.",
})
}
if (start >= end) {
return pushToast({
theme: "error",
message: "End time must be later than start time.",
})
}
const ed = await editor
const content = ed.getText({blockSeparator: "\n"}).trim()
loading = true
try {
const protect = await shouldProtect
2026-07-28 16:16:26 +00:00
const eventWriter = writer(TimeEvent)
.setIdentifier(d)
.setTitle(title)
.setLocation(location)
.setStart(start)
.setEnd(end)
.setContent(content)
.setProtected(protect)
.addTags(...ed.storage.nostr.getEditorTags())
if (h) {
2026-07-28 16:16:26 +00:00
eventWriter.setRoom(url, h)
} else {
eventWriter.forceRelays(url)
}
2026-07-28 16:16:26 +00:00
const calendarThunk = await command(eventWriter).then(publish)
const error = await calendarThunk.waitForError()
2025-10-06 18:23:19 +00:00
if (error) {
return pushToast({theme: "error", message: error})
}
2025-03-18 22:36:52 +00:00
draftKey.clear()
history.back()
if (shareToChat) {
publishRoomQuote({url, h, parent: calendarThunk.event, protect})
}
pushToast({message: "Your event has been saved!"})
} finally {
loading = false
}
2025-03-18 22:36:52 +00:00
}
let loading = $state(false)
2026-04-07 13:06:29 +00:00
const d = $state(initialValues?.d ?? randomId())
let title = $state(initialValues?.title ?? "")
let location = $state(initialValues?.location ?? "")
2025-03-18 22:36:52 +00:00
let start: number | undefined = $state(initialValues?.start)
let end: number | undefined = $state(initialValues?.end)
2026-04-07 13:06:29 +00:00
let content = $state(initialValues?.content ?? "")
const onChange = (json: object) => {
content = json
}
const editor = makeEditor({url, submit, uploading, onChange, content})
$effect(() => {
2026-04-07 13:06:29 +00:00
draftKey.set({d, title, location, start, end, content})
})
2025-03-18 22:36:52 +00:00
</script>
2026-01-30 22:14:12 +00:00
<Modal tag="form" novalidate onsubmit={preventDefault(submit)}>
<ModalBody>
{@render header()}
<Field>
{#snippet label()}
<p>Title*</p>
{/snippet}
{#snippet input()}
<label class="input flex w-full items-center gap-2">
2026-01-30 22:14:12 +00:00
<input bind:value={title} class="grow" type="text" />
</label>
{/snippet}
</Field>
<Field>
{#snippet label()}
<p>Summary</p>
{/snippet}
{#snippet input()}
<div class="relative z-feature flex gap-2">
2026-04-07 21:53:19 +00:00
<div class="input-editor grow overflow-hidden">
2026-01-30 22:14:12 +00:00
<EditorContent {editor} />
</div>
<Button
data-tip="Add an image"
2026-07-03 20:24:52 +00:00
class="button button-neutral button-input tip"
onclick={selectFiles}
disabled={loading}>
2026-01-30 22:14:12 +00:00
{#if $uploading}
<Spinner size="xs" />
2026-01-30 22:14:12 +00:00
{:else}
<Icon icon={GallerySend} />
{/if}
</Button>
2025-03-18 22:36:52 +00:00
</div>
2026-01-30 22:14:12 +00:00
{/snippet}
</Field>
<Field>
{#snippet label()}
2026-07-03 21:43:00 +00:00
<p>Date and time*</p>
2026-01-30 22:14:12 +00:00
{/snippet}
{#snippet input()}
2026-07-03 21:43:00 +00:00
<DateTimeRangeInput bind:start bind:end />
2026-01-30 22:14:12 +00:00
{/snippet}
</Field>
<Field>
{#snippet label()}
<p>Location (optional)</p>
{/snippet}
{#snippet input()}
<label class="input flex w-full items-center gap-2">
2026-01-30 22:14:12 +00:00
<Icon icon={MapPoint} />
<input bind:value={location} class="grow" type="text" />
</label>
{/snippet}
</Field>
</ModalBody>
2025-03-18 22:36:52 +00:00
<ModalFooter>
<Button class="button button-link" onclick={back} disabled={loading}>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowLeft} />
2025-03-18 22:36:52 +00:00
Go back
</Button>
<Button class="button button-primary" type="submit" disabled={$uploading || loading}>
<Spinner loading={$uploading || loading}>Save Event</Spinner>
2025-03-18 22:36:52 +00:00
</Button>
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>