Browse Source

Improve path-based event loading by d tag

master
buttercat1791 2 years ago committed by limina1
parent
commit
d894c5f743
  1. 27
      src/lib/Article.svelte
  2. 2
      src/lib/components/Navigation.svelte
  3. 2
      src/lib/consts.ts
  4. 2
      src/routes/+layout.svelte
  5. 12
      src/routes/d/[tag]/+page.svelte
  6. 31
      src/routes/d/[tag]/+page.ts

27
src/lib/Article.svelte

@ -6,26 +6,27 @@
import showdown from 'showdown'; import showdown from 'showdown';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { BookOutline } from 'flowbite-svelte-icons'; 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<IterableIterator<NDKEvent>> { $: activeHash = $page.url.hash;
const getEvents = async (index?: NDKEvent | null | undefined): Promise<Set<NDKEvent>> => {
if (index == null) { if (index == null) {
// TODO: Add error handling. // TODO: Add error handling.
} }
const eventSet = await $ndk.fetchEvents({ const eventIds = index!.getMatchingTags('e').map((value) => value[1]);
kinds: $alexandriaKinds, const events = await $ndk.fetchEvents({
ids: index!.getMatchingTags('e').map((value) => value[1]), // @ts-ignore
kinds: zettelKinds,
ids: eventIds,
}); });
return eventSet.values(); console.debug(`Fetched ${events.size} events from ${eventIds.length} references.`);
} return events;
};
$: eventPromises = getEvents(event);
$: activeHash = $page.url.hash;
function normalizeHashPath(str: string): string { function normalizeHashPath(str: string): string {
return str return str
@ -100,7 +101,7 @@
const converter = new showdown.Converter(); const converter = new showdown.Converter();
</script> </script>
{#await eventPromises} {#await getEvents(index)}
<Sidebar class='sidebar-leather fixed top-20 left-0 px-4 w-60'> <Sidebar class='sidebar-leather fixed top-20 left-0 px-4 w-60'>
<SidebarWrapper> <SidebarWrapper>
<Skeleton/> <Skeleton/>

2
src/lib/components/Navigation.svelte

@ -10,7 +10,7 @@
<Navbar class={`Navbar navbar-leather ${className}`}> <Navbar class={`Navbar navbar-leather ${className}`}>
<div class='flex flex-grow justify-between'> <div class='flex flex-grow justify-between'>
<NavBrand href='./'> <NavBrand href='/'>
<h1 class='font-serif'>Alexandria</h1> <h1 class='font-serif'>Alexandria</h1>
</NavBrand> </NavBrand>
</div> </div>

2
src/lib/consts.ts

@ -1,6 +1,6 @@
export const wikiKind = 30818; export const wikiKind = 30818;
export const indexKind = 30040; export const indexKind = 30040;
export const zettelKind = 30041; export const zettelKinds = [ 1, 30024, 30041, 30818];
export const standardRelays = [ "wss://thecitadel.nostr1.com" ]; export const standardRelays = [ "wss://thecitadel.nostr1.com" ];
export enum FeedType { export enum FeedType {

2
src/routes/+layout.svelte

@ -11,7 +11,7 @@
}); });
</script> </script>
<div class={'leather h-full w-full flex flex-col items-center'}> <div class={'leather min-h-full w-full flex flex-col items-center'}>
<Navigation class='sticky top-0' /> <Navigation class='sticky top-0' />
<slot /> <slot />
</div> </div>

12
src/routes/d/[tag]/+page.svelte

@ -1,12 +1,20 @@
<script lang="ts"> <script lang="ts">
import Article from '$lib/Article.svelte'; import Article from '$lib/Article.svelte';
import { ndk } from '$lib/ndk';
import { TextPlaceholder } from 'flowbite-svelte';
import type { PageData } from './$types'; import type { PageData } from './$types';
export let data: PageData; export let data: PageData;
$: ({ event } = data); const getIndexEvent = (d: string) => {
return $ndk.fetchEvent({ '#d': [d] });
};
</script> </script>
<main> <main>
<Article {event} /> {#await getIndexEvent(data.event.d)}
<TextPlaceholder size='xxl' />
{:then index}
<Article {index} />
{/await}
</main> </main>

31
src/routes/d/[tag]/+page.ts

@ -1,32 +1,11 @@
import { getNdkInstance } from "$lib/ndk"; import type { PageLoad } from './$types';
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;
export const load: PageLoad = async ({ params }) => { export const load: PageLoad = async ({ params }) => {
const ndk = getNdkInstance();
const { tag } = params; const { tag } = params;
let events: Set<NDKEvent> = new Set(); return {
let event: NDKEvent | null | undefined; event: {
d: tag,
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 };
}; };

Loading…
Cancel
Save