2022-04-12 01:09:13 +08:00
|
|
|
import React, { useReducer } from 'react';
|
2023-03-02 21:25:08 +08:00
|
|
|
import { throttle } from '/imports/utils/throttle';
|
2022-04-12 01:09:13 +08:00
|
|
|
import Service from './service';
|
|
|
|
|
|
|
|
export const CustomVirtualBackgroundsContext = React.createContext();
|
|
|
|
|
|
|
|
const reducer = (state, action) => {
|
2022-07-08 23:16:37 +08:00
|
|
|
const { save, del, update } = Service;
|
2022-04-12 01:09:13 +08:00
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
case 'load': {
|
2022-07-08 05:59:30 +08:00
|
|
|
const backgrounds = { ...state.backgrounds };
|
|
|
|
action.backgrounds.forEach((background) => {
|
|
|
|
backgrounds[background.uniqueId] = background;
|
|
|
|
});
|
2022-04-12 01:09:13 +08:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loaded: true,
|
2022-07-08 05:59:30 +08:00
|
|
|
backgrounds,
|
2022-04-12 01:09:13 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
case 'new': {
|
2022-07-08 23:16:37 +08:00
|
|
|
save(action.background);
|
2022-04-12 01:09:13 +08:00
|
|
|
return {
|
|
|
|
...state,
|
2022-07-08 05:59:30 +08:00
|
|
|
backgrounds: {
|
|
|
|
...state.backgrounds,
|
|
|
|
[action.background.uniqueId]: action.background,
|
|
|
|
},
|
2022-04-12 01:09:13 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
case 'delete': {
|
2022-07-08 05:59:30 +08:00
|
|
|
const { backgrounds } = state;
|
|
|
|
delete backgrounds[action.uniqueId];
|
2022-04-12 01:09:13 +08:00
|
|
|
del(action.uniqueId);
|
|
|
|
return {
|
2022-06-01 23:31:56 +08:00
|
|
|
...state,
|
2022-07-08 05:59:30 +08:00
|
|
|
backgrounds,
|
2022-04-12 01:09:13 +08:00
|
|
|
};
|
|
|
|
}
|
2022-07-08 23:16:37 +08:00
|
|
|
case 'update': {
|
|
|
|
if (action.background.custom) update(action.background);
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
backgrounds: {
|
|
|
|
...state.backgrounds,
|
|
|
|
[action.background.uniqueId]: action.background,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case 'setDefault': {
|
|
|
|
const backgrounds = { ...state.backgrounds };
|
|
|
|
action.backgrounds.forEach((background) => {
|
|
|
|
backgrounds[background.uniqueId] = background;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
defaultSetUp: true,
|
|
|
|
backgrounds,
|
|
|
|
};
|
|
|
|
}
|
2022-04-12 01:09:13 +08:00
|
|
|
default: {
|
|
|
|
throw new Error('Unknown custom background action.');
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 23:16:37 +08:00
|
|
|
};
|
2022-04-12 01:09:13 +08:00
|
|
|
|
|
|
|
export const CustomBackgroundsProvider = ({ children }) => {
|
|
|
|
const [state, dispatch] = useReducer(reducer, {
|
|
|
|
loaded: false,
|
2022-07-08 23:16:37 +08:00
|
|
|
defaultSetUp: false,
|
2022-07-08 05:59:30 +08:00
|
|
|
backgrounds: {},
|
2022-04-12 01:09:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const { load } = Service;
|
|
|
|
|
|
|
|
const loadFromDB = () => {
|
|
|
|
const onError = () => dispatch({
|
|
|
|
type: 'load',
|
2022-12-06 18:32:40 +08:00
|
|
|
backgrounds: [],
|
2022-04-12 01:09:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const onSuccess = (backgrounds) => dispatch({
|
|
|
|
type: 'load',
|
|
|
|
backgrounds,
|
|
|
|
});
|
|
|
|
|
|
|
|
load(onError, onSuccess);
|
2022-07-08 23:16:37 +08:00
|
|
|
};
|
2022-04-12 01:09:13 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<CustomVirtualBackgroundsContext.Provider
|
|
|
|
value={{
|
|
|
|
dispatch,
|
|
|
|
loaded: state.loaded,
|
2022-07-08 23:16:37 +08:00
|
|
|
defaultSetUp: state.defaultSetUp,
|
2022-07-08 05:59:30 +08:00
|
|
|
backgrounds: state.backgrounds,
|
2023-03-02 02:13:29 +08:00
|
|
|
loadFromDB: throttle(loadFromDB, 500, { leading: true, trailing: false }),
|
2022-04-12 01:09:13 +08:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</CustomVirtualBackgroundsContext.Provider>
|
|
|
|
);
|
2022-07-08 23:16:37 +08:00
|
|
|
};
|