@ -7,11 +7,15 @@
import EventDetails from '$lib/components/EventDetails.svelte';
import EventDetails from '$lib/components/EventDetails.svelte';
import RelayActions from '$lib/components/RelayActions.svelte';
import RelayActions from '$lib/components/RelayActions.svelte';
import CommentBox from '$lib/components/CommentBox.svelte';
import CommentBox from '$lib/components/CommentBox.svelte';
import { userBadge } from '$lib/snippets/UserSnippets.svelte';
import { getMatchingTags , toNpub } from '$lib/utils/nostrUtils';
let loading = $state(false);
let loading = $state(false);
let error = $state< string | null > (null);
let error = $state< string | null > (null);
let searchValue = $state< string | null > (null);
let searchValue = $state< string | null > (null);
let dTagValue = $state< string | null > (null);
let event = $state< NDKEvent | null > (null);
let event = $state< NDKEvent | null > (null);
let searchResults = $state< NDKEvent [ ] > ([]);
let profile = $state< {
let profile = $state< {
name?: string;
name?: string;
display_name?: string;
display_name?: string;
@ -27,6 +31,7 @@
function handleEventFound(newEvent: NDKEvent) {
function handleEventFound(newEvent: NDKEvent) {
event = newEvent;
event = newEvent;
searchResults = [];
if (newEvent.kind === 0) {
if (newEvent.kind === 0) {
try {
try {
profile = JSON.parse(newEvent.content);
profile = JSON.parse(newEvent.content);
@ -38,10 +43,33 @@
}
}
}
}
function handleSearchResults(results: NDKEvent[]) {
searchResults = results;
event = null;
profile = null;
}
function getSummary(event: NDKEvent): string | undefined {
return getMatchingTags(event, 'summary')[0]?.[1];
}
function getDeferrelNaddr(event: NDKEvent): string | undefined {
// Look for a 'deferrel' tag, e.g. ['deferrel', 'naddr1...']
return getMatchingTags(event, 'deferrel')[0]?.[1];
}
$effect(() => {
$effect(() => {
const id = $page.url.searchParams.get('id');
const id = $page.url.searchParams.get('id');
const dTag = $page.url.searchParams.get('d');
if (id !== searchValue) {
if (id !== searchValue) {
searchValue = id;
searchValue = id;
dTagValue = null;
}
if (dTag !== dTagValue) {
dTagValue = dTag;
searchValue = null;
}
}
});
});
@ -60,9 +88,19 @@
< P class = "mb-3" >
< P class = "mb-3" >
Use this page to view any event (npub, nprofile, nevent, naddr, note, pubkey, or eventID).
Use this page to view any event (npub, nprofile, nevent, naddr, note, pubkey, or eventID).
You can also search for events by d-tag using the format "d:tag-name".
< / P >
< / P >
< EventSearch { loading } { error } { searchValue } { event } onEventFound = { handleEventFound } / >
< EventSearch
{ loading }
{ error }
{ searchValue }
{ dTagValue }
{ event }
onEventFound={ handleEventFound }
onSearchResults={ handleSearchResults }
/>
{ #if event }
{ #if event }
< EventDetails { event } { profile } { searchValue } />
< EventDetails { event } { profile } { searchValue } />
< RelayActions { event } />
< RelayActions { event } />
@ -77,5 +115,57 @@
< / div >
< / div >
{ /if }
{ /if }
{ /if }
{ /if }
{ #if searchResults . length > 0 }
< div class = "mt-8" >
< Heading tag = "h2" class = "h-leather mb-4" >
Search Results for d-tag: "{ dTagValue } " ({ searchResults . length } events)
< / Heading >
< div class = "space-y-4" >
{ #each searchResults as result , index }
< button
class="w-full text-left border border-gray-300 dark:border-gray-600 rounded-lg p-4 bg-white dark:bg-primary-900/70 hover:bg-gray-100 dark:hover:bg-primary-800 focus:bg-gray-100 dark:focus:bg-primary-800 focus:outline-none focus:ring-2 focus:ring-primary-500 transition-colors overflow-hidden"
onclick={() => handleEventFound ( result )}
>
< div class = "flex flex-col gap-1" >
< div class = "flex items-center gap-2 mb-1" >
< span class = "font-medium text-gray-800 dark:text-gray-100" > Event { index + 1 } </ span >
< span class = "text-xs text-gray-600 dark:text-gray-400" > Kind: { result . kind } </ span >
< span class = "text-xs text-gray-600 dark:text-gray-400" >
{ @render userBadge ( toNpub ( result . pubkey ) as string , undefined )}
< / span >
< span class = "text-xs text-gray-500 dark:text-gray-400 ml-auto" >
{ result . created_at ? new Date ( result . created_at * 1000 ). toLocaleDateString () : 'Unknown date' }
< / span >
< / div >
{ #if getSummary ( result )}
< div class = "text-sm text-primary-900 dark:text-primary-200 mb-1 line-clamp-2" >
{ getSummary ( result )}
< / div >
{ /if }
{ #if getDeferrelNaddr ( result )}
< div class = "text-xs text-primary-800 dark:text-primary-300 mb-1" >
Read
< a
class="underline text-primary-700 dark:text-primary-400 hover:text-primary-900 dark:hover:text-primary-200 break-all"
href={ '/publications?d=' + encodeURIComponent ( dTagValue || '' )}
onclick={ e => e . stopPropagation ()}
tabindex="0"
>
{ getDeferrelNaddr ( result )}
< / a >
< / div >
{ /if }
{ #if result . content }
< div class = "text-sm text-gray-800 dark:text-gray-200 mt-1 line-clamp-2 break-words" >
{ result . content . slice ( 0 , 200 )}{ result . content . length > 200 ? '...' : '' }
< / div >
{ /if }
< / div >
< / button >
{ /each }
< / div >
< / div >
{ /if }
< / main >
< / main >
< / div >
< / div >