mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-18 21:12:13 +00:00
useModelState integrated in useBoard
This commit is contained in:
parent
984b045ced
commit
8080f2e72e
1 changed files with 22 additions and 28 deletions
|
|
@ -1,9 +1,14 @@
|
|||
const React = require('react');
|
||||
const { useServices } = require('stremio/services');
|
||||
const { useModelState } = require('stremio/common');
|
||||
|
||||
const mapBoardState = (state) => {
|
||||
const selected = state.board.selected;
|
||||
const catalog_resources = state.board.catalog_resources.map((catalog_resource) => {
|
||||
const initBoardState = () => ({
|
||||
selected: null,
|
||||
catalog_resources: []
|
||||
});
|
||||
|
||||
const mapBoardState = (board) => {
|
||||
const selected = board.selected;
|
||||
const catalog_resources = board.catalog_resources.map((catalog_resource) => {
|
||||
catalog_resource.href = `#/discover/${encodeURIComponent(catalog_resource.request.base)}/${encodeURIComponent(catalog_resource.request.path.type_name)}/${encodeURIComponent(catalog_resource.request.path.id)}`;
|
||||
return catalog_resource;
|
||||
});
|
||||
|
|
@ -11,31 +16,20 @@ const mapBoardState = (state) => {
|
|||
};
|
||||
|
||||
const useBoard = () => {
|
||||
const { core } = useServices();
|
||||
const [board, setBoard] = React.useState(() => {
|
||||
const state = core.getState();
|
||||
const board = mapBoardState(state);
|
||||
return board;
|
||||
const loadBoardAction = React.useMemo(() => ({
|
||||
action: 'Load',
|
||||
args: {
|
||||
load: 'CatalogsWithExtra',
|
||||
args: { extra: [] }
|
||||
}
|
||||
}), []);
|
||||
return useModelState({
|
||||
model: 'board',
|
||||
action: loadBoardAction,
|
||||
timeout: 1000,
|
||||
map: mapBoardState,
|
||||
init: initBoardState
|
||||
});
|
||||
React.useLayoutEffect(() => {
|
||||
const onNewState = () => {
|
||||
const state = core.getState();
|
||||
const board = mapBoardState(state);
|
||||
setBoard(board);
|
||||
};
|
||||
core.on('NewModel', onNewState);
|
||||
core.dispatch({
|
||||
action: 'Load',
|
||||
args: {
|
||||
load: 'CatalogsWithExtra',
|
||||
args: { extra: [] }
|
||||
}
|
||||
}, 'board');
|
||||
return () => {
|
||||
core.off('NewModel', onNewState);
|
||||
};
|
||||
}, []);
|
||||
return board;
|
||||
};
|
||||
|
||||
module.exports = useBoard;
|
||||
|
|
|
|||
Loading…
Reference in a new issue