flotilla/src/app/components/ThreadCreate.svelte

134 lines
3.8 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} 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 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"
import {pushToast} from "@app/util/toast"
import {PROTECTED} from "@app/core/state"
import {makeEditor} from "@app/editor"
import {canEnforceNip70} from "@app/core/commands"
2024-09-23 20:10:15 +00:00
2025-10-06 18:23:19 +00:00
type Props = {
url: string
2025-10-30 22:33:34 +00:00
h?: string
2025-10-06 18:23:19 +00:00
}
2025-10-30 22:33:34 +00:00
const {url, h}: Props = $props()
2024-09-23 20:10:15 +00:00
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 () => {
2025-01-02 18:04:28 +00:00
if ($uploading) 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]]
if (await shouldProtect) {
tags.push(PROTECTED)
}
2024-10-29 20:20:59 +00:00
2025-10-30 22:33:34 +00:00
if (h) {
tags.push(["h", h])
2025-10-06 18:23:19 +00:00
}
2024-10-24 19:55:48 +00:00
publishThunk({
relays: [url],
2025-06-10 20:35:57 +00:00
event: makeEvent(THREAD, {content, tags}),
2024-10-24 19:55:48 +00:00
})
2024-09-23 20:10:15 +00:00
2024-10-15 22:52:30 +00:00
history.back()
2024-09-23 20:10:15 +00:00
}
2025-04-15 18:03:27 +00:00
const editor = makeEditor({url, submit, uploading, placeholder: "What's on your mind?"})
2024-11-16 16:04:09 +00:00
2025-02-04 00:37:14 +00:00
let title: string = $state("")
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()}
<div class="note-editor flex-grow overflow-hidden">
<EditorContent {editor} />
</div>
{/snippet}
</Field>
<Button
data-tip="Add an image"
class="tooltip tooltip-left absolute bottom-1 right-2"
onclick={selectFiles}>
{#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}>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowLeft} />
2024-09-23 20:10:15 +00:00
Go back
</Button>
2024-09-23 20:58:01 +00:00
<Button type="submit" class="btn btn-primary">Create Thread</Button>
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>