Frage / Thema:
-Frage / Thema:
+Meine Idee hinzufügen
-Loading...
- {/if} +Meine Idee hinzufügen
+Loading...
+ {/if}diff --git a/src/routes/q/[id]/+page.svelte b/src/routes/q/[id]/+page.svelte index 7b3cf91..24551fb 100644 --- a/src/routes/q/[id]/+page.svelte +++ b/src/routes/q/[id]/+page.svelte @@ -15,7 +15,7 @@ sanitizer: DOMPurify.sanitize }); function submitComment() { - if(!$comment) { + if (!$comment) { return; } const commentEvent = new NDKEvent($ndk, { @@ -27,10 +27,31 @@ comment.set(''); } + /** + * Filters an array of objects to keep only unique objects based on their id property + * @param {Array} array - The array of objects to filter + * @returns {Array} - A new array containing only unique objects by id + */ + function getUniqueById(array) { + // Create a Map to track unique objects by their id + const uniqueMap = new Map(); + + // Loop through the array and add each object to the map with its id as the key + // This automatically overrides any previous entries with the same id + array.forEach((item) => { + if (item && item.id !== undefined) { + uniqueMap.set(item.id, item); + } + }); + + // Convert the Map values back to an array + return Array.from(uniqueMap.values()); + } + let comment = writable(''); let comments = writable([]); - let question = writable() - const [kind, pubkey, d] = data.id.split(":") + let question = writable(); + const [kind, pubkey, d] = data.id.split(':'); const showReactions = writable(false); $effect(() => { @@ -39,83 +60,83 @@ const sub = $ndk.subscribe({ kinds: [2222], '#E': [data.id] }); sub.on('event', (event) => { console.log(event); - $comments = Array.from(new Set([...$comments, event])); + const unique = getUniqueById([...$comments, event]); + $comments = [...unique]; console.log(`${event.content}`); }); - const questionSub = $ndk.subscribe({ kinds: [Number(kind)], authors: [pubkey], "#d": [d] }); + const questionSub = $ndk.subscribe({ kinds: [Number(kind)], authors: [pubkey], '#d': [d] }); questionSub.on('event', (event) => { $question = event; - if ($question?.tags.find(t => t[0] === "reactions")[1] === "true") { - console.log("reactions enabled") - $showReactions = "true" - } - - }); - + if ($question?.tags.find((t) => t[0] === 'reactions')[1] === 'true') { + console.log('reactions enabled'); + $showReactions = 'true'; + } + }); } }); async function startReactions(event) { - console.log("start reactions", event) - let updatedEvent = new NDKEvent($ndk, event ) - console.log("updated event", updatedEvent) - updatedEvent.tags.push(['reactions', "true"]) + console.log('start reactions', event); + let updatedEvent = new NDKEvent($ndk, event); + console.log('updated event', updatedEvent); + updatedEvent.tags.push(['reactions', 'true']); await updatedEvent.publishReplaceable(); - console.log("sent!", updatedEvent) - $showReactions = "true" + console.log('sent!', updatedEvent); + $showReactions = 'true'; } -
Loading...
- {/if} +Loading...
+ {/if}