feat: add member to group via UserSearch — pass addToGroupId param from GroupSettings

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
这个提交包含在:
徐勤民 2026-04-27 13:42:48 +08:00
父节点 8aab9d078f
当前提交 b31a91f924
共有 3 个文件被更改,包括 25 次插入13 次删除

查看文件

@ -14,7 +14,7 @@ export type RootStackParams = {
Main: undefined
SingleChat: { targetId: string; targetName: string; targetAvatar?: string }
GroupChat: { groupId: string; groupName: string }
UserSearch: undefined
UserSearch: { addToGroupId?: string } | undefined
GroupList: undefined
CreateGroup: undefined
GroupMembers: { groupId: string; groupName: string }

查看文件

@ -3,17 +3,20 @@ import {
View, Text, TextInput, FlatList, StyleSheet, TouchableOpacity,
SafeAreaView, ActivityIndicator, Alert, Image,
} from 'react-native'
import { useNavigation } from '@react-navigation/native'
import type { NativeStackNavigationProp } from '@react-navigation/native-stack'
import { useNavigation, useRoute } from '@react-navigation/native'
import type { NativeStackNavigationProp, NativeStackScreenProps } from '@react-navigation/native-stack'
import { ImSDK } from '@xuqm/rn-sdk'
import { demoApi, type UserProfile } from '../../api/demo'
import { load, save, K } from '../../utils/storage'
import type { RootStackParams } from '../../navigation/types'
type Nav = NativeStackNavigationProp<RootStackParams>
type Props = NativeStackScreenProps<RootStackParams, 'UserSearch'>
export default function UserSearchScreen() {
const navigation = useNavigation<Nav>()
const route = useRoute<Props['route']>()
const addToGroupId = (route.params as any)?.addToGroupId as string | undefined
const [keyword, setKeyword] = useState('')
const [results, setResults] = useState<UserProfile[]>([])
const [loading, setLoading] = useState(false)
@ -37,15 +40,24 @@ export default function UserSearchScreen() {
}, [])
const addContact = async (user: UserProfile) => {
try {
await ImSDK.addFriend(user.userId)
const current = (await load<UserProfile[]>(K.CONTACTS)) ?? []
if (!current.find(c => c.userId === user.userId)) {
await save(K.CONTACTS, [...current, user])
if (addToGroupId) {
try {
await ImSDK.addGroupMember(addToGroupId, user.userId)
Alert.alert('已添加到群聊', undefined, [{ text: '确定', onPress: () => navigation.goBack() }])
} catch (e: any) {
Alert.alert('添加失败', e?.message ?? '请重试')
}
} else {
try {
await ImSDK.addFriend(user.userId)
const current = (await load<UserProfile[]>(K.CONTACTS)) ?? []
if (!current.find(c => c.userId === user.userId)) {
await save(K.CONTACTS, [...current, user])
}
Alert.alert('已添加为好友')
} catch {
Alert.alert('添加失败')
}
Alert.alert('已添加为好友')
} catch {
Alert.alert('添加失败')
}
}
@ -87,7 +99,7 @@ export default function UserSearchScreen() {
</View>
</TouchableOpacity>
<TouchableOpacity style={styles.addBtn} onPress={() => addContact(item)}>
<Text style={styles.addBtnText}>+ </Text>
<Text style={styles.addBtnText}>{addToGroupId ? '加入群聊' : '+ 添加'}</Text>
</TouchableOpacity>
</View>
)

查看文件

@ -57,7 +57,7 @@ export default function GroupSettingsScreen({ route }: Props) {
{isAdmin && (
<>
<View style={styles.sep} />
<Row label="添加成员" onPress={() => (nav as any).navigate('UserSearch')} />
<Row label="添加成员" onPress={() => (nav as any).navigate('UserSearch', { addToGroupId: groupId })} />
</>
)}
</View>