feat: Implement sorting of peers by latest message timestamp and unread status in Nostr chat
- Introduce a computed property to sort peers based on the latest message timestamp and unread message count, enhancing the user experience by prioritizing relevant conversations. - Add methods to track and retrieve the latest message timestamp for each peer, ensuring accurate sorting. - Update the ChatComponent to utilize the new sorting logic, improving the display of peers in the chat interface. refactor: Reorganize fuzzy search and mobile detection logic in ChatComponent - Move fuzzy search implementation and mobile detection methods to improve code clarity and maintainability. - Ensure consistent functionality for searching peers by username or pubkey with typo tolerance. - Maintain mobile navigation logic for better user experience on smaller devices.
This commit is contained in:
parent
91e9756bf0
commit
b0101915c7
2 changed files with 91 additions and 23 deletions
|
|
@ -401,6 +401,62 @@ const scrollTarget = ref<HTMLElement | null>(null)
|
|||
// Mobile detection
|
||||
const isMobile = ref(false)
|
||||
|
||||
// Nostr chat composable
|
||||
const {
|
||||
isConnected,
|
||||
messages,
|
||||
connect,
|
||||
disconnect,
|
||||
subscribeToPeer,
|
||||
subscribeToPeerForNotifications,
|
||||
sendMessage: sendNostrMessage,
|
||||
onMessageAdded,
|
||||
markMessagesAsRead,
|
||||
getUnreadCount,
|
||||
getTotalUnreadCount,
|
||||
getLatestMessageTimestamp
|
||||
} = useNostrChat()
|
||||
|
||||
// Computed
|
||||
const currentMessages = computed(() => {
|
||||
if (!selectedPeer.value) return []
|
||||
return messages.value.get(selectedPeer.value.pubkey) || []
|
||||
})
|
||||
|
||||
// Sort peers by latest message timestamp (newest first) and unread status
|
||||
const sortedPeers = computed(() => {
|
||||
const sorted = [...peers.value].sort((a, b) => {
|
||||
const aTimestamp = getLatestMessageTimestamp(a.pubkey)
|
||||
const bTimestamp = getLatestMessageTimestamp(b.pubkey)
|
||||
const aUnreadCount = getUnreadCount(a.pubkey)
|
||||
const bUnreadCount = getUnreadCount(b.pubkey)
|
||||
|
||||
// First, sort by unread count (peers with unread messages appear first)
|
||||
if (aUnreadCount > 0 && bUnreadCount === 0) return -1
|
||||
if (aUnreadCount === 0 && bUnreadCount > 0) return 1
|
||||
|
||||
// Then, sort by latest message timestamp (newest first)
|
||||
if (aTimestamp !== bTimestamp) {
|
||||
return bTimestamp - aTimestamp
|
||||
}
|
||||
|
||||
// Finally, sort alphabetically by username for peers with same timestamp
|
||||
return (a.username || '').localeCompare(b.username || '')
|
||||
})
|
||||
|
||||
// Debug logging (only in development)
|
||||
if (process.env.NODE_ENV === 'development' && sorted.length > 0) {
|
||||
console.log('Sorted peers:', sorted.map(p => ({
|
||||
username: p.username,
|
||||
pubkey: p.pubkey.slice(0, 8) + '...',
|
||||
latestTimestamp: getLatestMessageTimestamp(p.pubkey),
|
||||
unreadCount: getUnreadCount(p.pubkey)
|
||||
})))
|
||||
}
|
||||
|
||||
return sorted
|
||||
})
|
||||
|
||||
// Fuzzy search for peers
|
||||
// This integrates the useFuzzySearch composable to provide intelligent search functionality
|
||||
// for finding peers by username or pubkey with typo tolerance and scoring
|
||||
|
|
@ -410,7 +466,7 @@ const {
|
|||
isSearching,
|
||||
resultCount,
|
||||
clearSearch
|
||||
} = useFuzzySearch(peers, {
|
||||
} = useFuzzySearch(sortedPeers, {
|
||||
fuseOptions: {
|
||||
keys: [
|
||||
{ name: 'username', weight: 0.7 }, // Username has higher weight for better UX
|
||||
|
|
@ -442,27 +498,6 @@ const goBackToPeers = () => {
|
|||
selectedPeer.value = null
|
||||
}
|
||||
|
||||
// Nostr chat composable
|
||||
const {
|
||||
isConnected,
|
||||
messages,
|
||||
connect,
|
||||
disconnect,
|
||||
subscribeToPeer,
|
||||
subscribeToPeerForNotifications,
|
||||
sendMessage: sendNostrMessage,
|
||||
onMessageAdded,
|
||||
markMessagesAsRead,
|
||||
getUnreadCount,
|
||||
getTotalUnreadCount
|
||||
} = useNostrChat()
|
||||
|
||||
// Computed
|
||||
const currentMessages = computed(() => {
|
||||
if (!selectedPeer.value) return []
|
||||
return messages.value.get(selectedPeer.value.pubkey) || []
|
||||
})
|
||||
|
||||
// Methods
|
||||
const loadPeers = async () => {
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue