2022-06-24 10:54:55 +02:00
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
2020-09-26 13:03:13 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2022-06-24 10:54:55 +02:00
|
|
|
import ButtonLayout from 'components/layout/ButtonLayout';
|
|
|
|
import useStore, { checkVersion } from 'store/version';
|
|
|
|
import { setItem } from 'lib/web';
|
2022-07-16 08:53:31 +02:00
|
|
|
import { REPO_URL, VERSION_CHECK } from 'lib/constants';
|
2020-09-30 01:25:44 +02:00
|
|
|
import Button from './Button';
|
2022-06-24 10:54:55 +02:00
|
|
|
import styles from './UpdateNotice.module.css';
|
2022-08-02 08:04:47 +02:00
|
|
|
import useUser from 'hooks/useUser';
|
|
|
|
import useConfig from 'hooks/useConfig';
|
2020-09-26 13:03:13 +02:00
|
|
|
|
|
|
|
export default function UpdateNotice() {
|
2022-08-02 08:04:47 +02:00
|
|
|
const { user } = useUser();
|
|
|
|
const { updatesDisabled } = useConfig();
|
2022-07-16 08:53:31 +02:00
|
|
|
const { latest, checked, hasUpdate, releaseUrl } = useStore();
|
2022-06-24 10:54:55 +02:00
|
|
|
const [dismissed, setDismissed] = useState(false);
|
2022-08-02 08:04:47 +02:00
|
|
|
const allowCheck = user?.is_admin && !updatesDisabled;
|
2022-06-24 10:54:55 +02:00
|
|
|
|
|
|
|
const updateCheck = useCallback(() => {
|
|
|
|
setItem(VERSION_CHECK, { version: latest, time: Date.now() });
|
|
|
|
}, [latest]);
|
2020-09-30 01:25:44 +02:00
|
|
|
|
2020-09-30 06:22:08 +02:00
|
|
|
function handleViewClick() {
|
|
|
|
updateCheck();
|
2022-06-24 10:54:55 +02:00
|
|
|
setDismissed(true);
|
2022-07-16 08:53:31 +02:00
|
|
|
location.href = releaseUrl || REPO_URL;
|
2020-09-30 01:25:44 +02:00
|
|
|
}
|
2020-09-26 13:03:13 +02:00
|
|
|
|
2020-09-30 06:22:08 +02:00
|
|
|
function handleDismissClick() {
|
|
|
|
updateCheck();
|
2022-06-24 10:54:55 +02:00
|
|
|
setDismissed(true);
|
2020-09-30 06:22:08 +02:00
|
|
|
}
|
2020-09-26 13:03:13 +02:00
|
|
|
|
2022-06-24 10:54:55 +02:00
|
|
|
useEffect(() => {
|
2022-08-02 08:04:47 +02:00
|
|
|
if (!checked && allowCheck) {
|
2022-06-24 10:54:55 +02:00
|
|
|
checkVersion();
|
|
|
|
}
|
2022-08-02 08:04:47 +02:00
|
|
|
}, [checked]);
|
2022-06-24 10:54:55 +02:00
|
|
|
|
2022-08-02 08:04:47 +02:00
|
|
|
if (!hasUpdate || dismissed || !allowCheck) {
|
2020-09-30 01:25:44 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-26 13:03:13 +02:00
|
|
|
return (
|
2020-09-30 01:25:44 +02:00
|
|
|
<div className={styles.notice}>
|
|
|
|
<div className={styles.message}>
|
2020-09-26 13:03:13 +02:00
|
|
|
<FormattedMessage
|
|
|
|
id="message.new-version-available"
|
2020-09-30 01:25:44 +02:00
|
|
|
defaultMessage="A new version of umami {version} is available!"
|
|
|
|
values={{ version: `v${latest}` }}
|
2020-09-26 13:03:13 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2022-03-03 23:23:57 +01:00
|
|
|
<ButtonLayout className={styles.buttons}>
|
2020-09-30 06:22:08 +02:00
|
|
|
<Button size="xsmall" variant="action" onClick={handleViewClick}>
|
2020-10-13 07:53:59 +02:00
|
|
|
<FormattedMessage id="label.view-details" defaultMessage="View details" />
|
2020-09-30 01:25:44 +02:00
|
|
|
</Button>
|
2020-09-30 06:22:08 +02:00
|
|
|
<Button size="xsmall" onClick={handleDismissClick}>
|
2020-10-13 07:53:59 +02:00
|
|
|
<FormattedMessage id="label.dismiss" defaultMessage="Dismiss" />
|
2020-09-30 01:25:44 +02:00
|
|
|
</Button>
|
|
|
|
</ButtonLayout>
|
2020-09-26 13:03:13 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|