Plugin, switch channel action

Hello,
I am working on web plugin which will switch channel after some event.

Currently I dispatching this action but it doesn’t work pretty well,
channel switch sometimes but there is problem with loading messages etc.

store.dispatch({type: ‘SELECT_CHANNEL’, data: channelId});

Which action I should use to do it properly?

Thanks!

Hi @slawek367, looks like we have an action in the webapp that lets you switch channels, however it is not exposed to plugins :frowning:

Good thing is it’s using all the components from MM-redux, so you can literally copy paste this and it should work for you.

I’m part of the toolkit team that supports the plugin framework, i’ll take a note and see if we can improve this behaviour.

Hi @ali-farooq0

I recreated this in plugin: https://pastebin.com/GgDVF96A
but the main problem is how I could load browserHistory object??

I think this is the only way to change channel but I cannot import it from plugin,
or maybe there is some other way to do this?

@slawek367, browserHistory is not a special object it’s basically just this: https://github.com/mattermost/mattermost-webapp/blob/master/utils/browser_history.jsx#L4

Hope that helps.

Hi @ali-farooq0 ,
yes I understand this but that is hardcoded in mattermost client and to push something to history object i need to import the same instance of object which is used in client. (and this is impossible to do by plugin side as I know?)

Here is some example: https://pastebin.com/QC0wt7kz even if i update browser history object then nothing changes. Im triggering that push method when some event occurs.

Ah true true.

Yeah it doesn’t look like another instance will work. Unfortunately that means you’ll have to do something like

if (history.pushState) {
    window.history.pushState("object or string", "Title", "/new-url");
} else {
  document.location.href = "/new-url";
}

Till we figure out a way to inject browserHistory to Plugin components.

Hi @ali-farooq0
unfortunately window.history.pushState updates url but app doesn’t listen to that changes and channel is not changed. In case of document.location.href channel changes but it refreshes whole page (instead reload only active channel) so it doesn’t satisfied me…

It is possible that in near future will be possible to import original browserHistory object created in main mattermost app or just add some method to mattermost redux which will be responsible for channel change?

Thank you!

Hey @slawek367, ahh that’s really unfortunately. Yes definitely I’ve take an action item to address the underlying issue so something like this doesn’t cause roadblocks in the future.

Thanks for your patience.

I know this is a bit old, but I’m working on same functionality.
Is there any new information this?

For anyone else.
This feature is going to be introduced in in coming release v5.22.
Then the WebappUtils property will be exposed to the window object and this holds the instance of browserHistory.
With this the answer from @ali-farooq0 works.

Sorry for double posting, but I found no way to edit my post.