Skip to content

Commit

Permalink
Updated Queries
Browse files Browse the repository at this point in the history
Updated queries for Conversation List to set initial data updated at, and also not refetech on mount

Removed unused properties from state
  • Loading branch information
Alex Risch authored and Alex Risch committed Aug 13, 2024
1 parent cfce17f commit fc647cd
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 32 deletions.
19 changes: 10 additions & 9 deletions components/ConversationList/GroupConversationItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { showActionSheetWithOptions } from "@components/StateHandlers/ActionSheetStateHandler";
import { useGroupConsent } from "@hooks/useGroupConsent";
import { translate } from "@i18n/index";
import { useGroupQuery } from "@queries/useGroupQuery";
import { useGroupNameQuery } from "@queries/useGroupNameQuery";
import { useGroupPhotoQuery } from "@queries/useGroupPhotoQuery";
import { NativeStackScreenProps } from "@react-navigation/native-stack";
import { actionSheetColors } from "@styles/colors";
import { saveTopicsData } from "@utils/api";
Expand All @@ -14,8 +15,6 @@ import {
useCurrentAccount,
} from "../../data/store/accountsStore";
import { useSelect } from "../../data/store/storeHelpers";
import { useGroupName } from "../../hooks/useGroupName";
import { useGroupPhoto } from "../../hooks/useGroupPhoto";
import { NavigationParamList } from "../../screens/Navigation/Navigation";
import { ConversationWithLastMessagePreview } from "../../utils/conversation";
import { conversationName } from "../../utils/str";
Expand All @@ -34,14 +33,17 @@ export const GroupConversationItem: FC<GroupConversationItemProps> = ({
route,
conversation,
}) => {
const userAddress = useCurrentAccount() as string;
const topic = conversation.topic;
const lastMessagePreview = conversation.lastMessagePreview;
const { data: group } = useGroupQuery(currentAccount(), topic);
const { groupName } = useGroupName(topic);
const { groupPhoto } = useGroupPhoto(topic);
const { data: groupName } = useGroupNameQuery(userAddress, topic, {
refetchOnMount: false,
});
const { data: groupPhoto } = useGroupPhotoQuery(userAddress, topic, {
refetchOnMount: false,
});
const { blockGroup } = useGroupConsent(topic);
const colorScheme = useColorScheme();
const userAddress = useCurrentAccount() as string;
const {
initialLoadDoneOnce,
openedConversationTopic,
Expand All @@ -64,7 +66,6 @@ export const GroupConversationItem: FC<GroupConversationItemProps> = ({

const handleDelete = useCallback(
(defaultAction: () => void) => {
if (!group) return;
showActionSheetWithOptions(
{
options: [
Expand Down Expand Up @@ -115,7 +116,7 @@ export const GroupConversationItem: FC<GroupConversationItemProps> = ({
}
);
},
[blockGroup, colorScheme, group, groupName, setTopicsData, topic]
[blockGroup, colorScheme, groupName, setTopicsData, topic]
);

return (
Expand Down
1 change: 0 additions & 1 deletion data/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export const xmtpConversationToDb = (
isGroup: xmtpConversation.isGroup,
isActive: xmtpConversation.isGroup ? !!xmtpConversation.isActive : true,
groupMembers: xmtpConversation.groupMembers,
groupAdmins: xmtpConversation.groupAdmins,
groupName: xmtpConversation.isGroup ? xmtpConversation.groupName : undefined,
groupPermissionLevel: xmtpConversation.groupPermissionLevel,
lastNotificationsSubscribedPeriod:
Expand Down
2 changes: 0 additions & 2 deletions data/store/chatStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ export type XmtpGroupConversation = XmtpConversationShared & {
isGroup: true;
peerAddress?: undefined;
groupMembers: string[];
groupAdmins: string[];
groupSuperAdmins: string[];
groupPermissionLevel: "all_members" | "admin_only" | "custom_policy";
groupName?: string;
groupCreator?: string;
Expand Down
2 changes: 0 additions & 2 deletions ios/ConverseNotificationExtension/Datatypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ struct SavedNotificationConversation: Codable {
var account: String
var isGroup: Bool = false
var groupMembers: [String?]?
var groupAdmins: [String?]?
var groupSuperAdmins: [String?]?
var groupPermissionLevel: String?
var groupName: String?
var spamScore: Double? {
Expand Down
3 changes: 2 additions & 1 deletion queries/useGroupIsActive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { queryClient } from "./queryClient";
import { useGroupQuery } from "./useGroupQuery";

export const useGroupIsActiveQuery = (account: string, topic: string) => {
const { data: group } = useGroupQuery(account, topic);
const { data: group, dataUpdatedAt } = useGroupQuery(account, topic);
return useQuery({
queryKey: groupIsActiveQueryKey(account, topic),
queryFn: async () => {
Expand All @@ -16,6 +16,7 @@ export const useGroupIsActiveQuery = (account: string, topic: string) => {
},
enabled: !!group,
initialData: group?.isGroupActive,
initialDataUpdatedAt: dataUpdatedAt,
});
};

Expand Down
16 changes: 12 additions & 4 deletions queries/useGroupNameQuery.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { useQuery } from "@tanstack/react-query";
import { useQuery, UseQueryOptions } from "@tanstack/react-query";

import { groupNameQueryKey } from "./QueryKeys";
import { queryClient } from "./queryClient";
import { useGroupQuery } from "./useGroupQuery";

export const useGroupNameQuery = (account: string, topic: string) => {
const { data: group } = useGroupQuery(account, topic);
export const useGroupNameQuery = (
account: string,
topic: string,
queryOptions?: Partial<
UseQueryOptions<string | undefined, Error, string | undefined>
>
) => {
const { data: group, dataUpdatedAt } = useGroupQuery(account, topic);
return useQuery({
queryKey: groupNameQueryKey(account, topic),
queryFn: async () => {
Expand All @@ -14,8 +20,10 @@ export const useGroupNameQuery = (account: string, topic: string) => {
}
return group.groupName();
},
enabled: !!group,
enabled: !!group && !!account,
initialData: group?.name,
initialDataUpdatedAt: dataUpdatedAt,
...queryOptions,
});
};

Expand Down
14 changes: 11 additions & 3 deletions queries/useGroupPhotoQuery.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { useQuery } from "@tanstack/react-query";
import { useQuery, UseQueryOptions } from "@tanstack/react-query";

import { groupPhotoQueryKey } from "./QueryKeys";
import { queryClient } from "./queryClient";
import { useGroupQuery } from "./useGroupQuery";

export const useGroupPhotoQuery = (account: string, topic: string) => {
const { data: group } = useGroupQuery(account, topic);
export const useGroupPhotoQuery = (
account: string,
topic: string,
queryOptions?: Partial<
UseQueryOptions<string | undefined, Error, string | undefined>
>
) => {
const { data: group, dataUpdatedAt } = useGroupQuery(account, topic);
return useQuery({
queryKey: groupPhotoQueryKey(account, topic),
queryFn: async () => {
Expand All @@ -16,6 +22,8 @@ export const useGroupPhotoQuery = (account: string, topic: string) => {
},
enabled: !!group,
initialData: group?.imageUrlSquare,
initialDataUpdatedAt: dataUpdatedAt,
...queryOptions,
});
};

Expand Down
10 changes: 0 additions & 10 deletions utils/xmtpRN/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,11 @@ const protocolGroupToStateConversation = async (
): Promise<XmtpConversation> => {
const groupMembers = await group.members();
const groupMembersAddresses: string[] = [];
const groupAdmins: string[] = [];
const groupSuperAdmins: string[] = [];
const groupAddedByInboxId = await group.addedByInboxId();
let groupCreator: string | undefined;
let groupAddedBy: string | undefined;

groupMembers.forEach((m) => {
if (m.permissionLevel === "admin" || m.permissionLevel === "super_admin") {
groupAdmins.push(m.addresses[0]);
}
if (m.permissionLevel === "super_admin") {
groupSuperAdmins.push(m.addresses[0]);
}
if (m.addresses[0]) {
groupMembersAddresses.push(m.addresses[0]);
}
Expand All @@ -100,8 +92,6 @@ const protocolGroupToStateConversation = async (
pending: false,
version: group.version,
isGroup: true,
groupAdmins,
groupSuperAdmins,
groupPermissionLevel: "custom_policy",
groupName: group.name,
groupCreator,
Expand Down

0 comments on commit fc647cd

Please sign in to comment.