Просмотр исходного кода

避免不同页面重复查询notification

visuddhinanda 2 лет назад
Родитель
Сommit
c64eb3c042
1 измененных файлов с 23 добавлено и 0 удалено
  1. 23 0
      dashboard/src/components/notification/NotificationIcon.tsx

+ 23 - 0
dashboard/src/components/notification/NotificationIcon.tsx

@@ -9,10 +9,33 @@ const NotificationIconWidget = () => {
   const [count, setCount] = useState<number>();
   const [count, setCount] = useState<number>();
   useEffect(() => {
   useEffect(() => {
     let timer = setInterval(() => {
     let timer = setInterval(() => {
+      const now = new Date();
+      const notificationUpdatedAt = localStorage.getItem(
+        "notification/updatedAt"
+      );
+      if (notificationUpdatedAt) {
+        if (now.getTime() - parseInt(notificationUpdatedAt) < 59000) {
+          const notificationCount = localStorage.getItem("notification/count");
+          if (notificationCount !== null) {
+            setCount(parseInt(notificationCount));
+            console.debug("has notification count");
+            return;
+          }
+        }
+      }
+
       const url = `/v2/notification?view=to&status=unread&limit=1`;
       const url = `/v2/notification?view=to&status=unread&limit=1`;
       console.info("url", url);
       console.info("url", url);
       get<INotificationListResponse>(url).then((json) => {
       get<INotificationListResponse>(url).then((json) => {
         if (json.ok) {
         if (json.ok) {
+          localStorage.setItem(
+            "notification/updatedAt",
+            now.getTime().toString()
+          );
+          localStorage.setItem(
+            "notification/count",
+            json.data.count.toString()
+          );
           setCount(json.data.count);
           setCount(json.data.count);
           if (json.data.count > 0) {
           if (json.data.count > 0) {
             const newMessageTime = json.data.rows[0].created_at;
             const newMessageTime = json.data.rows[0].created_at;