Files
jitsi-meet/react/features/toolbox/middleware.web.ts
Hristo Terezov e2ec4842a1 fix(toolbarButtons): filter visitor buttons in redux.
Filters the toolbarButtons in redux depending on the visitor state instead of filtering them every time in mapStateToProps. This will prevent unnecessary rerenders of the toolbar.

Additionally:
 - Moves visitor buttons const from features/config in features/toolbox.
 - Removes dublicate functions isButtonEnabled and isToolbarButtonEnabled.
 - Adds more buttons to the visitor allowed buttons which functionality has been any way accessible trough shortcuts or somewhere else.
 - Enables customButtons to be visible for visitors.
2024-02-29 18:51:47 -06:00

130 lines
3.7 KiB
TypeScript

import { AnyAction } from 'redux';
import { IReduxState } from '../app/types';
import { OVERWRITE_CONFIG, SET_CONFIG, UPDATE_CONFIG } from '../base/config/actionTypes';
import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
import { I_AM_VISITOR_MODE } from '../visitors/actionTypes';
import { iAmVisitor } from '../visitors/functions';
import {
CLEAR_TOOLBOX_TIMEOUT,
SET_FULL_SCREEN,
SET_TOOLBAR_BUTTONS,
SET_TOOLBOX_TIMEOUT
} from './actionTypes';
import { TOOLBAR_BUTTONS, VISITORS_MODE_BUTTONS } from './constants';
import './subscriber.web';
/**
* Middleware which intercepts Toolbox actions to handle changes to the
* visibility timeout of the Toolbox.
*
* @param {Store} store - The redux store.
* @returns {Function}
*/
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case CLEAR_TOOLBOX_TIMEOUT: {
const { timeoutID } = store.getState()['features/toolbox'];
clearTimeout(timeoutID ?? undefined);
break;
}
case UPDATE_CONFIG:
case OVERWRITE_CONFIG:
case I_AM_VISITOR_MODE:
case SET_CONFIG: {
const result = next(action);
const toolbarButtons = _getToolbarButtons(store.getState());
store.dispatch({
type: SET_TOOLBAR_BUTTONS,
toolbarButtons
});
return result;
}
case SET_FULL_SCREEN:
return _setFullScreen(next, action);
case SET_TOOLBOX_TIMEOUT: {
const { timeoutID } = store.getState()['features/toolbox'];
const { handler, timeoutMS }: { handler: Function; timeoutMS: number; } = action;
clearTimeout(timeoutID ?? undefined);
action.timeoutID = setTimeout(handler, timeoutMS);
break;
}
}
return next(action);
});
type DocumentElement = {
requestFullscreen?: Function;
webkitRequestFullscreen?: Function;
};
/**
* Makes an external request to enter or exit full screen mode.
*
* @param {Dispatch} next - The redux dispatch function to dispatch the
* specified action to the specified store.
* @param {Action} action - The redux action SET_FULL_SCREEN which is being
* dispatched in the specified store.
* @private
* @returns {Object} The value returned by {@code next(action)}.
*/
function _setFullScreen(next: Function, action: AnyAction) {
const result = next(action);
const { fullScreen } = action;
if (fullScreen) {
const documentElement: DocumentElement
= document.documentElement || {};
if (typeof documentElement.requestFullscreen === 'function') {
documentElement.requestFullscreen();
} else if (
typeof documentElement.webkitRequestFullscreen === 'function') {
documentElement.webkitRequestFullscreen();
}
return result;
}
if (typeof document.exitFullscreen === 'function') {
document.exitFullscreen();
} else if (typeof document.webkitExitFullscreen === 'function') {
document.webkitExitFullscreen();
}
return result;
}
/**
* Returns the list of enabled toolbar buttons.
*
* @param {Object} state - The redux state.
* @returns {Array<string>} - The list of enabled toolbar buttons.
*/
function _getToolbarButtons(state: IReduxState): Array<string> {
const { toolbarButtons, customToolbarButtons } = state['features/base/config'];
const customButtons = customToolbarButtons?.map(({ id }) => id);
let buttons = Array.isArray(toolbarButtons) ? toolbarButtons : TOOLBAR_BUTTONS;
if (iAmVisitor(state)) {
buttons = VISITORS_MODE_BUTTONS.filter(button => buttons.indexOf(button) > -1);
}
if (customButtons) {
return [ ...buttons, ...customButtons ];
}
return buttons;
}