Skip to content

Commit

Permalink
relay list at the end of articles and relay tab pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Sep 14, 2023
1 parent 087061c commit d6e5b6f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
21 changes: 18 additions & 3 deletions src/lib/cards/Article.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import { afterUpdate, onMount } from 'svelte';
import type { Event } from 'nostr-tools';
import { cachingSub, cachedArticles, userPreferredRelays } from '$lib/nostr';
import { cachingSub, cachedArticles, userPreferredRelays, getRelaysForEvent } from '$lib/nostr';
import { formatDate, next } from '$lib/utils';
import { parse } from '$lib/articleParser.js';
import type { SearchTab, Tab } from '$lib/types';
import type { RelayTab, SearchTab, Tab } from '$lib/types';
import { page } from '$app/stores';
import UserLabel from '$components/UserLabel.svelte';
Expand Down Expand Up @@ -60,6 +60,13 @@
else replaceSelf(nextTab);
}
function openRelay(relay: string, ev: MouseEvent) {
console.log('navigating to', relay);
let relayTab: RelayTab = { id: next(), type: 'relay', data: relay };
if (ev.button === 1) createChild(relayTab);
else replaceSelf(relayTab);
}
onMount(() => {
let cached = cachedArticles.get(eventId);
if (cached) {
Expand Down Expand Up @@ -109,11 +116,19 @@
>{#if copied}Copied!{:else}Share{/if}</a
>
&nbsp;• &nbsp;
<a class="cursor-pointer" on:mouseup|preventDefault={seeOthers}>Others</a>
<a class="cursor-pointer" on:mouseup|preventDefault={seeOthers}>Versions</a>
</span>

<!-- Content -->
{@html parse(event?.content)}

<div class="mt-4 text-indigo-900 drop-shadow">
{#each getRelaysForEvent(event) as r}
<span on:mouseup|preventDefault={openRelay.bind(null, r)} class="block cursor-pointer"
>{new URL(r).host}</span
>
{/each}
</div>
{/if}
</article>
</div>
18 changes: 11 additions & 7 deletions src/lib/cards/Relay.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
let tried = false;
onMount(() => {
setTimeout(() => {
tried = true;
}, 1500);
return cachingSub(
`relay-${tab.data}`,
tab.data,
[tab.data],
{ kinds: [wikiKind], limit: 25 },
handleUpdate,
(events) => {
tried = true;
results = events;
},
getA
);
function handleUpdate(events: Event[]) {
results = events;
}
});
function openArticle(result: Event, ev: MouseEvent) {
Expand Down Expand Up @@ -68,11 +72,11 @@
</p>
</div>
{/each}
{#if tried}
{#if tried && results.length === 0}
<div class="px-4 py-5 bg-white border border-gray-300 rounded-lg mt-2 min-h-[48px]">
<p class="mb-2">No articles found in this relay.</p>
</div>
{:else}
{:else if !tried}
<div class="px-4 py-5 rounded-lg mt-2 min-h-[48px]">Loading...</div>
{/if}
</div>
26 changes: 6 additions & 20 deletions src/lib/cards/Search.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import { onMount } from 'svelte';
import type { Event } from 'nostr-tools';
import { cachingSub, getA, getRelaysForEvent, userPreferredRelays, wikiKind } from '$lib/nostr';
import type { ArticleTab, RelayTab, Tab } from '$lib/types';
import { cachingSub, getA, userPreferredRelays, wikiKind } from '$lib/nostr';
import type { ArticleTab, Tab } from '$lib/types';
import { parsePlainText } from '$lib/articleParser';
import UserLabel from '$components/UserLabel.svelte';
import { next } from '$lib/utils';
Expand All @@ -18,30 +18,23 @@
onMount(() => {
setTimeout(() => {
tried = true;
}, 1000);
}, 1500);
return cachingSub(
`search-${query}`,
$userPreferredRelays,
{ kinds: [wikiKind], '#d': [query.toLowerCase()], limit: 25 },
handleUpdate,
(events) => {
results = events;
},
getA
);
function handleUpdate(events: Event[]) {
results = events;
}
});
function openArticle(result: Event, ev: MouseEvent) {
let articleTab: ArticleTab = { id: next(), type: 'article', data: result.id };
if (ev.button === 1) createChild(articleTab);
else replaceSelf(articleTab);
}
function openRelay(relay: string, ev: MouseEvent) {
let relayTab: RelayTab = { id: next(), type: 'relay', data: relay };
if (ev.button === 1) createChild(relayTab);
else replaceSelf(relayTab);
}
</script>

<div class="font-sans mx-auto p-6 lg:max-w-4xl lg:pt-6 lg:pb-28">
Expand All @@ -61,13 +54,6 @@
<span>
by <UserLabel pubkey={result.pubkey} />
</span>
<div class="text-indigo-900 drop-shadow">
{#each getRelaysForEvent(result) as r}
<span on:mouseup|preventDefault={openRelay.bind(null, r)} class="cursor-pointer"
>{new URL(r).host}</span
>
{/each}
</div>
</div>
<p class="text-xs">
{#if result.tags.find((e) => e[0] == 'summary')?.[0] && result.tags.find((e) => e[0] == 'summary')?.[1]}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/nostr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async function ensureRelay(url: string): Promise<Relay> {
}

export function getRelaysForEvent(event: Event): Iterable<string> {
return _seenOn.get(event)?.values() || [];
return _seenOn.get(event)?.keys() || [];
}

export async function broadcast(
Expand Down Expand Up @@ -222,7 +222,7 @@ export function cachingSub(
function cacheSeenOn(event: Event, relay: string) {
const relays = _seenOn.get(event) || new Set();
_seenOn.set(event, relays);
relays.add(relay);
relays.add(utils.normalizeURL(relay));
}

export async function getMetadata(pubkey: string): Promise<Metadata> {
Expand Down

0 comments on commit d6e5b6f

Please sign in to comment.