diff --git a/src/lib/stores/Issue.ts b/src/lib/stores/Issue.ts index d654801..e315381 100644 --- a/src/lib/stores/Issue.ts +++ b/src/lib/stores/Issue.ts @@ -1,4 +1,4 @@ -import { NDKRelaySet, type NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk' +import { NDKRelaySet, NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk' import { writable, type Writable } from 'svelte/store' import { base_relays, ndk } from './ndk' import { type IssueFull, full_defaults } from '$lib/components/issues/type' @@ -28,7 +28,14 @@ let sub_replies: NDKSubscription const sub_replies_to_replies: NDKSubscription[] = [] -export const ensureIssueFull = (repo_a: string, issue_id: string) => { +export const ensureIssueFull = ( + repo_a: string, + issue_id_or_event: string | NDKEvent +) => { + const issue_id = + typeof issue_id_or_event === 'string' + ? issue_id_or_event + : issue_id_or_event.id if (selected_issue_id == issue_id) return if (issue_id == '') { selected_issue_full.set({ ...full_defaults }) @@ -64,54 +71,59 @@ export const ensureIssueFull = (repo_a: string, issue_id: string) => { ? repo.relays : [...base_relays].concat(repo ? repo.relays : []) - sub = ndk.subscribe( - { - ids: [issue_id], - limit: 100, - }, - { - closeOnEose: false, - }, - NDKRelaySet.fromRelayUrls(relays_to_use, ndk) - ) - - sub.on('event', (event: NDKEvent) => { + const setEvent = (event: NDKEvent) => { try { - if (event.id == issue_id) { - selected_issue_full.update((full) => { - return { - ...full, - issue_event: event, - summary: { - ...full.summary, - title: extractIssueTitle(event.content), - descritpion: extractIssueDescription(event.content), - created_at: event.created_at, - comments: 0, - author: event.pubkey, - loading: false, - }, - } - }) - } + selected_issue_full.update((full) => { + return { + ...full, + issue_event: event, + summary: { + ...full.summary, + title: extractIssueTitle(event.content), + descritpion: extractIssueDescription(event.content), + created_at: event.created_at, + comments: 0, + author: event.pubkey, + loading: false, + }, + } + }) } catch {} - }) + } + if (typeof issue_id_or_event !== 'string') { + setEvent(issue_id_or_event) + } else { + sub = ndk.subscribe( + { + ids: [issue_id], + limit: 100, + }, + { + closeOnEose: false, + }, + NDKRelaySet.fromRelayUrls(relays_to_use, ndk) + ) + + sub.on('event', (event: NDKEvent) => { + if (event.id == issue_id) setEvent(event) + }) - sub.on('eose', () => { - selected_issue_full.update((full) => { - const updated = { - ...full, - summary: { - ...full.summary, - loading: false, - }, - } - if (full.loading === false) { - r({ ...updated }) - } - return updated + sub.on('eose', () => { + selected_issue_full.update((full) => { + const updated = { + ...full, + summary: { + ...full.summary, + loading: false, + }, + } + if (full.loading === false) { + r({ ...updated }) + } + return updated + }) }) - }) + } sub_replies = ndk.subscribe( { diff --git a/src/lib/stores/Proposal.ts b/src/lib/stores/Proposal.ts index fb966ce..c43fc33 100644 --- a/src/lib/stores/Proposal.ts +++ b/src/lib/stores/Proposal.ts @@ -28,7 +28,14 @@ let sub_replies: NDKSubscription const sub_replies_to_replies: NDKSubscription[] = [] -export const ensureProposalFull = (repo_a: string, proposal_id: string) => { +export const ensureProposalFull = ( + repo_a: string, + proposal_id_or_event: string | NDKEvent +) => { + const proposal_id = + typeof proposal_id_or_event === 'string' + ? proposal_id_or_event + : proposal_id_or_event.id if (selected_proposal_id == proposal_id) return if (proposal_id == '') { selected_proposal_full.set({ ...full_defaults }) @@ -64,59 +71,65 @@ export const ensureProposalFull = (repo_a: string, proposal_id: string) => { ? repo.relays : [...base_relays].concat(repo ? repo.relays : []) - sub = ndk.subscribe( - { - ids: [proposal_id], - limit: 100, - }, - { - closeOnEose: false, - }, - NDKRelaySet.fromRelayUrls(relays_to_use, ndk) - ) - - sub.on('event', (event: NDKEvent) => { + const setEvent = (event: NDKEvent) => { try { - if (event.id == proposal_id) { - selected_proposal_full.update((full) => { - return { - ...full, - proposal_event: event, - summary: { - ...full.summary, - title: ( - event.tagValue('name') || - event.tagValue('description') || - extractPatchMessage(event.content) || - '' - ).split('\n')[0], - descritpion: event.tagValue('description') || '', - created_at: event.created_at, - comments: 0, - author: event.pubkey, - loading: false, - }, - } - }) - } + selected_proposal_full.update((full) => { + return { + ...full, + proposal_event: event, + summary: { + ...full.summary, + title: ( + event.tagValue('name') || + event.tagValue('description') || + extractPatchMessage(event.content) || + '' + ).split('\n')[0], + descritpion: event.tagValue('description') || '', + created_at: event.created_at, + comments: 0, + author: event.pubkey, + loading: false, + }, + } + }) } catch {} - }) + } - sub.on('eose', () => { - selected_proposal_full.update((full) => { - const updated = { - ...full, - summary: { - ...full.summary, - loading: false, - }, - } - if (full.loading === false) { - r({ ...updated }) - } - return updated + if (typeof proposal_id_or_event !== 'string') { + setEvent(proposal_id_or_event) + } else { + sub = ndk.subscribe( + { + ids: [proposal_id], + limit: 100, + }, + { + closeOnEose: false, + }, + NDKRelaySet.fromRelayUrls(relays_to_use, ndk) + ) + + sub.on('event', (event: NDKEvent) => { + if (event.id == proposal_id) setEvent(event) }) - }) + + sub.on('eose', () => { + selected_proposal_full.update((full) => { + const updated = { + ...full, + summary: { + ...full.summary, + loading: false, + }, + } + if (full.loading === false) { + r({ ...updated }) + } + return updated + }) + }) + } sub_replies = ndk.subscribe( { diff --git a/src/routes/e/[nostr_ref]/+page.svelte b/src/routes/e/[nostr_ref]/+page.svelte index 40ea70c..aaeaaf2 100644 --- a/src/routes/e/[nostr_ref]/+page.svelte +++ b/src/routes/e/[nostr_ref]/+page.svelte @@ -45,10 +45,10 @@ ) } else { if (event.kind === issue_kind) { - ensureIssueFull(a, id) + ensureIssueFull(a, event) goto(`/r/${aToNaddr(a)}/issues/${nip19.noteEncode(id)}`) } else if (event.kind === patch_kind) { - ensureProposalFull(a, id) + ensureProposalFull(a, event) goto(`/r/${aToNaddr(a)}/proposals/${nip19.noteEncode(id)}`) } else { showError()