Alexander Zinchuk c3201cf632 Revert "Better browser history support (#1125)"
This reverts commit 3c19438f1a9f6db9a06dc3b48bd06d062befe0b8.
2021-06-14 22:12:34 +03:00

71 lines
1.5 KiB
TypeScript

import { addReducer, getDispatch, setGlobal } from '../../../lib/teact/teactn';
import {
exitMessageSelectMode,
updateCurrentMessageList,
} from '../../reducers';
import { selectCurrentMessageList } from '../../selectors';
window.addEventListener('popstate', (e) => {
if (!e.state) {
return;
}
const { chatId: id, threadId, messageListType: type } = e.state;
getDispatch().openChat({
id, threadId, type, noPushState: true,
});
});
addReducer('openChat', (global, actions, payload) => {
const {
id, threadId = -1, type = 'thread', noPushState,
} = payload!;
const currentMessageList = selectCurrentMessageList(global);
if (!currentMessageList
|| (
currentMessageList.chatId !== id
|| currentMessageList.threadId !== threadId
|| currentMessageList.type !== type
)) {
global = exitMessageSelectMode(global);
global = {
...global,
messages: {
...global.messages,
contentToBeScheduled: undefined,
},
...(id !== global.forwardMessages.toChatId && {
forwardMessages: {},
}),
};
setGlobal(global);
if (!noPushState) {
window.history.pushState({ chatId: id, threadId, messageListType: type }, '');
}
}
return updateCurrentMessageList(global, id, threadId, type);
});
addReducer('openChatWithInfo', (global, actions, payload) => {
setGlobal({
...global,
isChatInfoShown: true,
});
actions.openChat(payload);
});
addReducer('resetChatCreation', (global) => {
return {
...global,
chatCreation: undefined,
};
});