mattermost-mobile/app/actions/views/permalink.ts
Anurag Shivarathri b3283ec9cf
MM-37517 Thread permalink (#5758)
* Thread permalink fix

* Reverted and implemented solution by updating the props

* fix TS on selectors/entities/posts.ts

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2022-02-03 12:48:43 -03:00

86 lines
2.7 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {intlShape} from 'react-intl';
import {Keyboard} from 'react-native';
import {Navigation} from 'react-native-navigation';
import {showModalOverCurrentContext} from '@actions/navigation';
import {loadChannelsByTeamName} from '@actions/views/channel';
import {getPost as fetchPost, selectFocusedPostId} from '@mm-redux/actions/posts';
import {getPost} from '@mm-redux/selectors/entities/posts';
import {isCollapsedThreadsEnabled} from '@mm-redux/selectors/entities/preferences';
import {getCurrentTeam} from '@mm-redux/selectors/entities/teams';
import {permalinkBadTeam} from '@utils/general';
import {changeOpacity} from '@utils/theme';
import type {DispatchFunc, GetStateFunc} from '@mm-redux/types/actions';
let showingPermalink = false;
export function showPermalink(intl: typeof intlShape, teamName: string, postId: string, openAsPermalink = true) {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
const state = getState();
let name = teamName;
if (!name) {
name = getCurrentTeam(state).name;
}
const loadTeam = await dispatch(loadChannelsByTeamName(name, permalinkBadTeam.bind(null, intl)));
let isThreadPost;
const collapsedThreadsEnabled = isCollapsedThreadsEnabled(state);
if (collapsedThreadsEnabled) {
let post = getPost(state, postId);
if (!post) {
const {data} = await dispatch(fetchPost(postId));
if (data) {
post = data;
}
}
if (post) {
isThreadPost = Boolean(post.root_id);
} else {
return {};
}
}
if (!loadTeam.error) {
Keyboard.dismiss();
dispatch(selectFocusedPostId(postId));
const screen = 'Permalink';
const passProps = {
isPermalink: openAsPermalink,
isThreadPost,
focusedPostId: postId,
teamName,
};
if (showingPermalink) {
Navigation.updateProps(screen, passProps);
return {};
}
const options = {
layout: {
componentBackgroundColor: changeOpacity('#000', 0.2),
},
};
showingPermalink = true;
showModalOverCurrentContext(screen, passProps, options);
}
return {};
};
}
export function closePermalink() {
return async (dispatch: DispatchFunc) => {
showingPermalink = false;
return dispatch(selectFocusedPostId(''));
};
}