Final harvest round — gitgram-web scout returned no remaining valuable features. - gitgram-mobile (5): swipe-to-reply, date dividers, swipe-to-action chat UX (bluesky) - aitm-mcp (6): transport arg aliasing for hallucinated arg names (context7), + tool envelopes - aitm-api (3): beads search endpoint + comments/notes over the DAG (beads) - gitgram-backend (1): re-process @mentions on comment edit (munia) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
505 lines
14 KiB
TypeScript
505 lines
14 KiB
TypeScript
import React, { useRef, useState } from 'react';
|
|
import {
|
|
View,
|
|
Text,
|
|
StyleSheet,
|
|
TouchableOpacity,
|
|
Alert,
|
|
} from 'react-native';
|
|
import { useRouter } from 'expo-router';
|
|
import { Image } from 'expo-image';
|
|
import { Colors } from '../../constants/Colors';
|
|
import { postService } from '../../services/postService';
|
|
import { userService } from '../../services/userService';
|
|
import { logger } from '../../utils/logger';
|
|
import { useAuthStore } from '../../store/authStore';
|
|
import { useHaptics } from '../../hooks/useHaptics';
|
|
import { useToast } from '../../hooks/useToast';
|
|
import { timeAgo } from '../../utils/timeAgo';
|
|
import PostOptionsModal from '../common/PostOptionsModal';
|
|
import ReportModal from '../common/ReportModal';
|
|
import CodeBlock from '../common/CodeBlock';
|
|
import RichContent from '../common/RichContent';
|
|
import LikeBounceAnimation, {
|
|
LikeBounceAnimationHandle,
|
|
} from '../common/LikeBounceAnimation';
|
|
import CountWheel from '../common/CountWheel';
|
|
import { useLightbox } from '../common/Lightbox';
|
|
|
|
const DOUBLE_PRESS_DELAY = 300;
|
|
|
|
interface Post {
|
|
id: string;
|
|
userId: string;
|
|
content?: string;
|
|
mediaType: string;
|
|
mediaUrl?: string;
|
|
codeSnippet?: string;
|
|
language?: string;
|
|
user: {
|
|
id: string;
|
|
username: string;
|
|
avatar?: string;
|
|
};
|
|
likesCount: number;
|
|
commentsCount: number;
|
|
isLiked: boolean;
|
|
createdAt: string;
|
|
}
|
|
|
|
interface PostCardProps {
|
|
post: Post;
|
|
onLike?: () => void;
|
|
onPress?: () => void;
|
|
showFullContent?: boolean;
|
|
/**
|
|
* Called when the viewer hides this post ("Not interested") or blocks its
|
|
* author. The parent can drop the post from its feed list; if omitted the
|
|
* card hides itself locally.
|
|
*/
|
|
onHide?: (postId: string) => void;
|
|
}
|
|
|
|
export default function PostCard({ post, onLike, showFullContent = false, onHide }: PostCardProps) {
|
|
const router = useRouter();
|
|
const { user } = useAuthStore();
|
|
const haptics = useHaptics();
|
|
const openLightbox = useLightbox();
|
|
const { showToast, ToastComponent } = useToast();
|
|
const [isLiked, setIsLiked] = useState(post.isLiked);
|
|
const [likesCount, setLikesCount] = useState(post.likesCount);
|
|
// Stays false until the viewer toggles the like at least once, so the count
|
|
// wheel never animates on first mount (matching Bluesky's behaviour).
|
|
const [hasToggledLike, setHasToggledLike] = useState(false);
|
|
const [loading, setLoading] = useState(false);
|
|
const [showOptionsModal, setShowOptionsModal] = useState(false);
|
|
const [showReportModal, setShowReportModal] = useState(false);
|
|
const [hidden, setHidden] = useState(false);
|
|
const [isBlocked, setIsBlocked] = useState(false);
|
|
const [blocking, setBlocking] = useState(false);
|
|
const likeBounceRef = useRef<LikeBounceAnimationHandle>(null);
|
|
const lastTapRef = useRef(0);
|
|
|
|
const isOwner = user?.id === post.user.id;
|
|
|
|
// Hide this post from the viewer's feed ("Not interested"). Purely
|
|
// client-side: the parent feed can prune it, otherwise we self-hide.
|
|
const handleHidePost = () => {
|
|
haptics('Light');
|
|
if (onHide) {
|
|
onHide(post.id);
|
|
} else {
|
|
setHidden(true);
|
|
}
|
|
showToast('Post hidden', 'success');
|
|
};
|
|
|
|
const handleBlockUser = () => {
|
|
if (blocking) return;
|
|
const action = isBlocked ? 'Unblock' : 'Block';
|
|
Alert.alert(
|
|
`${action} @${post.user.username}?`,
|
|
isBlocked
|
|
? 'They will be able to see your content and interact with you again.'
|
|
: "You won't see their posts and they won't be able to interact with you.",
|
|
[
|
|
{ text: 'Cancel', style: 'cancel' },
|
|
{
|
|
text: action,
|
|
style: isBlocked ? 'default' : 'destructive',
|
|
onPress: async () => {
|
|
setBlocking(true);
|
|
try {
|
|
if (isBlocked) {
|
|
await userService.unblockUser(post.user.id);
|
|
setIsBlocked(false);
|
|
showToast(`Unblocked @${post.user.username}`, 'success');
|
|
} else {
|
|
await userService.blockUser(post.user.id);
|
|
setIsBlocked(true);
|
|
showToast(`Blocked @${post.user.username}`, 'success');
|
|
// A blocked author's post should leave the feed immediately.
|
|
if (onHide) {
|
|
onHide(post.id);
|
|
} else {
|
|
setHidden(true);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
logger.error('Block user error:', error);
|
|
showToast(`Failed to ${action.toLowerCase()} user`, 'error');
|
|
} finally {
|
|
setBlocking(false);
|
|
}
|
|
},
|
|
},
|
|
],
|
|
);
|
|
};
|
|
|
|
// mode: 'toggle' for the like button, 'like-only' for double-tap (which never
|
|
// unlikes, matching Instagram/TikTok behaviour).
|
|
const handleLike = async (mode: 'toggle' | 'like-only' = 'toggle') => {
|
|
if (loading) return;
|
|
if (mode === 'like-only' && isLiked) return;
|
|
|
|
haptics('Medium');
|
|
|
|
// Optimistic update - update UI immediately
|
|
const previousIsLiked = isLiked;
|
|
const previousLikesCount = likesCount;
|
|
|
|
setHasToggledLike(true);
|
|
setIsLiked(!isLiked);
|
|
setLikesCount(prev => !previousIsLiked ? prev + 1 : prev - 1);
|
|
setLoading(true);
|
|
|
|
try {
|
|
const response = await postService.likePost(post.id);
|
|
// Sync with server response (in case of race conditions)
|
|
setIsLiked(response.liked);
|
|
setLikesCount(response.likesCount || (response.liked ? previousLikesCount + 1 : previousLikesCount - 1));
|
|
onLike?.();
|
|
} catch (error) {
|
|
// Rollback on failure
|
|
setIsLiked(previousIsLiked);
|
|
setLikesCount(previousLikesCount);
|
|
logger.error('Like error:', error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleImagePress = () => {
|
|
const now = Date.now();
|
|
if (now - lastTapRef.current < DOUBLE_PRESS_DELAY) {
|
|
lastTapRef.current = 0;
|
|
likeBounceRef.current?.animate();
|
|
void handleLike('like-only');
|
|
return;
|
|
}
|
|
lastTapRef.current = now;
|
|
if (!showFullContent) {
|
|
router.push(`/post/${post.id}`);
|
|
}
|
|
};
|
|
|
|
const handleImageLongPress = () => {
|
|
if (!post.mediaUrl) return;
|
|
haptics('Light');
|
|
openLightbox([{ uri: post.mediaUrl, alt: post.content || undefined }], 0);
|
|
};
|
|
|
|
if (hidden) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<View style={styles.container}>
|
|
{ToastComponent}
|
|
{/* Card Header */}
|
|
<View style={styles.header}>
|
|
<TouchableOpacity
|
|
style={styles.headerLeft}
|
|
onPress={() => router.push(`/profile/${post.user.id}`)}
|
|
>
|
|
{post.user.avatar ? (
|
|
<Image source={{ uri: post.user.avatar }} style={styles.avatar} />
|
|
) : (
|
|
<View style={styles.avatarPlaceholder}>
|
|
<Text style={styles.avatarText}>{post.user.username[0].toUpperCase()}</Text>
|
|
</View>
|
|
)}
|
|
<View style={styles.headerTextWrap}>
|
|
<Text style={styles.username}>{post.user.username}</Text>
|
|
<Text style={styles.timestamp}>{timeAgo(post.createdAt)}</Text>
|
|
</View>
|
|
</TouchableOpacity>
|
|
<TouchableOpacity style={styles.moreButton} onPress={() => setShowOptionsModal(true)}>
|
|
<Text style={styles.moreIcon}>⋯</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
|
|
{/* Project Visual */}
|
|
{post.mediaType === 'image' && post.mediaUrl && (
|
|
<TouchableOpacity
|
|
onPress={handleImagePress}
|
|
onLongPress={handleImageLongPress}
|
|
delayLongPress={350}
|
|
activeOpacity={showFullContent ? 1 : 0.9}
|
|
>
|
|
<Image
|
|
source={{ uri: post.mediaUrl }}
|
|
style={styles.media}
|
|
contentFit="cover"
|
|
transition={200}
|
|
placeholder={{ blurhash: 'L6PZfSi_.AyE_3t7t7R**0o#DgR4' }}
|
|
cachePolicy="memory-disk"
|
|
accessibilityLabel={post.content || undefined}
|
|
/>
|
|
<LikeBounceAnimation ref={likeBounceRef} />
|
|
</TouchableOpacity>
|
|
)}
|
|
|
|
{post.mediaType === 'code' && post.codeSnippet && (
|
|
<View style={styles.codeWrapper}>
|
|
<CodeBlock
|
|
code={post.codeSnippet}
|
|
language={post.language || 'text'}
|
|
showLineNumbers={post.codeSnippet.split('\n').length > 3}
|
|
/>
|
|
</View>
|
|
)}
|
|
|
|
{/* Action Bar */}
|
|
<View style={styles.actionBar}>
|
|
<View style={styles.actionBarLeft}>
|
|
<TouchableOpacity
|
|
style={styles.actionButton}
|
|
onPress={() => handleLike('toggle')}
|
|
disabled={loading}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={isLiked ? 'Unlike post' : 'Like post'}
|
|
>
|
|
<Text style={styles.actionIcon}>{isLiked ? '❤️' : '🤍'}</Text>
|
|
</TouchableOpacity>
|
|
<TouchableOpacity
|
|
style={styles.actionButton}
|
|
onPress={() => router.push(`/post/${post.id}`)}
|
|
>
|
|
<Text style={styles.actionIcon}>💬</Text>
|
|
</TouchableOpacity>
|
|
<TouchableOpacity
|
|
style={styles.actionButton}
|
|
onPress={() => router.push(`/post/${post.id}/share`)}
|
|
>
|
|
<Text style={styles.actionIcon}>📤</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
<TouchableOpacity style={styles.actionButton}>
|
|
<Text style={styles.actionIcon}>🔖</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
|
|
{/* Engagement & Info */}
|
|
<View style={styles.engagement}>
|
|
<View style={styles.likesRow}>
|
|
{likesCount > 0 ? (
|
|
<>
|
|
<CountWheel
|
|
count={likesCount}
|
|
isToggled={isLiked}
|
|
hasBeenToggled={hasToggledLike}
|
|
renderCount={({ count }) => (
|
|
<Text style={styles.likesText}>{count.toLocaleString()}</Text>
|
|
)}
|
|
/>
|
|
<Text style={styles.likesText}>
|
|
{' '}
|
|
{likesCount === 1 ? 'like' : 'likes'}
|
|
</Text>
|
|
</>
|
|
) : (
|
|
<Text style={styles.likesText}>0 likes</Text>
|
|
)}
|
|
</View>
|
|
{post.content && (
|
|
<View style={styles.captionContainer}>
|
|
<Text style={styles.captionUsername}>{post.user.username} </Text>
|
|
<RichContent content={post.content} textStyle={styles.captionText} />
|
|
</View>
|
|
)}
|
|
{post.commentsCount > 0 && (
|
|
<TouchableOpacity onPress={() => router.push(`/post/${post.id}`)}>
|
|
<Text style={styles.commentsLink}>
|
|
View all {post.commentsCount} {post.commentsCount === 1 ? 'comment' : 'comments'}
|
|
</Text>
|
|
</TouchableOpacity>
|
|
)}
|
|
</View>
|
|
|
|
{/* Chips/Tags */}
|
|
{post.language && (
|
|
<View style={styles.chipsContainer}>
|
|
<View style={styles.chip}>
|
|
<Text style={styles.chipText}>{post.language}</Text>
|
|
</View>
|
|
</View>
|
|
)}
|
|
|
|
{/* Options Modal */}
|
|
<PostOptionsModal
|
|
visible={showOptionsModal}
|
|
onClose={() => setShowOptionsModal(false)}
|
|
onReport={() => setShowReportModal(true)}
|
|
onShare={() => router.push(`/post/${post.id}/share`)}
|
|
onHidePost={handleHidePost}
|
|
onBlockUser={handleBlockUser}
|
|
authorUsername={post.user.username}
|
|
isBlocked={isBlocked}
|
|
isOwner={isOwner}
|
|
/>
|
|
|
|
{/* Report Modal */}
|
|
<ReportModal
|
|
visible={showReportModal}
|
|
onClose={() => setShowReportModal(false)}
|
|
targetType="POST"
|
|
targetId={post.id}
|
|
targetName={post.content ? post.content.substring(0, 50) + (post.content.length > 50 ? '...' : '') : undefined}
|
|
/>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
width: '100%',
|
|
borderBottomWidth: 1,
|
|
borderBottomColor: Colors.border,
|
|
paddingVertical: 16,
|
|
},
|
|
header: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
justifyContent: 'space-between',
|
|
paddingHorizontal: 16,
|
|
paddingBottom: 12,
|
|
},
|
|
headerLeft: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
gap: 12,
|
|
},
|
|
avatar: {
|
|
width: 40,
|
|
height: 40,
|
|
borderRadius: 20,
|
|
},
|
|
avatarPlaceholder: {
|
|
width: 40,
|
|
height: 40,
|
|
borderRadius: 20,
|
|
backgroundColor: Colors.primary,
|
|
justifyContent: 'center',
|
|
alignItems: 'center',
|
|
},
|
|
avatarText: {
|
|
color: Colors.text,
|
|
fontWeight: 'bold',
|
|
fontSize: 16,
|
|
},
|
|
headerTextWrap: {
|
|
flexShrink: 1,
|
|
},
|
|
username: {
|
|
color: Colors.text,
|
|
fontWeight: 'bold',
|
|
fontSize: 14,
|
|
},
|
|
timestamp: {
|
|
color: Colors.textSecondary,
|
|
fontSize: 12,
|
|
marginTop: 1,
|
|
},
|
|
moreButton: {
|
|
width: 32,
|
|
height: 32,
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
borderRadius: 16,
|
|
},
|
|
moreIcon: {
|
|
fontSize: 24,
|
|
color: Colors.text,
|
|
},
|
|
media: {
|
|
width: '100%',
|
|
aspectRatio: 16 / 9,
|
|
backgroundColor: Colors.surface,
|
|
},
|
|
codeWrapper: {
|
|
marginHorizontal: 16,
|
|
},
|
|
actionBar: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
justifyContent: 'space-between',
|
|
paddingHorizontal: 16,
|
|
paddingTop: 12,
|
|
},
|
|
actionBarLeft: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
gap: 8,
|
|
},
|
|
actionButton: {
|
|
width: 40,
|
|
height: 40,
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
borderRadius: 20,
|
|
},
|
|
actionIcon: {
|
|
fontSize: 24,
|
|
},
|
|
engagement: {
|
|
paddingHorizontal: 16,
|
|
paddingTop: 4,
|
|
paddingBottom: 8,
|
|
},
|
|
likesRow: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
marginBottom: 8,
|
|
},
|
|
likesText: {
|
|
color: Colors.text,
|
|
fontSize: 14,
|
|
fontWeight: 'bold',
|
|
},
|
|
captionContainer: {
|
|
flexDirection: 'row',
|
|
flexWrap: 'wrap',
|
|
alignItems: 'flex-start',
|
|
marginBottom: 8,
|
|
},
|
|
captionText: {
|
|
color: Colors.text,
|
|
fontSize: 14,
|
|
lineHeight: 20,
|
|
},
|
|
captionUsername: {
|
|
color: Colors.text,
|
|
fontWeight: 'bold',
|
|
fontSize: 14,
|
|
lineHeight: 20,
|
|
},
|
|
commentsLink: {
|
|
color: Colors.textSecondary,
|
|
fontSize: 14,
|
|
marginTop: 8,
|
|
},
|
|
chipsContainer: {
|
|
flexDirection: 'row',
|
|
gap: 8,
|
|
paddingHorizontal: 16,
|
|
paddingTop: 4,
|
|
overflow: 'hidden',
|
|
},
|
|
chip: {
|
|
height: 28,
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
borderRadius: 14,
|
|
backgroundColor: `${Colors.primary}33`, // 20% opacity
|
|
paddingHorizontal: 12,
|
|
},
|
|
chipText: {
|
|
color: Colors.primary,
|
|
fontSize: 12,
|
|
fontWeight: '500',
|
|
},
|
|
});
|
|
|