Skip to content

Commit

Permalink
refactor(favorites): move videos key to cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane committed Oct 19, 2023
1 parent 6400c59 commit e49bab5
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 26 deletions.
19 changes: 11 additions & 8 deletions src/components/ButtonFavorite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
CardVideo,
} from "../types/interfaces/Card";
import { Channel } from "../types/interfaces/Channel";
import { Playlist } from "../types/interfaces/Playlist";
import {
FavoritePlaylist as Favorite,
Playlist,
} from "../types/interfaces/Playlist";
import { Video } from "../types/interfaces/Video";
import {
formatedCardChannel,
Expand Down Expand Up @@ -82,12 +85,12 @@ export const ButtonFavorite: React.FC<ButtonFavoriteProps> = memo(
return null;
}

const isFavorite = favorite.videos.find(
(favVideo) => getCardId(favVideo) === getCardId(card),
const isFavorite = favorite.cards.find(
(favCard) => getCardId(favCard) === getCardId(card),
);

const updateAndCommit = async (updatedFavoritePlaylist: Playlist) => {
await db.update(
const updateAndCommit = (updatedFavoritePlaylist: Favorite) => {
db.update(
"playlists",
{ title: "Favorites" },
() => updatedFavoritePlaylist,
Expand All @@ -110,7 +113,7 @@ export const ButtonFavorite: React.FC<ButtonFavoriteProps> = memo(

updateAndCommit({
...favorite,
videos: [formatedCard, ...favorite.videos],
cards: [formatedCard, ...favorite.cards],
});

notifications.show({
Expand All @@ -122,8 +125,8 @@ export const ButtonFavorite: React.FC<ButtonFavoriteProps> = memo(
const handleDelete = () => {
updateAndCommit({
...favorite,
videos: favorite.videos.filter(
(favVideo) => getCardId(favVideo) !== getCardId(card),
cards: favorite.cards.filter(
(favCard) => getCardId(favCard) !== getCardId(card),
),
});

Expand Down
14 changes: 7 additions & 7 deletions src/components/FavoritePlaylist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const FavoritePlaylist = memo(() => {
const favorite = useFavorite();
const { t } = useTranslation();

const data = favorite.videos;
const data = favorite.cards;

if (!data.length) {
return (
Expand All @@ -30,13 +30,13 @@ export const FavoritePlaylist = memo(() => {
}

const videos = data.filter(
(video) =>
(video.type === "video" || video.type === "scheduled") &&
video.lengthSeconds > 0,
(card) =>
(card.type === "video" || card.type === "scheduled") &&
card.lengthSeconds > 0,
);
const livestream = data.filter((video) => isLiveStream(video as CardVideo));
const playlists = data.filter((video) => video.type === "playlist");
const channels = data.filter((video) => video.type === "channel");
const livestream = data.filter((card) => isLiveStream(card as CardVideo));
const playlists = data.filter((card) => card.type === "playlist");
const channels = data.filter((card) => card.type === "channel");

return (
<Tabs defaultValue="all">
Expand Down
2 changes: 1 addition & 1 deletion src/components/ImportData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ const TransferListContainer: FC<TransferListContainerProps> = memo(
const userPlaylists = playlists.filter((p) => p.title !== "Favorites");

if (favoritePlaylist) {
const { validData } = await getVideosData(favoritePlaylist.videos);
const { validData } = await getVideosData(favoritePlaylist.cards);
importVideosToFavorites(validData.map(({ video }) => video));
setFavorite(getFavoritePlaylist());
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/RecentFavorites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { HorizontalGridList } from "./HorizontalGridList";

export const RecentFavorites = memo(() => {
const favorite = useFavorite();
const data = favorite.videos.slice(0, 10);
const data = favorite.cards.slice(0, 10);
const { t } = useTranslation();

return (
Expand Down
19 changes: 19 additions & 0 deletions src/database/19102023_migrate_playlist_favorite_cards_key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { db } from ".";

const migration = () => {
try {
const favoritePlaylist = db.queryAll("playlists", {
query: { title: "Favorites" },
})[0];

favoritePlaylist.cards = favoritePlaylist.videos;
delete favoritePlaylist.videos;

db.update("playlists", { title: "Favorites" }, () => favoritePlaylist);
db.commit();
} catch (error) {
console.log(error);
}
};

export default migration;
13 changes: 10 additions & 3 deletions src/database/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ export default (() => {
if (!migrationsName.includes("18102023_migrate_formate_storage_data")) {
try {
require("./18102023_migrate_formate_storage_data").default();
// saveMigration(
// "18102023_migrate_formate_storage_data",
// );
saveMigration("18102023_migrate_formate_storage_data");
} catch {}
}

if (
!migrationsName.includes("19102023_migrate_playlist_favorite_cards_key")
) {
try {
require("./19102023_migrate_playlist_favorite_cards_key").default();
saveMigration("19102023_migrate_playlist_favorite_cards_key");
} catch {}
}
})();
2 changes: 1 addition & 1 deletion src/database/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const removeDuplicateVideoId = (cards: Card[]): Card[] => {
export const importVideosToFavorites = (importedCards: Card[]): void => {
db.update("playlists", { title: "Favorites" }, (raw: FavoritePlaylist) => ({
...raw,
videos: removeDuplicateVideoId([...importedCards, ...raw.videos]),
videos: removeDuplicateVideoId([...importedCards, ...raw.cards]),
}));
db.commit();
};
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useResolveVideosPlaylist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export const useResolveVideosPlaylist = () => {
videos = query.data;
}
if (location.pathname === "/favorites") {
videos = getFavoritePlaylist().videos.filter(
(video) => video.type === "video",
videos = getFavoritePlaylist().cards.filter(
(card) => card.type === "video",
) as Video[];
}
if (location.pathname === "/most-popular") {
Expand Down
6 changes: 3 additions & 3 deletions src/types/interfaces/Playlist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Card, CardVideo } from "./Card";
import { Card } from "./Card";
import { Video } from "./Video";

export interface Playlist {
Expand All @@ -11,7 +11,7 @@ export interface Playlist {
playlistThumbnail: string;
}

export interface FavoritePlaylist extends Omit<Playlist, "videos" | "title"> {
export interface FavoritePlaylist extends Omit<Playlist, "title"> {
title: "Favorites";
videos: Card[];
cards: Card[];
}

0 comments on commit e49bab5

Please sign in to comment.