Unverified Commit e444b8f0 authored by Mattermost Build's avatar Mattermost Build Committed by GitHub
Browse files

Apps commands invalidate form cache (#7922) (#8049)

Automatic Merge
parent 9dbe3cdd
......@@ -843,6 +843,9 @@ export class AppCommandParser {
}
public setChannelContext = (channelID: string, rootPostID?: string) => {
if (this.channelID !== channelID || this.rootPostID !== rootPostID) {
this.forms = {};
}
this.channelID = channelID;
this.rootPostID = rootPostID;
}
......@@ -933,14 +936,17 @@ export class AppCommandParser {
}
public getForm = async (location: string, binding: AppBinding): Promise<{form?: AppForm; error?: string} | undefined> => {
const form = this.forms[location];
const rootID = this.rootPostID || '';
const key = `${this.channelID}-${rootID}-${location}`;
const form = this.forms[key];
if (form) {
return {form};
}
this.forms = {};
const fetched = await this.fetchForm(binding);
if (fetched?.form) {
this.forms[location] = fetched.form;
this.forms[key] = fetched.form;
}
return fetched;
}
......
......@@ -51,7 +51,11 @@ describe('CommandProvider', () => {
}]);
Client4.getCommandAutocompleteSuggestionsList = mockFunc;
const provider = new CommandProvider({isInRHS: false});
const provider = new CommandProvider({
teamId: 'current_team',
channelId: 'current_channel',
rootId: 'current_root',
});
const callback = jest.fn();
provider.handlePretextChanged('/jira issue', callback);
......@@ -86,7 +90,11 @@ describe('CommandProvider', () => {
}]);
Client4.getCommandAutocompleteSuggestionsList = mockFunc;
const provider = new CommandProvider({isInRHS: false});
const provider = new CommandProvider({
teamId: 'current_team',
channelId: 'current_channel',
rootId: 'current_root',
});
const callback = jest.fn();
provider.handlePretextChanged('/jira issue', callback);
......
......@@ -6,16 +6,11 @@ import React from 'react';
import {Store} from 'redux';
import {Client4} from 'mattermost-redux/client';
import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {getChannel, getCurrentChannel, getCurrentChannelId} from 'mattermost-redux/selectors/entities/channels';
import {appsEnabled} from 'mattermost-redux/selectors/entities/apps';
import {AutocompleteSuggestion} from 'mattermost-redux/types/integrations';
import {Post} from 'mattermost-redux/types/posts';
import {AutocompleteSuggestion, CommandArgs} from 'mattermost-redux/types/integrations';
import globalStore from 'stores/redux_store';
import {getSelectedPost} from 'selectors/rhs';
import * as UserAgent from 'utils/user_agent';
import * as Utils from 'utils/utils';
import {Constants} from 'utils/constants';
......@@ -80,7 +75,9 @@ export class CommandSuggestion extends Suggestion {
}
type Props = {
isInRHS: boolean;
teamId: string;
channelId: string;
rootId?: string;
};
export type Results = {
......@@ -93,7 +90,7 @@ export type Results = {
type ResultsCallback = (results: Results) => void;
export default class CommandProvider extends Provider {
private isInRHS: boolean;
private props: Props;
private store: Store<GlobalState>;
private triggerCharacter: string;
private appCommandParser: AppCommandParser;
......@@ -102,21 +99,16 @@ export default class CommandProvider extends Provider {
super();
this.store = globalStore;
this.isInRHS = props.isInRHS;
let rootId;
let channelId = getCurrentChannelId(this.store.getState());
if (this.isInRHS) {
const selectedPost = getSelectedPost(this.store.getState()) as Post;
if (selectedPost) {
channelId = selectedPost?.channel_id;
rootId = selectedPost?.root_id ? selectedPost.root_id : selectedPost.id;
}
}
this.appCommandParser = new AppCommandParser(this.store as any, intlShim, channelId, rootId);
this.props = props;
this.appCommandParser = new AppCommandParser(this.store as any, intlShim, props.channelId, props.rootId);
this.triggerCharacter = '/';
}
setProps(props: Props) {
this.props = props;
this.appCommandParser.setChannelContext(props.channelId, props.rootId);
}
handlePretextChanged(pretext: string, resultCallback: ResultsCallback) {
if (!pretext.startsWith(this.triggerCharacter)) {
return false;
......@@ -154,8 +146,10 @@ export default class CommandProvider extends Provider {
}
handleMobile(pretext: string, resultCallback: ResultsCallback) {
const {teamId} = this.props;
const command = pretext.toLowerCase();
Client4.getCommandsList(getCurrentTeamId(this.store.getState())).then(
Client4.getCommandsList(teamId).then(
(data) => {
let matches: AutocompleteSuggestion[] = [];
if (appsEnabled(this.store.getState())) {
......@@ -168,21 +162,23 @@ export default class CommandProvider extends Provider {
return;
}
if (cmd.trigger !== 'shortcuts') {
if ((this.triggerCharacter + cmd.trigger).indexOf(command) === 0) {
const s = this.triggerCharacter + cmd.trigger;
let hint = '';
if (cmd.auto_complete_hint && cmd.auto_complete_hint.length !== 0) {
hint = cmd.auto_complete_hint;
}
matches.push({
Suggestion: s,
Complete: '',
Hint: hint,
Description: cmd.auto_complete_desc,
IconData: '',
});
if (cmd.trigger === 'shortcuts') {
return;
}
if ((this.triggerCharacter + cmd.trigger).indexOf(command) === 0) {
const s = this.triggerCharacter + cmd.trigger;
let hint = '';
if (cmd.auto_complete_hint && cmd.auto_complete_hint.length !== 0) {
hint = cmd.auto_complete_hint;
}
matches.push({
Suggestion: s,
Complete: '',
Hint: hint,
Description: cmd.auto_complete_desc,
IconData: '',
});
}
});
......@@ -198,24 +194,17 @@ export default class CommandProvider extends Provider {
component: CommandSuggestion,
});
},
).catch(
() => {}, //eslint-disable-line no-empty-function
);
}
handleWebapp(pretext: string, resultCallback: ResultsCallback) {
const command = pretext.toLowerCase();
const teamId = getCurrentTeamId(this.store.getState());
const selectedPost = getSelectedPost(this.store.getState()) as Post | undefined;
let rootId;
if (this.isInRHS && selectedPost) {
rootId = selectedPost.root_id ? selectedPost.root_id : selectedPost.id;
}
const channel = this.isInRHS && selectedPost?.channel_id ? getChannel(this.store.getState(), selectedPost.channel_id) : getCurrentChannel(this.store.getState());
const args = {
channel_id: channel?.id,
...(rootId && {root_id: rootId, parent_id: rootId}),
const {teamId, channelId, rootId} = this.props;
const args: CommandArgs = {
team_id: teamId,
channel_id: channelId,
root_id: rootId,
};
Client4.getCommandAutocompleteSuggestionsList(command, teamId, args).then(
......
......@@ -177,7 +177,11 @@ describe('components/SuggestionBox', () => {
});
test('Test for suggestionBoxAlgn when slash command at beginning and when slash command in middle of text', () => {
const provider = new CommandProvider({isInRHS: true});
const provider = new CommandProvider({
teamId: 'current_team',
channelId: 'current_channel',
rootId: 'current_root',
});
const props = {
...baseProps,
providers: [provider],
......
......@@ -93,7 +93,9 @@ export default class Textbox extends React.PureComponent<Props> {
if (props.supportsCommands) {
this.suggestionProviders.push(new CommandProvider({
isInRHS: Boolean(this.props.rootId),
teamId: this.props.currentTeamId,
channelId: this.props.channelId,
rootId: this.props.rootId,
}));
}
......@@ -126,6 +128,13 @@ export default class Textbox extends React.PureComponent<Props> {
priorityProfiles: this.props.priorityProfiles,
});
}
if (providers[i] instanceof CommandProvider) {
(providers[i] as CommandProvider).setProps({
teamId: this.props.currentTeamId,
channelId: this.props.channelId,
rootId: this.props.rootId,
});
}
}
}
if (prevProps.value !== this.props.value) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment