Skip to content

Commit

Permalink
Merge pull request #505 from ephemeraHQ/ar/query-updates
Browse files Browse the repository at this point in the history
Updated Queries
  • Loading branch information
alexrisch authored Aug 13, 2024
2 parents cfce17f + 1e40121 commit 185f57a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 37 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
19 changes: 14 additions & 5 deletions components/PinnedConversations/PinnedConversation.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useGroupNameQuery } from "@queries/useGroupNameQuery";
import { useGroupPhotoQuery } from "@queries/useGroupPhotoQuery";
import { backgroundColor, textSecondaryColor } from "@styles/colors";
import { AvatarSizes } from "@styles/sizes";
import { FC, useCallback } from "react";
Expand All @@ -9,10 +11,12 @@ import {
} from "react-native";

import Avatar from "..//Avatar";
import { useChatStore, useProfilesStore } from "../../data/store/accountsStore";
import {
useChatStore,
useCurrentAccount,
useProfilesStore,
} from "../../data/store/accountsStore";
import { XmtpConversation } from "../../data/store/chatStore";
import { useGroupName } from "../../hooks/useGroupName";
import { useGroupPhoto } from "../../hooks/useGroupPhoto";
import { navigate } from "../../utils/navigation";
import { getPreferredAvatar, getPreferredName } from "../../utils/profile";
import GroupAvatar from "../GroupAvatar";
Expand All @@ -22,10 +26,15 @@ interface Props {
}

export const PinnedConversation: FC<Props> = ({ conversation }) => {
const account = useCurrentAccount() as string;
const profiles = useProfilesStore((s) => s.profiles);
const { topic, isGroup } = conversation;
const { groupName } = useGroupName(topic);
const { groupPhoto } = useGroupPhoto(topic);
const { data: groupName } = useGroupNameQuery(account, topic, {
refetchOnMount: false,
});
const { data: groupPhoto } = useGroupPhotoQuery(account, topic, {
refetchOnMount: false,
});
const title = isGroup ? groupName : conversation.conversationTitle;
const socials = profiles[conversation.peerAddress as string]?.socials;
const avatar = isGroup ? groupPhoto : getPreferredAvatar(socials);
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 185f57a

Please sign in to comment.