Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: restore last active conversation when returning to messenger #2610

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/components/messenger/list/conversation-list-panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { itemToOption } from '../../lib/utils';
import { ScrollbarContainer } from '../../../scrollbar-container';
import escapeRegExp from 'lodash/escapeRegExp';
import { getDirectMatches, getIndirectMatches } from './utils';
import { IconStar1 } from '@zero-tech/zui/icons';
import { getLastActiveTab, setLastActiveTab } from '../../../../lib/last-tab';

import { bemClassName } from '../../../../lib/bem';
import './conversation-list-panel.scss';
import { IconStar1 } from '@zero-tech/zui/icons';

const cn = bemClassName('messages-list');

Expand All @@ -30,7 +31,7 @@ export interface Properties {
onRemoveLabel: (payload: { roomId: string; label: string }) => void;
}

enum Tab {
export enum Tab {
All = 'all',
Favorites = 'favorites',
Work = 'work',
Expand Down Expand Up @@ -61,12 +62,22 @@ export class ConversationListPanel extends React.Component<Properties, State> {
this.tabListRef.current.addEventListener('wheel', this.horizontalScroll, { passive: false });
}

this.setInitialTab();
const lastActiveTab = getLastActiveTab();
if (lastActiveTab) {
this.setState({ selectedTab: lastActiveTab as Tab });
} else {
this.setInitialTab();
}
}

componentDidUpdate(prevProps) {
if (prevProps.isLabelDataLoaded !== this.props.isLabelDataLoaded) {
this.setInitialTab();
const lastActiveTab = getLastActiveTab();
if (lastActiveTab) {
this.setState({ selectedTab: lastActiveTab as Tab });
} else {
this.setInitialTab();
}
}
}

Expand Down Expand Up @@ -161,6 +172,7 @@ export class ConversationListPanel extends React.Component<Properties, State> {

selectTab = (tab) => {
this.setState({ selectedTab: tab });
setLastActiveTab(tab);
};

onAddLabel = (roomId: string, label) => {
Expand Down
15 changes: 15 additions & 0 deletions src/lib/last-tab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const LAST_TAB_KEY = 'last-active-tab';

export const setLastActiveTab = (tab: string): void => {
if (tab) {
localStorage.setItem(LAST_TAB_KEY, tab);
}
};

export const getLastActiveTab = (): string | null => {
return localStorage.getItem(LAST_TAB_KEY);
};

export const clearLastActiveTab = (): void => {
localStorage.removeItem(LAST_TAB_KEY);
};
5 changes: 5 additions & 0 deletions src/store/authentication/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { StoreBuilder } from '../test/store';
import { throwError } from 'redux-saga-test-plan/providers';
import { closeUserProfile } from '../user-profile/saga';
import { clearLastActiveConversation } from '../../lib/last-conversation';
import { clearLastActiveTab } from '../../lib/last-tab';

describe(nonceOrAuthorize, () => {
const signedWeb3Token = '0x000000000000000000000000000000000000000A';
Expand Down Expand Up @@ -256,6 +257,10 @@ describe(forceLogout, () => {
await expectLogoutSaga().call(clearLastActiveConversation).call(terminate).run();
});

it('clears the last active tab', async () => {
await expectLogoutSaga().call(clearLastActiveTab).call(terminate).run();
});

it('clears the user session', async () => {
await expectLogoutSaga().call(terminate).run();
});
Expand Down
2 changes: 2 additions & 0 deletions src/store/authentication/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getHistory } from '../../lib/browser';
import { completePendingUserProfile } from '../registration/saga';
import { closeUserProfile } from '../user-profile/saga';
import { clearLastActiveConversation } from '../../lib/last-conversation';
import { clearLastActiveTab } from '../../lib/last-tab';

export const currentUserSelector = () => (state) => {
return getDeepProperty(state, 'authentication.user.data', null);
Expand Down Expand Up @@ -110,6 +111,7 @@ export function* closeLogoutModal() {
export function* forceLogout() {
yield closeLogoutModal();
yield call(clearLastActiveConversation);
yield call(clearLastActiveTab);
yield call(terminate);
}

Expand Down
Loading