Batch notification loads to avoid layout reflows due to async
parent
b9c9895c45
commit
6ec33dd89f
|
@ -1,6 +1,7 @@
|
||||||
import {makeAutoObservable} from 'mobx'
|
import {makeAutoObservable, runInAction} from 'mobx'
|
||||||
import * as ListNotifications from '../../third-party/api/src/client/types/app/bsky/notification/list'
|
import * as ListNotifications from '../../third-party/api/src/client/types/app/bsky/notification/list'
|
||||||
import {RootStoreModel} from './root-store'
|
import {RootStoreModel} from './root-store'
|
||||||
|
import {PostThreadViewModel} from './post-thread-view'
|
||||||
import {Declaration} from './_common'
|
import {Declaration} from './_common'
|
||||||
import {hasProp} from '../lib/type-guards'
|
import {hasProp} from '../lib/type-guards'
|
||||||
import {APP_BSKY_GRAPH} from '../../third-party/api'
|
import {APP_BSKY_GRAPH} from '../../third-party/api'
|
||||||
|
@ -34,6 +35,9 @@ export class NotificationsViewItemModel implements GroupedNotification {
|
||||||
indexedAt: string = ''
|
indexedAt: string = ''
|
||||||
additional?: NotificationsViewItemModel[]
|
additional?: NotificationsViewItemModel[]
|
||||||
|
|
||||||
|
// additional data
|
||||||
|
additionalPost?: PostThreadViewModel
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public rootStore: RootStoreModel,
|
public rootStore: RootStoreModel,
|
||||||
reactKey: string,
|
reactKey: string,
|
||||||
|
@ -89,6 +93,13 @@ export class NotificationsViewItemModel implements GroupedNotification {
|
||||||
return this.reason === 'assertion'
|
return this.reason === 'assertion'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get needsAdditionalData() {
|
||||||
|
if (this.isUpvote || this.isRepost || this.isTrend || this.isReply) {
|
||||||
|
return !this.additionalPost
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
get isInvite() {
|
get isInvite() {
|
||||||
return (
|
return (
|
||||||
this.isAssertion && this.record.assertion === APP_BSKY_GRAPH.AssertMember
|
this.isAssertion && this.record.assertion === APP_BSKY_GRAPH.AssertMember
|
||||||
|
@ -107,6 +118,27 @@ export class NotificationsViewItemModel implements GroupedNotification {
|
||||||
}
|
}
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fetchAdditionalData() {
|
||||||
|
if (!this.needsAdditionalData) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let postUri
|
||||||
|
if (this.isReply) {
|
||||||
|
postUri = this.uri
|
||||||
|
} else if (this.isUpvote || this.isRead || this.isTrend) {
|
||||||
|
postUri = this.subjectUri
|
||||||
|
}
|
||||||
|
if (postUri) {
|
||||||
|
this.additionalPost = new PostThreadViewModel(this.rootStore, {
|
||||||
|
uri: postUri,
|
||||||
|
depth: 0,
|
||||||
|
})
|
||||||
|
await this.additionalPost.setup().catch(e => {
|
||||||
|
console.error('Failed to load post needed by notification', e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NotificationsViewModel {
|
export class NotificationsViewModel {
|
||||||
|
@ -246,7 +278,7 @@ export class NotificationsViewModel {
|
||||||
limit: PAGE_SIZE,
|
limit: PAGE_SIZE,
|
||||||
})
|
})
|
||||||
const res = await this.rootStore.api.app.bsky.notification.list(params)
|
const res = await this.rootStore.api.app.bsky.notification.list(params)
|
||||||
this._replaceAll(res)
|
await this._replaceAll(res)
|
||||||
this._xIdle()
|
this._xIdle()
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this._xIdle(`Failed to load notifications: ${e.toString()}`)
|
this._xIdle(`Failed to load notifications: ${e.toString()}`)
|
||||||
|
@ -264,7 +296,7 @@ export class NotificationsViewModel {
|
||||||
before: this.loadMoreCursor,
|
before: this.loadMoreCursor,
|
||||||
})
|
})
|
||||||
const res = await this.rootStore.api.app.bsky.notification.list(params)
|
const res = await this.rootStore.api.app.bsky.notification.list(params)
|
||||||
this._appendAll(res)
|
await this._appendAll(res)
|
||||||
this._xIdle()
|
this._xIdle()
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this._xIdle(`Failed to load notifications: ${e.toString()}`)
|
this._xIdle(`Failed to load notifications: ${e.toString()}`)
|
||||||
|
@ -296,25 +328,37 @@ export class NotificationsViewModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _replaceAll(res: ListNotifications.Response) {
|
private async _replaceAll(res: ListNotifications.Response) {
|
||||||
this.notifications.length = 0
|
this.notifications.length = 0
|
||||||
this._appendAll(res)
|
return this._appendAll(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
private _appendAll(res: ListNotifications.Response) {
|
private async _appendAll(res: ListNotifications.Response) {
|
||||||
this.loadMoreCursor = res.data.cursor
|
this.loadMoreCursor = res.data.cursor
|
||||||
this.hasMore = !!this.loadMoreCursor
|
this.hasMore = !!this.loadMoreCursor
|
||||||
let counter = this.notifications.length
|
let counter = this.notifications.length
|
||||||
|
const promises = []
|
||||||
|
const itemModels: NotificationsViewItemModel[] = []
|
||||||
for (const item of groupNotifications(res.data.notifications)) {
|
for (const item of groupNotifications(res.data.notifications)) {
|
||||||
this._append(counter++, item)
|
const itemModel = new NotificationsViewItemModel(
|
||||||
|
this.rootStore,
|
||||||
|
`item-${counter++}`,
|
||||||
|
item,
|
||||||
|
)
|
||||||
|
if (itemModel.needsAdditionalData) {
|
||||||
|
promises.push(itemModel.fetchAdditionalData())
|
||||||
|
}
|
||||||
|
itemModels.push(itemModel)
|
||||||
}
|
}
|
||||||
}
|
await Promise.all(promises).catch(e => {
|
||||||
|
console.error(
|
||||||
private _append(keyId: number, item: GroupedNotification) {
|
'Uncaught failure during notifications-view _appendAll()',
|
||||||
// TODO: validate .record
|
e,
|
||||||
this.notifications.push(
|
)
|
||||||
new NotificationsViewItemModel(this.rootStore, `item-${keyId}`, item),
|
})
|
||||||
)
|
runInAction(() => {
|
||||||
|
this.notifications = this.notifications.concat(itemModels)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private _updateAll(res: ListNotifications.Response) {
|
private _updateAll(res: ListNotifications.Response) {
|
||||||
|
|
|
@ -4,11 +4,12 @@ import {StyleSheet, Text, View} from 'react-native'
|
||||||
import {AtUri} from '../../../third-party/uri'
|
import {AtUri} from '../../../third-party/uri'
|
||||||
import {FontAwesomeIcon, Props} from '@fortawesome/react-native-fontawesome'
|
import {FontAwesomeIcon, Props} from '@fortawesome/react-native-fontawesome'
|
||||||
import {NotificationsViewItemModel} from '../../../state/models/notifications-view'
|
import {NotificationsViewItemModel} from '../../../state/models/notifications-view'
|
||||||
|
import {PostThreadViewModel} from '../../../state/models/post-thread-view'
|
||||||
import {s, colors} from '../../lib/styles'
|
import {s, colors} from '../../lib/styles'
|
||||||
import {ago, pluralize} from '../../../lib/strings'
|
import {ago, pluralize} from '../../../lib/strings'
|
||||||
import {UpIconSolid} from '../../lib/icons'
|
import {UpIconSolid} from '../../lib/icons'
|
||||||
import {UserAvatar} from '../util/UserAvatar'
|
import {UserAvatar} from '../util/UserAvatar'
|
||||||
import {PostText} from '../post/PostText'
|
import {ErrorMessage} from '../util/ErrorMessage'
|
||||||
import {Post} from '../post/Post'
|
import {Post} from '../post/Post'
|
||||||
import {Link} from '../util/Link'
|
import {Link} from '../util/Link'
|
||||||
import {InviteAccepter} from './InviteAccepter'
|
import {InviteAccepter} from './InviteAccepter'
|
||||||
|
@ -51,7 +52,7 @@ export const FeedItem = observer(function FeedItem({
|
||||||
]}
|
]}
|
||||||
href={itemHref}
|
href={itemHref}
|
||||||
title={itemTitle}>
|
title={itemTitle}>
|
||||||
<Post uri={item.uri} />
|
<Post uri={item.uri} initView={item.additionalPost} />
|
||||||
</Link>
|
</Link>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -170,7 +171,7 @@ export const FeedItem = observer(function FeedItem({
|
||||||
</Text>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
{item.isUpvote || item.isRepost || item.isTrend ? (
|
{item.isUpvote || item.isRepost || item.isTrend ? (
|
||||||
<PostText uri={item.subjectUri} style={[s.gray5]} />
|
<AdditionalPostText additionalPost={item.additionalPost} />
|
||||||
) : (
|
) : (
|
||||||
<></>
|
<></>
|
||||||
)}
|
)}
|
||||||
|
@ -181,17 +182,24 @@ export const FeedItem = observer(function FeedItem({
|
||||||
<InviteAccepter item={item} />
|
<InviteAccepter item={item} />
|
||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
{item.isReply ? (
|
|
||||||
<View style={s.pt5}>
|
|
||||||
<Post uri={item.uri} />
|
|
||||||
</View>
|
|
||||||
) : (
|
|
||||||
<></>
|
|
||||||
)}
|
|
||||||
</Link>
|
</Link>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function AdditionalPostText({
|
||||||
|
additionalPost,
|
||||||
|
}: {
|
||||||
|
additionalPost?: PostThreadViewModel
|
||||||
|
}) {
|
||||||
|
if (!additionalPost) {
|
||||||
|
return <View />
|
||||||
|
}
|
||||||
|
if (additionalPost.error) {
|
||||||
|
return <ErrorMessage message={additionalPost.error} />
|
||||||
|
}
|
||||||
|
return <Text style={[s.gray5]}>{additionalPost.thread?.record.text}</Text>
|
||||||
|
}
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
outer: {
|
outer: {
|
||||||
backgroundColor: colors.white,
|
backgroundColor: colors.white,
|
||||||
|
|
|
@ -15,19 +15,25 @@ import {UserAvatar} from '../util/UserAvatar'
|
||||||
import {useStores} from '../../../state'
|
import {useStores} from '../../../state'
|
||||||
import {s, colors} from '../../lib/styles'
|
import {s, colors} from '../../lib/styles'
|
||||||
|
|
||||||
export const Post = observer(function Post({uri}: {uri: string}) {
|
export const Post = observer(function Post({
|
||||||
|
uri,
|
||||||
|
initView,
|
||||||
|
}: {
|
||||||
|
uri: string
|
||||||
|
initView?: PostThreadViewModel
|
||||||
|
}) {
|
||||||
const store = useStores()
|
const store = useStores()
|
||||||
const [view, setView] = useState<PostThreadViewModel | undefined>()
|
const [view, setView] = useState<PostThreadViewModel | undefined>(initView)
|
||||||
const [deleted, setDeleted] = useState(false)
|
const [deleted, setDeleted] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (view?.params.uri === uri) {
|
if (initView || view?.params.uri === uri) {
|
||||||
return // no change needed? or trigger refresh?
|
return // no change needed? or trigger refresh?
|
||||||
}
|
}
|
||||||
const newView = new PostThreadViewModel(store, {uri, depth: 0})
|
const newView = new PostThreadViewModel(store, {uri, depth: 0})
|
||||||
setView(newView)
|
setView(newView)
|
||||||
newView.setup().catch(err => console.error('Failed to fetch post', err))
|
newView.setup().catch(err => console.error('Failed to fetch post', err))
|
||||||
}, [uri, view?.params.uri, store])
|
}, [initView, uri, view?.params.uri, store])
|
||||||
|
|
||||||
// deleted
|
// deleted
|
||||||
// =
|
// =
|
||||||
|
|
Loading…
Reference in New Issue