2024-10-03 18:49:52 +00:00
|
|
|
<script lang="ts">
|
2024-10-08 18:39:16 +00:00
|
|
|
import {onMount} from "svelte"
|
2026-03-11 22:48:36 +00:00
|
|
|
import {SvelteSet} from "svelte/reactivity"
|
2024-10-08 18:39:16 +00:00
|
|
|
import type {Readable} from "svelte/store"
|
2024-10-17 23:57:09 +00:00
|
|
|
import {tryCatch} from "@welshman/lib"
|
2026-03-11 22:48:36 +00:00
|
|
|
import {isShareableRelayUrl, isIPAddress, normalizeRelayUrl} from "@welshman/util"
|
|
|
|
|
import type {Thunk} from "@welshman/app"
|
2026-07-28 16:16:26 +00:00
|
|
|
import {Relays} from "@welshman/app"
|
2024-10-03 18:49:52 +00:00
|
|
|
import {createScroller} from "@lib/html"
|
2026-03-11 22:48:36 +00:00
|
|
|
import {errorMessage} from "@lib/util"
|
2025-09-10 22:26:39 +00:00
|
|
|
import Magnifier from "@assets/icons/magnifier.svg?dataurl"
|
|
|
|
|
import AddCircle from "@assets/icons/add-circle.svg?dataurl"
|
2024-10-03 18:49:52 +00:00
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
|
|
|
|
import Button from "@lib/components/Button.svelte"
|
2026-06-24 20:56:53 +00:00
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
2026-01-30 22:14:12 +00:00
|
|
|
import Modal from "@lib/components/Modal.svelte"
|
|
|
|
|
import ModalBody from "@lib/components/ModalBody.svelte"
|
2026-03-11 22:48:36 +00:00
|
|
|
import ModalFooter from "@lib/components/ModalFooter.svelte"
|
2024-10-03 18:49:52 +00:00
|
|
|
import RelayItem from "@app/components/RelayItem.svelte"
|
2026-06-08 21:14:40 +00:00
|
|
|
import {pushToast} from "@app/toast"
|
2026-07-28 16:16:26 +00:00
|
|
|
import {app} from "@app/core"
|
2024-10-03 18:49:52 +00:00
|
|
|
|
2025-02-04 00:37:14 +00:00
|
|
|
interface Props {
|
|
|
|
|
relays: Readable<string[]>
|
2026-03-11 22:48:36 +00:00
|
|
|
addRelay: (url: string) => Promise<Thunk>
|
|
|
|
|
matchRelay?: (url: string) => boolean
|
2025-02-04 00:37:14 +00:00
|
|
|
}
|
2024-10-03 18:49:52 +00:00
|
|
|
|
2026-03-11 22:48:36 +00:00
|
|
|
const {relays, addRelay, matchRelay}: Props = $props()
|
|
|
|
|
|
|
|
|
|
const back = () => history.back()
|
|
|
|
|
|
|
|
|
|
const customUrl = $derived(tryCatch(() => normalizeRelayUrl(term)))
|
|
|
|
|
|
|
|
|
|
const add = async (url: string) => {
|
|
|
|
|
loading.add(url)
|
|
|
|
|
|
|
|
|
|
try {
|
2026-07-28 16:16:26 +00:00
|
|
|
const thunk = await addRelay(url)
|
|
|
|
|
const error = await thunk.waitForError()
|
2026-03-11 22:48:36 +00:00
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
pushToast({
|
|
|
|
|
theme: "error",
|
|
|
|
|
message: `Failed to add relay: ${errorMessage(error)}`,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
loading.delete(url)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-03 18:49:52 +00:00
|
|
|
|
2025-02-04 00:37:14 +00:00
|
|
|
let term = $state("")
|
|
|
|
|
let limit = $state(20)
|
|
|
|
|
let element: Element | undefined = $state()
|
|
|
|
|
|
2026-03-11 22:48:36 +00:00
|
|
|
const loading = $state(new SvelteSet<string>())
|
2026-07-28 16:16:26 +00:00
|
|
|
const relaySearch = $app.use(Relays).relaySearch
|
2026-03-11 22:48:36 +00:00
|
|
|
|
|
|
|
|
const searchResults = $derived(
|
|
|
|
|
$relaySearch
|
|
|
|
|
.searchValues(term)
|
2026-07-28 16:16:26 +00:00
|
|
|
.filter((url: string) => {
|
2026-03-11 22:48:36 +00:00
|
|
|
if (matchRelay?.(url) === false) return false
|
|
|
|
|
if ($relays.includes(url)) return false
|
|
|
|
|
if (isIPAddress(url)) return false
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
.slice(0, limit),
|
|
|
|
|
)
|
2024-10-17 23:57:09 +00:00
|
|
|
|
2024-10-03 18:49:52 +00:00
|
|
|
onMount(() => {
|
|
|
|
|
const scroller = createScroller({
|
2025-02-04 00:37:14 +00:00
|
|
|
element: element!,
|
2024-10-03 18:49:52 +00:00
|
|
|
delay: 300,
|
|
|
|
|
onScroll: () => {
|
|
|
|
|
limit += 20
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
scroller.stop()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
2026-01-30 22:14:12 +00:00
|
|
|
<Modal>
|
|
|
|
|
<ModalBody>
|
2026-06-24 20:56:53 +00:00
|
|
|
<label class="input input-group flex w-full items-center gap-2">
|
2026-01-30 22:14:12 +00:00
|
|
|
<Icon icon={Magnifier} />
|
|
|
|
|
<input bind:value={term} class="grow" type="text" placeholder="Search for relays..." />
|
|
|
|
|
</label>
|
2026-06-24 20:56:53 +00:00
|
|
|
<div class="flex flex-col -m-6 mt-0 h-[50vh] gap-2 overflow-auto p-6 pt-2" bind:this={element}>
|
2026-06-03 18:08:08 +00:00
|
|
|
{#if customUrl && isShareableRelayUrl(customUrl) && !$relays.includes(customUrl)}
|
2026-01-30 22:14:12 +00:00
|
|
|
<RelayItem url={term}>
|
|
|
|
|
<Button
|
2026-06-24 20:56:53 +00:00
|
|
|
class="button button-outline button-sm flex items-center"
|
2026-03-11 22:48:36 +00:00
|
|
|
disabled={loading.has(customUrl)}
|
|
|
|
|
onclick={() => add(customUrl)}>
|
|
|
|
|
{#if loading.has(customUrl)}
|
2026-06-24 20:56:53 +00:00
|
|
|
<Spinner size="sm" />
|
2026-03-11 22:48:36 +00:00
|
|
|
{:else}
|
|
|
|
|
<Icon icon={AddCircle} />
|
|
|
|
|
{/if}
|
2026-01-30 22:14:12 +00:00
|
|
|
Add Relay
|
|
|
|
|
</Button>
|
|
|
|
|
</RelayItem>
|
|
|
|
|
{/if}
|
2026-03-11 22:48:36 +00:00
|
|
|
{#each searchResults as url (url)}
|
2026-01-30 22:14:12 +00:00
|
|
|
<RelayItem {url}>
|
2026-03-11 22:48:36 +00:00
|
|
|
<Button
|
2026-06-24 20:56:53 +00:00
|
|
|
class="button button-outline button-sm flex items-center"
|
2026-03-11 22:48:36 +00:00
|
|
|
disabled={loading.has(url)}
|
|
|
|
|
onclick={() => add(url)}>
|
|
|
|
|
{#if loading.has(url)}
|
2026-06-24 20:56:53 +00:00
|
|
|
<Spinner size="sm" />
|
2026-03-11 22:48:36 +00:00
|
|
|
{:else}
|
|
|
|
|
<Icon icon={AddCircle} />
|
|
|
|
|
{/if}
|
2026-01-30 22:14:12 +00:00
|
|
|
Add Relay
|
|
|
|
|
</Button>
|
|
|
|
|
</RelayItem>
|
|
|
|
|
{/each}
|
|
|
|
|
</div>
|
|
|
|
|
</ModalBody>
|
2026-03-11 22:48:36 +00:00
|
|
|
<ModalFooter>
|
2026-06-24 20:56:53 +00:00
|
|
|
<Button class="button button-primary grow" onclick={back}>Done</Button>
|
2026-03-11 22:48:36 +00:00
|
|
|
</ModalFooter>
|
2026-01-30 22:14:12 +00:00
|
|
|
</Modal>
|