[MM-56290] Convert ./components/channel_header_dropdown/menu_items/toggle_favorite_channel/toggle_favorite_channel.tsx from Class Component to Function Component (#25762)

* [MM-56290] Convert `./components/channel_header_dropdown/menu_items/toggle_favorite_channel/toggle_favorite_channel.tsx` from Class Component to Function Component

* fix: update snapshots and check-types issue
Этот коммит содержится в:
Syed Ali Abbas Zaidi
2024-01-05 21:57:19 +05:00
коммит произвёл GitHub
родитель 077221a940
Коммит d9c2a8825b
2 изменённых файлов: 36 добавлений и 38 удалений

Просмотреть файл

@@ -48,7 +48,7 @@ exports[`components/ChannelHeaderDropdown should match snapshot with no plugin i
openUp={false} openUp={false}
/> />
<Memo(MenuGroup)> <Memo(MenuGroup)>
<Connect(ToggleFavoriteChannel) <Connect(Component)
channel={ channel={
Object { Object {
"create_at": 0, "create_at": 0,
@@ -880,7 +880,7 @@ exports[`components/ChannelHeaderDropdown should match snapshot with plugins 1`]
openUp={false} openUp={false}
/> />
<Memo(MenuGroup)> <Memo(MenuGroup)>
<Connect(ToggleFavoriteChannel) <Connect(Component)
channel={ channel={
Object { Object {
"create_at": 0, "create_at": 0,

Просмотреть файл

@@ -1,61 +1,59 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import React from 'react'; import React, {memo, useCallback} from 'react';
import type {MouseEvent} from 'react';
import {useIntl} from 'react-intl';
import type {Channel} from '@mattermost/types/channels'; import type {Channel} from '@mattermost/types/channels';
import Menu from 'components/widgets/menu/menu'; import Menu from 'components/widgets/menu/menu';
import {localizeMessage} from 'utils/utils';
type Action = { type Action = {
favoriteChannel: (channelId: string) => void; favoriteChannel: (channelId: string) => void;
unfavoriteChannel: (channelId: string) => void; unfavoriteChannel: (channelId: string) => void;
}; };
type Props = { type Props = {
show: boolean; show?: boolean;
channel: Channel; channel: Channel;
isFavorite: boolean; isFavorite: boolean;
actions: Action; actions: Action;
}; };
export default class ToggleFavoriteChannel extends React.PureComponent<Props> { const ToggleFavoriteChannel = ({
static defaultProps = { show = true,
show: true,
};
toggleFavoriteChannel = (channelId: string) => {
const {
isFavorite, isFavorite,
actions: { actions: {
favoriteChannel, favoriteChannel,
unfavoriteChannel, unfavoriteChannel,
}, },
} = this.props; channel,
}: Props) => {
const intl = useIntl();
const toggleFavoriteChannel = useCallback((channelId: string) => {
return isFavorite ? unfavoriteChannel(channelId) : favoriteChannel(channelId); return isFavorite ? unfavoriteChannel(channelId) : favoriteChannel(channelId);
}; }, [isFavorite, favoriteChannel, unfavoriteChannel]);
handleClick = (e: React.MouseEvent<HTMLButtonElement>): void => { const handleClick = useCallback((e: MouseEvent<HTMLButtonElement>): void => {
e.preventDefault(); e.preventDefault();
this.toggleFavoriteChannel(this.props.channel.id); toggleFavoriteChannel(channel.id);
}; }, [channel.id, toggleFavoriteChannel]);
render() {
let text; let text;
if (this.props.isFavorite) { if (isFavorite) {
text = localizeMessage('channelHeader.removeFromFavorites', 'Remove from Favorites'); text = intl.formatMessage({id: 'channelHeader.removeFromFavorites', defaultMessage: 'Remove from Favorites'});
} else { } else {
text = localizeMessage('channelHeader.addToFavorites', 'Add to Favorites'); text = intl.formatMessage({id: 'channelHeader.addToFavorites', defaultMessage: 'Add to Favorites'});
} }
return ( return (
<Menu.ItemAction <Menu.ItemAction
show={this.props.show} show={show}
onClick={this.handleClick} onClick={handleClick}
text={text} text={text}
/> />
); );
} };
}
export default memo(ToggleFavoriteChannel);