Adds add to favorites button in SongRow
This commit is contained in:
@@ -61,49 +61,48 @@ export class FavoritesStore {
|
||||
return this.favorites;
|
||||
}
|
||||
|
||||
async addFavorite(item: PlaylistItem, fetchMetadata: boolean = true): Promise<void> {
|
||||
async addFavorite(filename: string): Promise<void> {
|
||||
// Check if the item already exists by filename
|
||||
const exists = this.favorites.some(f => f.filename === item.filename);
|
||||
const exists = this.favorites.some(f => f.filename === filename);
|
||||
if (!exists) {
|
||||
this.favorites.push({
|
||||
...item,
|
||||
filename: filename,
|
||||
id: this.favorites.length // Generate new ID
|
||||
});
|
||||
await this.saveFavorites();
|
||||
} else {
|
||||
// Otherwise, update the item with the new metadata
|
||||
const index = this.favorites.findIndex(f => f.filename === item.filename);
|
||||
this.favorites[index] = {
|
||||
...this.favorites[index],
|
||||
...item,
|
||||
};
|
||||
await this.saveFavorites();
|
||||
}
|
||||
|
||||
// If the item is missing metadata, fetch it
|
||||
if (fetchMetadata && !item.metadata) {
|
||||
await this.fetchMetadata(item);
|
||||
// Fetch metadata for the new favorite
|
||||
await this.fetchMetadata(filename);
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchMetadata(item: PlaylistItem): Promise<void> {
|
||||
console.log("Fetching metadata for " + item.filename);
|
||||
const metadata = await getLinkPreview(item.filename);
|
||||
private async fetchMetadata(filename: string): Promise<void> {
|
||||
console.log("Fetching metadata for " + filename);
|
||||
const metadata = await getLinkPreview(filename);
|
||||
|
||||
item.metadata = {
|
||||
title: (metadata as any)?.title,
|
||||
description: (metadata as any)?.description,
|
||||
siteName: (metadata as any)?.siteName,
|
||||
const item: PlaylistItem = {
|
||||
filename: filename,
|
||||
id: this.favorites.length,
|
||||
metadata: {
|
||||
title: (metadata as any)?.title,
|
||||
description: (metadata as any)?.description,
|
||||
siteName: (metadata as any)?.siteName,
|
||||
},
|
||||
};
|
||||
|
||||
console.log("Metadata fetched for " + item.filename);
|
||||
console.log(item);
|
||||
|
||||
await this.addFavorite(item, false);
|
||||
const index = this.favorites.findIndex(f => f.filename === filename);
|
||||
if (index !== -1) {
|
||||
this.favorites[index] = item;
|
||||
await this.saveFavorites();
|
||||
}
|
||||
}
|
||||
|
||||
async removeFavorite(id: number): Promise<void> {
|
||||
this.favorites = this.favorites.filter(f => f.id !== id);
|
||||
async removeFavorite(filename: string): Promise<void> {
|
||||
console.log("Removing favorite " + filename);
|
||||
this.favorites = this.favorites.filter(f => f.filename !== filename);
|
||||
await this.saveFavorites();
|
||||
}
|
||||
|
||||
|
||||
@@ -176,8 +176,8 @@ export class MediaPlayer {
|
||||
return this.favoritesStore.addFavorite(item);
|
||||
}
|
||||
|
||||
public async removeFavorite(id: number) {
|
||||
return this.favoritesStore.removeFavorite(id);
|
||||
public async removeFavorite(filename: string) {
|
||||
return this.favoritesStore.removeFavorite(filename);
|
||||
}
|
||||
|
||||
public async clearFavorites() {
|
||||
|
||||
@@ -140,14 +140,15 @@ apiRouter.get("/favorites", withErrorHandling(async (req, res) => {
|
||||
}));
|
||||
|
||||
apiRouter.post("/favorites", withErrorHandling(async (req, res) => {
|
||||
const item = req.body as PlaylistItem;
|
||||
await mediaPlayer.addFavorite(item);
|
||||
const { filename } = req.body as { filename: string };
|
||||
console.log("Adding favorite: " + filename);
|
||||
await mediaPlayer.addFavorite(filename);
|
||||
res.send(JSON.stringify({ success: true }));
|
||||
}));
|
||||
|
||||
apiRouter.delete("/favorites/:id", withErrorHandling(async (req, res) => {
|
||||
const { id } = req.params;
|
||||
await mediaPlayer.removeFavorite(parseInt(id));
|
||||
apiRouter.delete("/favorites/:filename", withErrorHandling(async (req, res) => {
|
||||
const { filename } = req.params as { filename: string };
|
||||
await mediaPlayer.removeFavorite(filename);
|
||||
res.send(JSON.stringify({ success: true }));
|
||||
}));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user