allow for pinning saved feeds

This commit is contained in:
Ansh Nanda 2023-05-16 15:50:52 -07:00
parent dd788550be
commit f2e39d8ad2
7 changed files with 170 additions and 58 deletions

View file

@ -153,4 +153,8 @@ export class AlgoItemModel {
})
this.data = res.data.view
}
serialize() {
return JSON.stringify(this.data)
}
}

View file

@ -4,6 +4,7 @@ import {RootStoreModel} from '../../root-store'
import {bundleAsync} from 'lib/async/bundle'
import {cleanError} from 'lib/strings/errors'
import {AlgoItemModel} from './algo-item'
import {hasProp, isObj} from 'lib/type-guards'
const PAGE_SIZE = 30
@ -18,6 +19,7 @@ export class SavedFeedsModel {
// data
feeds: AlgoItemModel[] = []
pinned: AlgoItemModel[] = []
constructor(public rootStore: RootStoreModel) {
makeAutoObservable(
@ -29,6 +31,24 @@ export class SavedFeedsModel {
)
}
serialize() {
return {
pinned: this.pinned.map(f => f.serialize()),
}
}
hydrate(v: unknown) {
if (isObj(v)) {
if (hasProp(v, 'pinned')) {
const pinnedSerialized = (v as any).pinned as string[]
const pinnedDeserialized = pinnedSerialized.map(
(s: string) => new AlgoItemModel(this.rootStore, JSON.parse(s)),
)
this.pinned = pinnedDeserialized
}
}
}
get hasContent() {
return this.feeds.length > 0
}
@ -51,6 +71,28 @@ export class SavedFeedsModel {
)
}
get savedFeedsWithoutPinned() {
return this.feeds.filter(
f => !this.pinned.find(p => p.data.uri === f.data.uri),
)
}
togglePinnedFeed(feed: AlgoItemModel) {
if (!this.isPinned(feed)) {
this.pinned.push(feed)
} else {
this.pinned = this.pinned.filter(f => f.data.uri !== feed.data.uri)
}
}
reorderPinnedFeeds(temp: AlgoItemModel[]) {
this.pinned = temp
}
isPinned(feed: AlgoItemModel) {
return this.pinned.find(f => f.data.uri === feed.data.uri) ? true : false
}
// public api
// =

View file

@ -69,6 +69,7 @@ export class MeModel {
displayName: this.displayName,
description: this.description,
avatar: this.avatar,
savedFeeds: this.savedFeeds.serialize(),
}
}
@ -90,6 +91,9 @@ export class MeModel {
if (hasProp(v, 'avatar') && typeof v.avatar === 'string') {
avatar = v.avatar
}
if (hasProp(v, 'savedFeeds') && isObj(v.savedFeeds)) {
this.savedFeeds.hydrate(v.savedFeeds)
}
if (did && handle) {
this.did = did
this.handle = handle