mattermost-mobile/app/mm-redux/selectors/entities/apps.ts
Daniel Espino García dd36545079
[MM-36041] Ensure post options and app commands always have the bindings for its channel (#5563)
* Ensure post options always have the bindings for its channel

* Mimic webapp model

* Address feedback

* Add return to validate bindings

* Address feedback

* Use empty bindings constant to avoid rerenderings

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>
2021-09-24 10:32:58 -04:00

50 lines
1.9 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {createSelector} from 'reselect';
import {AppBinding} from '@mm-redux/types/apps';
import {GlobalState} from '@mm-redux/types/store';
import {appsEnabled} from '@utils/apps';
export function getThreadAppsBindingsChannelId(state: GlobalState): string {
return state.entities.apps.threadBindingsChannelId;
}
export const makeAppBindingsSelector = (location: string) => {
return createSelector(
(state: GlobalState) => state.entities.apps.bindings,
(state: GlobalState) => appsEnabled(state),
(bindings: AppBinding[], areAppsEnabled: boolean) => {
if (!areAppsEnabled || !bindings) {
return [];
}
const headerBindings = bindings.filter((b) => b.location === location);
return headerBindings.reduce((accum: AppBinding[], current: AppBinding) => accum.concat(current.bindings || []), []);
},
);
};
export const makeRHSAppBindingSelector = (location: string) => {
return createSelector(
(state: GlobalState) => state.entities.apps.threadBindings,
(state: GlobalState) => appsEnabled(state),
(bindings: AppBinding[], areAppsEnabled: boolean) => {
if (!areAppsEnabled || !bindings) {
return [];
}
const headerBindings = bindings.filter((b) => b.location === location);
return headerBindings.reduce((accum: AppBinding[], current: AppBinding) => accum.concat(current.bindings || []), []);
},
);
};
export const getAppCommandForm = (state: GlobalState, location: string) => {
return state.entities.apps.bindingsForms[location];
};
export const getAppRHSCommandForm = (state: GlobalState, location: string) => {
return state.entities.apps.threadBindingsForms[location];
};