bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/components-data/users-context/context.jsx

134 lines
3.3 KiB
React
Raw Normal View History

import React, {
createContext,
useReducer,
} from 'react';
import ChatLogger from '/imports/ui/components/chat/chat-logger/ChatLogger';
export const ACTIONS = {
TEST: 'test',
ADDED: 'added',
CHANGED: 'changed',
REMOVED: 'removed',
ADDED_USER_PERSISTENT_DATA: 'added_user_persistent_data',
CHANGED_USER_PERSISTENT_DATA: 'changed_user_persistent_data',
};
export const UsersContext = createContext();
const reducer = (state, action) => {
switch (action.type) {
case ACTIONS.TEST: {
return {
...state,
...action.value,
};
}
case ACTIONS.ADDED:
case ACTIONS.CHANGED: {
ChatLogger.debug('UsersContextProvider::reducer::added', { ...action });
const { user } = action.value;
2021-04-15 20:12:21 +08:00
const newState = { ...state };
if (!newState[user.meetingId]) {
newState[user.meetingId] = {};
}
newState[user.meetingId][user.userId] = {
...user,
};
return newState;
}
case ACTIONS.REMOVED: {
ChatLogger.debug('UsersContextProvider::reducer::removed', { ...action });
2021-04-15 20:12:21 +08:00
const { user } = action.value;
2022-06-14 22:29:59 +08:00
const stateUser = state[user.meetingId][user.userId];
if (stateUser) {
const newState = { ...state };
2022-06-14 22:29:59 +08:00
newState[user.meetingId][user.userId] = {
...stateUser,
loggedOut: true,
};
return newState;
}
2022-06-14 22:29:59 +08:00
return state;
}
2021-04-15 20:12:21 +08:00
// USER PERSISTENT DATA
case ACTIONS.ADDED_USER_PERSISTENT_DATA: {
const { user } = action.value;
2021-06-30 22:29:03 +08:00
if (state[user.meetingId] && state[user.meetingId][user.userId]) {
2022-06-14 22:29:59 +08:00
if (state[user.meetingId][user.userId].loggedOut) {
const newState = { ...state };
newState[user.meetingId][user.userId] = {
...state[user.meetingId][user.userId],
loggedOut: false,
};
return newState;
}
return state;
}
2021-04-15 20:12:21 +08:00
const newState = { ...state };
if (!newState[user.meetingId]) {
newState[user.meetingId] = {};
}
newState[user.meetingId][user.userId] = {
...user,
};
return newState;
}
case ACTIONS.CHANGED_USER_PERSISTENT_DATA: {
const { user } = action.value;
2021-04-15 20:12:21 +08:00
const stateUser = state[user.meetingId][user.userId];
if (stateUser) {
2021-04-15 20:12:21 +08:00
const newState = { ...state };
newState[user.meetingId][user.userId] = {
...stateUser,
...user,
};
2021-04-15 20:12:21 +08:00
return newState;
}
return state;
}
default: {
throw new Error(`Unexpected action: ${JSON.stringify(action)}`);
}
}
};
export const UsersContextProvider = (props) => {
const [usersContextState, usersContextDispatch] = useReducer(reducer, {});
ChatLogger.debug('UsersContextProvider::usersContextState', usersContextState);
return (
<UsersContext.Provider value={
{
...props,
dispatch: usersContextDispatch,
users: { ...usersContextState },
}
}
>
{props.children}
</UsersContext.Provider>
);
};
export const UsersContextConsumer = Component => props => (
<UsersContext.Consumer>
{contexts => <Component {...props} {...contexts} />}
</UsersContext.Consumer>
);
export const withUsersConsumer = Component => UsersContextConsumer(Component);
export default {
UsersContextConsumer,
UsersContextProvider,
};