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:
padreug 2025-08-08 23:19:35 +02:00
parent 91e9756bf0
commit b0101915c7
2 changed files with 91 additions and 23 deletions

View file

@ -74,6 +74,9 @@ export function useNostrChat() {
// Reactive unread counts
const unreadCounts = ref<Map<string, number>>(new Map())
// Track latest message timestamp for each peer (for sorting)
const latestMessageTimestamps = ref<Map<string, number>>(new Map())
// Computed
const isLoggedIn = computed(() => !!currentUser.value)
@ -97,6 +100,26 @@ export function useNostrChat() {
return total
}
// Get latest message timestamp for a peer
const getLatestMessageTimestamp = (peerPubkey: string): number => {
return latestMessageTimestamps.value.get(peerPubkey) || 0
}
// Get all latest message timestamps
const getAllLatestMessageTimestamps = (): Map<string, number> => {
return new Map(latestMessageTimestamps.value)
}
// Update latest message timestamp for a peer
const updateLatestMessageTimestamp = (peerPubkey: string, timestamp: number): void => {
const currentLatest = latestMessageTimestamps.value.get(peerPubkey) || 0
if (timestamp > currentLatest) {
latestMessageTimestamps.value.set(peerPubkey, timestamp)
// Force reactivity
latestMessageTimestamps.value = new Map(latestMessageTimestamps.value)
}
}
// Update unread count for a peer
const updateUnreadCount = (peerPubkey: string, count: number): void => {
if (count > 0) {
@ -584,6 +607,9 @@ export function useNostrChat() {
// Force reactivity by triggering a change
messages.value = new Map(messages.value)
// Update latest message timestamp for this peer (for sorting)
updateLatestMessageTimestamp(peerPubkey, message.created_at)
// Track unread messages (only for received messages, not sent ones)
if (!isSentByMe) {
const unreadData = getUnreadData(peerPubkey)
@ -748,6 +774,9 @@ export function useNostrChat() {
// Force reactivity by triggering a change
messages.value = new Map(messages.value)
// Update latest message timestamp for this peer (for sorting)
updateLatestMessageTimestamp(peerPubkey, message.created_at)
// Trigger callback if set
if (onMessageAdded.value) {
onMessageAdded.value(peerPubkey)
@ -792,6 +821,10 @@ export function useNostrChat() {
getTotalUnreadCount,
clearAllUnreadCounts,
clearProcessedMessageIds,
debugUnreadData
debugUnreadData,
// Timestamp methods (for sorting)
getLatestMessageTimestamp,
getAllLatestMessageTimestamps
}
}