Skip to content
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

feat(wrapper): expose Card React components #2734

Merged
merged 6 commits into from
Dec 29, 2023
Merged
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
65 changes: 65 additions & 0 deletions jsHelper/spicetifyWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,21 @@ window.Spicetify = {
"Toggle"
];

const REACT_CARD_COMPONENTS = [
"Default",
"Hero",
"CardImage",
"Base",
"Album",
"Artist",
"Audiobook",
"Episode",
"Playlist",
"Profile",
"Show",
"Track"
];

const REACT_HOOK = ["DragHandler", "usePanelState", "useExtractedColor"];

let count = SPICETIFY_METHOD.length;
Expand Down Expand Up @@ -269,6 +284,17 @@ window.Spicetify = {
});
console.log(`${count}/${REACT_COMPONENT.length} Spicetify.ReactComponent methods and objects are OK.`);

count = REACT_CARD_COMPONENTS.length;
REACT_CARD_COMPONENTS.forEach(object => {
if (Spicetify.ReactComponent.Cards[object] === undefined || Spicetify.ReactComponent.Cards[object] === null) {
console.error(
`Spicetify.ReactComponent.Cards.${object} is not available. Please open an issue in the Spicetify repository to inform us about it.`
);
count--;
}
});
console.log(`${count}/${REACT_CARD_COMPONENTS.length} Spicetify.ReactComponent.Cards methods and objects are OK.`);

count = REACT_HOOK.length;
REACT_HOOK.forEach(method => {
if (Spicetify.ReactHook[method] === undefined || Spicetify.ReactHook[method] === null) {
Expand Down Expand Up @@ -296,6 +322,12 @@ window.Spicetify = {
}
});

Object.keys(Spicetify.ReactComponent.Cards).forEach(key => {
if (!REACT_CARD_COMPONENTS.includes(key)) {
console.log(`Spicetify.ReactComponent.Cards object ${key} exists but is not in the method list. Consider adding it.`);
}
});

Object.keys(Spicetify.ReactHook).forEach(key => {
if (!REACT_HOOK.includes(key)) {
console.log(`Spicetify.ReactHook method ${key} exists but is not in the method list. Consider adding it.`);
Expand Down Expand Up @@ -349,6 +381,32 @@ window.Spicetify = {
})
.filter(Boolean);

let cardTypesToFind = ["album", "artist", "audiobook", "episode", "playlist", "profile", "show", "track"];
const cards = [
...functionModules
.flatMap(m => {
return cardTypesToFind.map(type => {
if (m.toString().includes(`featureIdentifier:"${type}"`)) {
cardTypesToFind.splice(cardTypesToFind.indexOf(type), 1);
return [type[0].toUpperCase() + type.slice(1), m];
}
});
})
.filter(Boolean),
...modules
.flatMap(m => {
return cardTypesToFind.map(type => {
try {
if (m?.type?.toString().includes(`featureIdentifier:"${type}"`)) {
cardTypesToFind.splice(cardTypesToFind.indexOf(type), 1);
return [type[0].toUpperCase() + type.slice(1), m];
}
} catch {}
});
})
.filter(Boolean)
];

Object.assign(Spicetify, {
React: cache.find(m => m?.useMemo),
ReactDOM: cache.find(m => m?.createPortal),
Expand Down Expand Up @@ -425,6 +483,13 @@ window.Spicetify = {
},
Chip: modules.find(m => m?.render?.toString().includes("invertedDark") && m?.render?.toString().includes("isUsingKeyboard")),
Toggle: functionModules.find(m => m.toString().includes("onSelected") && m.toString().includes('type:"checkbox"')),
Cards: {
Base: modules.find(m => m?.type?.toString().includes("navigationUrl") && m?.type?.toString().includes("isHero")),
Default: modules.find(m => m?.toString().includes('"card-click-handler"')),
Hero: modules.find(m => m?.toString().includes('"herocard-click-handler"')),
CardImage: functionModules.find(m => m.toString().includes("isHero") && m.toString().includes("withWaves")),
...Object.fromEntries(cards)
},
...Object.fromEntries(menus)
},
ReactHook: {
Expand Down