mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-03-11 21:27:05 +00:00
useInLibrary refactored to use ctx
This commit is contained in:
parent
6f2ef06cd6
commit
7e6e6a896f
1 changed files with 13 additions and 7 deletions
|
|
@ -4,14 +4,20 @@ const React = require('react');
|
|||
const { useServices } = require('stremio/services');
|
||||
const useModelState = require('stremio/common/useModelState');
|
||||
|
||||
const mapLibraryState = (ctx) => {
|
||||
return ctx.library;
|
||||
};
|
||||
|
||||
const useInLibrary = (metaItem) => {
|
||||
const { core } = useServices();
|
||||
const initLibraryItemsState = React.useCallback(() => {
|
||||
return core.transport.getState('library_items');
|
||||
const initLibraryState = React.useCallback(() => {
|
||||
const ctx = core.transport.getState('ctx');
|
||||
return mapLibraryState(ctx);
|
||||
}, []);
|
||||
const libraryItems = useModelState({
|
||||
model: 'library_items',
|
||||
init: initLibraryItemsState
|
||||
const library = useModelState({
|
||||
model: 'ctx',
|
||||
init: initLibraryState,
|
||||
map: mapLibraryState
|
||||
});
|
||||
const addToLibrary = React.useCallback((metaItem) => {
|
||||
core.transport.dispatch({
|
||||
|
|
@ -32,8 +38,8 @@ const useInLibrary = (metaItem) => {
|
|||
});
|
||||
}, []);
|
||||
const inLibrary = React.useMemo(() => {
|
||||
return libraryItems.ids.includes(metaItem !== null ? metaItem.id : null);
|
||||
}, [metaItem, libraryItems]);
|
||||
return metaItem !== null && metaItem.id in library && !library[metaItem.id].removed;
|
||||
}, [metaItem, library]);
|
||||
const toggleInLibrary = React.useMemo(() => {
|
||||
return metaItem !== null ?
|
||||
() => inLibrary ? removeFromLibrary(metaItem) : addToLibrary(metaItem)
|
||||
|
|
|
|||
Loading…
Reference in a new issue