mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-03-11 21:27:05 +00:00
useToggleInLibrary hook implemented
This commit is contained in:
parent
13f58fcbff
commit
166fb7c506
3 changed files with 37 additions and 54 deletions
|
|
@ -32,11 +32,11 @@ const useDeepEqualEffect = require('./useDeepEqualEffect');
|
|||
const useDeepEqualMemo = require('./useDeepEqualMemo');
|
||||
const useDeepEqualState = require('./useDeepEqualState');
|
||||
const useFullscreen = require('./useFullscreen');
|
||||
const useInLibrary = require('./useInLibrary');
|
||||
const useLiveRef = require('./useLiveRef');
|
||||
const useModelState = require('./useModelState');
|
||||
const useProfile = require('./useProfile');
|
||||
const useStreamingServer = require('./useStreamingServer');
|
||||
const useToggleInLibrary = require('./useToggleInLibrary');
|
||||
|
||||
module.exports = {
|
||||
AddonDetailsModal,
|
||||
|
|
@ -73,9 +73,9 @@ module.exports = {
|
|||
useDeepEqualMemo,
|
||||
useDeepEqualState,
|
||||
useFullscreen,
|
||||
useInLibrary,
|
||||
useLiveRef,
|
||||
useModelState,
|
||||
useProfile,
|
||||
useStreamingServer,
|
||||
useToggleInLibrary,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,52 +0,0 @@
|
|||
// Copyright (C) 2017-2020 Smart code 203358507
|
||||
|
||||
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 initLibraryState = React.useCallback(() => {
|
||||
const ctx = core.transport.getState('ctx');
|
||||
return mapLibraryState(ctx);
|
||||
}, []);
|
||||
const library = useModelState({
|
||||
model: 'ctx',
|
||||
init: initLibraryState,
|
||||
map: mapLibraryState
|
||||
});
|
||||
const addToLibrary = React.useCallback((metaItem) => {
|
||||
core.transport.dispatch({
|
||||
action: 'Ctx',
|
||||
args: {
|
||||
action: 'AddToLibrary',
|
||||
args: metaItem
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
const removeFromLibrary = React.useCallback((metaItem) => {
|
||||
core.transport.dispatch({
|
||||
action: 'Ctx',
|
||||
args: {
|
||||
action: 'RemoveFromLibrary',
|
||||
args: metaItem.id
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
const inLibrary = React.useMemo(() => {
|
||||
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)
|
||||
:
|
||||
null;
|
||||
}, [metaItem, inLibrary]);
|
||||
return [inLibrary, toggleInLibrary];
|
||||
};
|
||||
|
||||
module.exports = useInLibrary;
|
||||
35
src/common/useToggleInLibrary.js
Normal file
35
src/common/useToggleInLibrary.js
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright (C) 2017-2020 Smart code 203358507
|
||||
|
||||
const React = require('react');
|
||||
const { useServices } = require('stremio/services');
|
||||
|
||||
const useToggleInLibrary = (metaItem) => {
|
||||
const { core } = useServices();
|
||||
const addToLibrary = React.useCallback(() => {
|
||||
core.transport.dispatch({
|
||||
action: 'Ctx',
|
||||
args: {
|
||||
action: 'AddToLibrary',
|
||||
args: metaItem
|
||||
}
|
||||
});
|
||||
}, [metaItem]);
|
||||
const removeFromLibrary = React.useCallback(() => {
|
||||
core.transport.dispatch({
|
||||
action: 'Ctx',
|
||||
args: {
|
||||
action: 'RemoveFromLibrary',
|
||||
args: metaItem.id
|
||||
}
|
||||
});
|
||||
}, [metaItem]);
|
||||
return metaItem !== null ?
|
||||
metaItem.inLibrary ?
|
||||
removeFromLibrary
|
||||
:
|
||||
addToLibrary
|
||||
:
|
||||
null;
|
||||
};
|
||||
|
||||
module.exports = useToggleInLibrary;
|
||||
Loading…
Reference in a new issue