Skip to content

Commit

Permalink
feat: Group Names from network
Browse files Browse the repository at this point in the history
Added group naming from network level
Removed local handling
  • Loading branch information
Alex Risch authored and Alex Risch committed May 31, 2024
1 parent 41fadbf commit c0ba37d
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 58 deletions.
8 changes: 4 additions & 4 deletions src/components/GroupHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {Text} from './common/Text';
interface GroupHeaderProps {
peerAddresses: string[];
onGroupPress: () => void;
groupId: string;
groupTopic: string;
}

const HeaderContainer: FC<PropsWithChildren> = ({children}) => {
Expand All @@ -33,10 +33,10 @@ const HeaderContainer: FC<PropsWithChildren> = ({children}) => {
export const GroupHeader: FC<GroupHeaderProps> = ({
peerAddresses,
onGroupPress,
groupId,
groupTopic,
}) => {
const {goBack} = useTypedNavigation();
const groupDisplayName = useGroupName(peerAddresses, groupId);
const {groupName} = useGroupName(groupTopic);

return (
<HeaderContainer>
Expand All @@ -56,7 +56,7 @@ export const GroupHeader: FC<GroupHeaderProps> = ({
typography="text-lg/heavy"
numberOfLines={1}
textAlign={'center'}>
{groupDisplayName}
{groupName}
</Text>
</VStack>
<Pressable onPress={onGroupPress}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/GroupListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const GroupListItem: FC<GroupListItemProps> = ({group}) => {
const topic = group?.topic;
const {data: addresses} = useGroupParticipantsQuery(topic);
const {navigate} = useTypedNavigation();
const groupName = useGroupName(addresses ?? [], topic);
const {groupName} = useGroupName(topic);
const {data: messages, isLoading, isError} = useFirstGroupMessageQuery(topic);
const firstMessage = messages?.[0];

Expand Down
22 changes: 7 additions & 15 deletions src/components/modals/GroupInfoModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import {Alert, DeviceEventEmitter} from 'react-native';
import {AppConfig} from '../../consts/AppConfig';
import {SupportedContentTypes} from '../../consts/ContentTypes';
import {EventEmitterEvents} from '../../consts/EventEmitters';
import {useClient} from '../../hooks/useClient';
import {useContactInfo} from '../../hooks/useContactInfo';
import {useGroupName} from '../../hooks/useGroupName';
import {translate} from '../../i18n';
import {mmkvStorage} from '../../services/mmkvStorage';
import {colors} from '../../theme/colors';
import {AvatarWithFallback} from '../AvatarWithFallback';
import {Button} from '../common/Button';
Expand Down Expand Up @@ -85,7 +84,6 @@ export const GroupInfoModal: FC<GroupInfoModalProps> = ({
group,
address: currentAddress,
}) => {
const {client} = useClient();
const [editing, setEditing] = useState(false);
const [groupName, setGroupName] = useState('');
const onRemovePress = useCallback(
Expand All @@ -103,19 +101,18 @@ export const GroupInfoModal: FC<GroupInfoModalProps> = ({
},
[group, hide],
);
const {groupName: currentGroupName, updateGroupName} = useGroupName(
group?.topic ?? '',
);

const onBlur = useCallback(() => {
if (!groupName) {
return;
}
mmkvStorage.saveGroupName(
client?.address ?? '',
group?.topic ?? '',
groupName,
);
updateGroupName(groupName);
setEditing(false);
setGroupName('');
}, [client?.address, group?.topic, groupName]);
}, [groupName, updateGroupName]);

return (
<Modal onBackgroundPress={hide} isOpen={shown}>
Expand All @@ -140,12 +137,7 @@ export const GroupInfoModal: FC<GroupInfoModalProps> = ({
) : (
<HStack w={'100%'} alignItems={'center'} justifyContent={'center'}>
<Text typography="text-xl/bold" textAlign={'center'}>
{(!!group &&
mmkvStorage.getGroupName(
client?.address ?? '',
group?.topic,
)) ??
translate('group')}
{currentGroupName ?? translate('group')}
</Text>
<Pressable onPress={() => setEditing(true)} alignSelf={'flex-end'}>
<Icon
Expand Down
27 changes: 12 additions & 15 deletions src/hooks/useGroupName.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {useMemo} from 'react';
import {mmkvStorage} from '../services/mmkvStorage';
import {formatAddress} from '../utils/formatAddress';
import {useClient} from './useClient';
import {useGroupNameMutation} from '../mutations/useGroupNameMutation';
import {useGroupNameQuery} from '../queries/useGroupNameQuery';

export const useGroupName = (addresses: string[], groupId: string) => {
const {client} = useClient();
const data = useMemo(() => {
const groupDisplayName = addresses.map(formatAddress).join(', ');
return groupDisplayName;
}, [addresses]);
const savedGroupName = mmkvStorage.getGroupName(
client?.address ?? '',
groupId,
);
return savedGroupName ?? data;
export const useGroupName = (groupTopic: string) => {
const {data: groupName} = useGroupNameQuery(groupTopic ?? '');
const {mutateAsync} = useGroupNameMutation(groupTopic ?? '');

return {
groupName,
updateGroupName: async (newGroupName: string) => {
await mutateAsync(newGroupName);
},
};
};
3 changes: 3 additions & 0 deletions src/mutations/MutationKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const MutationKeys = {
GroupName: 'GroupName',
};
23 changes: 23 additions & 0 deletions src/mutations/useGroupNameMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {useMutation} from '@tanstack/react-query';
import {useGroup} from '../hooks/useGroup';
import {QueryKeys} from '../queries/QueryKeys';
import {queryClient} from '../services/queryClient';
import {MutationKeys} from './MutationKeys';

export interface GroupNameQueryOptions {
limit?: number;
}

export const useGroupNameMutation = (topic: string) => {
const {data: group} = useGroup(topic);

return useMutation({
mutationKey: [MutationKeys.GroupName, topic],
mutationFn: async (newGroupName: string) => {
return group?.updateGroupName(newGroupName);
},
onSuccess: (_, newGroupName) => {
queryClient.setQueryData([QueryKeys.GroupName, topic], newGroupName);
},
});
};
1 change: 1 addition & 0 deletions src/queries/QueryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum QueryKeys {
Groups = 'groups',
GroupParticipants = 'group_participants',
GroupMessages = 'group_messages',
GroupName = 'group_name',
FirstGroupMessage = 'first_group_messages',
FramesClient = 'frames_client',
Frame = 'frame',
Expand Down
22 changes: 22 additions & 0 deletions src/queries/useGroupNameQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {useQuery} from '@tanstack/react-query';
import {useGroup} from '../hooks/useGroup';
import {QueryKeys} from './QueryKeys';

export interface GroupNameQueryOptions {
limit?: number;
}

export const useGroupNameQuery = (topic: string) => {
const {data: group} = useGroup(topic);

return useQuery<string | undefined, unknown>({
queryKey: [QueryKeys.GroupName, topic],
queryFn: async () => {
if (!group) {
return undefined;
}
return group.groupName();
},
enabled: !!group,
});
};
2 changes: 1 addition & 1 deletion src/screens/GroupScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const GroupScreen = () => {
}}>
<Box backgroundColor={colors.backgroundPrimary} paddingBottom={10}>
<GroupHeader
groupId={group?.topic ?? ''}
groupTopic={group?.topic ?? ''}
peerAddresses={addresses ?? []}
onGroupPress={() => setShowGroupModal(true)}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/screens/NewConversationScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const NewConversationScreen = () => {
<GroupHeader
peerAddresses={addresses}
onGroupPress={() => {}}
groupId="new_convo"
groupTopic="new_convo"
/>
) : (
<ConversationHeader
Expand Down
21 changes: 0 additions & 21 deletions src/services/mmkvStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ enum MMKVKeys {
IMAGE_CACHE = 'IMAGE_CACHE',

// Groups
GROUP_NAME = 'GROUP_NAME',
GROUP_ID_PUSH_SUBSCRIPTION = 'GROUP_ID_PUSH_SUBSCRIPTION',
GROUP_PARTICIPANTS = 'GROUP_PARTICIPANTS',
GROUP_CONSENT = 'GROUP_CONSENT',
Expand Down Expand Up @@ -243,26 +242,6 @@ class MMKVStorage {

// #endregion

// #region Group Name

private getGroupNameKey = (address: string, topic: string) => {
return `${MMKVKeys.GROUP_NAME}_${address}_${topic}`;
};

saveGroupName = (address: string, topic: string, groupName: string) => {
return this.storage.set(this.getGroupNameKey(address, topic), groupName);
};

getGroupName = (address: string, topic: string) => {
return this.storage.getString(this.getGroupNameKey(address, topic));
};

clearGroupName = (address: string, topic: string) => {
return this.storage.delete(this.getGroupNameKey(address, topic));
};

//#endregion Group Name

//#region Group Id Push Subscription
private getGroupIdPushSubscriptionKey = (topic: string) => {
return `${MMKVKeys.GROUP_ID_PUSH_SUBSCRIPTION}_${topic}`;
Expand Down

0 comments on commit c0ba37d

Please sign in to comment.