refactor: improve home feed parsing (#234)

* chore: update tests

* style: format code

* docs: update API ref
This commit is contained in:
LuanRT
2022-11-12 01:31:11 -03:00
committed by GitHub
parent 95ff1e6c5e
commit da517fe6d1
11 changed files with 89 additions and 20 deletions

View File

@@ -1,15 +1,14 @@
import Parser from '../index';
import { YTNode } from '../helpers';
import ChipCloudChip from './ChipCloudChip';
class FeedFilterChipBar extends YTNode {
export default class FeedFilterChipBar extends YTNode {
static type = 'FeedFilterChipBar';
contents;
constructor(data: any) {
super();
this.contents = Parser.parse(data.contents);
this.contents = Parser.parseArray<ChipCloudChip>(data.contents, ChipCloudChip);
}
}
export default FeedFilterChipBar;
}

View File

@@ -12,8 +12,8 @@ class RichGrid extends YTNode {
super();
// XXX: we don't parse the masthead since it is usually an advertisement
// XXX: reflowOptions aren't parsed, I think its only used internally for layout
this.header = Parser.parse(data.header);
this.contents = Parser.parse(data.contents);
this.header = Parser.parseItem(data.header);
this.contents = Parser.parseArray(data.contents);
}
}

View File

@@ -8,8 +8,7 @@ class RichItem extends YTNode {
constructor(data: any) {
super();
// TODO: check this
this.content = Parser.parse(data.content);
this.content = Parser.parseItem(data.content);
}
}

View File

@@ -4,11 +4,11 @@ import { YTNode } from '../helpers';
class RichSection extends YTNode {
static type = 'RichSection';
contents;
content;
constructor(data: any) {
super();
this.contents = Parser.parse(data.content);
this.content = Parser.parseItem(data.content);
}
}

View File

@@ -13,7 +13,7 @@ class RichShelf extends YTNode {
constructor(data: any) {
super();
this.title = new Text(data.title);
this.contents = Parser.parse(data.contents);
this.contents = Parser.parseArray(data.contents);
this.endpoint = data.endpoint ? new NavigationEndpoint(data.endpoint) : null;
}
}

View File

@@ -0,0 +1,42 @@
import Actions from '../../core/Actions';
import FilterableFeed from '../../core/FilterableFeed';
import ChipCloudChip from '../classes/ChipCloudChip';
import FeedTabbedHeader from '../classes/FeedTabbedHeader';
import RichGrid from '../classes/RichGrid';
import { ReloadContinuationItemsCommand, AppendContinuationItemsAction } from '..';
export default class HomeFeed extends FilterableFeed {
contents: RichGrid | AppendContinuationItemsAction | ReloadContinuationItemsCommand;
header: FeedTabbedHeader;
constructor(actions: Actions, data: any, already_parsed = false) {
super(actions, data, already_parsed);
this.header = this.memo.getType<FeedTabbedHeader>(FeedTabbedHeader)?.[0];
this.contents =
this.memo.getType<RichGrid>(RichGrid)?.[0] ||
this.page.on_response_received_actions?.[0];
}
/**
* Applies given filter to the feed.
* @param filter - Filter to apply.
*/
async applyFilter(filter: string | ChipCloudChip): Promise<HomeFeed> {
const feed = await super.getFilteredFeed(filter);
return new HomeFeed(this.actions, feed.page, true);
}
/**
* Retrieves next batch of contents.
*/
async getContinuation(): Promise<HomeFeed> {
const feed = await super.getContinuation();
// Keep the page header
feed.page.header = this.page.header;
feed.page.header_memo.set(this.header.type, [ this.header ]);
return new HomeFeed(this.actions, feed.page, true);
}
}