flotilla/src/app/components/ThreadCreate.svelte

183 lines
4.9 KiB
Svelte
Raw Normal View History

2024-09-23 20:10:15 +00:00
<script lang="ts">
2025-01-02 18:04:28 +00:00
import {writable} from "svelte/store"
2025-06-10 20:35:57 +00:00
import {makeEvent, THREAD} from "@welshman/util"
import {publishThunk, waitForThunkError} from "@welshman/app"
2025-02-04 03:28:29 +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 AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
2024-09-23 20:10:15 +00:00
import Icon from "@lib/components/Icon.svelte"
2024-10-29 20:20:59 +00:00
import Field from "@lib/components/Field.svelte"
2024-09-23 20:10:15 +00:00
import Button from "@lib/components/Button.svelte"
import Spinner from "@lib/components/Spinner.svelte"
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"
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-02-05 16:17:51 +00:00
import EditorContent from "@app/editor/EditorContent.svelte"
2026-06-08 21:14:40 +00:00
import {pushToast} from "@app/toast"
import {PROTECTED} from "@app/core/state"
import {makeEditor} from "@app/editor"
2026-06-08 21:14:40 +00:00
import {DraftKey} from "@app/drafts"
import {canEnforceNip70, publishRoomQuote} from "@app/core/commands"
2024-09-23 20:10:15 +00:00
2026-04-07 13:06:29 +00:00
type Values = {
content?: string | object
title?: string
}
2025-10-06 18:23:19 +00:00
type Props = {
url: string
2025-10-30 22:33:34 +00:00
h?: string
shareToChat?: boolean
2025-10-06 18:23:19 +00:00
}
const {url, h, shareToChat = false}: Props = $props()
2026-04-07 13:06:29 +00:00
const draftKey = new DraftKey<Values>(`thread:${url}:${h ?? ""}`)
const initialValues = draftKey.get()
const shouldProtect = canEnforceNip70(url)
2025-01-02 18:04:28 +00:00
const uploading = writable(false)
2024-09-23 20:10:15 +00:00
const back = () => history.back()
const selectFiles = () => editor.then(ed => ed.commands.selectFiles())
const submit = async () => {
if ($uploading || loading) return
2024-11-16 16:04:09 +00:00
2024-10-29 20:20:59 +00:00
if (!title) {
return pushToast({
theme: "error",
message: "Please provide a title for your thread.",
})
}
const ed = await editor
const content = ed.getText({blockSeparator: "\n"}).trim()
2024-10-24 19:55:48 +00:00
if (!content.trim()) {
return pushToast({
2024-10-24 22:48:45 +00:00
theme: "error",
2024-10-24 19:55:48 +00:00
message: "Please provide a message for your thread.",
})
}
const tags = [...ed.storage.nostr.getEditorTags(), ["title", title]]
loading = true
2024-10-29 20:20:59 +00:00
try {
const protect = await shouldProtect
if (protect) {
tags.push(PROTECTED)
}
if (h) {
tags.push(["h", h])
}
const threadThunk = publishThunk({
relays: [url],
event: makeEvent(THREAD, {content, tags}),
})
2025-10-06 18:23:19 +00:00
const error = await waitForThunkError(threadThunk)
2024-09-23 20:10:15 +00:00
if (error) {
return pushToast({theme: "error", message: error})
}
draftKey.clear()
history.back()
if (shareToChat) {
publishRoomQuote({url, h, parent: threadThunk.event, protect})
}
} finally {
loading = false
}
2024-09-23 20:10:15 +00:00
}
let loading = $state(false)
2026-04-07 13:06:29 +00:00
let title = $state(initialValues?.title ?? "")
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})
})
2024-09-23 20:10:15 +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 Thread</ModalTitle>
<ModalSubtitle>Share a link, or start a discussion.</ModalSubtitle>
2026-01-30 22:14:12 +00:00
</ModalHeader>
<div class="col-8 relative">
<Field>
{#snippet label()}
<p>Title*</p>
{/snippet}
{#snippet input()}
<label class="input input-bordered flex w-full items-center gap-2">
<!-- svelte-ignore a11y_autofocus -->
<input
autofocus={!isMobile}
bind:value={title}
class="grow"
type="text"
placeholder="What is this thread about?" />
</label>
{/snippet}
</Field>
<Field>
{#snippet label()}
<p>Message*</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="tooltip tooltip-left absolute bottom-1 right-2"
onclick={selectFiles}
disabled={loading}>
2026-01-30 22:14:12 +00:00
{#if $uploading}
<span class="loading loading-spinner loading-xs"></span>
{:else}
<Icon icon={Paperclip} size={3} />
{/if}
</Button>
</div>
</ModalBody>
<ModalFooter>
<Button class="btn btn-link" onclick={back} disabled={loading}>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowLeft} />
2024-09-23 20:10:15 +00:00
Go back
</Button>
<Button type="submit" class="btn btn-primary" disabled={$uploading || loading}>
<Spinner {loading}>Create Thread</Spinner>
</Button>
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>