Fixes to likes list

zio/stable
Paul Frazee 2023-01-19 16:54:30 -06:00
parent a24b29d64b
commit 6dcf9f8d5b
2 changed files with 68 additions and 83 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 {AppBskyFeedGetVotes as GetVotes} from '@atproto/api'
AppBskyFeedGetVotes as GetVotes,
AppBskyActorRef as ActorRef,
} from '@atproto/api'
import {RootStoreModel} from './root-store' import {RootStoreModel} from './root-store'
export class VotesViewItemModel implements GetVotes.Vote { const PAGE_SIZE = 30
// ui state
_reactKey: string = ''
// data export type VoteItem = GetVotes.Vote
direction: 'up' | 'down' = 'up'
indexedAt: string = ''
createdAt: string = ''
actor: ActorRef.WithInfo = {
did: '',
handle: '',
declaration: {cid: '', actorType: ''},
}
constructor(reactKey: string, v: GetVotes.Vote) {
makeAutoObservable(this)
this._reactKey = reactKey
Object.assign(this, v)
}
}
export class VotesViewModel { export class VotesViewModel {
// state // state
@ -35,10 +15,13 @@ export class VotesViewModel {
error = '' error = ''
resolvedUri = '' resolvedUri = ''
params: GetVotes.QueryParams params: GetVotes.QueryParams
hasMore = true
loadMoreCursor?: string
private _loadMorePromise: Promise<void> | undefined
// data // data
uri: string = '' uri: string = ''
votes: VotesViewItemModel[] = [] votes: VoteItem[] = []
constructor(public rootStore: RootStoreModel, params: GetVotes.QueryParams) { constructor(public rootStore: RootStoreModel, params: GetVotes.QueryParams) {
makeAutoObservable( makeAutoObservable(
@ -67,19 +50,20 @@ export class VotesViewModel {
// 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
@ -118,28 +102,28 @@ export class VotesViewModel {
}) })
} }
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.getVotes( 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.votes = []
}
const res = await this.rootStore.api.app.bsky.feed.getVotes(params)
this._appendAll(res)
this._xIdle() this._xIdle()
} catch (e: any) { } catch (e: any) {
this._xIdle(e) this._xIdle(e)
} }
} }
private _replaceAll(res: GetVotes.Response) { private _appendAll(res: GetVotes.Response) {
this.votes.length = 0 this.loadMoreCursor = res.data.cursor
let counter = 0 this.hasMore = !!this.loadMoreCursor
for (const item of res.data.votes) { this.votes = this.votes.concat(res.data.votes)
this._append(counter++, item)
}
}
private _append(keyId: number, item: GetVotes.Vote) {
this.votes.push(new VotesViewItemModel(`item-${keyId}`, item))
} }
} }

View File

@ -1,10 +1,7 @@
import React, {useEffect} from 'react' import React, {useEffect} from 'react'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import {ActivityIndicator, FlatList, StyleSheet, View} from 'react-native' import {ActivityIndicator, FlatList, StyleSheet, View} from 'react-native'
import { import {VotesViewModel, VotesItem} from '../../../state/models/votes-view'
VotesViewModel,
VotesViewItemModel,
} from '../../../state/models/votes-view'
import {Link} from '../util/Link' import {Link} from '../util/Link'
import {Text} from '../util/text/Text' import {Text} from '../util/text/Text'
import {ErrorMessage} from '../util/error/ErrorMessage' import {ErrorMessage} from '../util/error/ErrorMessage'
@ -20,30 +17,25 @@ export const PostVotedBy = observer(function PostVotedBy({
direction: 'up' | 'down' direction: 'up' | 'down'
}) { }) {
const store = useStores() const store = useStores()
const [view, setView] = React.useState<VotesViewModel | undefined>() const view = React.useMemo(
() => new VotesViewModel(store, {uri, direction}),
[store, uri, direction],
)
useEffect(() => { useEffect(() => {
if (view?.params.uri === uri) { view.loadMore().catch(err => store.log.error('Failed to fetch votes', err))
return // no change needed? or trigger refresh? }, [view, store.log])
}
const newView = new VotesViewModel(store, {uri, direction})
setView(newView)
newView
.setup()
.catch(err => store.log.error('Failed to fetch voted 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 votes', err))
} }
// loading if (!view.hasLoaded) {
// =
if (
!view ||
(view.isLoading && !view.isRefreshing) ||
view.params.uri !== uri
) {
return ( return (
<View> <View>
<ActivityIndicator /> <ActivityIndicator />
@ -67,22 +59,27 @@ export const PostVotedBy = observer(function PostVotedBy({
// loaded // loaded
// = // =
const renderItem = ({item}: {item: VotesViewItemModel}) => ( const renderItem = ({item}: {item: VotesItem}) => <LikedByItem item={item} />
<LikedByItem item={item} />
)
return ( return (
<View>
<FlatList <FlatList
data={view.votes} data={view.votes}
keyExtractor={item => item._reactKey} keyExtractor={item => item.actor.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 LikedByItem = ({item}: {item: VotesViewItemModel}) => { const LikedByItem = ({item}: {item: VotesItem}) => {
return ( return (
<Link <Link
style={styles.outer} style={styles.outer}
@ -135,4 +132,8 @@ const styles = StyleSheet.create({
paddingTop: 10, paddingTop: 10,
paddingBottom: 10, paddingBottom: 10,
}, },
footer: {
height: 200,
paddingTop: 20,
},
}) })