show algos by user on profile

This commit is contained in:
Ansh Nanda 2023-05-12 19:32:39 -07:00
parent fa4af20764
commit 760b5309e0
4 changed files with 99 additions and 17 deletions

View file

@ -1,9 +1,7 @@
import {makeAutoObservable} from 'mobx'
import {
AppBskyFeedGetBookmarkedFeeds as GetBookmarkedFeeds,
// AppBskyFeedBookmarkFeed as bookmarkedFeed,
// AppBskyFeedUnbookmarkFeed as unbookmarkFeed,
AppBskyFeedDefs as FeedDefs,
AppBskyFeedGetActorFeeds as GetActorFeeds,
} from '@atproto/api'
import {RootStoreModel} from '../root-store'
import {bundleAsync} from 'lib/async/bundle'
@ -23,7 +21,10 @@ export class ActorFeedsModel {
// data
feeds: FeedDefs.GeneratorView[] = []
constructor(public rootStore: RootStoreModel) {
constructor(
public rootStore: RootStoreModel,
public params: GetActorFeeds.QueryParams,
) {
makeAutoObservable(
this,
{
@ -69,10 +70,11 @@ export class ActorFeedsModel {
this._xLoading(replace)
try {
const res = await this.rootStore.agent.app.bsky.feed.getActorFeeds({
actor: 'did:plc:dpny6d4qwwxu5b6dp3qob5ok', // TODO: take this as input param
actor: this.params.actor,
limit: PAGE_SIZE,
cursor: replace ? undefined : this.loadMoreCursor,
})
console.log('res', res.data.feeds)
if (replace) {
this._replaceAll(res)
} else {
@ -106,12 +108,12 @@ export class ActorFeedsModel {
// helper functions
// =
_replaceAll(res: GetBookmarkedFeeds.Response) {
_replaceAll(res: GetActorFeeds.Response) {
this.feeds = []
this._appendAll(res)
}
_appendAll(res: GetBookmarkedFeeds.Response) {
_appendAll(res: GetActorFeeds.Response) {
this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor
this.feeds = this.feeds.concat(res.data.feeds)

View file

@ -2,13 +2,20 @@ import {makeAutoObservable} from 'mobx'
import {RootStoreModel} from '../root-store'
import {ProfileModel} from '../content/profile'
import {PostsFeedModel} from '../feeds/posts'
import {ActorFeedsModel} from '../feeds/actor'
import {AppBskyFeedDefs} from '@atproto/api'
export enum Sections {
Posts = 'Posts',
PostsWithReplies = 'Posts & replies',
CustomAlgorithms = 'Algos',
}
const USER_SELECTOR_ITEMS = [Sections.Posts, Sections.PostsWithReplies]
const USER_SELECTOR_ITEMS = [
Sections.Posts,
Sections.PostsWithReplies,
Sections.CustomAlgorithms,
]
export interface ProfileUiParams {
user: string
@ -22,6 +29,7 @@ export class ProfileUiModel {
// data
profile: ProfileModel
feed: PostsFeedModel
algos: ActorFeedsModel
// ui state
selectedViewIndex = 0
@ -43,15 +51,19 @@ export class ProfileUiModel {
actor: params.user,
limit: 10,
})
this.algos = new ActorFeedsModel(rootStore, {actor: params.user})
}
get currentView(): PostsFeedModel {
get currentView(): PostsFeedModel | ActorFeedsModel {
if (
this.selectedView === Sections.Posts ||
this.selectedView === Sections.PostsWithReplies
) {
return this.feed
}
if (this.selectedView === Sections.CustomAlgorithms) {
return this.algos
}
throw new Error(`Invalid selector value: ${this.selectedViewIndex}`)
}
@ -71,12 +83,17 @@ export class ProfileUiModel {
get selectedView() {
return this.selectorItems[this.selectedViewIndex]
}
isGeneratorView(v: any) {
return AppBskyFeedDefs.isGeneratorView(v)
}
get uiItems() {
let arr: any[] = []
// if loading, return loading item to show loading spinner
if (this.isInitialLoading) {
arr = arr.concat([ProfileUiModel.LOADING_ITEM])
} else if (this.currentView.hasError) {
// if error, return error item to show error message
arr = arr.concat([
{
_reactKey: '__error__',
@ -84,12 +101,16 @@ export class ProfileUiModel {
},
])
} else {
// not loading, no error, show content
if (
this.selectedView === Sections.Posts ||
this.selectedView === Sections.PostsWithReplies
this.selectedView === Sections.PostsWithReplies ||
this.selectedView === Sections.CustomAlgorithms
) {
if (this.feed.hasContent) {
if (this.selectedView === Sections.Posts) {
if (this.selectedView === Sections.CustomAlgorithms) {
arr = this.algos.feeds
} else if (this.selectedView === Sections.Posts) {
arr = this.feed.nonReplyFeed
} else {
arr = this.feed.slices.slice()
@ -101,6 +122,7 @@ export class ProfileUiModel {
arr = arr.concat([ProfileUiModel.EMPTY_ITEM])
}
} else {
// fallback, add empty item, to show empty message
arr = arr.concat([ProfileUiModel.EMPTY_ITEM])
}
}