From c30bd1d5199a386b1f8a580d8884989fd85d625e Mon Sep 17 00:00:00 2001 From: silberengel Date: Mon, 14 Jul 2025 21:30:38 +0200 Subject: [PATCH] Fix the page redirection to sustain the Amber sessions --- src/lib/components/CommentBox.svelte | 10 ++-- src/lib/components/EventDetails.svelte | 45 +++++++++++++--- src/lib/components/LoginMenu.svelte | 3 +- src/lib/components/Preview.svelte | 1 + src/lib/components/PublicationHeader.svelte | 32 +++++++++--- src/lib/components/util/Details.svelte | 25 ++++++--- src/lib/snippets/UserSnippets.svelte | 58 +++++++++++++++++---- src/lib/stores/userStore.ts | 2 +- src/lib/utils/nostrUtils.ts | 11 ++-- src/routes/about/+page.svelte | 5 +- src/routes/events/+page.svelte | 9 ++-- src/routes/start/+page.svelte | 21 ++++---- 12 files changed, 164 insertions(+), 58 deletions(-) diff --git a/src/lib/components/CommentBox.svelte b/src/lib/components/CommentBox.svelte index 552ad5f..ab37d19 100644 --- a/src/lib/components/CommentBox.svelte +++ b/src/lib/components/CommentBox.svelte @@ -263,11 +263,15 @@ {/if} {#if success} + {@const s = success} - Comment published successfully to {success.relay}! - + Comment published successfully to {s.relay}! + {/if} diff --git a/src/lib/components/EventDetails.svelte b/src/lib/components/EventDetails.svelte index 17c3d0c..1672820 100644 --- a/src/lib/components/EventDetails.svelte +++ b/src/lib/components/EventDetails.svelte @@ -8,6 +8,8 @@ import type { NDKEvent } from '$lib/utils/nostrUtils'; import { getMatchingTags } from '$lib/utils/nostrUtils'; import ProfileHeader from "$components/cards/ProfileHeader.svelte"; + import { goto } from '$app/navigation'; + import { onMount } from 'svelte'; const { event, profile = null, searchValue = null } = $props<{ event: NDKEvent; @@ -27,6 +29,12 @@ let showFullContent = $state(false); let parsedContent = $state(''); let contentPreview = $state(''); + let authorTag: string = $derived(getMatchingTags(event, 'author')[0]?.[1] ?? ''); + let pTag: string = $derived(getMatchingTags(event, 'p')[0]?.[1] ?? ''); + + function isValidNostrPubkey(str: string): boolean { + return /^[a-f0-9]{64}$/i.test(str) || (str.startsWith('npub1') && str.length >= 59 && str.length <= 63); + } function getEventTitle(event: NDKEvent): string { return getMatchingTags(event, 'title')[0]?.[1] || 'Untitled'; @@ -48,9 +56,9 @@ function renderTag(tag: string[]): string { if (tag[0] === 'a' && tag.length > 1) { const [kind, pubkey, d] = tag[1].split(':'); - return `a:${tag[1]}`; + return ``; } else if (tag[0] === 'e' && tag.length > 1) { - return `e:${tag[1]}`; + return ``; } else { return `${tag[0]}:${tag[1]}`; } @@ -100,6 +108,21 @@ const norm = (s: string) => s.replace(/^nostr:/, '').toLowerCase(); return norm(value) === norm(searchValue); } + + onMount(() => { + function handleInternalLinkClick(event: MouseEvent) { + const target = event.target as HTMLElement; + if (target.tagName === 'A') { + const href = (target as HTMLAnchorElement).getAttribute('href'); + if (href && href.startsWith('/')) { + event.preventDefault(); + goto(href); + } + } + } + document.addEventListener('click', handleInternalLinkClick); + return () => document.removeEventListener('click', handleInternalLinkClick); + });
@@ -108,11 +131,19 @@ {/if}
- {#if toNpub(event.pubkey)} - Author: {@render userBadge(toNpub(event.pubkey) as string, profile?.display_name || event.pubkey)} - {:else} - Author: {profile?.display_name || event.pubkey} - {/if} + Author: + {#if authorTag && pTag && isValidNostrPubkey(pTag)} + {authorTag} {@render userBadge(pTag, '')} + {:else if authorTag} + {authorTag} + {:else if pTag && isValidNostrPubkey(pTag)} + {@render userBadge(pTag, '')} + {:else if authorTag} + {authorTag} + {:else} + unknown + {/if} +
diff --git a/src/lib/components/LoginMenu.svelte b/src/lib/components/LoginMenu.svelte index 921d5f6..c5a037a 100644 --- a/src/lib/components/LoginMenu.svelte +++ b/src/lib/components/LoginMenu.svelte @@ -5,6 +5,7 @@ import { get } from 'svelte/store'; import NDK, { NDKNip46Signer, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk'; import { onMount } from 'svelte'; + import { goto } from '$app/navigation'; // UI state let isLoadingExtension: boolean = $state(false); @@ -230,7 +231,7 @@
  • diff --git a/src/lib/components/util/Details.svelte b/src/lib/components/util/Details.svelte index e776e9d..f2fe837 100644 --- a/src/lib/components/util/Details.svelte +++ b/src/lib/components/util/Details.svelte @@ -11,10 +11,8 @@ let { event, isModal = false } = $props(); let title: string = $derived(getMatchingTags(event, 'title')[0]?.[1]); - let author: string = $derived(getMatchingTags(event, 'author')[0]?.[1] ?? 'unknown'); let version: string = $derived(getMatchingTags(event, 'version')[0]?.[1] ?? '1'); let image: string = $derived(getMatchingTags(event, 'image')[0]?.[1] ?? null); - let originalAuthor: string = $derived(getMatchingTags(event, 'p')[0]?.[1] ?? null); let summary: string = $derived(getMatchingTags(event, 'summary')[0]?.[1] ?? null); let type: string = $derived(getMatchingTags(event, 'type')[0]?.[1] ?? null); let language: string = $derived(getMatchingTags(event, 'l')[0]?.[1] ?? null); @@ -25,6 +23,12 @@ let rootId: string = $derived(getMatchingTags(event, 'd')[0]?.[1] ?? null); let kind = $derived(event.kind); + let authorTag: string = $derived(getMatchingTags(event, 'author')[0]?.[1] ?? ''); + let pTag: string = $derived(getMatchingTags(event, 'p')[0]?.[1] ?? ''); + + function isValidNostrPubkey(str: string): boolean { + return /^[a-f0-9]{64}$/i.test(str) || (str.startsWith('npub1') && str.length >= 59 && str.length <= 63); + } @@ -32,7 +36,8 @@
    {#if !isModal}
    -

    {@render userBadge(event.pubkey, author)}

    + +

    {@render userBadge(event.pubkey, '')}

    {/if} @@ -46,10 +51,16 @@

    {title}

    by - {#if originalAuthor !== null} - {@render userBadge(originalAuthor, author)} + {#if authorTag && pTag && isValidNostrPubkey(pTag)} + {authorTag} {@render userBadge(pTag, '')} + {:else if authorTag} + {authorTag} + {:else if pTag && isValidNostrPubkey(pTag)} + {@render userBadge(pTag, '')} + {:else if authorTag} + {authorTag} {:else} - {author} + unknown {/if}

    {#if version !== '1' } @@ -81,7 +92,7 @@ {:else} Author: {/if} - {@render userBadge(event.pubkey, author)} + {@render userBadge(event.pubkey, '')}
    diff --git a/src/lib/snippets/UserSnippets.svelte b/src/lib/snippets/UserSnippets.svelte index d8c960e..7fc5632 100644 --- a/src/lib/snippets/UserSnippets.svelte +++ b/src/lib/snippets/UserSnippets.svelte @@ -1,18 +1,58 @@ {#snippet userBadge(identifier: string, displayText: string | undefined)} - {#if toNpub(identifier)} - {#await createProfileLinkWithVerification(toNpub(identifier) as string, displayText)} - {@html createProfileLink(toNpub(identifier) as string, displayText)} - {:then html} - {@html html} - {:catch} - {@html createProfileLink(toNpub(identifier) as string, displayText)} - {/await} + {@const npub = toNpub(identifier)} + {#if npub} + {#if !displayText || displayText.trim().toLowerCase() === 'unknown'} + {#await getUserMetadata(npub) then profile} + {@const p = profile as NostrProfileWithLegacy} + + + + {:catch} + + + + {/await} + {:else} + {#await createProfileLinkWithVerification(npub as string, displayText)} + + + + {:then html} + + + {@html html.replace(/([\s\S]*<\/a>)/, '').trim()} + + {:catch} + + + + {/await} + {/if} {:else} {displayText ?? ''} {/if} diff --git a/src/lib/stores/userStore.ts b/src/lib/stores/userStore.ts index 5c95ff8..3e3d77f 100644 --- a/src/lib/stores/userStore.ts +++ b/src/lib/stores/userStore.ts @@ -168,7 +168,7 @@ export async function loginWithAmber(amberSigner: NDKSigner, user: NDKUser) { if (!ndk) throw new Error('NDK not initialized'); // Only clear previous login state after successful login const npub = user.npub; - const profile = await getUserMetadata(npub); + const profile = await getUserMetadata(npub, true); // Force fresh fetch const [persistedInboxes, persistedOutboxes] = getPersistedRelays(user); for (const relay of persistedInboxes) { ndk.addExplicitRelay(relay); diff --git a/src/lib/utils/nostrUtils.ts b/src/lib/utils/nostrUtils.ts index b85dbb9..faa001b 100644 --- a/src/lib/utils/nostrUtils.ts +++ b/src/lib/utils/nostrUtils.ts @@ -46,11 +46,11 @@ function escapeHtml(text: string): string { /** * Get user metadata for a nostr identifier (npub or nprofile) */ -export async function getUserMetadata(identifier: string): Promise { +export async function getUserMetadata(identifier: string, force = false): Promise { // Remove nostr: prefix if present const cleanId = identifier.replace(/^nostr:/, ''); - if (npubCache.has(cleanId)) { + if (!force && npubCache.has(cleanId)) { return npubCache.get(cleanId)!; } @@ -111,7 +111,8 @@ export function createProfileLink(identifier: string, displayText: string | unde const defaultText = `${cleanId.slice(0, 8)}...${cleanId.slice(-4)}`; const escapedText = escapeHtml(displayText || defaultText); - return `@${escapedText}`; + // Remove target="_blank" for internal navigation + return `@${escapedText}`; } /** @@ -167,9 +168,9 @@ export async function createProfileLinkWithVerification(identifier: string, disp const type = nip05.endsWith('edu') ? 'edu' : 'standard'; switch (type) { case 'edu': - return `@${displayIdentifier}${graduationCapSvg}`; + return `@${displayIdentifier}${graduationCapSvg}`; case 'standard': - return `@${displayIdentifier}${badgeCheckSvg}`; + return `@${displayIdentifier}${badgeCheckSvg}`; } } /** diff --git a/src/routes/about/+page.svelte b/src/routes/about/+page.svelte index a6badf3..b5b7c09 100644 --- a/src/routes/about/+page.svelte +++ b/src/routes/about/+page.svelte @@ -1,6 +1,7 @@ diff --git a/src/routes/start/+page.svelte b/src/routes/start/+page.svelte index 05d0776..04177de 100644 --- a/src/routes/start/+page.svelte +++ b/src/routes/start/+page.svelte @@ -1,5 +1,6 @@