From d894c5f7431d2c320bc551b8fe1e22c78e34eeb3 Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Fri, 23 Aug 2024 09:50:22 -0500 Subject: Improve path-based event loading by d tag --- src/lib/Article.svelte | 27 ++++++++++++++------------- src/lib/components/Navigation.svelte | 2 +- src/lib/consts.ts | 2 +- src/routes/+layout.svelte | 2 +- src/routes/d/[tag]/+page.svelte | 12 ++++++++++-- src/routes/d/[tag]/+page.ts | 33 ++++++--------------------------- 6 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/lib/Article.svelte b/src/lib/Article.svelte index 0a17d41..96213e6 100644 --- a/src/lib/Article.svelte +++ b/src/lib/Article.svelte @@ -6,26 +6,27 @@ import showdown from 'showdown'; import { onMount } from 'svelte'; import { BookOutline } from 'flowbite-svelte-icons'; - import { alexandriaKinds } from './stores'; + import { zettelKinds } from './consts'; - export let event: NDKEvent | null | undefined; + export let index: NDKEvent | null | undefined; - async function getEvents(index?: NDKEvent | null | undefined): Promise> { + $: activeHash = $page.url.hash; + + const getEvents = async (index?: NDKEvent | null | undefined): Promise> => { if (index == null) { // TODO: Add error handling. } - const eventSet = await $ndk.fetchEvents({ - kinds: $alexandriaKinds, - ids: index!.getMatchingTags('e').map((value) => value[1]), + const eventIds = index!.getMatchingTags('e').map((value) => value[1]); + const events = await $ndk.fetchEvents({ + // @ts-ignore + kinds: zettelKinds, + ids: eventIds, }); - - return eventSet.values(); - } - $: eventPromises = getEvents(event); - - $: activeHash = $page.url.hash; + console.debug(`Fetched ${events.size} events from ${eventIds.length} references.`); + return events; + }; function normalizeHashPath(str: string): string { return str @@ -100,7 +101,7 @@ const converter = new showdown.Converter(); -{#await eventPromises} +{#await getEvents(index)} diff --git a/src/lib/components/Navigation.svelte b/src/lib/components/Navigation.svelte index d682bab..0534f2e 100644 --- a/src/lib/components/Navigation.svelte +++ b/src/lib/components/Navigation.svelte @@ -10,7 +10,7 @@
- +

Alexandria

diff --git a/src/lib/consts.ts b/src/lib/consts.ts index e9f766d..26f8911 100644 --- a/src/lib/consts.ts +++ b/src/lib/consts.ts @@ -1,6 +1,6 @@ export const wikiKind = 30818; export const indexKind = 30040; -export const zettelKind = 30041; +export const zettelKinds = [ 1, 30024, 30041, 30818]; export const standardRelays = [ "wss://thecitadel.nostr1.com" ]; export enum FeedType { diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index d8c30ea..7a16a5c 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -11,7 +11,7 @@ }); -
+
diff --git a/src/routes/d/[tag]/+page.svelte b/src/routes/d/[tag]/+page.svelte index 120e3f9..0771c89 100644 --- a/src/routes/d/[tag]/+page.svelte +++ b/src/routes/d/[tag]/+page.svelte @@ -1,12 +1,20 @@
-
+ {#await getIndexEvent(data.event.d)} + + {:then index} +
+ {/await}
diff --git a/src/routes/d/[tag]/+page.ts b/src/routes/d/[tag]/+page.ts index a9181ef..79712f2 100644 --- a/src/routes/d/[tag]/+page.ts +++ b/src/routes/d/[tag]/+page.ts @@ -1,32 +1,11 @@ -import { getNdkInstance } from "$lib/ndk"; -import type { NDKEvent, NDKKind } from "@nostr-dev-kit/ndk"; -import { error } from "@sveltejs/kit"; -import type { PageLoad } from "./$types"; - -// MichaelJ - 23 July 2024 - Disable server-side rendering so that the load function can use the -// browser's local storage to retrieve saved relays and the cache adapter for the NDK instance. -export const ssr = false; +import type { PageLoad } from './$types'; export const load: PageLoad = async ({ params }) => { - const ndk = getNdkInstance(); const { tag } = params; - let events: Set = new Set(); - let event: NDKEvent | null | undefined; - - try { - events = await ndk.fetchEvents({ - kinds: [30040 as NDKKind], - '#d': [tag], - }); - event = events.values().next().value; - } catch (err) { - console.error(err); - } - - if (events.size === 0) { - error(404, 'No events found with the given d tag.'); - } - - return { event }; + return { + event: { + d: tag, + } + }; }; -- cgit