2026-02-04 00:19:14 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
import type {Snippet} from "svelte"
|
2026-02-25 00:09:50 +00:00
|
|
|
import {removeUndefined, randomId, uniq} from "@welshman/lib"
|
2026-09-09 02:24:05 +00:00
|
|
|
import {relay} from "@welshman/util"
|
2026-07-28 16:16:26 +00:00
|
|
|
import {publish} from "@welshman/app"
|
|
|
|
|
import {Classified} from "@welshman/domain"
|
2026-02-04 00:19:14 +00:00
|
|
|
import {isMobile, preventDefault} from "@lib/html"
|
2026-02-25 00:09:50 +00:00
|
|
|
import {normalizeTopic} from "@lib/util"
|
2026-02-04 00:19:14 +00:00
|
|
|
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
|
|
|
|
|
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"
|
|
|
|
|
import Modal from "@lib/components/Modal.svelte"
|
|
|
|
|
import ModalBody from "@lib/components/ModalBody.svelte"
|
|
|
|
|
import ImagesInput from "@lib/components/ImagesInput.svelte"
|
|
|
|
|
import CurrencyInput from "@app/components/CurrencyInput.svelte"
|
2026-02-25 00:09:50 +00:00
|
|
|
import TopicMultiSelect from "@app/components/TopicMultiSelect.svelte"
|
2026-02-04 00:19:14 +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"
|
2026-02-04 00:19:14 +00:00
|
|
|
import {makeEditor} from "@app/editor"
|
2026-06-08 21:14:40 +00:00
|
|
|
import {DraftKey} from "@app/drafts"
|
2026-07-03 20:03:04 +00:00
|
|
|
import {compressFileForUpload, uploadFile} from "@app/uploads"
|
2026-02-04 00:19:14 +00:00
|
|
|
|
2026-04-07 13:06:29 +00:00
|
|
|
type Values = {
|
|
|
|
|
d: string
|
|
|
|
|
title: string
|
|
|
|
|
content: string | object
|
|
|
|
|
price: number
|
|
|
|
|
currency: string
|
|
|
|
|
images: (string | File)[]
|
|
|
|
|
status: string
|
|
|
|
|
topics: string[]
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-04 00:19:14 +00:00
|
|
|
type Props = {
|
|
|
|
|
url: string
|
|
|
|
|
h?: string
|
2026-04-10 17:11:48 +00:00
|
|
|
shareToChat?: boolean
|
2026-02-04 00:19:14 +00:00
|
|
|
header: Snippet
|
2026-04-07 13:06:29 +00:00
|
|
|
initialValues?: Values
|
2026-02-04 00:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 17:11:48 +00:00
|
|
|
let {url, h, shareToChat = false, header, initialValues}: Props = $props()
|
2026-04-07 12:06:29 +00:00
|
|
|
|
2026-04-07 13:06:29 +00:00
|
|
|
const draftKey = new DraftKey<Values>(`classified:${url}:${h ?? ""}`)
|
2026-04-07 12:06:29 +00:00
|
|
|
|
|
|
|
|
if (!initialValues) {
|
2026-04-07 13:06:29 +00:00
|
|
|
initialValues = draftKey.get()
|
2026-04-07 12:06:29 +00:00
|
|
|
}
|
2026-02-04 00:19:14 +00:00
|
|
|
|
|
|
|
|
const back = () => history.back()
|
|
|
|
|
|
|
|
|
|
const submit = async () => {
|
|
|
|
|
loading = true
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!title) {
|
|
|
|
|
return pushToast({
|
|
|
|
|
theme: "error",
|
|
|
|
|
message: "Please provide a title for your listing.",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ed = await editor
|
|
|
|
|
const content = ed.getText({blockSeparator: "\n"}).trim()
|
|
|
|
|
|
|
|
|
|
if (!content.trim()) {
|
|
|
|
|
return pushToast({
|
|
|
|
|
theme: "error",
|
|
|
|
|
message: "Please provide a description for your listing.",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 16:16:26 +00:00
|
|
|
const imageUrls: string[] = []
|
2026-02-04 00:19:14 +00:00
|
|
|
|
|
|
|
|
for (const image of images) {
|
|
|
|
|
if (typeof image === "string") {
|
2026-07-28 16:16:26 +00:00
|
|
|
imageUrls.push(image)
|
2026-02-04 00:19:14 +00:00
|
|
|
} else {
|
2026-07-03 20:03:04 +00:00
|
|
|
const {result, error} = await uploadFile(await compressFileForUpload(image), {url})
|
2026-02-04 00:19:14 +00:00
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return pushToast({
|
|
|
|
|
theme: "error",
|
|
|
|
|
message: `Failed to upload file ${image.name}`,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result) {
|
2026-07-28 16:16:26 +00:00
|
|
|
imageUrls.push(result.url)
|
2026-02-04 00:19:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 16:16:26 +00:00
|
|
|
const protect = await $relays.hasNip(url, 70)
|
|
|
|
|
|
|
|
|
|
const eventWriter = writer(Classified)
|
|
|
|
|
.setIdentifier(d)
|
|
|
|
|
.setContent(content)
|
|
|
|
|
.setTitle(title)
|
|
|
|
|
.setSummary(content)
|
|
|
|
|
.setPrice(price, currency)
|
|
|
|
|
.setStatus(status)
|
|
|
|
|
.setTopics(topics)
|
|
|
|
|
.setImages(imageUrls)
|
|
|
|
|
.setProtected(protect)
|
|
|
|
|
.addTags(...ed.storage.nostr.getEditorTags())
|
|
|
|
|
|
|
|
|
|
if (h) {
|
|
|
|
|
eventWriter.setRoom(url, h)
|
|
|
|
|
} else {
|
2026-09-09 02:24:05 +00:00
|
|
|
eventWriter.forceRoutes(relay(url))
|
2026-07-28 16:16:26 +00:00
|
|
|
}
|
2026-02-04 00:19:14 +00:00
|
|
|
|
2026-07-28 16:16:26 +00:00
|
|
|
const classifiedThunk = await command(eventWriter).then(publish)
|
|
|
|
|
const error = await classifiedThunk.waitForError()
|
2026-04-10 17:11:48 +00:00
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return pushToast({theme: "error", message: error})
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 12:06:29 +00:00
|
|
|
draftKey.clear()
|
2026-02-04 00:19:14 +00:00
|
|
|
history.back()
|
2026-04-10 17:11:48 +00:00
|
|
|
|
|
|
|
|
if (shareToChat) {
|
|
|
|
|
publishRoomQuote({url, h, parent: classifiedThunk.event, protect})
|
|
|
|
|
}
|
2026-02-04 00:19:14 +00:00
|
|
|
} finally {
|
|
|
|
|
loading = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let loading = $state(false)
|
2026-04-07 13:06:29 +00:00
|
|
|
const d = $state(initialValues?.d ?? randomId())
|
2026-04-07 12:06:29 +00:00
|
|
|
let title = $state(initialValues?.title ?? "")
|
|
|
|
|
let status = $state(initialValues?.status ?? "active")
|
2026-04-07 13:06:29 +00:00
|
|
|
let price = $state(initialValues?.price ?? 0)
|
2026-04-07 12:06:29 +00:00
|
|
|
let currency = $state(initialValues?.currency ?? "SAT")
|
2026-04-07 13:06:29 +00:00
|
|
|
let images = $state(initialValues?.images ?? [])
|
|
|
|
|
let topics = $state(uniq(removeUndefined(initialValues?.topics?.map(normalizeTopic) ?? [])))
|
|
|
|
|
let content = $state(initialValues?.content ?? "")
|
|
|
|
|
|
|
|
|
|
const onChange = (json: object) => {
|
|
|
|
|
content = json
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const editor = makeEditor({url, submit, onChange, content})
|
2026-04-07 12:06:29 +00:00
|
|
|
|
|
|
|
|
$effect(() => {
|
2026-04-07 13:06:29 +00:00
|
|
|
draftKey.set({d, title, status, price, currency, images, topics, content})
|
2026-04-07 12:06:29 +00:00
|
|
|
})
|
2026-02-04 00:19:14 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<Modal tag="form" onsubmit={preventDefault(submit)}>
|
|
|
|
|
<ModalBody>
|
2026-02-04 01:09:30 +00:00
|
|
|
{@render header?.()}
|
2026-06-24 20:56:53 +00:00
|
|
|
<div class="flex flex-col gap-8 relative">
|
2026-02-04 00:19:14 +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-02-04 00:19:14 +00:00
|
|
|
<!-- svelte-ignore a11y_autofocus -->
|
|
|
|
|
<input
|
|
|
|
|
autofocus={!isMobile}
|
|
|
|
|
bind:value={title}
|
|
|
|
|
class="grow"
|
|
|
|
|
type="text"
|
|
|
|
|
placeholder="What is this listing for?" />
|
|
|
|
|
</label>
|
|
|
|
|
{/snippet}
|
|
|
|
|
</Field>
|
|
|
|
|
<Field>
|
|
|
|
|
{#snippet label()}
|
|
|
|
|
<p>Description*</p>
|
|
|
|
|
{/snippet}
|
|
|
|
|
{#snippet input()}
|
2026-04-07 21:53:19 +00:00
|
|
|
<div class="note-editor grow overflow-hidden">
|
2026-02-04 00:19:14 +00:00
|
|
|
<EditorContent {editor} />
|
|
|
|
|
</div>
|
|
|
|
|
{/snippet}
|
|
|
|
|
</Field>
|
2026-02-25 00:09:50 +00:00
|
|
|
<Field>
|
|
|
|
|
{#snippet label()}
|
|
|
|
|
<p>Topics</p>
|
|
|
|
|
{/snippet}
|
|
|
|
|
{#snippet input()}
|
|
|
|
|
<TopicMultiSelect bind:value={topics} />
|
|
|
|
|
{/snippet}
|
|
|
|
|
</Field>
|
2026-02-04 00:19:14 +00:00
|
|
|
<Field>
|
|
|
|
|
{#snippet label()}
|
|
|
|
|
<p>Price*</p>
|
|
|
|
|
{/snippet}
|
|
|
|
|
{#snippet input()}
|
|
|
|
|
<div class="join grid grid-cols-2">
|
2026-06-24 20:56:53 +00:00
|
|
|
<label class="input flex w-full items-center gap-2">
|
2026-02-04 00:19:14 +00:00
|
|
|
<input bind:value={price} class="grow w-32" type="number" />
|
|
|
|
|
</label>
|
2026-06-24 20:56:53 +00:00
|
|
|
<CurrencyInput bind:value={currency} />
|
2026-02-04 00:19:14 +00:00
|
|
|
</div>
|
|
|
|
|
{/snippet}
|
|
|
|
|
</Field>
|
|
|
|
|
{#if initialValues}
|
|
|
|
|
<Field>
|
|
|
|
|
{#snippet label()}
|
|
|
|
|
<p>Status*</p>
|
|
|
|
|
{/snippet}
|
|
|
|
|
{#snippet input()}
|
2026-06-24 20:56:53 +00:00
|
|
|
<select class="select input w-full" bind:value={status}>
|
2026-02-04 00:19:14 +00:00
|
|
|
<option value="active">Active</option>
|
|
|
|
|
<option value="sold">Sold</option>
|
|
|
|
|
</select>
|
|
|
|
|
{/snippet}
|
|
|
|
|
</Field>
|
|
|
|
|
{/if}
|
|
|
|
|
<Field>
|
|
|
|
|
{#snippet label()}
|
|
|
|
|
<p>Images (optional)</p>
|
|
|
|
|
{/snippet}
|
|
|
|
|
{#snippet input()}
|
|
|
|
|
<ImagesInput bind:value={images} />
|
|
|
|
|
{/snippet}
|
|
|
|
|
</Field>
|
|
|
|
|
</div>
|
|
|
|
|
</ModalBody>
|
|
|
|
|
<ModalFooter>
|
2026-06-24 20:56:53 +00:00
|
|
|
<Button onclick={back} disabled={loading} class="button button-link">
|
2026-02-04 00:19:14 +00:00
|
|
|
<Icon icon={AltArrowLeft} />
|
|
|
|
|
Go back
|
|
|
|
|
</Button>
|
2026-06-24 20:56:53 +00:00
|
|
|
<Button type="submit" disabled={loading} class="button button-primary">
|
2026-02-04 01:09:30 +00:00
|
|
|
<Spinner {loading}>Save Listing</Spinner>
|
2026-02-04 00:19:14 +00:00
|
|
|
</Button>
|
|
|
|
|
</ModalFooter>
|
|
|
|
|
</Modal>
|