2024-09-23 20:10:15 +00:00
|
|
|
<script lang="ts">
|
2025-01-02 18:04:28 +00:00
|
|
|
import {writable} from "svelte/store"
|
2026-07-28 16:16:26 +00:00
|
|
|
import {Thread} from "@welshman/domain"
|
|
|
|
|
import {publish} 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"
|
2026-04-10 17:11:48 +00:00
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
2024-10-10 21:33:50 +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"
|
2024-10-10 21:33:50 +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-02-05 16:17:51 +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 {DraftKey} from "@app/drafts"
|
2026-07-28 16:16:26 +00:00
|
|
|
import {makeEditor} from "@app/editor"
|
|
|
|
|
import {publishRoomQuote} from "@app/rooms"
|
|
|
|
|
import {pushToast} from "@app/toast"
|
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
|
2026-04-10 17:11:48 +00:00
|
|
|
shareToChat?: boolean
|
2026-08-17 18:18:38 +00:00
|
|
|
initialValues?: Values
|
2025-10-06 18:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
2026-08-17 18:18:38 +00:00
|
|
|
const {url, h, shareToChat = false, initialValues}: Props = $props()
|
2026-04-07 13:06:29 +00:00
|
|
|
const draftKey = new DraftKey<Values>(`thread:${url}:${h ?? ""}`)
|
2026-08-17 18:18:38 +00:00
|
|
|
const draft = draftKey.get()
|
2026-07-28 16:16:26 +00:00
|
|
|
const shouldProtect = $relays.hasNip(url, 70)
|
2025-08-19 21:03:04 +00:00
|
|
|
|
2025-01-02 18:04:28 +00:00
|
|
|
const uploading = writable(false)
|
|
|
|
|
|
2024-09-23 20:10:15 +00:00
|
|
|
const back = () => history.back()
|
|
|
|
|
|
2025-04-29 19:24:20 +00:00
|
|
|
const selectFiles = () => editor.then(ed => ed.commands.selectFiles())
|
|
|
|
|
|
|
|
|
|
const submit = async () => {
|
2026-04-10 17:11:48 +00:00
|
|
|
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.",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 19:24:20 +00:00
|
|
|
const ed = await editor
|
|
|
|
|
const content = ed.getText({blockSeparator: "\n"}).trim()
|
2024-10-24 19:55:48 +00:00
|
|
|
|
2026-07-28 16:16:26 +00:00
|
|
|
if (!content) {
|
2024-10-24 19:55:48 +00:00
|
|
|
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.",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 17:11:48 +00:00
|
|
|
loading = true
|
2024-10-29 20:20:59 +00:00
|
|
|
|
2026-04-10 17:11:48 +00:00
|
|
|
try {
|
|
|
|
|
const protect = await shouldProtect
|
2026-07-28 16:16:26 +00:00
|
|
|
const eventWriter = writer(Thread)
|
|
|
|
|
.setContent(content)
|
|
|
|
|
.setTitle(title)
|
|
|
|
|
.setProtected(protect)
|
|
|
|
|
.addTags(...ed.storage.nostr.getEditorTags())
|
|
|
|
|
.forceRelays(url)
|
2026-04-10 17:11:48 +00:00
|
|
|
|
|
|
|
|
if (h) {
|
2026-07-28 16:16:26 +00:00
|
|
|
eventWriter.setRoom(url, h)
|
2026-04-10 17:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-28 16:16:26 +00:00
|
|
|
const thunk = await command(eventWriter).then(publish)
|
|
|
|
|
const error = await thunk.waitForError()
|
2024-09-23 20:10:15 +00:00
|
|
|
|
2026-04-10 17:11:48 +00:00
|
|
|
if (error) {
|
|
|
|
|
return pushToast({theme: "error", message: error})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draftKey.clear()
|
|
|
|
|
history.back()
|
|
|
|
|
|
|
|
|
|
if (shareToChat) {
|
2026-07-28 16:16:26 +00:00
|
|
|
publishRoomQuote({url, h, parent: thunk.event, protect})
|
2026-04-10 17:11:48 +00:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
loading = false
|
|
|
|
|
}
|
2024-09-23 20:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 17:11:48 +00:00
|
|
|
let loading = $state(false)
|
|
|
|
|
|
2026-08-17 18:18:38 +00:00
|
|
|
let title = $state(initialValues?.title ?? draft?.title ?? "")
|
|
|
|
|
let content = $state(initialValues?.content ?? draft?.content ?? "")
|
2026-04-07 13:06:29 +00:00
|
|
|
|
|
|
|
|
const onChange = (json: object) => {
|
|
|
|
|
content = json
|
|
|
|
|
}
|
2026-04-07 12:06:29 +00:00
|
|
|
|
|
|
|
|
const editor = makeEditor({
|
|
|
|
|
url,
|
|
|
|
|
submit,
|
|
|
|
|
uploading,
|
|
|
|
|
onChange,
|
|
|
|
|
placeholder: "What's on your mind?",
|
2026-04-07 13:06:29 +00:00
|
|
|
content,
|
2026-04-07 12:06:29 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
$effect(() => {
|
2026-04-07 13:06:29 +00:00
|
|
|
draftKey.update({title, content})
|
2026-04-07 12:06:29 +00:00
|
|
|
})
|
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>
|
2026-06-24 20:56:53 +00:00
|
|
|
<div class="flex flex-col gap-8 relative">
|
2026-01-30 22:14:12 +00:00
|
|
|
<Field>
|
|
|
|
|
{#snippet label()}
|
|
|
|
|
<p>Title*</p>
|
|
|
|
|
{/snippet}
|
|
|
|
|
{#snippet input()}
|
2026-06-24 20:56:53 +00:00
|
|
|
<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}
|
|
|
|
|
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"
|
2026-06-24 20:56:53 +00:00
|
|
|
class="tip tip-left absolute bottom-1 right-2"
|
2026-04-10 17:11:48 +00:00
|
|
|
onclick={selectFiles}
|
|
|
|
|
disabled={loading}>
|
2026-01-30 22:14:12 +00:00
|
|
|
{#if $uploading}
|
2026-06-24 20:56:53 +00:00
|
|
|
<Spinner size="xs" />
|
2026-01-30 22:14:12 +00:00
|
|
|
{:else}
|
|
|
|
|
<Icon icon={Paperclip} size={3} />
|
|
|
|
|
{/if}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</ModalBody>
|
2024-10-10 21:33:50 +00:00
|
|
|
<ModalFooter>
|
2026-06-24 20:56:53 +00:00
|
|
|
<Button class="button button-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>
|
2026-06-24 20:56:53 +00:00
|
|
|
<Button type="submit" class="button button-primary" disabled={$uploading || loading}>
|
2026-04-10 17:11:48 +00:00
|
|
|
<Spinner {loading}>Create Thread</Spinner>
|
|
|
|
|
</Button>
|
2024-10-10 21:33:50 +00:00
|
|
|
</ModalFooter>
|
2026-01-30 22:14:12 +00:00
|
|
|
</Modal>
|