-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
213 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { useContext } from "react"; | ||
import { ChatContext } from "../chat-context-provider"; | ||
import { DirectContact, useNostrPublishMutation } from "../nostr"; | ||
import { Kind } from "nostr-tools"; | ||
import { ChatQueries } from "../queries"; | ||
import { updateContactsBasedOnResult } from "./utils"; | ||
|
||
export function usePinContact() { | ||
const queryClient = useQueryClient(); | ||
const { activeUsername } = useContext(ChatContext); | ||
|
||
const { mutateAsync: publishDirectContact } = useNostrPublishMutation( | ||
["chats/nostr-publish-direct-contact"], | ||
Kind.Contacts, | ||
() => {}, | ||
); | ||
|
||
return useMutation( | ||
["chats/nostr-update-direct-contacts-pins"], | ||
async ({ | ||
contact, | ||
pinned, | ||
}: { | ||
contact: DirectContact; | ||
pinned: boolean; | ||
}) => { | ||
const directContacts = | ||
queryClient.getQueryData<DirectContact[]>([ | ||
ChatQueries.ORIGINAL_DIRECT_CONTACTS, | ||
activeUsername, | ||
]) ?? []; | ||
console.debug("[ns-query] Updating direct contact pin", contact, pinned); | ||
|
||
const contactTags = directContacts.map((c) => [ | ||
"p", | ||
c.pubkey, | ||
"", | ||
c.name, | ||
]); | ||
const pinTags = directContacts | ||
.filter((c) => contact.pubkey !== c.pubkey) | ||
.map((c) => ["pinned", c.pubkey, c.pinned ? "true" : "false"]); | ||
const lastSeenTags = directContacts.map((c) => [ | ||
"lastSeenDate", | ||
c.pubkey, | ||
c.lastSeenDate?.getTime().toString() ?? "", | ||
]); | ||
|
||
await publishDirectContact({ | ||
tags: [ | ||
...contactTags, | ||
...lastSeenTags, | ||
...pinTags, | ||
["pinned", contact.pubkey, pinned ? "true" : "false"], | ||
], | ||
eventMetadata: "", | ||
}); | ||
|
||
contact.pinned = pinned; | ||
return contact; | ||
}, | ||
{ | ||
onSuccess: (contact) => { | ||
if (contact) { | ||
updateContactsBasedOnResult(queryClient, activeUsername, contact); | ||
} | ||
}, | ||
}, | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./update-contacts-based-on-result"; |
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,24 @@ | ||
import { DirectContact } from "../../nostr"; | ||
import { ChatQueries } from "../../queries"; | ||
import { QueryClient } from "@tanstack/react-query"; | ||
|
||
export function updateContactsBasedOnResult( | ||
queryClient: QueryClient, | ||
activeUsername: string | undefined, | ||
contact: DirectContact, | ||
) { | ||
queryClient.setQueryData<DirectContact[]>( | ||
[ChatQueries.ORIGINAL_DIRECT_CONTACTS, activeUsername], | ||
(directContacts) => [ | ||
...(directContacts ?? []).filter((dc) => dc.pubkey !== contact.pubkey), | ||
contact, | ||
], | ||
); | ||
queryClient.setQueryData<DirectContact[]>( | ||
[ChatQueries.DIRECT_CONTACTS, activeUsername], | ||
(directContacts) => [ | ||
...(directContacts ?? []).filter((dc) => dc.pubkey !== contact.pubkey), | ||
contact, | ||
], | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ export type DirectContact = { | |
name: string; | ||
pubkey: string; | ||
lastSeenDate?: Date; | ||
pinned?: boolean; | ||
}; |
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,24 +1,24 @@ | ||
export type PublicMessage = { | ||
interface BaseMessage { | ||
id: string; | ||
root: string; | ||
content: string; | ||
creator: string; | ||
created: number; | ||
forwardedFrom?: string; | ||
} | ||
|
||
export interface PublicMessage extends BaseMessage { | ||
root: string; | ||
children?: PublicMessage[]; | ||
mentions: string[]; | ||
sent?: number; | ||
}; | ||
} | ||
|
||
export type DirectMessage = { | ||
id: string; | ||
export interface DirectMessage extends BaseMessage { | ||
root?: string; | ||
content: string; | ||
peer: string; | ||
creator: string; | ||
created: number; | ||
children?: DirectMessage[]; | ||
decrypted: boolean; | ||
sent?: number; | ||
}; | ||
} | ||
|
||
export type Message = PublicMessage | DirectMessage; |
Oops, something went wrong.