From 356b9af4ffe582bef07842eb2aacc8ec696def51 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Sun, 6 Mar 2022 12:11:16 +0100 Subject: [PATCH] implement filtered bookmarks that error check the provider Co-authored-by: William Oldham --- README.md | 1 + src/providers/methods/helpers.ts | 32 ++++++++++++++++++++++++++---- src/providers/methods/providers.ts | 3 ++- src/providers/types.ts | 8 ++++++++ src/state/bookmark/context.tsx | 23 +++++++++++++-------- src/views/MediaView.tsx | 7 +++++-- src/views/SearchView.tsx | 14 +++++++------ 7 files changed, 67 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5ddfb100..e01cdc8f 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Check out [this project's issues](https://github.com/JamesHawkinss/movie-web/iss - [ ] Implement all scrapers - [ ] implement sources that are not mp4 - [ ] Migrate old video progress +- [ ] Bug: go back doesn't work if used directly from link ## After all rewrite code has been written diff --git a/src/providers/methods/helpers.ts b/src/providers/methods/helpers.ts index abb09466..e8d41def 100644 --- a/src/providers/methods/helpers.ts +++ b/src/providers/methods/helpers.ts @@ -1,16 +1,40 @@ -import { MWMediaType } from "providers"; -import { mediaProviders } from "./providers"; +import { MWMediaType, MWMediaProviderMetadata } from "providers"; +import { mediaProviders, mediaProvidersUnchecked } from "./providers"; /* ** Fetch all enabled providers for a specific type */ - export function GetProvidersForType(type: MWMediaType) { +export function GetProvidersForType(type: MWMediaType) { return mediaProviders.filter((v) => v.type.includes(type)); } /* ** Get a provider by a id */ - export function getProviderFromId(id: string) { +export function getProviderFromId(id: string) { return mediaProviders.find((v) => v.id === id); } + +/* + ** Get a provider metadata + */ +export function getProviderMetadata(id: string): MWMediaProviderMetadata { + const provider = mediaProvidersUnchecked.find((v) => v.id === id); + + if (!provider) { + return { + exists: false, + type: [], + enabled: false, + id, + }; + } + + return { + exists: true, + type: provider.type, + enabled: provider.enabled, + id, + provider, + }; +} diff --git a/src/providers/methods/providers.ts b/src/providers/methods/providers.ts index 6496add2..26e112ca 100644 --- a/src/providers/methods/providers.ts +++ b/src/providers/methods/providers.ts @@ -2,9 +2,10 @@ import { tempScraper } from "providers/list/temp"; import { theFlixScraper } from "providers/list/theflix"; import { MWWrappedMediaProvider, WrapProvider } from "providers/wrapper"; -const mediaProvidersUnchecked: MWWrappedMediaProvider[] = [ +export const mediaProvidersUnchecked: MWWrappedMediaProvider[] = [ WrapProvider(theFlixScraper), WrapProvider(tempScraper), ]; + export const mediaProviders: MWWrappedMediaProvider[] = mediaProvidersUnchecked.filter((v) => v.enabled); diff --git a/src/providers/types.ts b/src/providers/types.ts index 6e871f87..fb133199 100644 --- a/src/providers/types.ts +++ b/src/providers/types.ts @@ -43,6 +43,14 @@ export interface MWMediaProvider { getStream(media: MWPortableMedia): Promise; } +export interface MWMediaProviderMetadata { + exists: boolean; + id?: string; + enabled: boolean; + type: MWMediaType[]; + provider?: MWMediaProvider; +} + export interface MWMassProviderOutput { providers: { id: string; diff --git a/src/state/bookmark/context.tsx b/src/state/bookmark/context.tsx index d8ade779..b05936b2 100644 --- a/src/state/bookmark/context.tsx +++ b/src/state/bookmark/context.tsx @@ -1,4 +1,4 @@ -import { MWMediaMeta } from "providers"; +import { getProviderMetadata, MWMediaMeta } from "providers"; import { createContext, ReactNode, useContext, useState } from "react"; import { BookmarkStore } from "./store"; @@ -8,11 +8,13 @@ interface BookmarkStoreData { interface BookmarkStoreDataWrapper { setItemBookmark(media: MWMediaMeta, bookedmarked: boolean): void; + getFilteredBookmarks(): MWMediaMeta[]; bookmarkStore: BookmarkStoreData; } const BookmarkedContext = createContext({ setItemBookmark: () => {}, + getFilteredBookmarks: () => [], bookmarkStore: { bookmarks: [], }, @@ -40,7 +42,7 @@ export function BookmarkContextProvider(props: { children: ReactNode }) { setItemBookmark(media: MWMediaMeta, bookmarked: boolean) { setBookmarked((data: BookmarkStoreData) => { if (bookmarked) { - const itemIndex = getBookmarkIndexFromPortable(data, media); + const itemIndex = getBookmarkIndexFromMedia(data.bookmarks, media); if (itemIndex === -1) { const item = { mediaId: media.mediaId, @@ -54,7 +56,7 @@ export function BookmarkContextProvider(props: { children: ReactNode }) { data.bookmarks.push(item); } } else { - const itemIndex = getBookmarkIndexFromPortable(data, media); + const itemIndex = getBookmarkIndexFromMedia(data.bookmarks, media); if (itemIndex !== -1) { data.bookmarks.splice(itemIndex); } @@ -62,6 +64,11 @@ export function BookmarkContextProvider(props: { children: ReactNode }) { return data; }); }, + getFilteredBookmarks() { + return bookmarkStorage.bookmarks.filter((bookmark) => { + return getProviderMetadata(bookmark.providerId)?.enabled; + }); + }, bookmarkStore: bookmarkStorage, }; @@ -76,11 +83,11 @@ export function useBookmarkContext() { return useContext(BookmarkedContext); } -function getBookmarkIndexFromPortable( - store: BookmarkStoreData, +function getBookmarkIndexFromMedia( + bookmarks: MWMediaMeta[], media: MWMediaMeta ): number { - const a = store.bookmarks.findIndex((v) => { + const a = bookmarks.findIndex((v) => { return ( v.mediaId === media.mediaId && v.providerId === media.providerId && @@ -92,9 +99,9 @@ function getBookmarkIndexFromPortable( } export function getIfBookmarkedFromPortable( - store: BookmarkStoreData, + bookmarks: MWMediaMeta[], media: MWMediaMeta ): boolean { - const bookmarked = getBookmarkIndexFromPortable(store, media); + const bookmarked = getBookmarkIndexFromMedia(bookmarks, media); return bookmarked !== -1; } diff --git a/src/views/MediaView.tsx b/src/views/MediaView.tsx index 17d9dfaf..3a4853a7 100644 --- a/src/views/MediaView.tsx +++ b/src/views/MediaView.tsx @@ -36,8 +36,11 @@ function StyledMediaView(props: StyledMediaViewProps) { store.watched, props.media )?.progress; - const { setItemBookmark, bookmarkStore } = useBookmarkContext(); - const isBookmarked = getIfBookmarkedFromPortable(bookmarkStore, props.media); + const { setItemBookmark, getFilteredBookmarks } = useBookmarkContext(); + const isBookmarked = getIfBookmarkedFromPortable( + getFilteredBookmarks(), + props.media + ); function updateProgress(e: Event) { if (!props.media) return; diff --git a/src/views/SearchView.tsx b/src/views/SearchView.tsx index cc7eff12..74e192fb 100644 --- a/src/views/SearchView.tsx +++ b/src/views/SearchView.tsx @@ -171,20 +171,22 @@ export function SearchView() { } function ExtraItems() { - const { bookmarkStore } = useBookmarkContext(); + const { getFilteredBookmarks } = useBookmarkContext(); const { watched } = useWatchedContext(); + + const bookmarks = getFilteredBookmarks(); + const watchedItems = watched.items.filter( - (v) => !getIfBookmarkedFromPortable(bookmarkStore, v) + (v) => !getIfBookmarkedFromPortable(bookmarks, v) ); - if (watchedItems.length === 0 && bookmarkStore.bookmarks.length === 0) - return null; + if (watchedItems.length === 0 && bookmarks.length === 0) return null; return (
- {bookmarkStore.bookmarks.length > 0 ? ( + {bookmarks.length > 0 ? ( - {bookmarkStore.bookmarks.map((v) => ( + {bookmarks.map((v) => (