Skip to content

fix(Parser): parse all contents in home tab #891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/parser/classes/Tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ export default class Tab extends YTNode {
this.title = data.title || 'N/A';
this.selected = !!data.selected;
this.endpoint = new NavigationEndpoint(data.endpoint);
this.content = Parser.parseItem(data.content, [ SectionList, MusicQueue, RichGrid ]);
const contents = Parser.parseItems(data.content, [ SectionList, MusicQueue, RichGrid ]);
this.content = null;
if (contents !== null && contents.length > 0) {
for (const item of contents) {
if (item.is(SectionList) && item.contents.length > 0) {
this.content = item;
} else if (item.is(RichGrid) && item.contents.length > 0) {
this.content = item;
} else if (item.is(MusicQueue)) {
this.content = item;
}
}

if (this.content === null) {
this.content = contents[0];
}
}
}
}
23 changes: 23 additions & 0 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,29 @@ export function parseItem(data?: RawNode, validTypes?: YTNodeConstructor | YTNod
return null;
}

/**
* Parses multiple items in an object as an array of items.
* @param data - The data to parse.
* @param validTypes - YTNode types that are allowed to be parsed.
*/
export function parseItems<T extends YTNode, K extends YTNodeConstructor<T>[]>(data: RawNode | undefined, validTypes: K): InstanceType<K[number]>[] | null;
export function parseItems<T extends YTNode>(data: RawNode | undefined, validTypes: YTNodeConstructor<T>): T[] | null;
export function parseItems(data?: RawNode): YTNode[];
export function parseItems(data?: RawNode, validTypes?: YTNodeConstructor | YTNodeConstructor[]) {
if (!data) return null;
const keys = Object.keys(data);
const results: YTNode[] = [];
for (const key of keys) {
const temp_data = { [key]: data[key] };

const result = parseItem(temp_data, validTypes as YTNodeConstructor);
if (result) {
results.push(result);
}
}
return results;
}

/**
* Parses an array of items.
* @param data - The data to parse.
Expand Down