Browse Source

corrected my-notes going blank on refresh and showing when no one is logged-in

master
silberengel 7 months ago
parent
commit
0e720f70e8
  1. 4
      src/lib/components/Navigation.svelte
  2. 31
      src/lib/ndk.ts
  3. 37
      src/routes/my-notes/+page.svelte

4
src/lib/components/Navigation.svelte

@ -31,7 +31,9 @@
<NavLi href="/visualize">Visualize</NavLi> <NavLi href="/visualize">Visualize</NavLi>
<NavLi href="/start">Getting Started</NavLi> <NavLi href="/start">Getting Started</NavLi>
<NavLi href="/events">Events</NavLi> <NavLi href="/events">Events</NavLi>
<NavLi href="/my-notes">My Notes</NavLi> {#if userState.signedIn}
<NavLi href="/my-notes">My Notes</NavLi>
{/if}
<NavLi href="/about">About</NavLi> <NavLi href="/about">About</NavLi>
<NavLi href="/contact">Contact</NavLi> <NavLi href="/contact">Contact</NavLi>
<NavLi> <NavLi>

31
src/lib/ndk.ts

@ -91,20 +91,8 @@ function clearPersistentRelaySet(): void {
} }
} }
// Subscribe to userStore changes and update ndkSignedIn accordingly // AI-NOTE: userStore subscription moved to initNdk function to prevent initialization errors
userStore.subscribe(async (userState) => { // The subscription will be set up after userStore is properly initialized
ndkSignedIn.set(userState.signedIn);
// Refresh relay stores when user state changes
const ndk = get(ndkInstance);
if (ndk) {
try {
await refreshRelayStores(ndk);
} catch (error) {
console.warn('[NDK.ts] Failed to refresh relay stores on user state change:', error);
}
}
});
/** /**
* Custom authentication policy that handles NIP-42 authentication manually * Custom authentication policy that handles NIP-42 authentication manually
@ -655,6 +643,21 @@ export function initNdk(): NDK {
attemptConnection(); attemptConnection();
// AI-NOTE: Set up userStore subscription after NDK initialization to prevent initialization errors
userStore.subscribe(async (userState) => {
ndkSignedIn.set(userState.signedIn);
// Refresh relay stores when user state changes
const ndk = get(ndkInstance);
if (ndk) {
try {
await refreshRelayStores(ndk);
} catch (error) {
console.warn('[NDK.ts] Failed to refresh relay stores on user state change:', error);
}
}
});
return ndk; return ndk;
} }

37
src/routes/my-notes/+page.svelte

@ -1,5 +1,6 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte"; import { onMount } from "svelte";
import { goto } from "$app/navigation";
import { userStore } from "$lib/stores/userStore"; import { userStore } from "$lib/stores/userStore";
import { ndkInstance } from "$lib/ndk"; import { ndkInstance } from "$lib/ndk";
import type { NDKEvent } from "@nostr-dev-kit/ndk"; import type { NDKEvent } from "@nostr-dev-kit/ndk";
@ -9,26 +10,26 @@
import asciidoctor from "asciidoctor"; import asciidoctor from "asciidoctor";
import { postProcessAsciidoctorHtml } from "$lib/utils/markup/asciidoctorPostProcessor"; import { postProcessAsciidoctorHtml } from "$lib/utils/markup/asciidoctorPostProcessor";
let events: NDKEvent[] = []; let events: NDKEvent[] = $state([]);
let loading = true; let loading = $state(true);
let error: string | null = null; let error: string | null = $state(null);
let showTags: Record<string, boolean> = {}; let showTags: Record<string, boolean> = $state({});
let renderedContent: Record<string, string> = {}; let renderedContent: Record<string, string> = $state({});
// Tag type and tag filter state // Tag type and tag filter state
const tagTypes = ["t", "title", "m", "w"]; // 'm' is MIME type const tagTypes = ["t", "title", "m", "w"]; // 'm' is MIME type
let selectedTagTypes: Set<string> = new Set(); let selectedTagTypes: Set<string> = $state(new Set());
let tagTypeLabels: Record<string, string> = { let tagTypeLabels: Record<string, string> = {
t: "hashtag", t: "hashtag",
title: "", title: "",
m: "mime", m: "mime",
w: "wiki", w: "wiki",
}; };
let tagFilter: Set<string> = new Set(); let tagFilter: Set<string> = $state(new Set());
// Unique tags by type // Unique tags by type
let uniqueTagsByType: Record<string, Set<string>> = {}; let uniqueTagsByType: Record<string, Set<string>> = $state({});
let allUniqueTags: Set<string> = new Set(); let allUniqueTags: Set<string> = $state(new Set());
async function fetchMyNotes() { async function fetchMyNotes() {
loading = true; loading = true;
@ -132,7 +133,7 @@
} }
// Compute which tags to show in the filter // Compute which tags to show in the filter
$: tagsToShow = (() => { let tagsToShow = $derived.by(() => {
if (selectedTagTypes.size === 0) { if (selectedTagTypes.size === 0) {
return []; return [];
} }
@ -143,10 +144,10 @@
} }
} }
return Array.from(tags).sort(); return Array.from(tags).sort();
})(); });
// Compute filtered events // Compute filtered events
$: filteredEvents = (() => { let filteredEvents = $derived.by(() => {
if (selectedTagTypes.size === 0 && tagFilter.size === 0) { if (selectedTagTypes.size === 0 && tagFilter.size === 0) {
return events; return events;
} }
@ -164,7 +165,17 @@
// Otherwise, event must have at least one of the selected tags // Otherwise, event must have at least one of the selected tags
return relevantTags.some((tag) => tagFilter.has(tag[1])); return relevantTags.some((tag) => tagFilter.has(tag[1]));
}); });
})(); });
// AI-NOTE: Check authentication status immediately and redirect if not logged in
$effect(() => {
const user = get(userStore);
if (!user.signedIn) {
// Redirect to home page if not logged in
goto('/');
return;
}
});
onMount(fetchMyNotes); onMount(fetchMyNotes);
</script> </script>

Loading…
Cancel
Save