Skip to content
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

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions features/consent/use-dm-consent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ForCurrentInbox/Account

const currentAccount = useCurrentAccount()!;

return useMutation({
mutationFn: async (args: { consent: "allow" | "deny" }) => {
mutationFn: async (args: {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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");
}
Expand All @@ -45,6 +45,7 @@ export function useDmConsent(args: {
]);
},
onMutate: (args) => {
const { peerInboxId, conversationId, topic } = args;
const conversation = getConversationQueryData({
account: currentAccount,
topic,
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const ConversationListAwaitingRequests = memo(
const conversationsMetadataQueries = useQueries({
queries: (likelyNotSpam ?? []).map((conversation) =>
getConversationMetadataQueryOptions({
account: currentAccount!,
account: currentAccount,
topic: conversation.topic,
})
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export const ConversationListItemDm = memo(function ConversationListItemDm({
topic: conversationTopic,
});

const deleteDm = useDeleteDm(conversation as DmWithCodecsType);
const deleteDm = useDeleteDm({
topic: conversationTopic,
});
const { restoreConversationAsync } = useRestoreConversation({
topic: conversationTopic,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCurrentAccount } from "@/data/store/accountsStore";
import { AnimatedCenter } from "@/design-system/Center";
import { AnimatedHStack } from "@/design-system/HStack";
import { AnimatedVStack } from "@/design-system/VStack";
Expand All @@ -8,14 +9,14 @@ import { useConversationListPinnedConversationsStyles } from "@/features/convers
import { useConversationsCount } from "@/features/conversation-list/hooks/use-conversations-count";
import { usePinnedConversations } from "@/features/conversation-list/hooks/use-pinned-conversations";
import { isConversationGroup } from "@/features/conversation/utils/is-conversation-group";
import { useConversationQuery } from "@/queries/useConversationQuery";
import { ThemedStyle, useAppTheme } from "@/theme/useAppTheme";
import { captureError } from "@/utils/capture-error";
import { hexToRGBA } from "@/utils/colors";
import { chunk } from "@/utils/general";
import { ConversationTopic } from "@xmtp/react-native-sdk";
import { memo } from "react";
import { ViewStyle } from "react-native";
import { useConversationByTopic } from "../hooks/use-conversation-by-topic";

export const ConversationListPinnedConversations = memo(
function ConversationListPinnedConversations() {
Expand Down Expand Up @@ -84,7 +85,9 @@ const PinnedConversationsSkeleton = memo(function () {
const { avatarSize } = useConversationListPinnedConversationsStyles();

return (
<AnimatedHStack style={themed($container)}>
<AnimatedHStack
style={[themed($container), { justifyContent: "space-between" }]}
thierryskoda marked this conversation as resolved.
Show resolved Hide resolved
>
<ConversationListItemAvatarSkeleton
color={hexToRGBA("#FF8080", 0.15)}
size={avatarSize}
Expand All @@ -105,7 +108,12 @@ const PinnedConversationWrapper = memo(
function PinnedConversationWrapper(props: { topic: ConversationTopic }) {
const { topic } = props;

const conversation = useConversationByTopic(topic);
const currentAccount = useCurrentAccount();

const { data: conversation } = useConversationQuery({
topic,
account: currentAccount!,
});

if (!conversation) {
captureError(
Expand Down
17 changes: 0 additions & 17 deletions features/conversation-list/hooks/use-conversation-by-topic.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { useCurrentAccount } from "@/data/store/accountsStore";
import { iconRegistry } from "@/design-system/Icon/Icon";
import { VStack } from "@/design-system/VStack";
import { useConversationByTopic } from "@/features/conversation-list/hooks/use-conversation-by-topic";
import { useConversationIsPinned } from "@/features/conversation-list/hooks/use-conversation-is-pinned";
import { useConversationIsUnread } from "@/features/conversation-list/hooks/use-conversation-is-unread";
import { useDeleteDm } from "@/features/conversation-list/hooks/use-delete-dm";
import { useDeleteGroup } from "@/features/conversation-list/hooks/use-delete-group";
import { useToggleReadStatus } from "@/features/conversation-list/hooks/use-toggle-read-status";
import { ConversationPreview } from "@/features/conversation/conversation-preview/conversation-preview";
import { isConversationGroup } from "@/features/conversation/utils/is-conversation-group";
import { translate } from "@/i18n";
import { ConversationPreview } from "@/features/conversation/conversation-preview/conversation-preview";
import { useConversationQuery } from "@/queries/useConversationQuery";
import { useAppTheme } from "@/theme/useAppTheme";
import { captureErrorWithToast } from "@/utils/capture-error";
import { Haptics } from "@/utils/haptics";
import { ConversationTopic } from "@xmtp/react-native-sdk";
import { useCallback } from "react";
import {
ContextMenuViewProps,
MenuActionConfig,
Expand Down Expand Up @@ -138,18 +140,29 @@ function useConversationContextMenuDeleteItem(args: {
conversationTopic: ConversationTopic;
}): IUseContextMenuItem {
const { conversationTopic } = args;

const currentAccount = useCurrentAccount();
const { theme } = useAppTheme();

const conversation = useConversationByTopic(conversationTopic);
const { data: conversation } = useConversationQuery({
account: currentAccount!,
topic: conversationTopic,
});

const deleteGroup = useDeleteGroup({ groupTopic: conversationTopic });
const deleteDm = useDeleteDm({ topic: conversationTopic });

const handleDeleteFn = conversation
? isConversationGroup(conversation)
? // eslint-disable-next-line react-hooks/rules-of-hooks
useDeleteGroup({ groupTopic: conversationTopic })
: // eslint-disable-next-line react-hooks/rules-of-hooks
useDeleteDm(conversation)
: () => null;
const handleDelete = useCallback(async () => {
if (!conversation) return;
thierryskoda marked this conversation as resolved.
Show resolved Hide resolved
try {
if (isConversationGroup(conversation)) {
await deleteGroup();
} else {
await deleteDm();
}
} catch (error) {
captureErrorWithToast(error);
}
}, [conversation, deleteGroup, deleteDm]);

return {
actionKey: "delete",
Expand All @@ -160,6 +173,6 @@ function useConversationContextMenuDeleteItem(args: {
iconTint: theme.colors.global.caution,
},
menuAttributes: ["destructive"],
onPress: handleDeleteFn,
onPress: handleDelete,
} satisfies IUseContextMenuItem;
}
52 changes: 38 additions & 14 deletions features/conversation-list/hooks/use-delete-dm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 },
});

Expand All @@ -58,7 +63,7 @@ export const useDeleteDm = (dm: DmWithCodecsType) => {
onError: (error, _, context) => {
updateConversationMetadataQueryData({
account: currentAccount,
topic: dm.topic,
topic,
updateData: { isDeleted: context?.previousIsDeleted },
});
},
Expand All @@ -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"),
Expand All @@ -85,6 +98,9 @@ export const useDeleteDm = (dm: DmWithCodecsType) => {
await deleteDmAsync();
await updateDmConsentAsync({
consent: "deny",
peerInboxId: peerInboxId,
conversationId,
topic,
});
} catch (error) {
captureErrorWithToast(error);
Expand All @@ -111,5 +127,13 @@ export const useDeleteDm = (dm: DmWithCodecsType) => {
}
}
);
}, [colorScheme, preferredName, deleteDmAsync, updateDmConsentAsync]);
}, [
colorScheme,
preferredName,
deleteDmAsync,
updateDmConsentAsync,
peerInboxId,
conversationId,
topic,
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import { useEffect, useMemo } from "react";
export const useConversationListConversations = () => {
const currentAccount = useCurrentAccount();

const {
data: conversations,
isLoading,
refetch,
} = useQuery(
const { data: conversations, refetch } = useQuery(
getConversationsQueryOptions({
account: currentAccount!,
})
Expand Down Expand Up @@ -96,5 +92,5 @@ export const useConversationListConversations = () => {
*/
}, [conversations, conversationsMetadataQueries]);

return { data: filteredAndSortedConversations, isLoading, refetch };
return { data: filteredAndSortedConversations, refetch };
};
Loading
Loading