feat(notes): add repost filter
This commit is contained in:
parent
e92d602f3a
commit
79660f63ec
@ -1,7 +1,7 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { PropsWithChildren } from 'react'
|
import { PropsWithChildren } from 'react'
|
||||||
import { Filter } from '.'
|
import { Filter } from '.'
|
||||||
import { FilterOptions, SortBy } from 'types'
|
import { FilterOptions, RepostFilter, SortBy } from 'types'
|
||||||
import { Dropdown } from './Dropdown'
|
import { Dropdown } from './Dropdown'
|
||||||
import { Option } from './Option'
|
import { Option } from './Option'
|
||||||
import { DEFAULT_FILTER_OPTIONS } from 'utils'
|
import { DEFAULT_FILTER_OPTIONS } from 'utils'
|
||||||
@ -86,6 +86,27 @@ export const FeedFilter = React.memo(
|
|||||||
</Dropdown>
|
</Dropdown>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* Repost filter */}
|
||||||
|
{tab === 2 && (
|
||||||
|
<Dropdown label={filterOptions.repost}>
|
||||||
|
{Object.values(RepostFilter).map((item, index) =>
|
||||||
|
item === RepostFilter.Only_Repost ? null : (
|
||||||
|
<Option
|
||||||
|
key={`repost-${index}`}
|
||||||
|
onClick={() =>
|
||||||
|
setFilterOptions((prev) => ({
|
||||||
|
...prev,
|
||||||
|
repost: item
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{item}
|
||||||
|
</Option>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
</Dropdown>
|
||||||
|
)}
|
||||||
|
|
||||||
{children}
|
{children}
|
||||||
</Filter>
|
</Filter>
|
||||||
)
|
)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { useLoaderData } from 'react-router-dom'
|
import { useLoaderData } from 'react-router-dom'
|
||||||
import { FeedPageLoaderResult } from './loader'
|
import { FeedPageLoaderResult } from './loader'
|
||||||
import { useAppSelector, useNDKContext } from 'hooks'
|
import { useAppSelector, useLocalStorage, useNDKContext } from 'hooks'
|
||||||
import { useEffect, useMemo, useState } from 'react'
|
import { useEffect, useMemo, useState } from 'react'
|
||||||
import { LoadingSpinner } from 'components/LoadingSpinner'
|
import { LoadingSpinner } from 'components/LoadingSpinner'
|
||||||
import {
|
import {
|
||||||
@ -11,6 +11,8 @@ import {
|
|||||||
} from '@nostr-dev-kit/ndk'
|
} from '@nostr-dev-kit/ndk'
|
||||||
import { NoteSubmit } from 'components/Notes/NoteSubmit'
|
import { NoteSubmit } from 'components/Notes/NoteSubmit'
|
||||||
import { Note } from 'components/Notes/Note'
|
import { Note } from 'components/Notes/Note'
|
||||||
|
import { FeedPostsFilter, RepostFilter } from 'types'
|
||||||
|
import { DEFAULT_FILTER_OPTIONS } from 'utils'
|
||||||
|
|
||||||
export const FeedTabPosts = () => {
|
export const FeedTabPosts = () => {
|
||||||
const SHOWING_STEP = 20
|
const SHOWING_STEP = 20
|
||||||
@ -24,6 +26,12 @@ export const FeedTabPosts = () => {
|
|||||||
const [isLoadMoreVisible, setIsLoadMoreVisible] = useState(true)
|
const [isLoadMoreVisible, setIsLoadMoreVisible] = useState(true)
|
||||||
const [showing, setShowing] = useState(SHOWING_STEP)
|
const [showing, setShowing] = useState(SHOWING_STEP)
|
||||||
|
|
||||||
|
const filterKey = 'filter-feed-2'
|
||||||
|
const [filterOptions] = useLocalStorage<FeedPostsFilter>(
|
||||||
|
filterKey,
|
||||||
|
DEFAULT_FILTER_OPTIONS
|
||||||
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!userPubkey) return
|
if (!userPubkey) return
|
||||||
|
|
||||||
@ -65,14 +73,23 @@ export const FeedTabPosts = () => {
|
|||||||
// )
|
// )
|
||||||
// )
|
// )
|
||||||
|
|
||||||
// Filter reply events
|
_notes = _notes.filter((n) => {
|
||||||
_notes = _notes.filter((n) => n.getMatchingTags('e').length === 0)
|
if (n.kind === NDKKind.Text) {
|
||||||
|
// Filter out the replies (Kind 1 events with e tags are replies to other kind 1 events)
|
||||||
|
return n.getMatchingTags('e').length === 0
|
||||||
|
}
|
||||||
|
// Filter repost events if the option is set to hide reposts
|
||||||
|
return !(
|
||||||
|
n.kind === NDKKind.Repost &&
|
||||||
|
filterOptions.repost === RepostFilter.Hide_Repost
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
_notes = _notes.sort((a, b) => (b.created_at ?? 0) - (a.created_at ?? 0))
|
_notes = _notes.sort((a, b) => (b.created_at ?? 0) - (a.created_at ?? 0))
|
||||||
|
|
||||||
showing > 0 && _notes.splice(showing)
|
showing > 0 && _notes.splice(showing)
|
||||||
return _notes
|
return _notes
|
||||||
}, [notes, showing])
|
}, [filterOptions.repost, notes, showing])
|
||||||
|
|
||||||
if (!userPubkey) return null
|
if (!userPubkey) return null
|
||||||
|
|
||||||
|
@ -41,4 +41,4 @@ export interface FilterOptions {
|
|||||||
repost: RepostFilter
|
repost: RepostFilter
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FeedPostsFilter = Pick<FilterOptions, 'nsfw'>
|
export type FeedPostsFilter = Pick<FilterOptions, 'nsfw' | 'repost'>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user