-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1587 from ecency/bugfix/chats-issues
Bugfix/chats issues
- Loading branch information
Showing
10 changed files
with
128 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/common/features/chats/queries/composed-contacts-and-channels-query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
useChannelsQuery, | ||
useDirectContactsQuery, | ||
useKeysQuery, | ||
useNostrGetUserProfileQuery | ||
} from "@ecency/ns-query"; | ||
import { useMemo } from "react"; | ||
import { isAfter } from "date-fns"; | ||
|
||
export function useComposedContactsAndChannelsQuery() { | ||
const { publicKey } = useKeysQuery(); | ||
const { data: contactsData } = useDirectContactsQuery(); | ||
const { data: channelsData } = useChannelsQuery(); | ||
|
||
const { data: activeUserNostrProfiles } = useNostrGetUserProfileQuery(publicKey); | ||
|
||
const channelsLastSeenDate = useMemo( | ||
() => | ||
channelsData.reduce<Record<string, Date>>((acc, channel) => { | ||
if (activeUserNostrProfiles?.[0] && channel) { | ||
const channelsLastSeenTimestamps = | ||
activeUserNostrProfiles?.[0].channelsLastSeenDate ?? {}; | ||
const lastSeenTimestamp = channelsLastSeenTimestamps[channel.id]; | ||
return { | ||
...acc, | ||
[channel.id]: lastSeenTimestamp | ||
}; | ||
} | ||
return acc; | ||
}, {}), | ||
[channelsData, activeUserNostrProfiles] | ||
); | ||
|
||
return useMemo( | ||
() => | ||
[...(contactsData ?? []), ...channelsData] | ||
.sort((a, b) => { | ||
let lastSeenDateA: Date | undefined; | ||
if ("lastSeenDate" in a) { | ||
lastSeenDateA = a.lastSeenDate; | ||
} else if ("id" in a) { | ||
lastSeenDateA = channelsLastSeenDate[a.id]; | ||
} | ||
|
||
let lastSeenDateB: Date | undefined; | ||
if ("lastSeenDate" in b) { | ||
lastSeenDateB = b.lastSeenDate; | ||
} else if ("id" in b) { | ||
lastSeenDateB = channelsLastSeenDate[b.id]; | ||
} | ||
|
||
if ( | ||
lastSeenDateA instanceof Date && | ||
lastSeenDateB instanceof Date && | ||
isAfter(lastSeenDateA, lastSeenDateB) | ||
) { | ||
return -1; | ||
} | ||
return 0; | ||
}) | ||
.sort((a, b) => { | ||
if ("pinned" in a && "pinned" in b) { | ||
return +(b.pinned ?? false) - +(a.pinned ?? false); | ||
} else if ("pinned" in a) { | ||
return -1; | ||
} else if ("pinned" in b) { | ||
return 1; | ||
} | ||
return 0; | ||
}), | ||
[contactsData, channelsData, channelsLastSeenDate] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./search-users-query"; | ||
export * from "./composed-contacts-and-channels-query"; |
Oops, something went wrong.