Files
mostlymatter/webapp/channels/src/components/threading/hooks.ts
Daniel Espino García d13429aa92 Enable import order (#24091)
* Enable import order

* Add new order groups and add consistent-type-imports

* Remove types group, move react to top and fix issues

* Add store and reducers to the redux group and move imports from types folder to the end

* Fix tsc

* Remove react rule

* Fix test

* Fix eslint disable
2023-09-06 12:51:55 +02:00

45 строки
1.7 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {useMemo, useCallback} from 'react';
import {useSelector, shallowEqual} from 'react-redux';
import {useParams, useHistory} from 'react-router-dom';
import type {Team} from '@mattermost/types/teams';
import type {UserThread} from '@mattermost/types/threads';
import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
/**
* GlobalThreads-specific hook for nav/routing, selection, and common data needed for actions.
*/
export function useThreadRouting() {
const matchParams = useParams<{team: string; threadIdentifier?: UserThread['id']}>();
const params = useMemo(() => matchParams, [matchParams.threadIdentifier, matchParams.team]);
const history = useHistory();
const currentTeamId = useSelector(getCurrentTeamId, shallowEqual);
const currentUserId = useSelector(getCurrentUserId, shallowEqual);
const select = useCallback((threadId?: UserThread['id']) => {
return history.push(`/${params.team}/threads${threadId ? '/' + threadId : ''}`);
}, [params.team]);
const clear = useCallback(() => history.replace(`/${params.team}/threads`), [params.team]);
const goToInChannel = useCallback((threadId?: UserThread['id'], teamName: Team['name'] = params.team) => {
return history.push(`/${teamName}/pl/${threadId ?? params.threadIdentifier}`);
}, [params.threadIdentifier, params.team]);
return {
params,
history,
currentTeamId,
currentUserId,
clear,
select,
goToInChannel,
};
}