import React, {useState, useEffect} from 'react' import {observer} from 'mobx-react-lite' import {bsky, AdxUri} from '@adxp/mock-api' import { ActivityIndicator, Image, StyleSheet, Text, TouchableOpacity, View, } from 'react-native' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {PostThreadViewModel} from '../../../state/models/post-thread-view' import {ComposePostModel} from '../../../state/models/shell' import {Link} from '../util/Link' import {useStores} from '../../../state' import {s, colors} from '../../lib/styles' import {ago} from '../../lib/strings' import {AVIS} from '../../lib/assets' export const Post = observer(function Post({uri}: {uri: string}) { const store = useStores() const [view, setView] = useState() useEffect(() => { if (view?.params.uri === uri) { return // no change needed? or trigger refresh? } const newView = new PostThreadViewModel(store, {uri, depth: 0}) setView(newView) newView.setup().catch(err => console.error('Failed to fetch post', err)) }, [uri, view?.params.uri, store]) // loading // = if (!view || view.isLoading || view.params.uri !== uri) { return ( ) } // error // = if (view.hasError || !view.thread) { return ( {view.error || 'Thread not found'} ) } // loaded // = const item = view.thread const record = view.thread?.record as unknown as bsky.Post.Record const itemHref = useMemo(() => { const urip = new AdxUri(item.uri) return `/profile/${item.author.name}/post/${urip.recordKey}` }, [item.uri, item.author.name]) const itemTitle = `Post by ${item.author.name}` const authorHref = `/profile/${item.author.name}` const authorTitle = item.author.name const onPressReply = () => { store.shell.openModal(new ComposePostModel(item.uri)) } const onPressToggleRepost = () => { item .toggleRepost() .catch(e => console.error('Failed to toggle repost', record, e)) } const onPressToggleLike = () => { item .toggleLike() .catch(e => console.error('Failed to toggle like', record, e)) } return ( {item.author.displayName} @{item.author.name} · {ago(item.indexedAt)} {record.text} {item.replyCount} {item.repostCount} {item.likeCount} ) }) const styles = StyleSheet.create({ outer: { marginTop: 1, borderRadius: 6, backgroundColor: colors.white, padding: 10, }, layout: { flexDirection: 'row', }, layoutAvi: { width: 60, }, avi: { width: 50, height: 50, borderRadius: 25, resizeMode: 'cover', }, layoutContent: { flex: 1, }, meta: { flexDirection: 'row', paddingTop: 2, paddingBottom: 2, }, metaItem: { paddingRight: 5, }, postText: { paddingBottom: 8, }, ctrls: { flexDirection: 'row', }, ctrl: { flexDirection: 'row', alignItems: 'center', flex: 1, paddingLeft: 4, paddingRight: 4, }, ctrlIcon: { marginRight: 5, color: colors.gray5, }, ctrlIconReposted: { marginRight: 5, color: colors.green3, }, ctrlIconLiked: { marginRight: 5, color: colors.red3, }, })