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

feat: Group Names from network #118

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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 @@
interface GroupHeaderProps {
peerAddresses: string[];
onGroupPress: () => void;
groupId: string;
groupTopic: string;
}

const HeaderContainer: FC<PropsWithChildren> = ({children}) => {
Expand All @@ -33,10 +33,10 @@
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,12 +56,12 @@
typography="text-lg/heavy"
numberOfLines={1}
textAlign={'center'}>
{groupDisplayName}
{groupName}
</Text>
</VStack>
<Pressable onPress={onGroupPress}>
<GroupAvatarStack
style={{paddingRight: 30}}

Check warning on line 64 in src/components/GroupHeader.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { paddingRight: 30 }
addresses={peerAddresses}
/>
</Pressable>
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 @@
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 Expand Up @@ -57,7 +57,7 @@
<HStack space={[2, 3]} alignItems={'center'} w={'100%'} padding={'16px'}>
<Box marginRight={'30px'}>
<GroupAvatarStack
style={{paddingRight: 10}}

Check warning on line 60 in src/components/GroupListItem.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { paddingRight: 10 }
addresses={addresses ?? []}
/>
</Box>
Expand All @@ -78,7 +78,7 @@
<Text
alignSelf={'flex-start'}
typography="text-xs/regular"
style={{textAlignVertical: 'top'}}>

Check warning on line 81 in src/components/GroupListItem.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { textAlignVertical: 'top' }
{getMessageTimeDisplay(lastMessageTime)}
</Text>
)}
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 {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 @@ -41,7 +40,7 @@
{displayName}
</Text>
<AvatarWithFallback
style={{marginLeft: 10}}

Check warning on line 43 in src/components/modals/GroupInfoModal.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { marginLeft: 10 }
size={40}
address={address}
avatarUri={avatarUrl}
Expand Down Expand Up @@ -85,7 +84,6 @@
group,
address: currentAddress,
}) => {
const {client} = useClient();
const [editing, setEditing] = useState(false);
const [groupName, setGroupName] = useState('');
const onRemovePress = useCallback(
Expand All @@ -103,19 +101,18 @@
},
[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,19 +137,14 @@
) : (
<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
name={'pencil-square'}
type={'mini'}
color={colors.actionPrimary}
style={{marginLeft: 8}}

Check warning on line 147 in src/components/modals/GroupInfoModal.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { marginLeft: 8 }
/>
</Pressable>
</HStack>
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
Loading