flotilla/src/app/components/GoalCreate.svelte

216 lines
6 KiB
Svelte
Raw Normal View History

2025-07-07 22:28:36 +00:00
<script lang="ts">
import {writable} from "svelte/store"
2026-07-28 16:16:26 +00:00
import {ZapGoal} from "@welshman/domain"
import {publish} from "@welshman/app"
2025-07-07 22:28:36 +00:00
import {isMobile, preventDefault} from "@lib/html"
2025-09-10 22:26:39 +00:00
import Paperclip from "@assets/icons/paperclip-2.svg?dataurl"
import Bolt from "@assets/icons/bolt.svg?dataurl"
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
2025-07-07 22:28:36 +00:00
import Icon from "@lib/components/Icon.svelte"
import Field from "@lib/components/Field.svelte"
import FieldInline from "@lib/components/FieldInline.svelte"
import Button from "@lib/components/Button.svelte"
import Spinner from "@lib/components/Spinner.svelte"
2025-07-07 22:28:36 +00:00
import ModalHeader from "@lib/components/ModalHeader.svelte"
2026-02-02 18:09:14 +00:00
import ModalTitle from "@lib/components/ModalTitle.svelte"
import ModalSubtitle from "@lib/components/ModalSubtitle.svelte"
2025-07-07 22:28:36 +00:00
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"
2025-07-07 22:28:36 +00:00
import EditorContent from "@app/editor/EditorContent.svelte"
2026-07-28 16:16:26 +00:00
import {command, relays, writer} from "@app/core"
2026-06-08 21:14:40 +00:00
import {pushToast} from "@app/toast"
2026-07-28 16:16:26 +00:00
import {publishRoomQuote} from "@app/rooms"
2025-07-07 22:28:36 +00:00
import {makeEditor} from "@app/editor"
2026-06-08 21:14:40 +00:00
import {DraftKey} from "@app/drafts"
2025-07-07 22:28:36 +00:00
2026-04-07 13:06:29 +00:00
type Values = {
title: string
content: string | object
amount: number
}
2025-10-06 18:23:19 +00:00
type Props = {
url: string
2025-10-30 22:33:34 +00:00
h?: string
2026-04-07 13:06:29 +00:00
initialValues?: Values
shareToChat?: boolean
2025-10-06 18:23:19 +00:00
}
let {url, h, initialValues, shareToChat = false}: Props = $props()
2026-04-07 13:06:29 +00:00
const draftKey = new DraftKey<Values>(`goal:${url}:${h ?? ""}`)
if (!initialValues) {
2026-04-07 13:06:29 +00:00
initialValues = draftKey.get()
}
2025-07-07 22:28:36 +00:00
2026-07-28 16:16:26 +00:00
const shouldProtect = $relays.hasNip(url, 70)
2025-07-07 22:28:36 +00:00
const uploading = writable(false)
const back = () => history.back()
const selectFiles = () => editor.then(ed => ed.commands.selectFiles())
const submit = async () => {
if ($uploading || loading) return
2025-07-07 22:28:36 +00:00
2026-04-07 13:06:29 +00:00
if (!title) {
2025-07-07 22:28:36 +00:00
return pushToast({
theme: "error",
message: "Please provide a title for your funding goal.",
})
}
const ed = await editor
2026-04-07 13:06:29 +00:00
const content = ed.getText({blockSeparator: "\n"}).trim()
2025-07-07 22:28:36 +00:00
2026-04-07 13:06:29 +00:00
if (!content.trim()) {
2025-07-07 22:28:36 +00:00
return pushToast({
theme: "error",
message: "Please provide details about your funding goal.",
})
}
loading = true
try {
const protect = await shouldProtect
2026-07-28 16:16:26 +00:00
const eventWriter = writer(ZapGoal)
.setTitle(title)
.setSummary(content)
.setAmount(amount)
.setUrls([url])
.setProtected(protect)
.addTags(...ed.storage.nostr.getEditorTags())
.forceRelays(url)
if (h) {
2026-07-28 16:16:26 +00:00
eventWriter.setRoom(url, h)
}
2026-07-28 16:16:26 +00:00
const goalThunk = await command(eventWriter).then(publish)
const error = await goalThunk.waitForError()
2025-07-07 22:28:36 +00:00
if (error) {
return pushToast({theme: "error", message: error})
}
draftKey.clear()
history.back()
if (shareToChat) {
publishRoomQuote({url, h, parent: goalThunk.event, protect})
}
} finally {
loading = false
}
2025-07-07 22:28:36 +00:00
}
let loading = $state(false)
2026-04-07 13:06:29 +00:00
let title = $state(initialValues?.title ?? "")
let amount = $state(initialValues?.amount ?? 1000)
let content = $state(initialValues?.content ?? "")
const onChange = (json: object) => {
content = json
}
const editor = makeEditor({
url,
submit,
uploading,
onChange,
placeholder: "What's on your mind?",
2026-04-07 13:06:29 +00:00
content,
})
$effect(() => {
2026-04-07 13:06:29 +00:00
draftKey.update({title, content, amount})
})
2025-07-07 22:28:36 +00:00
</script>
2026-01-30 22:14:12 +00:00
<Modal tag="form" onsubmit={preventDefault(submit)}>
<ModalBody>
<ModalHeader>
2026-02-02 18:09:14 +00:00
<ModalTitle>Create a Funding Goal</ModalTitle>
<ModalSubtitle>Request contributions for your fundraiser.</ModalSubtitle>
2026-01-30 22:14:12 +00:00
</ModalHeader>
<div class="flex flex-col gap-8 relative">
2025-07-07 22:28:36 +00:00
<Field>
{#snippet label()}
2026-01-30 22:14:12 +00:00
<p>Title*</p>
2025-07-07 22:28:36 +00:00
{/snippet}
{#snippet input()}
<label class="input flex w-full items-center gap-2">
2026-01-30 22:14:12 +00:00
<!-- svelte-ignore a11y_autofocus -->
<input
autofocus={!isMobile}
2026-04-07 13:06:29 +00:00
bind:value={title}
2026-01-30 22:14:12 +00:00
class="grow"
type="text"
placeholder="What do funds go towards?" />
</label>
2025-07-07 22:28:36 +00:00
{/snippet}
</Field>
2026-01-30 22:14:12 +00:00
<div class="relative">
<Field>
{#snippet label()}
<p>Details*</p>
{/snippet}
{#snippet input()}
2026-04-07 21:53:19 +00:00
<div class="note-editor grow overflow-hidden">
2026-01-30 22:14:12 +00:00
<EditorContent {editor} />
</div>
{/snippet}
</Field>
<Button
data-tip="Add an image"
class="tip tip-left absolute bottom-1 right-2"
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={Paperclip} size={3} />
{/if}
</Button>
</div>
<div class="flex flex-col gap-3">
2026-01-30 22:14:12 +00:00
<FieldInline>
{#snippet label()}
Goal Amount (sats)*
{/snippet}
{#snippet input()}
2026-04-07 21:53:19 +00:00
<div class="flex grow justify-end">
<label class="input flex w-auto items-center gap-2">
2026-01-30 22:14:12 +00:00
<Icon icon={Bolt} />
<input bind:value={amount} type="number" class="w-28 grow" />
<p class="shrink-0 opacity-50">sats</p>
2026-01-30 22:14:12 +00:00
</label>
</div>
{/snippet}
</FieldInline>
<input
class="range w-full"
2026-01-30 22:14:12 +00:00
type="range"
min="1000"
max="100000"
step="1000"
bind:value={amount} />
</div>
2025-07-07 22:28:36 +00:00
</div>
2026-01-30 22:14:12 +00:00
</ModalBody>
2025-07-07 22:28:36 +00:00
<ModalFooter>
<Button class="button button-link" onclick={back} disabled={loading}>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowLeft} />
2025-07-07 22:28:36 +00:00
Go back
</Button>
<Button type="submit" class="button button-primary" disabled={$uploading || loading}>
<Spinner {loading}>Create Goal</Spinner>
</Button>
2025-07-07 22:28:36 +00:00
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>