[MM-57714] Convert ./components/announcement_bar/version_bar/version_bar.tsx from Class Component to Function Component (#27966)

* fix: convert from class to function component

* fix: move 'reloadPage' function out of the component
Этот коммит содержится в:
Rita Anene
2024-08-26 09:35:14 +01:00
коммит произвёл GitHub
родитель cefb8abc05
Коммит 12ba21c4af

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

@@ -1,78 +1,63 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import React, {useEffect, useState} from 'react';
import {FormattedMessage} from 'react-intl';
import {AnnouncementBarTypes} from 'utils/constants';
import AnnouncementBar from '../default_announcement_bar';
interface Props {
type Props = {
buildHash?: string;
}
interface State {
buildHashOnAppLoad?: string;
}
const reloadPage = () => {
window.location.reload();
};
export default class VersionBar extends React.PureComponent <Props, State> {
constructor(props: Props) {
super(props);
const VersionBar = ({
buildHash,
}: Props) => {
const [buildHashOnAppLoad, setBuildHashOnAppLoad] = useState<string|undefined>(buildHash);
this.state = {
buildHashOnAppLoad: props.buildHash,
};
}
static getDerivedStateFromProps(props: Props, state: State) {
if (!state.buildHashOnAppLoad && props.buildHash) {
return {
buildHashOnAppLoad: props.buildHash,
};
useEffect(() => {
if (!buildHashOnAppLoad && buildHash) {
setBuildHashOnAppLoad(buildHash);
}
}, [buildHash, buildHashOnAppLoad]);
if (!buildHashOnAppLoad || buildHashOnAppLoad === buildHash) {
return null;
}
reloadPage = () => {
window.location.reload();
};
render() {
const {buildHashOnAppLoad} = this.state;
const {buildHash} = this.props;
if (!buildHashOnAppLoad) {
return null;
}
if (buildHashOnAppLoad !== buildHash) {
return (
<AnnouncementBar
type={AnnouncementBarTypes.ANNOUNCEMENT}
message={
<React.Fragment>
if (buildHashOnAppLoad !== buildHash) {
return (
<AnnouncementBar
type={AnnouncementBarTypes.ANNOUNCEMENT}
message={
<>
<FormattedMessage
id='version_bar.new'
defaultMessage='A new version of Mattermost is available.'
/>
<a
onClick={reloadPage}
style={{marginLeft: '.5rem'}}
>
<FormattedMessage
id='version_bar.new'
defaultMessage='A new version of Mattermost is available.'
id='version_bar.refresh'
defaultMessage='Refresh the app now'
/>
<a
onClick={this.reloadPage}
style={{marginLeft: '.5rem'}}
>
<FormattedMessage
id='version_bar.refresh'
defaultMessage='Refresh the app now'
/>
</a>
{'.'}
</React.Fragment>
}
/>
);
}
return null;
</a>
{'.'}
</>
}
/>
);
}
}
return null;
};
export default React.memo(VersionBar);