fix(announcements): remember dismissed announcement (#4255)
This commit is contained in:
parent
fc6ce9e99b
commit
b381f16ace
@ -1,6 +1,9 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import type { AppNameType, Announcement } from './schema';
|
import type { AppNameType, Announcement } from './schema';
|
||||||
import { useAnnouncement } from './hooks/use-announcement';
|
import {
|
||||||
|
useAnnouncement,
|
||||||
|
useDismissedAnnouncement,
|
||||||
|
} from './hooks/use-announcement';
|
||||||
import {
|
import {
|
||||||
AnnouncementBanner as Banner,
|
AnnouncementBanner as Banner,
|
||||||
ExternalLink,
|
ExternalLink,
|
||||||
@ -36,6 +39,7 @@ export const AnnouncementBanner = ({
|
|||||||
}: AnnouncementBannerProps) => {
|
}: AnnouncementBannerProps) => {
|
||||||
const [isVisible, setVisible] = useState(false);
|
const [isVisible, setVisible] = useState(false);
|
||||||
const { data, reload } = useAnnouncement(app, configUrl);
|
const { data, reload } = useAnnouncement(app, configUrl);
|
||||||
|
const [, setDismissed] = useDismissedAnnouncement();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
@ -88,7 +92,10 @@ export const AnnouncementBanner = ({
|
|||||||
<button
|
<button
|
||||||
className="absolute right-0 top-0 p-4 w-10 h-full flex items-center justify-center text-white"
|
className="absolute right-0 top-0 p-4 w-10 h-full flex items-center justify-center text-white"
|
||||||
data-testid="app-announcement-close"
|
data-testid="app-announcement-close"
|
||||||
onClick={() => setVisible(false)}
|
onClick={() => {
|
||||||
|
setVisible(false);
|
||||||
|
setDismissed(data);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<VegaIcon name={VegaIconNames.CROSS} size={24} />
|
<VegaIcon name={VegaIconNames.CROSS} size={24} />
|
||||||
</button>
|
</button>
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import type { AppNameType, Announcement } from '../schema';
|
import type { AppNameType, Announcement } from '../schema';
|
||||||
import { AnnouncementsSchema } from '../schema';
|
import { AnnouncementsSchema } from '../schema';
|
||||||
|
import { sha256 } from 'ethers/lib/utils';
|
||||||
|
import { useLocalStorageSnapshot } from '@vegaprotocol/react-helpers';
|
||||||
|
|
||||||
const getData = async (name: AppNameType, url: string) => {
|
const getData = async (name: AppNameType, url: string) => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
@ -31,6 +33,22 @@ type State = {
|
|||||||
error: null | string;
|
error: null | string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const checksum = (data: object) => sha256(Buffer.from(JSON.stringify(data)));
|
||||||
|
|
||||||
|
export const useDismissedAnnouncement = (): [
|
||||||
|
string | null | undefined,
|
||||||
|
(data: object) => void
|
||||||
|
] => {
|
||||||
|
const [dismissed, setDismissedInStorage] = useLocalStorageSnapshot(
|
||||||
|
'dismissed-announcement'
|
||||||
|
);
|
||||||
|
const setDismissed = useCallback(
|
||||||
|
(data: object) => setDismissedInStorage(checksum(data)),
|
||||||
|
[setDismissedInStorage]
|
||||||
|
);
|
||||||
|
return [dismissed, setDismissed];
|
||||||
|
};
|
||||||
|
|
||||||
export const useAnnouncement = (name: AppNameType, url: string) => {
|
export const useAnnouncement = (name: AppNameType, url: string) => {
|
||||||
const [state, setState] = useState<State>({
|
const [state, setState] = useState<State>({
|
||||||
loading: true,
|
loading: true,
|
||||||
@ -38,12 +56,14 @@ export const useAnnouncement = (name: AppNameType, url: string) => {
|
|||||||
error: null,
|
error: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const [dismissed] = useDismissedAnnouncement();
|
||||||
|
|
||||||
const fetchData = useCallback(() => {
|
const fetchData = useCallback(() => {
|
||||||
let mounted = true;
|
let mounted = true;
|
||||||
|
|
||||||
getData(name, url)
|
getData(name, url)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
if (mounted) {
|
if (mounted && dismissed !== checksum(data)) {
|
||||||
setState({
|
setState({
|
||||||
loading: false,
|
loading: false,
|
||||||
data,
|
data,
|
||||||
@ -64,7 +84,7 @@ export const useAnnouncement = (name: AppNameType, url: string) => {
|
|||||||
return () => {
|
return () => {
|
||||||
mounted = false;
|
mounted = false;
|
||||||
};
|
};
|
||||||
}, [name, url, setState]);
|
}, [name, url, dismissed]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchData();
|
fetchData();
|
||||||
|
Loading…
Reference in New Issue
Block a user