feat: add refresh button for non-touch devices

This commit is contained in:
codytseng
2025-09-15 21:20:46 +08:00
parent 2926be0d87
commit de9ed04ca8
6 changed files with 140 additions and 64 deletions

View File

@@ -74,7 +74,10 @@ export default function KindFilter({
<Button <Button
variant="ghost" variant="ghost"
size="titlebar-icon" size="titlebar-icon"
className="relative w-fit px-3" className={cn(
'relative w-fit px-3 focus:text-foreground',
!isDifferentFromSaved && 'text-muted-foreground'
)}
onClick={() => { onClick={() => {
if (isSmallScreen) { if (isSmallScreen) {
setOpen(true) setOpen(true)

View File

@@ -4,8 +4,10 @@ import { useKindFilter } from '@/providers/KindFilterProvider'
import { useUserTrust } from '@/providers/UserTrustProvider' import { useUserTrust } from '@/providers/UserTrustProvider'
import storage from '@/services/local-storage.service' import storage from '@/services/local-storage.service'
import { TFeedSubRequest, TNoteListMode } from '@/types' import { TFeedSubRequest, TNoteListMode } from '@/types'
import { useRef, useState } from 'react' import { useMemo, useRef, useState } from 'react'
import KindFilter from '../KindFilter' import KindFilter from '../KindFilter'
import { RefreshButton } from '../RefreshButton'
import { isTouchDevice } from '@/lib/utils'
export default function NormalFeed({ export default function NormalFeed({
subRequests, subRequests,
@@ -20,6 +22,7 @@ export default function NormalFeed({
const { showKinds } = useKindFilter() const { showKinds } = useKindFilter()
const [temporaryShowKinds, setTemporaryShowKinds] = useState(showKinds) const [temporaryShowKinds, setTemporaryShowKinds] = useState(showKinds)
const [listMode, setListMode] = useState<TNoteListMode>(() => storage.getNoteListMode()) const [listMode, setListMode] = useState<TNoteListMode>(() => storage.getNoteListMode())
const supportTouch = useMemo(() => isTouchDevice(), [])
const noteListRef = useRef<TNoteListRef>(null) const noteListRef = useRef<TNoteListRef>(null)
const handleListModeChange = (mode: TNoteListMode) => { const handleListModeChange = (mode: TNoteListMode) => {
@@ -47,7 +50,10 @@ export default function NormalFeed({
handleListModeChange(listMode as TNoteListMode) handleListModeChange(listMode as TNoteListMode)
}} }}
options={ options={
<KindFilter showKinds={temporaryShowKinds} onShowKindsChange={handleShowKindsChange} /> <>
{!supportTouch && <RefreshButton onClick={() => noteListRef.current?.refresh()} />}
<KindFilter showKinds={temporaryShowKinds} onShowKindsChange={handleShowKindsChange} />
</>
} }
/> />
<NoteList <NoteList

View File

@@ -27,6 +27,7 @@ import {
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import PullToRefresh from 'react-simple-pull-to-refresh' import PullToRefresh from 'react-simple-pull-to-refresh'
import NoteCard, { NoteCardLoadingSkeleton } from '../NoteCard' import NoteCard, { NoteCardLoadingSkeleton } from '../NoteCard'
import { isTouchDevice } from '@/lib/utils'
const LIMIT = 200 const LIMIT = 200
const ALGO_LIMIT = 500 const ALGO_LIMIT = 500
@@ -64,6 +65,7 @@ const NoteList = forwardRef(
const [timelineKey, setTimelineKey] = useState<string | undefined>(undefined) const [timelineKey, setTimelineKey] = useState<string | undefined>(undefined)
const [refreshCount, setRefreshCount] = useState(0) const [refreshCount, setRefreshCount] = useState(0)
const [showCount, setShowCount] = useState(SHOW_COUNT) const [showCount, setShowCount] = useState(SHOW_COUNT)
const supportTouch = useMemo(() => isTouchDevice(), [])
const bottomRef = useRef<HTMLDivElement | null>(null) const bottomRef = useRef<HTMLDivElement | null>(null)
const topRef = useRef<HTMLDivElement | null>(null) const topRef = useRef<HTMLDivElement | null>(null)
@@ -124,7 +126,14 @@ const NoteList = forwardRef(
}, 20) }, 20)
} }
useImperativeHandle(ref, () => ({ scrollToTop }), []) const refresh = () => {
scrollToTop()
setTimeout(() => {
setRefreshCount((count) => count + 1)
}, 500)
}
useImperativeHandle(ref, () => ({ scrollToTop, refresh }), [])
useEffect(() => { useEffect(() => {
if (!subRequests.length) return if (!subRequests.length) return
@@ -250,45 +259,51 @@ const NoteList = forwardRef(
}, 0) }, 0)
} }
const list = (
<div className="min-h-screen">
{filteredEvents.map((event) => (
<NoteCard
key={event.id}
className="w-full"
event={event}
filterMutedNotes={filterMutedNotes}
/>
))}
{hasMore || loading ? (
<div ref={bottomRef}>
<NoteCardLoadingSkeleton />
</div>
) : events.length ? (
<div className="text-center text-sm text-muted-foreground mt-2">{t('no more notes')}</div>
) : (
<div className="flex justify-center w-full mt-2">
<Button size="lg" onClick={() => setRefreshCount((count) => count + 1)}>
{t('reload notes')}
</Button>
</div>
)}
</div>
)
return ( return (
<div> <div>
{filteredNewEvents.length > 0 && ( {filteredNewEvents.length > 0 && (
<NewNotesButton newEvents={filteredNewEvents} onClick={showNewEvents} /> <NewNotesButton newEvents={filteredNewEvents} onClick={showNewEvents} />
)} )}
<div ref={topRef} className="scroll-mt-[calc(6rem+1px)]" /> <div ref={topRef} className="scroll-mt-[calc(6rem+1px)]" />
<PullToRefresh {supportTouch ? (
onRefresh={async () => { <PullToRefresh
setRefreshCount((count) => count + 1) onRefresh={async () => {
await new Promise((resolve) => setTimeout(resolve, 1000)) refresh()
}} await new Promise((resolve) => setTimeout(resolve, 1000))
pullingContent="" }}
> pullingContent=""
<div className="min-h-screen"> >
{filteredEvents.map((event) => ( {list}
<NoteCard </PullToRefresh>
key={event.id} ) : (
className="w-full" list
event={event} )}
filterMutedNotes={filterMutedNotes}
/>
))}
{hasMore || loading ? (
<div ref={bottomRef}>
<NoteCardLoadingSkeleton />
</div>
) : events.length ? (
<div className="text-center text-sm text-muted-foreground mt-2">
{t('no more notes')}
</div>
) : (
<div className="flex justify-center w-full mt-2">
<Button size="lg" onClick={() => setRefreshCount((count) => count + 1)}>
{t('reload notes')}
</Button>
</div>
)}
</div>
</PullToRefresh>
<div className="h-40" /> <div className="h-40" />
</div> </div>
) )
@@ -299,4 +314,5 @@ export default NoteList
export type TNoteListRef = { export type TNoteListRef = {
scrollToTop: (behavior?: ScrollBehavior) => void scrollToTop: (behavior?: ScrollBehavior) => void
refresh: () => void
} }

View File

@@ -23,6 +23,8 @@ import PullToRefresh from 'react-simple-pull-to-refresh'
import Tabs from '../Tabs' import Tabs from '../Tabs'
import { NotificationItem } from './NotificationItem' import { NotificationItem } from './NotificationItem'
import { NotificationSkeleton } from './NotificationItem/Notification' import { NotificationSkeleton } from './NotificationItem/Notification'
import { isTouchDevice } from '@/lib/utils'
import { RefreshButton } from '../RefreshButton'
const LIMIT = 100 const LIMIT = 100
const SHOW_COUNT = 30 const SHOW_COUNT = 30
@@ -43,6 +45,8 @@ const NotificationList = forwardRef((_, ref) => {
const [visibleNotifications, setVisibleNotifications] = useState<NostrEvent[]>([]) const [visibleNotifications, setVisibleNotifications] = useState<NostrEvent[]>([])
const [showCount, setShowCount] = useState(SHOW_COUNT) const [showCount, setShowCount] = useState(SHOW_COUNT)
const [until, setUntil] = useState<number | undefined>(dayjs().unix()) const [until, setUntil] = useState<number | undefined>(dayjs().unix())
const supportTouch = useMemo(() => isTouchDevice(), [])
const topRef = useRef<HTMLDivElement | null>(null)
const bottomRef = useRef<HTMLDivElement | null>(null) const bottomRef = useRef<HTMLDivElement | null>(null)
const filterKinds = useMemo(() => { const filterKinds = useMemo(() => {
switch (notificationType) { switch (notificationType) {
@@ -235,6 +239,34 @@ const NotificationList = forwardRef((_, ref) => {
} }
}, [pubkey, timelineKey, until, loading, showCount, notifications]) }, [pubkey, timelineKey, until, loading, showCount, notifications])
const refresh = () => {
topRef.current?.scrollIntoView({ behavior: 'instant', block: 'start' })
setTimeout(() => {
setRefreshCount((count) => count + 1)
}, 500)
}
const list = (
<div className={notificationListStyle === NOTIFICATION_LIST_STYLE.COMPACT ? 'pt-2' : ''}>
{visibleNotifications.map((notification) => (
<NotificationItem
key={notification.id}
notification={notification}
isNew={notification.created_at > lastReadTime}
/>
))}
<div className="text-center text-sm text-muted-foreground">
{until || loading ? (
<div ref={bottomRef}>
<NotificationSkeleton />
</div>
) : (
t('no more notifications')
)}
</div>
</div>
)
return ( return (
<div> <div>
<Tabs <Tabs
@@ -249,33 +281,22 @@ const NotificationList = forwardRef((_, ref) => {
setShowCount(SHOW_COUNT) setShowCount(SHOW_COUNT)
setNotificationType(type as TNotificationType) setNotificationType(type as TNotificationType)
}} }}
options={!supportTouch ? <RefreshButton onClick={() => refresh()} /> : null}
/> />
<PullToRefresh <div ref={topRef} className="scroll-mt-[calc(6rem+1px)]" />
onRefresh={async () => { {supportTouch ? (
setRefreshCount((count) => count + 1) <PullToRefresh
await new Promise((resolve) => setTimeout(resolve, 1000)) onRefresh={async () => {
}} refresh()
pullingContent="" await new Promise((resolve) => setTimeout(resolve, 1000))
> }}
<div className={notificationListStyle === NOTIFICATION_LIST_STYLE.COMPACT ? 'pt-2' : ''}> pullingContent=""
{visibleNotifications.map((notification) => ( >
<NotificationItem {list}
key={notification.id} </PullToRefresh>
notification={notification} ) : (
isNew={notification.created_at > lastReadTime} list
/> )}
))}
<div className="text-center text-sm text-muted-foreground">
{until || loading ? (
<div ref={bottomRef}>
<NotificationSkeleton />
</div>
) : (
t('no more notifications')
)}
</div>
</div>
</PullToRefresh>
</div> </div>
) )
}) })

View File

@@ -2,12 +2,14 @@ import KindFilter from '@/components/KindFilter'
import NoteList, { TNoteListRef } from '@/components/NoteList' import NoteList, { TNoteListRef } from '@/components/NoteList'
import Tabs from '@/components/Tabs' import Tabs from '@/components/Tabs'
import { BIG_RELAY_URLS } from '@/constants' import { BIG_RELAY_URLS } from '@/constants'
import { isTouchDevice } from '@/lib/utils'
import { useKindFilter } from '@/providers/KindFilterProvider' import { useKindFilter } from '@/providers/KindFilterProvider'
import { useNostr } from '@/providers/NostrProvider' import { useNostr } from '@/providers/NostrProvider'
import client from '@/services/client.service' import client from '@/services/client.service'
import storage from '@/services/local-storage.service' import storage from '@/services/local-storage.service'
import { TFeedSubRequest, TNoteListMode } from '@/types' import { TFeedSubRequest, TNoteListMode } from '@/types'
import { useEffect, useMemo, useRef, useState } from 'react' import { useEffect, useMemo, useRef, useState } from 'react'
import { RefreshButton } from '../RefreshButton'
export default function ProfileFeed({ export default function ProfileFeed({
pubkey, pubkey,
@@ -34,6 +36,7 @@ export default function ProfileFeed({
return _tabs return _tabs
}, [myPubkey, pubkey]) }, [myPubkey, pubkey])
const supportTouch = useMemo(() => isTouchDevice(), [])
useEffect(() => { useEffect(() => {
const init = async () => { const init = async () => {
@@ -100,7 +103,10 @@ export default function ProfileFeed({
}} }}
threshold={Math.max(800, topSpace)} threshold={Math.max(800, topSpace)}
options={ options={
<KindFilter showKinds={temporaryShowKinds} onShowKindsChange={handleShowKindsChange} /> <>
{!supportTouch && <RefreshButton onClick={() => noteListRef.current?.refresh()} />}
<KindFilter showKinds={temporaryShowKinds} onShowKindsChange={handleShowKindsChange} />
</>
} }
/> />
<NoteList <NoteList

View File

@@ -0,0 +1,24 @@
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
import { RefreshCcw } from 'lucide-react'
import { useState } from 'react'
export function RefreshButton({ onClick }: { onClick: () => void }) {
const [refreshing, setRefreshing] = useState(false)
return (
<Button
variant="ghost"
size="titlebar-icon"
disabled={refreshing}
onClick={() => {
setRefreshing(true)
onClick()
setTimeout(() => setRefreshing(false), 500)
}}
className="text-muted-foreground focus:text-foreground [&_svg]:size-4"
>
<RefreshCcw className={cn(refreshing ? 'animate-spin' : '')} />
</Button>
)
}