Skip to content

Commit

Permalink
fix thread calculations.
Browse files Browse the repository at this point in the history
before this we were not accounting for the fact that a comment replying directly to
(at least a) custom base event should be considered a root.

fixes #26
  • Loading branch information
fiatjaf committed Nov 26, 2023
1 parent 15e0919 commit 3397d29
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
2 changes: 1 addition & 1 deletion embeddable/embed.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import {createRoot} from 'react-dom/client'
import {NoComment} from '../lib/index.js'
import {NoComment} from '../src/NoComment.jsx'

const script = document.getElementById('nocomment')

Expand Down
80 changes: 43 additions & 37 deletions src/NoComment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,44 @@ export function NoComment({
skip,
customBase
}) {
let customBaseTag
if (customBase) {
try {
let {type, data} = nip19.decode(customBase)
switch (type) {
case 'note':
customBaseTag = {
filter: {'#e': [data]},
reference: ['e', data, '', 'root']
}
break
case 'nevent':
customBaseTag = {
filter: {'#e': [data.id]},
reference: ['e', data.id, data.relays[0] || '', 'root']
}
break
case 'naddr':
const {kind, pubkey, identifier} = data
customBaseTag = {
filter: {'#a': [`${kind}:${pubkey}:${identifier}`]},
reference: [
'a',
`${kind}:${pubkey}:${identifier}`,
data.relays[0] || '',
'root'
]
}
break
}
} catch (err) {
customBaseTag = {
filter: {'#e': [customBase]},
reference: ['e', customBase, '', 'root']
let customBaseTag = useMemo(() => {
if (customBase) {
try {
let {type, data} = nip19.decode(customBase)
switch (type) {
case 'note':
return {
ref: data,
filter: {'#e': [data]},
reference: ['e', data, '', 'root']
}
case 'nevent':
return {
ref: data.id,
filter: {'#e': [data.id]},
reference: ['e', data.id, data.relays[0] || '', 'root']
}
case 'naddr':
const {kind, pubkey, identifier} = data
return {
ref: `${kind}:${pubkey}:${identifier}`,
filter: {'#a': [`${kind}:${pubkey}:${identifier}`]},
reference: [
'a',
`${kind}:${pubkey}:${identifier}`,
data.relays[0] || '',
'root'
]
}
}
} catch (err) {
return {
filter: {'#e': [customBase]},
reference: ['e', customBase, '', 'root']
}
}
}
}
}, [customBase])

let ownerTag = null
if (owner) {
Expand Down Expand Up @@ -83,7 +84,10 @@ export function NoComment({
const pool = useRef(new SimplePool())
const [baseTag] = useDebounce(baseTagImmediate, 1000)
const [events] = useDebounce(eventsImmediate, 1000, {leading: true})
const threads = useMemo(() => computeThreads(events), [events])
const threads = useMemo(() => {
if (!baseTag) return
return computeThreads(baseTag, events)
}, [baseTag, events])
const [privateKey, setPrivateKey] = useState(null)
const [chosenRelays, setChosenRelays] = useState(relays)

Expand Down Expand Up @@ -136,7 +140,9 @@ export function NoComment({
}
}, [baseTag, chosenRelays.length])

if (skip && skip !== '' && skip === location.pathname) return
if (skip && skip !== '' && skip === location.pathname) {
return
}

return (
<Container>
Expand Down
5 changes: 2 additions & 3 deletions src/Thread.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default function Thread({
)
}

export function computeThreads(events) {
export function computeThreads(baseTag, events) {
let threadableEvents = events.map(event => ({...event, replies: []}))

let threads = []
Expand All @@ -82,7 +82,7 @@ export function computeThreads(events) {

// if this is not a reply to another comment we assume it is a reply
// to the "root"/base -- i.e. a top-level comment
if (!reply) {
if (!reply || reply === baseTag.ref) {
threads.unshift(event)
continue
}
Expand All @@ -91,7 +91,6 @@ export function computeThreads(events) {
parent.replies.unshift(event)
}

console.log(threads)
return threads

function getImmediateReply(tags) {
Expand Down

0 comments on commit 3397d29

Please sign in to comment.