fix: query content kinds via NIP-50 in room and space search

This commit is contained in:
Bhavishy
2026-04-02 23:42:48 +05:30
parent 329ad507f6
commit 8990a205d2
+19 -52
View File
@@ -2,16 +2,15 @@
import {tick} from "svelte"
import {debounce} from "throttle-debounce"
import {request} from "@welshman/net"
import {userSearchRelayList} from "@welshman/app"
import {formatTimestampAsDate, groupBy, now, uniqBy, MINUTE, HOUR, DAY, WEEK} from "@welshman/lib"
import {formatTimestampAsDate, groupBy, now, MINUTE, HOUR, DAY, WEEK} from "@welshman/lib"
import type {TrustedEvent, Filter} from "@welshman/util"
import {getRelaysFromList, MESSAGE, sortEventsDesc} from "@welshman/util"
import {sortEventsDesc} from "@welshman/util"
import CloseCircle from "@assets/icons/close-circle.svg?dataurl"
import Magnifier from "@assets/icons/magnifier.svg?dataurl"
import {fly} from "@lib/transition"
import Button from "@lib/components/Button.svelte"
import Icon from "@lib/components/Icon.svelte"
import {DEFAULT_RELAYS} from "@app/core/state"
import {CONTENT_KINDS} from "@app/core/state"
import {goToEvent} from "@app/util/routes"
type Props = {
@@ -21,9 +20,9 @@
const {url, h}: Props = $props()
type SearchScope = "room" | "space" | "everything"
type SearchScope = "room" | "space"
const scopes: SearchScope[] = h ? ["room", "space", "everything"] : ["space", "everything"]
const scopes: SearchScope[] = h ? ["room", "space"] : ["space"]
let term = $state("")
let show = $state(false)
@@ -31,23 +30,12 @@
let results = $state<TrustedEvent[]>([])
let loading = $state(false)
let input: HTMLInputElement | undefined = $state()
let currentSearchId = 0
let controller: AbortController | undefined
const searchRelayUrls = $derived(getRelaysFromList($userSearchRelayList))
const effectiveEverythingRelays = $derived(
uniqBy(identity => identity, searchRelayUrls).length > 0
? uniqBy(identity => identity, searchRelayUrls)
: DEFAULT_RELAYS,
)
const relayStatus = $derived(
scope === "room"
? `Using space relay: ${url} (room filter applied).`
: scope === "space"
? `Using space relay: ${url}.`
: searchRelayUrls.length > 0
? `Using your search relays (${effectiveEverythingRelays.length}).`
: `Using default relays (${effectiveEverythingRelays.length}) because no search relays are configured.`,
: `Using space relay: ${url}.`,
)
const open = () => {
@@ -68,23 +56,17 @@
controller = undefined
}
const getRelayUrls = () => {
if (scope === "everything") {
return effectiveEverythingRelays
}
return [url]
}
const getRelayUrls = () => [url]
const getFilter = (searchTerm: string): Filter => {
if (scope === "room" && h) {
return {kinds: [MESSAGE], "#h": [h], search: searchTerm}
return {kinds: CONTENT_KINDS, "#h": [h], search: searchTerm}
}
return {kinds: [MESSAGE], search: searchTerm}
return {kinds: CONTENT_KINDS, search: searchTerm}
}
const search = debounce(300, async (searchTerm: string, searchId: number) => {
const search = debounce(300, async (searchTerm: string) => {
controller?.abort()
if (!searchTerm.trim()) {
@@ -104,32 +86,23 @@
filters: [getFilter(searchTerm.trim())],
})
if (searchId === currentSearchId) {
results = sortEventsDesc(uniqBy((event: TrustedEvent) => event.id, events))
}
results = sortEventsDesc(events)
} catch (error) {
if (
!(error instanceof DOMException && error.name === "AbortError") &&
searchId === currentSearchId
) {
if (!(error instanceof DOMException && error.name === "AbortError")) {
results = []
}
} finally {
if (searchId === currentSearchId) {
loading = false
}
loading = false
}
})
const onInput = () => {
currentSearchId += 1
void search(term, currentSearchId)
void search(term)
}
const setScope = (value: SearchScope) => {
scope = value
currentSearchId += 1
void search(term, currentSearchId)
void search(term)
}
const eventsByAge = $derived(groupBy(e => getAgeSection(e.created_at), results))
@@ -195,11 +168,7 @@
bind:value={term}
class="min-w-0 grow"
type="text"
placeholder={scope === "room"
? "Search this room..."
: scope === "space"
? "Search this space..."
: "Search everything..."}
placeholder={scope === "room" ? "Search this room..." : "Search this space..."}
oninput={onInput} />
</label>
<div class="flex flex-wrap gap-1">
@@ -207,7 +176,7 @@
<Button
class={value === scope ? "btn btn-neutral btn-xs" : "btn btn-ghost btn-xs"}
onclick={() => setScope(value)}>
{value === "room" ? "Room" : value === "space" ? "Space" : "Everything"}
{value === "room" ? "Room" : "Space"}
</Button>
{/each}
</div>
@@ -216,10 +185,8 @@
{#if !term}
<p class="text-sm opacity-70">
{scope === "room"
? "Search for messages in this room."
: scope === "space"
? "Search for messages in this space."
: "Search across your configured search relays."}
? "Search for content in this room."
: "Search for content in this space."}
</p>
{:else if loading}
<p class="text-sm opacity-70">Searching...</p>