Reorganize custom-feed state models and add the missing _reactKey attribute

zio/stable
Paul Frazee 2023-05-17 13:52:16 -05:00
parent a2b089d315
commit b672006f7e
12 changed files with 44 additions and 38 deletions

View File

@ -2,14 +2,16 @@ import {AppBskyFeedDefs, AtUri} from '@atproto/api'
import {makeAutoObservable} from 'mobx' import {makeAutoObservable} from 'mobx'
import {RootStoreModel} from 'state/models/root-store' import {RootStoreModel} from 'state/models/root-store'
export class AlgoItemModel { export class CustomFeedModel {
// data // data
_reactKey: string
data: AppBskyFeedDefs.GeneratorView data: AppBskyFeedDefs.GeneratorView
constructor( constructor(
public rootStore: RootStoreModel, public rootStore: RootStoreModel,
view: AppBskyFeedDefs.GeneratorView, view: AppBskyFeedDefs.GeneratorView,
) { ) {
this._reactKey = view.uri
this.data = view this.data = view
makeAutoObservable( makeAutoObservable(
this, this,
@ -23,7 +25,6 @@ export class AlgoItemModel {
// local actions // local actions
// = // =
set toggleSaved(value: boolean) { set toggleSaved(value: boolean) {
console.log('toggleSaved', this.data.viewer)
if (this.data.viewer) { if (this.data.viewer) {
this.data.viewer.saved = value this.data.viewer.saved = value
} }

View File

@ -1,9 +1,9 @@
import {makeAutoObservable} from 'mobx' import {makeAutoObservable} from 'mobx'
import {AppBskyFeedGetActorFeeds as GetActorFeeds} from '@atproto/api' import {AppBskyFeedGetActorFeeds as GetActorFeeds} from '@atproto/api'
import {RootStoreModel} from '../../root-store' import {RootStoreModel} from '../root-store'
import {bundleAsync} from 'lib/async/bundle' import {bundleAsync} from 'lib/async/bundle'
import {cleanError} from 'lib/strings/errors' import {cleanError} from 'lib/strings/errors'
import {AlgoItemModel} from './algo-item' import {CustomFeedModel} from '../feeds/custom-feed'
const PAGE_SIZE = 30 const PAGE_SIZE = 30
@ -17,7 +17,7 @@ export class ActorFeedsModel {
loadMoreCursor?: string loadMoreCursor?: string
// data // data
feeds: AlgoItemModel[] = [] feeds: CustomFeedModel[] = []
constructor( constructor(
public rootStore: RootStoreModel, public rootStore: RootStoreModel,
@ -72,7 +72,6 @@ export class ActorFeedsModel {
limit: PAGE_SIZE, limit: PAGE_SIZE,
cursor: replace ? undefined : this.loadMoreCursor, cursor: replace ? undefined : this.loadMoreCursor,
}) })
console.log('res', res.data.feeds)
if (replace) { if (replace) {
this._replaceAll(res) this._replaceAll(res)
} else { } else {
@ -115,7 +114,7 @@ export class ActorFeedsModel {
this.loadMoreCursor = res.data.cursor this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor this.hasMore = !!this.loadMoreCursor
for (const f of res.data.feeds) { for (const f of res.data.feeds) {
this.feeds.push(new AlgoItemModel(this.rootStore, f)) this.feeds.push(new CustomFeedModel(this.rootStore, f))
} }
} }
} }

View File

@ -8,7 +8,7 @@ import {PostsFeedModel} from './feeds/posts'
import {NotificationsFeedModel} from './feeds/notifications' import {NotificationsFeedModel} from './feeds/notifications'
import {MyFollowsCache} from './cache/my-follows' import {MyFollowsCache} from './cache/my-follows'
import {isObj, hasProp} from 'lib/type-guards' import {isObj, hasProp} from 'lib/type-guards'
import {SavedFeedsModel} from './feeds/algo/saved' import {SavedFeedsModel} from './ui/saved-feeds'
const PROFILE_UPDATE_INTERVAL = 10 * 60 * 1e3 // 10min const PROFILE_UPDATE_INTERVAL = 10 * 60 * 1e3 // 10min
const NOTIFS_UPDATE_INTERVAL = 30 * 1e3 // 30sec const NOTIFS_UPDATE_INTERVAL = 30 * 1e3 // 30sec

View File

@ -3,7 +3,7 @@ import {AppBskyFeedDefs} from '@atproto/api'
import {RootStoreModel} from '../root-store' import {RootStoreModel} from '../root-store'
import {ProfileModel} from '../content/profile' import {ProfileModel} from '../content/profile'
import {PostsFeedModel} from '../feeds/posts' import {PostsFeedModel} from '../feeds/posts'
import {ActorFeedsModel} from '../feeds/algo/actor' import {ActorFeedsModel} from '../lists/actor-feeds'
import {ListsListModel} from '../lists/lists-list' import {ListsListModel} from '../lists/lists-list'
export enum Sections { export enum Sections {

View File

@ -1,9 +1,9 @@
import {makeAutoObservable, runInAction} from 'mobx' import {makeAutoObservable, runInAction} from 'mobx'
import {AppBskyFeedGetSavedFeeds as GetSavedFeeds} from '@atproto/api' import {AppBskyFeedGetSavedFeeds as GetSavedFeeds} from '@atproto/api'
import {RootStoreModel} from '../../root-store' import {RootStoreModel} from '../root-store'
import {bundleAsync} from 'lib/async/bundle' import {bundleAsync} from 'lib/async/bundle'
import {cleanError} from 'lib/strings/errors' import {cleanError} from 'lib/strings/errors'
import {AlgoItemModel} from './algo-item' import {CustomFeedModel} from '../feeds/custom-feed'
import {hasProp, isObj} from 'lib/type-guards' import {hasProp, isObj} from 'lib/type-guards'
const PAGE_SIZE = 30 const PAGE_SIZE = 30
@ -18,8 +18,8 @@ export class SavedFeedsModel {
loadMoreCursor?: string loadMoreCursor?: string
// data // data
feeds: AlgoItemModel[] = [] feeds: CustomFeedModel[] = []
pinned: AlgoItemModel[] = [] pinned: CustomFeedModel[] = []
constructor(public rootStore: RootStoreModel) { constructor(public rootStore: RootStoreModel) {
makeAutoObservable( makeAutoObservable(
@ -42,7 +42,7 @@ export class SavedFeedsModel {
if (hasProp(v, 'pinned')) { if (hasProp(v, 'pinned')) {
const pinnedSerialized = (v as any).pinned as string[] const pinnedSerialized = (v as any).pinned as string[]
const pinnedDeserialized = pinnedSerialized.map( const pinnedDeserialized = pinnedSerialized.map(
(s: string) => new AlgoItemModel(this.rootStore, JSON.parse(s)), (s: string) => new CustomFeedModel(this.rootStore, JSON.parse(s)),
) )
this.pinned = pinnedDeserialized this.pinned = pinnedDeserialized
} }
@ -83,7 +83,7 @@ export class SavedFeedsModel {
) )
} }
togglePinnedFeed(feed: AlgoItemModel) { togglePinnedFeed(feed: CustomFeedModel) {
if (!this.isPinned(feed)) { if (!this.isPinned(feed)) {
this.pinned.push(feed) this.pinned.push(feed)
} else { } else {
@ -95,15 +95,15 @@ export class SavedFeedsModel {
this.pinned = this.pinned.filter(f => f.data.uri !== uri) this.pinned = this.pinned.filter(f => f.data.uri !== uri)
} }
reorderPinnedFeeds(temp: AlgoItemModel[]) { reorderPinnedFeeds(temp: CustomFeedModel[]) {
this.pinned = temp this.pinned = temp
} }
isPinned(feed: AlgoItemModel) { isPinned(feed: CustomFeedModel) {
return this.pinned.find(f => f.data.uri === feed.data.uri) ? true : false return this.pinned.find(f => f.data.uri === feed.data.uri) ? true : false
} }
movePinnedItem(item: AlgoItemModel, direction: 'up' | 'down') { movePinnedItem(item: CustomFeedModel, direction: 'up' | 'down') {
if (this.pinned.length < 2) { if (this.pinned.length < 2) {
throw new Error('Array must have at least 2 items') throw new Error('Array must have at least 2 items')
} }
@ -182,11 +182,11 @@ export class SavedFeedsModel {
this.feeds = this.feeds.filter(f => f.data.uri !== uri) this.feeds = this.feeds.filter(f => f.data.uri !== uri)
} }
addFeed(algoItem: AlgoItemModel) { addFeed(algoItem: CustomFeedModel) {
this.feeds.push(new AlgoItemModel(this.rootStore, algoItem.data)) this.feeds.push(new CustomFeedModel(this.rootStore, algoItem.data))
} }
async save(algoItem: AlgoItemModel) { async save(algoItem: CustomFeedModel) {
try { try {
await this.rootStore.agent.app.bsky.feed.saveFeed({ await this.rootStore.agent.app.bsky.feed.saveFeed({
feed: algoItem.getUri, feed: algoItem.getUri,
@ -198,7 +198,7 @@ export class SavedFeedsModel {
} }
} }
async unsave(algoItem: AlgoItemModel) { async unsave(algoItem: CustomFeedModel) {
const uri = algoItem.getUri const uri = algoItem.getUri
try { try {
await this.rootStore.agent.app.bsky.feed.unsaveFeed({ await this.rootStore.agent.app.bsky.feed.unsaveFeed({
@ -243,7 +243,7 @@ export class SavedFeedsModel {
this.loadMoreCursor = res.data.cursor this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor this.hasMore = !!this.loadMoreCursor
for (const f of res.data.feeds) { for (const f of res.data.feeds) {
this.feeds.push(new AlgoItemModel(this.rootStore, f)) this.feeds.push(new CustomFeedModel(this.rootStore, f))
} }
} }
} }

View File

@ -12,7 +12,7 @@ import {colors, s} from 'lib/styles'
import {UserAvatar} from '../util/UserAvatar' import {UserAvatar} from '../util/UserAvatar'
import {Button} from '../util/forms/Button' import {Button} from '../util/forms/Button'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import {AlgoItemModel} from 'state/models/feeds/algo/algo-item' import {CustomFeedModel} from 'state/models/feeds/custom-feed'
import {useFocusEffect, useNavigation} from '@react-navigation/native' import {useFocusEffect, useNavigation} from '@react-navigation/native'
import {NavigationProp} from 'lib/routes/types' import {NavigationProp} from 'lib/routes/types'
import {useStores} from 'state/index' import {useStores} from 'state/index'
@ -28,7 +28,7 @@ const AlgoItem = observer(
showBottom = true, showBottom = true,
reloadOnFocus = false, reloadOnFocus = false,
}: { }: {
item: AlgoItemModel item: CustomFeedModel
style?: StyleProp<ViewStyle> style?: StyleProp<ViewStyle>
showBottom?: boolean showBottom?: boolean
reloadOnFocus?: boolean reloadOnFocus?: boolean

View File

@ -3,12 +3,18 @@ import {View, TouchableOpacity, StyleSheet} from 'react-native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {colors} from 'lib/styles' import {colors} from 'lib/styles'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import {AlgoItemModel} from 'state/models/feeds/algo/algo-item' import {CustomFeedModel} from 'state/models/feeds/custom-feed'
import {SavedFeedsModel} from 'state/models/feeds/algo/saved' import {SavedFeedsModel} from 'state/models/ui/saved-feeds'
import AlgoItem from './AlgoItem' import AlgoItem from './AlgoItem'
export const SavedFeedItem = observer( export const SavedFeedItem = observer(
({item, savedFeeds}: {item: AlgoItemModel; savedFeeds: SavedFeedsModel}) => { ({
item,
savedFeeds,
}: {
item: CustomFeedModel
savedFeeds: SavedFeedsModel
}) => {
const isPinned = savedFeeds.isPinned(item) const isPinned = savedFeeds.isPinned(item)
return ( return (

View File

@ -1,10 +1,10 @@
import {useEffect, useState} from 'react' import {useEffect, useState} from 'react'
import {useStores} from 'state/index' import {useStores} from 'state/index'
import {AlgoItemModel} from 'state/models/feeds/algo/algo-item' import {CustomFeedModel} from 'state/models/feeds/custom-feed'
export function useCustomFeed(uri: string) { export function useCustomFeed(uri: string) {
const store = useStores() const store = useStores()
const [item, setItem] = useState<AlgoItemModel>() const [item, setItem] = useState<CustomFeedModel>()
useEffect(() => { useEffect(() => {
async function fetchView() { async function fetchView() {
const res = await store.agent.app.bsky.feed.getFeedGenerator({ const res = await store.agent.app.bsky.feed.getFeedGenerator({
@ -16,7 +16,7 @@ export function useCustomFeed(uri: string) {
async function buildFeedItem() { async function buildFeedItem() {
const view = await fetchView() const view = await fetchView()
if (view) { if (view) {
const temp = new AlgoItemModel(store, view) const temp = new CustomFeedModel(store, view)
setItem(temp) setItem(temp)
} }
} }

View File

@ -26,7 +26,7 @@ import {getYoutubeVideoId} from 'lib/strings/url-helpers'
import QuoteEmbed from './QuoteEmbed' import QuoteEmbed from './QuoteEmbed'
import {AutoSizedImage} from '../images/AutoSizedImage' import {AutoSizedImage} from '../images/AutoSizedImage'
import AlgoItem from 'view/com/algos/AlgoItem' import AlgoItem from 'view/com/algos/AlgoItem'
import {AlgoItemModel} from 'state/models/feeds/algo/algo-item' import {CustomFeedModel} from 'state/models/feeds/custom-feed'
type Embed = type Embed =
| AppBskyEmbedRecord.View | AppBskyEmbedRecord.View
@ -172,7 +172,7 @@ export function PostEmbeds({
) { ) {
return ( return (
<AlgoItem <AlgoItem
item={new AlgoItemModel(store, embed.record)} item={new CustomFeedModel(store, embed.record)}
style={[pal.view, pal.border, styles.extOuter]} style={[pal.view, pal.border, styles.extOuter]}
reloadOnFocus={true} reloadOnFocus={true}
/> />

View File

@ -26,7 +26,7 @@ import DraggableFlatList, {
} from 'react-native-draggable-flatlist' } from 'react-native-draggable-flatlist'
import {SavedFeedItem} from 'view/com/algos/SavedFeedItem' import {SavedFeedItem} from 'view/com/algos/SavedFeedItem'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {AlgoItemModel} from 'state/models/feeds/algo/algo-item' import {CustomFeedModel} from 'state/models/feeds/custom-feed'
type Props = NativeStackScreenProps<CommonNavigatorParams, 'PinnedFeeds'> type Props = NativeStackScreenProps<CommonNavigatorParams, 'PinnedFeeds'>
@ -101,7 +101,7 @@ export const PinnedFeeds = withAuthRequired(
) )
const PinnedItem = observer( const PinnedItem = observer(
({item, drag}: {item: AlgoItemModel; drag: () => void}) => { ({item, drag}: {item: CustomFeedModel; drag: () => void}) => {
const pal = usePalette('default') const pal = usePalette('default')
const rootStore = useStores() const rootStore = useStores()
const savedFeeds = useMemo(() => rootStore.me.savedFeeds, [rootStore]) const savedFeeds = useMemo(() => rootStore.me.savedFeeds, [rootStore])

View File

@ -26,7 +26,7 @@ import {s, colors} from 'lib/styles'
import {useAnalytics} from 'lib/analytics' import {useAnalytics} from 'lib/analytics'
import {ComposeIcon2} from 'lib/icons' import {ComposeIcon2} from 'lib/icons'
import AlgoItem from 'view/com/algos/AlgoItem' import AlgoItem from 'view/com/algos/AlgoItem'
import {AlgoItemModel} from 'state/models/feeds/algo/algo-item' import {CustomFeedModel} from 'state/models/feeds/custom-feed'
import {useSetTitle} from 'lib/hooks/useSetTitle' import {useSetTitle} from 'lib/hooks/useSetTitle'
import {combinedDisplayName} from 'lib/strings/display-names' import {combinedDisplayName} from 'lib/strings/display-names'
@ -188,7 +188,7 @@ export const ProfileScreen = withAuthRequired(
return ( return (
<FeedSlice slice={item} ignoreMuteFor={uiState.profile.did} /> <FeedSlice slice={item} ignoreMuteFor={uiState.profile.did} />
) )
} else if (item instanceof AlgoItemModel) { } else if (item instanceof CustomFeedModel) {
return <AlgoItem item={item} /> return <AlgoItem item={item} />
} }
} }

View File

@ -21,7 +21,7 @@ import {CenteredView} from 'view/com/util/Views'
import {Text} from 'view/com/util/text/Text' import {Text} from 'view/com/util/text/Text'
import {isDesktopWeb, isWeb} from 'platform/detection' import {isDesktopWeb, isWeb} from 'platform/detection'
import {s} from 'lib/styles' import {s} from 'lib/styles'
import {SavedFeedsModel} from 'state/models/feeds/algo/saved' import {SavedFeedsModel} from 'state/models/ui/saved-feeds'
import {Link} from 'view/com/util/Link' import {Link} from 'view/com/util/Link'
import {UserAvatar} from 'view/com/util/UserAvatar' import {UserAvatar} from 'view/com/util/UserAvatar'
import {SavedFeedItem} from 'view/com/algos/SavedFeedItem' import {SavedFeedItem} from 'view/com/algos/SavedFeedItem'