Fixes to reposted-by list

zio/stable
Paul Frazee 2023-01-19 16:50:16 -06:00
parent c11bfc7541
commit a24b29d64b
2 changed files with 73 additions and 86 deletions

View File

@ -1,31 +1,11 @@
import {makeAutoObservable, runInAction} from 'mobx' import {makeAutoObservable, runInAction} from 'mobx'
import {AtUri} from '../../third-party/uri' import {AtUri} from '../../third-party/uri'
import { import {AppBskyFeedGetRepostedBy as GetRepostedBy} from '@atproto/api'
AppBskyFeedGetRepostedBy as GetRepostedBy,
AppBskySystemDeclRef,
} from '@atproto/api'
type DeclRef = AppBskySystemDeclRef.Main
import {RootStoreModel} from './root-store' import {RootStoreModel} from './root-store'
export class RepostedByViewItemModel implements GetRepostedBy.RepostedBy { const PAGE_SIZE = 30
// ui state
_reactKey: string = ''
// data export type RepostedByItem = GetRepostedBy.RepostedBy
did: string = ''
handle: string = ''
displayName: string = ''
avatar?: string
declaration: DeclRef = {cid: '', actorType: ''}
createdAt?: string
indexedAt: string = ''
constructor(reactKey: string, v: RepostedByItem) {
makeAutoObservable(this)
this._reactKey = reactKey
Object.assign(this, v)
}
}
export class RepostedByViewModel { export class RepostedByViewModel {
// state // state
@ -35,10 +15,13 @@ export class RepostedByViewModel {
error = '' error = ''
resolvedUri = '' resolvedUri = ''
params: GetRepostedBy.QueryParams params: GetRepostedBy.QueryParams
hasMore = true
loadMoreCursor?: string
private _loadMorePromise: Promise<void> | undefined
// data // data
uri: string = '' uri: string = ''
repostedBy: RepostedByViewItemModel[] = [] repostedBy: RepostedByItem[] = []
constructor( constructor(
public rootStore: RootStoreModel, public rootStore: RootStoreModel,
@ -70,19 +53,20 @@ export class RepostedByViewModel {
// public api // public api
// = // =
async setup() { async refresh() {
return this.loadMore(true)
}
async loadMore(isRefreshing = false) {
if (this._loadMorePromise) {
return this._loadMorePromise
}
if (!this.resolvedUri) { if (!this.resolvedUri) {
await this._resolveUri() await this._resolveUri()
} }
await this._fetch() this._loadMorePromise = this._loadMore(isRefreshing)
} await this._loadMorePromise
this._loadMorePromise = undefined
async refresh() {
await this._fetch(true)
}
async loadMore() {
// TODO
} }
// state transitions // state transitions
@ -121,34 +105,28 @@ export class RepostedByViewModel {
}) })
} }
private async _fetch(isRefreshing = false) { private async _loadMore(isRefreshing = false) {
this._xLoading(isRefreshing) this._xLoading(isRefreshing)
try { try {
const res = await this.rootStore.api.app.bsky.feed.getRepostedBy( const params = Object.assign({}, this.params, {
Object.assign({}, this.params, {uri: this.resolvedUri}), uri: this.resolvedUri,
) limit: PAGE_SIZE,
this._replaceAll(res) before: this.loadMoreCursor,
})
if (this.isRefreshing) {
this.repostedBy = []
}
const res = await this.rootStore.api.app.bsky.feed.getRepostedBy(params)
await this._appendAll(res)
this._xIdle() this._xIdle()
} catch (e: any) { } catch (e: any) {
this._xIdle(e) this._xIdle(e)
} }
} }
private async _refresh() { private _appendAll(res: GetRepostedBy.Response) {
this._xLoading(true) this.loadMoreCursor = res.data.cursor
// TODO: refetch and update items this.hasMore = !!this.loadMoreCursor
this._xIdle() this.repostedBy = this.repostedBy.concat(res.data.repostedBy)
}
private _replaceAll(res: GetRepostedBy.Response) {
this.repostedBy.length = 0
let counter = 0
for (const item of res.data.repostedBy) {
this._append(counter++, item)
}
}
private _append(keyId: number, item: RepostedByItem) {
this.repostedBy.push(new RepostedByViewItemModel(`item-${keyId}`, item))
} }
} }

View File

@ -3,7 +3,7 @@ import {observer} from 'mobx-react-lite'
import {ActivityIndicator, FlatList, StyleSheet, View} from 'react-native' import {ActivityIndicator, FlatList, StyleSheet, View} from 'react-native'
import { import {
RepostedByViewModel, RepostedByViewModel,
RepostedByViewItemModel, RepostedByItem,
} from '../../../state/models/reposted-by-view' } from '../../../state/models/reposted-by-view'
import {UserAvatar} from '../util/UserAvatar' import {UserAvatar} from '../util/UserAvatar'
import {ErrorMessage} from '../util/error/ErrorMessage' import {ErrorMessage} from '../util/error/ErrorMessage'
@ -18,31 +18,29 @@ export const PostRepostedBy = observer(function PostRepostedBy({
uri: string uri: string
}) { }) {
const store = useStores() const store = useStores()
// Using default import (React.use...) instead of named import (use...) to be able to mock store's data in jest environment const view = React.useMemo(
const [view, setView] = React.useState<RepostedByViewModel | undefined>() () => new RepostedByViewModel(store, {uri}),
[store, uri],
)
useEffect(() => { useEffect(() => {
if (view?.params.uri === uri) { view
return // no change needed? or trigger refresh? .loadMore()
} .catch(err => store.log.error('Failed to fetch user followers', err))
const newView = new RepostedByViewModel(store, {uri}) }, [view, store.log])
setView(newView)
newView
.setup()
.catch(err => store.log.error('Failed to fetch reposted by', err))
}, [uri, view?.params.uri, store])
const onRefresh = () => { const onRefresh = () => {
view?.refresh() view.refresh()
}
const onEndReached = () => {
view
.loadMore()
.catch(err =>
view?.rootStore.log.error('Failed to load more followers', err),
)
} }
// loading if (!view.hasLoaded) {
// =
if (
!view ||
(view.isLoading && !view.isRefreshing) ||
view.params.uri !== uri
) {
return ( return (
<View> <View>
<ActivityIndicator /> <ActivityIndicator />
@ -66,22 +64,29 @@ export const PostRepostedBy = observer(function PostRepostedBy({
// loaded // loaded
// = // =
const renderItem = ({item}: {item: RepostedByViewItemModel}) => ( const renderItem = ({item}: {item: RepostedByItem}) => (
<RepostedByItem item={item} /> <RepostedByItemCom item={item} />
) )
return ( return (
<View>
<FlatList <FlatList
data={view.repostedBy} data={view.repostedBy}
keyExtractor={item => item._reactKey} keyExtractor={item => item.did}
refreshing={view.isRefreshing}
onRefresh={onRefresh}
onEndReached={onEndReached}
renderItem={renderItem} renderItem={renderItem}
contentContainerStyle={{paddingBottom: 200}} initialNumToRender={15}
/> ListFooterComponent={() => (
<View style={styles.footer}>
{view.isLoading && <ActivityIndicator />}
</View> </View>
)}
extraData={view.isLoading}
/>
) )
}) })
const RepostedByItem = ({item}: {item: RepostedByViewItemModel}) => { const RepostedByItemCom = ({item}: {item: RepostedByItem}) => {
return ( return (
<Link <Link
style={styles.outer} style={styles.outer}
@ -132,4 +137,8 @@ const styles = StyleSheet.create({
paddingTop: 10, paddingTop: 10,
paddingBottom: 10, paddingBottom: 10,
}, },
footer: {
height: 200,
paddingTop: 20,
},
}) })