flotilla/src/app/components/SpaceEdit.svelte

135 lines
4.1 KiB
Svelte
Raw Normal View History

<script lang="ts">
2026-07-28 16:16:26 +00:00
import {displayRelayUrl} from "@welshman/util"
import type {RelayInfo} from "@welshman/domain"
import Widget from "@assets/icons/widget-4.svg?dataurl"
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
import {preventDefault} from "@lib/html"
import FieldInline from "@lib/components/FieldInline.svelte"
import Icon from "@lib/components/Icon.svelte"
import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte"
2026-01-30 22:14:12 +00:00
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.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"
import ImageIcon from "@lib/components/ImageIcon.svelte"
2026-07-15 16:30:17 +00:00
import IconInput from "@lib/components/IconInput.svelte"
2026-06-08 21:14:40 +00:00
import {pushToast} from "@app/toast"
import {clearModals} from "@app/modal"
import {resolveImageInput} from "@app/uploads"
2026-07-28 16:16:26 +00:00
import {relayManagement, relays} from "@app/core"
type Props = {
url: string
2026-07-28 16:16:26 +00:00
initialValues: Partial<RelayInfo>
}
2025-12-18 02:43:13 +00:00
const {url, initialValues = {}}: Props = $props()
2026-08-21 15:31:53 +00:00
const values = $state({...initialValues})
const compressOptions = {maxWidth: 128, maxHeight: 128}
const back = () => history.back()
const submit = async () => {
if (values.name != initialValues.name) {
2026-07-28 16:16:26 +00:00
const res = await $relayManagement.forUrl(url).changeRelayName(values.name || "")
if (res.error) {
return pushToast({theme: "error", message: res.error})
}
}
if (values.description != initialValues.description) {
2026-07-28 16:16:26 +00:00
const res = await $relayManagement
.forUrl(url)
.changeRelayDescription(values.description || "")
if (res.error) {
return pushToast({theme: "error", message: res.error})
}
}
const icon = await resolveImageInput(imageFile, imagePreview, compressOptions)
if (icon && icon !== initialValues.icon) {
const res = await $relayManagement.forUrl(url).changeRelayIcon(icon)
if (res.error) {
return pushToast({theme: "error", message: res.error})
}
}
pushToast({message: "Your changes have been saved!"})
2026-07-28 16:16:26 +00:00
$relays.forceLoad(url)
clearModals()
}
const trySubmit = async () => {
loading = true
try {
await submit()
} finally {
loading = false
}
}
let loading = $state(false)
let imageFile = $state<File | undefined>()
let imagePreview = $state(initialValues.icon)
</script>
2026-01-30 22:14:12 +00:00
<Modal tag="form" onsubmit={preventDefault(trySubmit)}>
<ModalBody>
<ModalHeader>
2026-06-22 20:36:57 +00:00
<ModalTitle>Edit this Space</ModalTitle>
2026-02-02 18:09:14 +00:00
<ModalSubtitle><span class="text-primary">{displayRelayUrl(url)}</span></ModalSubtitle>
2026-01-30 22:14:12 +00:00
</ModalHeader>
<FieldInline>
{#snippet label()}
<p>Icon</p>
{/snippet}
{#snippet input()}
2026-07-15 16:30:17 +00:00
<IconInput bind:file={imageFile} bind:preview={imagePreview} />
2026-01-30 22:14:12 +00:00
{/snippet}
</FieldInline>
<FieldInline>
{#snippet label()}
<p>Name</p>
{/snippet}
{#snippet input()}
<label class="input flex w-full items-center gap-2">
2026-01-30 22:14:12 +00:00
{#if imagePreview}
<ImageIcon src={imagePreview} alt="" />
{:else}
<Icon icon={Widget} />
2026-01-30 22:14:12 +00:00
{/if}
<input bind:value={values.name} class="grow" type="text" />
</label>
{/snippet}
</FieldInline>
<FieldInline>
{#snippet label()}
<p class="flex flex-col items-start h-full">Description</p>
2026-01-30 22:14:12 +00:00
{/snippet}
{#snippet input()}
<textarea bind:value={values.description} class="min-h-24 textarea input flex w-full"
></textarea>
2026-01-30 22:14:12 +00:00
{/snippet}
</FieldInline>
</ModalBody>
<ModalFooter>
<Button class="button button-link" onclick={back}>
<Icon icon={AltArrowLeft} />
Go back
</Button>
<Button type="submit" class="button button-primary" disabled={loading}>
<Spinner {loading}>Save Changes</Spinner>
</Button>
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>