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

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

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