-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Many fixes #1554
fix: Many fixes #1554
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,17 @@ import { | |
import { updateConsentForGroupsForAccount } from "./update-consent-for-groups-for-account"; | ||
import { updateInboxIdsConsentForAccount } from "./update-inbox-ids-consent-for-account"; | ||
|
||
export function useDmConsent(args: { | ||
peerInboxId: InboxId; | ||
conversationId: ConversationId; | ||
topic: ConversationTopic; | ||
}) { | ||
const { peerInboxId, conversationId, topic } = args; | ||
|
||
export function useDmConsent() { | ||
const currentAccount = useCurrentAccount()!; | ||
|
||
return useMutation({ | ||
mutationFn: async (args: { consent: "allow" | "deny" }) => { | ||
mutationFn: async (args: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does moving the arguments to the functions instead of the hook fix anything or is it just more idiomatic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just feel like if we're only using the args when calling the mutation, might as well pass them when we call the mutation. |
||
consent: "allow" | "deny"; | ||
peerInboxId: InboxId; | ||
conversationId: ConversationId; | ||
topic: ConversationTopic; | ||
}) => { | ||
const { peerInboxId, conversationId, topic } = args; | ||
if (!peerInboxId) { | ||
throw new Error("Peer inbox id not found"); | ||
} | ||
|
@@ -45,6 +45,7 @@ export function useDmConsent(args: { | |
]); | ||
}, | ||
onMutate: (args) => { | ||
const { peerInboxId, conversationId, topic } = args; | ||
const conversation = getConversationQueryData({ | ||
account: currentAccount, | ||
topic, | ||
|
@@ -70,7 +71,8 @@ export function useDmConsent(args: { | |
return { previousDmConsent: conversation.state }; | ||
} | ||
}, | ||
onError: (error, _, context) => { | ||
onError: (error, variables, context) => { | ||
const { topic } = variables; | ||
const { previousDmConsent } = context || {}; | ||
if (previousDmConsent) { | ||
const dm = getDmQueryData({ | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,49 +7,54 @@ import { | |
getConversationMetadataQueryData, | ||
updateConversationMetadataQueryData, | ||
} from "@/queries/conversation-metadata-query"; | ||
import { getConversationQueryOptions } from "@/queries/useConversationQuery"; | ||
import { useDmPeerInboxId } from "@/queries/useDmPeerInbox"; | ||
import { actionSheetColors } from "@/styles/colors"; | ||
import { useAppTheme } from "@/theme/useAppTheme"; | ||
import { deleteTopic } from "@/utils/api/topics"; | ||
import { captureErrorWithToast } from "@/utils/capture-error"; | ||
import { DmWithCodecsType } from "@/utils/xmtpRN/client.types"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { useMutation, useQuery } from "@tanstack/react-query"; | ||
import { ConversationTopic } from "@xmtp/react-native-sdk"; | ||
import { useCallback } from "react"; | ||
|
||
export const useDeleteDm = (dm: DmWithCodecsType) => { | ||
export const useDeleteDm = ({ topic }: { topic: ConversationTopic }) => { | ||
const { theme } = useAppTheme(); | ||
const colorScheme = theme.isDark ? "dark" : "light"; | ||
|
||
const currentAccount = useCurrentAccount()!; | ||
|
||
const { data: conversationId } = useQuery({ | ||
...getConversationQueryOptions({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be memoized? I guess we are pretty close to react compiler usage There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO that doesn't change anything since it's with a useQuery but might be wrong |
||
account: currentAccount, | ||
topic, | ||
}), | ||
select: (conversation) => conversation?.id, | ||
}); | ||
|
||
const { data: peerInboxId } = useDmPeerInboxId({ | ||
account: currentAccount, | ||
topic: dm.topic, | ||
topic, | ||
}); | ||
|
||
const { mutateAsync: updateDmConsentAsync } = useDmConsent({ | ||
peerInboxId: peerInboxId!, | ||
conversationId: dm.id, | ||
topic: dm.topic, | ||
}); | ||
const { mutateAsync: updateDmConsentAsync } = useDmConsent(); | ||
|
||
const preferredName = usePreferredInboxName(peerInboxId); | ||
|
||
const { mutateAsync: deleteDmAsync } = useMutation({ | ||
mutationFn: () => | ||
deleteTopic({ | ||
account: currentAccount, | ||
topic: dm.topic, | ||
topic, | ||
}), | ||
onMutate: () => { | ||
const previousIsDeleted = getConversationMetadataQueryData({ | ||
account: currentAccount, | ||
topic: dm.topic, | ||
topic, | ||
})?.isDeleted; | ||
|
||
updateConversationMetadataQueryData({ | ||
account: currentAccount, | ||
topic: dm.topic, | ||
topic, | ||
updateData: { isDeleted: true }, | ||
}); | ||
|
||
|
@@ -58,7 +63,7 @@ export const useDeleteDm = (dm: DmWithCodecsType) => { | |
onError: (error, _, context) => { | ||
updateConversationMetadataQueryData({ | ||
account: currentAccount, | ||
topic: dm.topic, | ||
topic, | ||
updateData: { isDeleted: context?.previousIsDeleted }, | ||
}); | ||
}, | ||
|
@@ -67,6 +72,14 @@ export const useDeleteDm = (dm: DmWithCodecsType) => { | |
return useCallback(() => { | ||
const title = `${translate("delete_chat_with")} ${preferredName}?`; | ||
|
||
if (!conversationId) { | ||
throw new Error("Conversation not found in useDeleteDm"); | ||
} | ||
|
||
if (!peerInboxId) { | ||
throw new Error("Peer inbox id not found in useDeleteDm"); | ||
} | ||
|
||
const actions = [ | ||
{ | ||
label: translate("delete"), | ||
|
@@ -85,6 +98,9 @@ export const useDeleteDm = (dm: DmWithCodecsType) => { | |
await deleteDmAsync(); | ||
await updateDmConsentAsync({ | ||
consent: "deny", | ||
peerInboxId: peerInboxId, | ||
conversationId, | ||
topic, | ||
}); | ||
} catch (error) { | ||
captureErrorWithToast(error); | ||
|
@@ -111,5 +127,13 @@ export const useDeleteDm = (dm: DmWithCodecsType) => { | |
} | ||
} | ||
); | ||
}, [colorScheme, preferredName, deleteDmAsync, updateDmConsentAsync]); | ||
}, [ | ||
colorScheme, | ||
preferredName, | ||
deleteDmAsync, | ||
updateDmConsentAsync, | ||
peerInboxId, | ||
conversationId, | ||
topic, | ||
]); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ForCurrentInbox/Account