feat(post): add post reference functionality with inline cards and suggestion search
Add support for referencing/quoting other posts within post content. Includes: - New `post_ref` segment type with `PostRefSegmentData` interface for post references - New `PostRefCard` component to display referenced posts inline - Post suggestion search triggered by `#` in `PostMentionInput` - Updated `PostContentRenderer` to render post reference segments - Add `suggestPosts` and `recordRefClick` API methods in postService Additional improvements: - Add image gallery preview for user profile covers and avatars - Make text selectable across CommentItem, PostContentRenderer, and PostDetailScreen - Migrate FlatList to FlashList in PostDetailScreen and UserProfileScreen - Add nestedScrollEnabled to CreatePostScreen - Add clickable member avatars in GroupMembersScreen
This commit is contained in:
@@ -8,7 +8,7 @@ import React, { useState, useEffect, useCallback, useRef } from 'react';
|
||||
import {
|
||||
View,
|
||||
TextInput,
|
||||
FlatList,
|
||||
ScrollView,
|
||||
TouchableOpacity,
|
||||
StyleSheet,
|
||||
Platform,
|
||||
@@ -20,6 +20,7 @@ import Avatar from '../common/Avatar';
|
||||
import { useAppColors, spacing, fontSizes, borderRadius } from '../../theme';
|
||||
import { useAuthStore } from '../../stores';
|
||||
import { authService } from '../../services/auth';
|
||||
import { postService } from '../../services/post/postService';
|
||||
import { MessageSegment, AtSegmentData } from '../../types';
|
||||
|
||||
interface MentionUser {
|
||||
@@ -28,6 +29,14 @@ interface MentionUser {
|
||||
avatar: string | null;
|
||||
}
|
||||
|
||||
interface SuggestPost {
|
||||
id: string;
|
||||
title: string;
|
||||
channel?: string;
|
||||
}
|
||||
|
||||
type SuggestionMode = 'none' | 'mention' | 'postref';
|
||||
|
||||
interface PostMentionInputProps {
|
||||
value: string;
|
||||
onChangeText: (text: string) => void;
|
||||
@@ -37,6 +46,7 @@ interface PostMentionInputProps {
|
||||
style?: any;
|
||||
minHeight?: number;
|
||||
autoExpand?: boolean;
|
||||
onPostRefPasted?: (postId: string, title: string) => void;
|
||||
}
|
||||
|
||||
const PostMentionInput: React.FC<PostMentionInputProps> = ({
|
||||
@@ -48,14 +58,17 @@ const PostMentionInput: React.FC<PostMentionInputProps> = ({
|
||||
style,
|
||||
minHeight = 100,
|
||||
autoExpand = false,
|
||||
onPostRefPasted,
|
||||
}) => {
|
||||
const colors = useAppColors();
|
||||
const currentUser = useAuthStore(s => s.currentUser);
|
||||
const [followingUsers, setFollowingUsers] = useState<MentionUser[]>([]);
|
||||
const [showMentions, setShowMentions] = useState(false);
|
||||
const [suggestPosts, setSuggestPosts] = useState<SuggestPost[]>([]);
|
||||
const [suggestionMode, setSuggestionMode] = useState<SuggestionMode>('none');
|
||||
const [mentionQuery, setMentionQuery] = useState('');
|
||||
const [mentionStartIndex, setMentionStartIndex] = useState(-1);
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [postSearchTimer, setPostSearchTimer] = useState<ReturnType<typeof setTimeout> | null>(null);
|
||||
const inputRef = useRef<TextInput>(null);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -125,8 +138,42 @@ const PostMentionInput: React.FC<PostMentionInputProps> = ({
|
||||
onChangeText(text);
|
||||
|
||||
const cursorPos = text.length;
|
||||
let atStart = -1;
|
||||
|
||||
// Check for # trigger (post ref search)
|
||||
let hashStart = -1;
|
||||
for (let i = cursorPos - 1; i >= 0; i--) {
|
||||
if (text[i] === '#') {
|
||||
hashStart = i;
|
||||
break;
|
||||
}
|
||||
if (text[i] === ' ' || text[i] === '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hashStart >= 0) {
|
||||
const query = text.slice(hashStart + 1, cursorPos);
|
||||
if (query.length >= 1 && !query.includes(' ') && !query.includes('\n')) {
|
||||
if (postSearchTimer) {
|
||||
clearTimeout(postSearchTimer);
|
||||
}
|
||||
const timer = setTimeout(async () => {
|
||||
try {
|
||||
const results = await postService.suggestPosts(query, 8);
|
||||
setSuggestPosts(results);
|
||||
setSuggestionMode('postref');
|
||||
setMentionStartIndex(hashStart);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}, 300);
|
||||
setPostSearchTimer(timer);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for @ trigger (mention)
|
||||
let atStart = -1;
|
||||
for (let i = cursorPos - 1; i >= 0; i--) {
|
||||
if (text[i] === '@') {
|
||||
atStart = i;
|
||||
@@ -142,15 +189,15 @@ const PostMentionInput: React.FC<PostMentionInputProps> = ({
|
||||
if (!query.includes(' ') && !query.includes('\n')) {
|
||||
setMentionQuery(query);
|
||||
setMentionStartIndex(atStart);
|
||||
setShowMentions(true);
|
||||
setSuggestionMode('mention');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setShowMentions(false);
|
||||
setSuggestionMode('none');
|
||||
rebuildSegments(text);
|
||||
},
|
||||
[onChangeText, rebuildSegments]
|
||||
[onChangeText, rebuildSegments, postSearchTimer],
|
||||
);
|
||||
|
||||
const handleSelectMention = useCallback(
|
||||
@@ -168,7 +215,7 @@ const PostMentionInput: React.FC<PostMentionInputProps> = ({
|
||||
});
|
||||
|
||||
onChangeText(newText);
|
||||
setShowMentions(false);
|
||||
setSuggestionMode('none');
|
||||
setMentionQuery('');
|
||||
setMentionStartIndex(-1);
|
||||
|
||||
@@ -176,7 +223,31 @@ const PostMentionInput: React.FC<PostMentionInputProps> = ({
|
||||
|
||||
inputRef.current?.focus();
|
||||
},
|
||||
[value, mentionStartIndex, onChangeText, rebuildSegments]
|
||||
[value, mentionStartIndex, onChangeText, rebuildSegments],
|
||||
);
|
||||
|
||||
const handleSelectPost = useCallback(
|
||||
(post: SuggestPost) => {
|
||||
if (mentionStartIndex < 0) return;
|
||||
|
||||
const before = value.slice(0, mentionStartIndex);
|
||||
const refText = `#${post.title} `;
|
||||
const newText = before + refText;
|
||||
|
||||
onChangeText(newText);
|
||||
setSuggestionMode('none');
|
||||
setSuggestPosts([]);
|
||||
setMentionStartIndex(-1);
|
||||
|
||||
if (onPostRefPasted) {
|
||||
onPostRefPasted(post.id, post.title);
|
||||
}
|
||||
|
||||
rebuildSegments(newText);
|
||||
|
||||
inputRef.current?.focus();
|
||||
},
|
||||
[value, mentionStartIndex, onChangeText, rebuildSegments, onPostRefPasted],
|
||||
);
|
||||
|
||||
const renderMentionItem = ({ item }: { item: MentionUser }) => (
|
||||
@@ -221,26 +292,66 @@ const PostMentionInput: React.FC<PostMentionInputProps> = ({
|
||||
]}
|
||||
/>
|
||||
|
||||
{showMentions && filteredUsers.length > 0 && (
|
||||
{suggestionMode === 'mention' && filteredUsers.length > 0 && (
|
||||
<View style={[styles.mentionPanel, { backgroundColor: colors.background.paper, borderColor: colors.divider }]}>
|
||||
<FlatList
|
||||
data={filteredUsers}
|
||||
renderItem={renderMentionItem}
|
||||
keyExtractor={item => item.id}
|
||||
<ScrollView
|
||||
keyboardShouldPersistTaps="handled"
|
||||
nestedScrollEnabled
|
||||
style={styles.mentionList}
|
||||
/>
|
||||
>
|
||||
{filteredUsers.map(item => (
|
||||
<React.Fragment key={item.id}>
|
||||
{renderMentionItem({ item })}
|
||||
</React.Fragment>
|
||||
))}
|
||||
</ScrollView>
|
||||
</View>
|
||||
)}
|
||||
|
||||
{showMentions && filteredUsers.length === 0 && (
|
||||
{suggestionMode === 'mention' && filteredUsers.length === 0 && (
|
||||
<View style={[styles.mentionPanel, { backgroundColor: colors.background.paper, borderColor: colors.divider }]}>
|
||||
<Text style={[styles.emptyText, { color: colors.text.hint }]}>
|
||||
没有找到匹配的关注用户
|
||||
</Text>
|
||||
</View>
|
||||
)}
|
||||
|
||||
{suggestionMode === 'postref' && suggestPosts.length > 0 && (
|
||||
<View style={[styles.mentionPanel, { backgroundColor: colors.background.paper, borderColor: colors.divider }]}>
|
||||
<ScrollView
|
||||
keyboardShouldPersistTaps="handled"
|
||||
nestedScrollEnabled
|
||||
style={styles.mentionList}
|
||||
>
|
||||
{suggestPosts.map(item => (
|
||||
<TouchableOpacity
|
||||
key={item.id}
|
||||
style={[styles.mentionItem, { borderBottomColor: colors.divider }]}
|
||||
onPress={() => handleSelectPost(item)}
|
||||
activeOpacity={0.7}
|
||||
>
|
||||
<MaterialCommunityIcons name="file-document-outline" size={18} color={colors.primary.main} />
|
||||
<View style={styles.mentionInfo}>
|
||||
<Text style={[styles.mentionName, { color: colors.text.primary }]} numberOfLines={1}>
|
||||
{item.title}
|
||||
</Text>
|
||||
<Text style={[styles.mentionHint, { color: colors.text.hint }]}>
|
||||
点击引用帖子
|
||||
</Text>
|
||||
</View>
|
||||
</TouchableOpacity>
|
||||
))}
|
||||
</ScrollView>
|
||||
</View>
|
||||
)}
|
||||
|
||||
{suggestionMode === 'postref' && suggestPosts.length === 0 && (
|
||||
<View style={[styles.mentionPanel, { backgroundColor: colors.background.paper, borderColor: colors.divider }]}>
|
||||
<Text style={[styles.emptyText, { color: colors.text.hint }]}>
|
||||
没有找到匹配的帖子
|
||||
</Text>
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user