Harvest valuable features from external/ reference repos, adapted to each product's stack and wired in; all products typecheck clean. - gitgram-web (5): clickable @mention/#hashtag linkify in rendered content (munia), + feed/notification/accessibility patterns; install @testing-library/dom - gitgram-backend (3): feed thread dedup + muted-thread removal (bluesky), GitHub star/follow write-back via OAuth (gh-social) - gitgram-mobile (4): double-tap-to-like + heart bounce (proximity), haptics layer (bluesky useHaptics), + gesture/feed UX - aitm-api (4): beads ready/dependency-DAG endpoints + atomic claim (beads) - aitm-mcp (6): beads_ready work-queue tool, context-efficient projections, transport arg aliasing (context7/pal-mcp/beads) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
240 lines
6.2 KiB
TypeScript
240 lines
6.2 KiB
TypeScript
import React, { useRef, useState } from 'react';
|
|
import {
|
|
View,
|
|
Text,
|
|
StyleSheet,
|
|
TouchableOpacity,
|
|
} from 'react-native';
|
|
import { useRouter } from 'expo-router';
|
|
import { Colors } from '../../constants/Colors';
|
|
import { Image } from 'expo-image';
|
|
import { momentService } from '../../services/momentService';
|
|
import { logger } from '../../utils/logger';
|
|
import Icon from 'react-native-vector-icons/MaterialIcons';
|
|
import { useHaptics } from '../../hooks/useHaptics';
|
|
import LikeBounceAnimation, {
|
|
LikeBounceAnimationHandle,
|
|
} from '../common/LikeBounceAnimation';
|
|
|
|
const DOUBLE_PRESS_DELAY = 300;
|
|
|
|
interface MomentCardProps {
|
|
moment: {
|
|
id: string;
|
|
screenshotUrl: string;
|
|
selfieUrl: string;
|
|
caption?: string;
|
|
user: {
|
|
id: string;
|
|
username: string;
|
|
avatar?: string;
|
|
};
|
|
likesCount: number;
|
|
commentsCount: number;
|
|
createdAt: string;
|
|
};
|
|
}
|
|
|
|
export default function MomentCard({ moment }: MomentCardProps) {
|
|
const router = useRouter();
|
|
const haptics = useHaptics();
|
|
const likeBounceRef = useRef<LikeBounceAnimationHandle>(null);
|
|
const lastTapRef = useRef(0);
|
|
const [isLiked, setIsLiked] = useState(false);
|
|
const [likesCount, setLikesCount] = useState(moment.likesCount);
|
|
const [liking, setLiking] = useState(false);
|
|
|
|
const handleLike = async (mode: 'toggle' | 'like-only' = 'toggle') => {
|
|
if (liking) return;
|
|
if (mode === 'like-only' && isLiked) return;
|
|
|
|
haptics('Medium');
|
|
|
|
const previousIsLiked = isLiked;
|
|
setIsLiked(!previousIsLiked);
|
|
setLikesCount((prev) => (!previousIsLiked ? prev + 1 : prev - 1));
|
|
setLiking(true);
|
|
|
|
try {
|
|
const response = await momentService.likeMoment(moment.id);
|
|
setIsLiked(response.liked);
|
|
setLikesCount(moment.likesCount + (response.liked ? 1 : 0));
|
|
} catch (error) {
|
|
// Roll back the optimistic update on failure.
|
|
setIsLiked(previousIsLiked);
|
|
setLikesCount((prev) => (!previousIsLiked ? prev - 1 : prev + 1));
|
|
logger.error('Like moment error:', error);
|
|
} finally {
|
|
setLiking(false);
|
|
}
|
|
};
|
|
|
|
const handlePreviewPress = () => {
|
|
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;
|
|
router.push(`/moments/${moment.id}`);
|
|
};
|
|
|
|
return (
|
|
<View style={styles.container}>
|
|
<TouchableOpacity activeOpacity={0.95} onPress={handlePreviewPress}>
|
|
<View style={styles.momentContainer}>
|
|
<View style={styles.screenshotContainer}>
|
|
<Image
|
|
source={{ uri: moment.screenshotUrl }}
|
|
style={styles.screenshot}
|
|
accessibilityLabel={moment.caption || `Moment by ${moment.user.username}`}
|
|
/>
|
|
</View>
|
|
<View style={styles.selfieContainer}>
|
|
<Image
|
|
source={{ uri: moment.selfieUrl }}
|
|
style={styles.selfie}
|
|
accessibilityLabel={`Selfie by ${moment.user.username}`}
|
|
/>
|
|
</View>
|
|
</View>
|
|
<LikeBounceAnimation ref={likeBounceRef} color="#ff4757" />
|
|
</TouchableOpacity>
|
|
|
|
<View style={styles.infoContainer}>
|
|
<View style={styles.userInfo}>
|
|
{moment.user.avatar ? (
|
|
<Image source={{ uri: moment.user.avatar }} style={styles.avatar} />
|
|
) : (
|
|
<View style={styles.avatarPlaceholder}>
|
|
<Text style={styles.avatarText}>
|
|
{moment.user.username[0].toUpperCase()}
|
|
</Text>
|
|
</View>
|
|
)}
|
|
<Text style={styles.username}>@{moment.user.username}</Text>
|
|
</View>
|
|
|
|
{moment.caption && (
|
|
<Text style={styles.caption} numberOfLines={2}>
|
|
{moment.caption}
|
|
</Text>
|
|
)}
|
|
|
|
<View style={styles.actions}>
|
|
<TouchableOpacity
|
|
style={styles.actionButton}
|
|
onPress={() => handleLike('toggle')}
|
|
disabled={liking}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={isLiked ? 'Unlike moment' : 'Like moment'}
|
|
>
|
|
<Icon
|
|
name={isLiked ? 'favorite' : 'favorite-border'}
|
|
size={20}
|
|
color={isLiked ? Colors.error || '#ff4444' : Colors.text}
|
|
/>
|
|
<Text style={styles.actionText}>{likesCount}</Text>
|
|
</TouchableOpacity>
|
|
|
|
<TouchableOpacity
|
|
style={styles.actionButton}
|
|
onPress={() => router.push(`/moments/${moment.id}/reactions`)}
|
|
>
|
|
<Icon name="comment" size={20} color={Colors.text} />
|
|
<Text style={styles.actionText}>{moment.commentsCount}</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
</View>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
backgroundColor: Colors.surface,
|
|
borderRadius: 12,
|
|
marginBottom: 16,
|
|
overflow: 'hidden',
|
|
},
|
|
momentContainer: {
|
|
flexDirection: 'row',
|
|
height: 200,
|
|
},
|
|
screenshotContainer: {
|
|
flex: 2,
|
|
backgroundColor: Colors.background,
|
|
},
|
|
screenshot: {
|
|
width: '100%',
|
|
height: '100%',
|
|
},
|
|
selfieContainer: {
|
|
flex: 1,
|
|
backgroundColor: Colors.background,
|
|
borderLeftWidth: 2,
|
|
borderLeftColor: Colors.border,
|
|
},
|
|
selfie: {
|
|
width: '100%',
|
|
height: '100%',
|
|
},
|
|
infoContainer: {
|
|
padding: 12,
|
|
},
|
|
userInfo: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
marginBottom: 8,
|
|
gap: 8,
|
|
},
|
|
avatar: {
|
|
width: 32,
|
|
height: 32,
|
|
borderRadius: 16,
|
|
},
|
|
avatarPlaceholder: {
|
|
width: 32,
|
|
height: 32,
|
|
borderRadius: 16,
|
|
backgroundColor: Colors.accent,
|
|
justifyContent: 'center',
|
|
alignItems: 'center',
|
|
},
|
|
avatarText: {
|
|
color: Colors.text,
|
|
fontSize: 14,
|
|
fontWeight: 'bold',
|
|
},
|
|
username: {
|
|
fontSize: 14,
|
|
fontWeight: '600',
|
|
color: Colors.text,
|
|
},
|
|
caption: {
|
|
fontSize: 14,
|
|
color: Colors.text,
|
|
marginBottom: 8,
|
|
lineHeight: 20,
|
|
},
|
|
actions: {
|
|
flexDirection: 'row',
|
|
gap: 16,
|
|
paddingTop: 8,
|
|
borderTopWidth: 1,
|
|
borderTopColor: Colors.border,
|
|
},
|
|
actionButton: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
gap: 4,
|
|
},
|
|
actionText: {
|
|
fontSize: 14,
|
|
color: Colors.text,
|
|
},
|
|
});
|
|
|