2022-06-24 10:54:55 +02:00
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
2023-03-22 22:05:55 +01:00
|
|
|
import { Button, Row, Column } from 'react-basics';
|
2022-08-29 05:20:54 +02:00
|
|
|
import { setItem } from 'next-basics';
|
2022-06-24 10:54:55 +02:00
|
|
|
import useStore, { checkVersion } from 'store/version';
|
2022-07-16 08:53:31 +02:00
|
|
|
import { REPO_URL, VERSION_CHECK } from 'lib/constants';
|
2022-06-24 10:54:55 +02:00
|
|
|
import styles from './UpdateNotice.module.css';
|
2023-03-22 22:05:55 +01:00
|
|
|
import useMessages from 'hooks/useMessages';
|
2020-09-26 13:03:13 +02:00
|
|
|
|
|
|
|
export default function UpdateNotice() {
|
2023-03-22 22:05:55 +01:00
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
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);
|
|
|
|
|
|
|
|
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-09-21 15:19:18 +02:00
|
|
|
open(releaseUrl || REPO_URL, '_blank');
|
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-09 23:58:27 +02:00
|
|
|
if (!checked) {
|
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-09 23:58:27 +02:00
|
|
|
if (!hasUpdate || dismissed) {
|
2020-09-30 01:25:44 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-26 13:03:13 +02:00
|
|
|
return (
|
2023-03-02 08:59:01 +01:00
|
|
|
<Row className={styles.notice}>
|
|
|
|
<Column variant="two" className={styles.message}>
|
|
|
|
{formatMessage(messages.newVersionAvailable, { version: `v${latest}` })}
|
|
|
|
</Column>
|
|
|
|
<Column className={styles.buttons}>
|
|
|
|
<Button variant="primary" onClick={handleViewClick}>
|
|
|
|
{formatMessage(labels.viewDetails)}
|
2020-09-30 01:25:44 +02:00
|
|
|
</Button>
|
2023-03-02 08:59:01 +01:00
|
|
|
<Button onClick={handleDismissClick}>{formatMessage(labels.dismiss)}</Button>
|
|
|
|
</Column>
|
|
|
|
</Row>
|
2020-09-26 13:03:13 +02:00
|
|
|
);
|
|
|
|
}
|