feat: prevent auto-scrolling to newly posted reply
This commit is contained in:
@@ -8,7 +8,6 @@ import {
|
|||||||
} from '@/lib/event'
|
} from '@/lib/event'
|
||||||
import { generateEventIdFromETag } from '@/lib/tag'
|
import { generateEventIdFromETag } from '@/lib/tag'
|
||||||
import { useSecondaryPage } from '@/PageManager'
|
import { useSecondaryPage } from '@/PageManager'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
|
||||||
import { useNoteStats } from '@/providers/NoteStatsProvider'
|
import { useNoteStats } from '@/providers/NoteStatsProvider'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
import { Event as NEvent, kinds } from 'nostr-tools'
|
import { Event as NEvent, kinds } from 'nostr-tools'
|
||||||
@@ -29,7 +28,6 @@ export default function ReplyNoteList({
|
|||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const { currentIndex } = useSecondaryPage()
|
const { currentIndex } = useSecondaryPage()
|
||||||
const { pubkey } = useNostr()
|
|
||||||
const [rootInfo, setRootInfo] = useState<{ id: string; pubkey: string } | undefined>(undefined)
|
const [rootInfo, setRootInfo] = useState<{ id: string; pubkey: string } | undefined>(undefined)
|
||||||
const [timelineKey, setTimelineKey] = useState<string | undefined>(undefined)
|
const [timelineKey, setTimelineKey] = useState<string | undefined>(undefined)
|
||||||
const [until, setUntil] = useState<number | undefined>(undefined)
|
const [until, setUntil] = useState<number | undefined>(undefined)
|
||||||
@@ -67,6 +65,13 @@ export default function ReplyNoteList({
|
|||||||
fetchRootEvent()
|
fetchRootEvent()
|
||||||
}, [event])
|
}, [event])
|
||||||
|
|
||||||
|
const onNewReply = useCallback((evt: NEvent) => {
|
||||||
|
setEvents((pre) => {
|
||||||
|
if (pre.some((reply) => reply.id === evt.id)) return pre
|
||||||
|
return [...pre, evt]
|
||||||
|
})
|
||||||
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!rootInfo) return
|
if (!rootInfo) return
|
||||||
const handleEventPublished = (data: Event) => {
|
const handleEventPublished = (data: Event) => {
|
||||||
@@ -82,7 +87,7 @@ export default function ReplyNoteList({
|
|||||||
return () => {
|
return () => {
|
||||||
client.removeEventListener('eventPublished', handleEventPublished)
|
client.removeEventListener('eventPublished', handleEventPublished)
|
||||||
}
|
}
|
||||||
}, [rootInfo])
|
}, [rootInfo, onNewReply])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (loading || !rootInfo || currentIndex !== index) return
|
if (loading || !rootInfo || currentIndex !== index) return
|
||||||
@@ -134,7 +139,7 @@ export default function ReplyNoteList({
|
|||||||
return () => {
|
return () => {
|
||||||
promise.then((closer) => closer?.())
|
promise.then((closer) => closer?.())
|
||||||
}
|
}
|
||||||
}, [rootInfo, currentIndex, index])
|
}, [rootInfo, currentIndex, index, onNewReply])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const replies: NEvent[] = []
|
const replies: NEvent[] = []
|
||||||
@@ -180,24 +185,6 @@ export default function ReplyNoteList({
|
|||||||
setLoading(false)
|
setLoading(false)
|
||||||
}, [loading, until, timelineKey])
|
}, [loading, until, timelineKey])
|
||||||
|
|
||||||
const onNewReply = useCallback(
|
|
||||||
(evt: NEvent) => {
|
|
||||||
setEvents((pre) => {
|
|
||||||
if (pre.some((reply) => reply.id === evt.id)) return pre
|
|
||||||
return [...pre, evt]
|
|
||||||
})
|
|
||||||
if (evt.pubkey === pubkey) {
|
|
||||||
setTimeout(() => {
|
|
||||||
if (bottomRef.current) {
|
|
||||||
bottomRef.current.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
|
|
||||||
}
|
|
||||||
highlightReply(evt.id, false)
|
|
||||||
}, 100)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[pubkey]
|
|
||||||
)
|
|
||||||
|
|
||||||
const highlightReply = useCallback((eventId: string, scrollTo = true) => {
|
const highlightReply = useCallback((eventId: string, scrollTo = true) => {
|
||||||
if (scrollTo) {
|
if (scrollTo) {
|
||||||
const ref = replyRefs.current[eventId]
|
const ref = replyRefs.current[eventId]
|
||||||
|
|||||||
Reference in New Issue
Block a user