Skip to content

Commit

Permalink
feat: v2.2.1
Browse files Browse the repository at this point in the history
- Pass location from listener to toggleButton function
- Refactor
  - Move fetching functions to services.ts
  - Rename util.ts to utils.ts
  • Loading branch information
Akasiek committed Feb 1, 2024
1 parent 20a3497 commit c3eed24
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 29 deletions.
4 changes: 2 additions & 2 deletions dist/LuckyLP.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lucky-lp",
"version": "2.2.0",
"version": "2.2.1",
"private": true,
"scripts": {
"build": "spicetify-creator",
Expand Down
12 changes: 8 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { toggleButton } from "./button";

async function main() {
while (!Spicetify?.showNotification || !Spicetify?.Platform?.History) {
while (
!Spicetify?.showNotification &&
!Spicetify?.Platform?.History &&
!Spicetify?.CosmosAsync
) {
await new Promise((resolve) => setTimeout(resolve, 500));
}

// Show button on app load
toggleButton();
toggleButton(Spicetify.Platform.History.location);

// Show button depending on navigation
Spicetify.Platform.History.listen(() => {
toggleButton();
Spicetify.Platform.History.listen((location: Location) => {
toggleButton(location);
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/button.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ShuffleIcon from "./shuffleIcon";
import { handleClick, isAlbumPage, isHomePage } from "./util";
import { handleClick, isAlbumPage, isHomePage } from "./utils";

export const toggleButton = () => {
if (isHomePage() || isAlbumPage()) {
export const toggleButton = (location: Location) => {
if (isHomePage(location.pathname) || isAlbumPage(location.pathname)) {
addButton();
} else {
removeButton();
Expand Down
19 changes: 19 additions & 0 deletions src/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IAlbum, ISavedAlbums } from "./types/api";

export const getAlbumsCount = async (): Promise<number> => {
const result: ISavedAlbums = await Spicetify.CosmosAsync.get(
"https://api.spotify.com/v1/me/albums?limit=1",
);

return result.total;
};

export const getRandomAlbum = async (total: number): Promise<IAlbum> => {
const randomIndex = Math.floor(Math.random() * (total + 1));

const randomResult: ISavedAlbums = await Spicetify.CosmosAsync.get(
`https://api.spotify.com/v1/me/albums?limit=1&offset=${randomIndex - 1}`,
);

return randomResult.items[0];
};
23 changes: 6 additions & 17 deletions src/util.ts → src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
import { IAlbum, ISavedAlbums } from "./types/api";
import { getAlbumsCount, getRandomAlbum } from "./services";

export const isHomePage = () => {
const pathname = Spicetify.Platform.History.location.pathname;
export const isHomePage = (pathname: string) => {
return pathname === "/";
};

export const isAlbumPage = () => {
const pathname = Spicetify.Platform.History.location.pathname;
export const isAlbumPage = (pathname: string) => {
return pathname.startsWith("/album/");
};

const getRandomAlbumIndex = (total: number): number =>
Math.floor(Math.random() * (total + 1));

const getRandomSavedAlbum = async (): Promise<false | IAlbum> => {
try {
const result: ISavedAlbums = await Spicetify.CosmosAsync.get(
"https://api.spotify.com/v1/me/albums?limit=1",
);
const albumsCount = await getAlbumsCount();

if (result && result?.total) {
const albumsCount = result.total;
const randomIndex = getRandomAlbumIndex(albumsCount);
const randomResult: ISavedAlbums = await Spicetify.CosmosAsync.get(
`https://api.spotify.com/v1/me/albums?limit=1&offset=${randomIndex - 1}`,
);
const randomAlbum = randomResult.items[0];
if (albumsCount) {
const randomAlbum = await getRandomAlbum(albumsCount);

Spicetify.Platform.History.push(`/album/${randomAlbum.album.id}`);

Expand Down

0 comments on commit c3eed24

Please sign in to comment.