From 89e7a9929247ad76ac7a740a071d3d32e8bacd59 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Thu, 17 Nov 2022 16:26:00 -0600 Subject: [PATCH] Fix post thread title --- src/view/com/post-thread/PostThread.tsx | 26 +++++----------- src/view/screens/PostThread.tsx | 40 +++++++++++++++++++++---- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/view/com/post-thread/PostThread.tsx b/src/view/com/post-thread/PostThread.tsx index ee87a6bf..6b7f96e0 100644 --- a/src/view/com/post-thread/PostThread.tsx +++ b/src/view/com/post-thread/PostThread.tsx @@ -10,20 +10,14 @@ import {SharePostModel} from '../../../state/models/shell-ui' import {PostThreadItem} from './PostThreadItem' import {ErrorMessage} from '../util/ErrorMessage' -export const PostThread = observer(function PostThread({uri}: {uri: string}) { +export const PostThread = observer(function PostThread({ + uri, + view, +}: { + uri: string + view: PostThreadViewModel +}) { const store = useStores() - const [view, setView] = useState() - - useEffect(() => { - if (view?.params.uri === uri) { - console.log('Post thread doing nothing') - return // no change needed? or trigger refresh? - } - console.log('Fetching post thread', uri) - const newView = new PostThreadViewModel(store, {uri}) - setView(newView) - newView.setup().catch(err => console.error('Failed to fetch thread', err)) - }, [uri, view?.params.uri, store]) const onPressShare = (uri: string) => { store.shell.openModal(new SharePostModel(uri)) @@ -34,11 +28,7 @@ export const PostThread = observer(function PostThread({uri}: {uri: string}) { // loading // = - if ( - !view || - (view.isLoading && !view.isRefreshing) || - view.params.uri !== uri - ) { + if ((view.isLoading && !view.isRefreshing) || view.params.uri !== uri) { return ( diff --git a/src/view/screens/PostThread.tsx b/src/view/screens/PostThread.tsx index 69dda742..7db247a3 100644 --- a/src/view/screens/PostThread.tsx +++ b/src/view/screens/PostThread.tsx @@ -1,27 +1,57 @@ -import React, {useEffect} from 'react' +import React, {useEffect, useMemo, useState} from 'react' import {View} from 'react-native' import {makeRecordUri} from '../lib/strings' import {ViewHeader} from '../com/util/ViewHeader' import {PostThread as PostThreadComponent} from '../com/post-thread/PostThread' +import {PostThreadViewModel} from '../../state/models/post-thread-view' import {ScreenParams} from '../routes' import {useStores} from '../../state' export const PostThread = ({navIdx, visible, params}: ScreenParams) => { const store = useStores() const {name, rkey} = params + const [viewSubtitle, setViewSubtitle] = useState(`by ${name}`) const uri = makeRecordUri(name, 'app.bsky.feed.post', rkey) + const view = useMemo( + () => new PostThreadViewModel(store, {uri}), + [uri], + ) + const setTitle = () => { + const author = view.thread?.author + const niceName = author?.handle || name + setViewSubtitle(`by ${niceName}`) + store.nav.setTitle(navIdx, `Post by ${niceName}`) + } useEffect(() => { - if (visible) { - store.nav.setTitle(navIdx, `Post by ${name}`) + let aborted = false + if (!visible) { + return + } + setTitle() + if (!view.hasLoaded && !view.isLoading) { + console.log('Fetching post thread', uri) + view.setup().then( + () => { + if (!aborted) { + setTitle() + } + }, + err => { + console.error('Failed to fetch thread', err) + }, + ) + } + return () => { + aborted = true } }, [visible, store.nav, name]) return ( - + - + )