diff --git a/app/ui-sidenav/client/sideNav.js b/app/ui-sidenav/client/sideNav.js index 2f35e510f3f2..323895a2f288 100644 --- a/app/ui-sidenav/client/sideNav.js +++ b/app/ui-sidenav/client/sideNav.js @@ -1,4 +1,5 @@ import { Meteor } from 'meteor/meteor'; +import { Tracker } from 'meteor/tracker'; import { ReactiveVar } from 'meteor/reactive-var'; import { FlowRouter } from 'meteor/kadira:flow-router'; import { Template } from 'meteor/templating'; @@ -79,13 +80,28 @@ Template.sideNav.events({ }); const redirectToDefaultChannelIfNeeded = () => { - const currentRouteState = FlowRouter.current(); - const needToBeRedirect = ['/', '/home']; - const firstChannelAfterLogin = settings.get('First_Channel_After_Login'); - const room = roomTypes.findRoom('c', firstChannelAfterLogin, Meteor.userId()); - if (room && room._id && needToBeRedirect.includes(currentRouteState.path)) { + const needToBeRedirect = () => ['/', '/home'].includes(FlowRouter.current().path); + + Tracker.autorun((c) => { + const firstChannelAfterLogin = settings.get('First_Channel_After_Login'); + + if (!needToBeRedirect()) { + return c.stop(); + } + + if (!firstChannelAfterLogin) { + return c.stop(); + } + + const room = roomTypes.findRoom('c', firstChannelAfterLogin, Meteor.userId()); + + if (!room) { + return; + } + + c.stop(); FlowRouter.go(`/channel/${ firstChannelAfterLogin }`); - } + }); }; Template.sideNav.onRendered(function() {