@orderly.network/ui-notification 2.8.10 → 2.8.11-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,12 +1,11 @@
1
1
  import { useMemo, useState, useEffect, useCallback } from 'react';
2
2
  import { useTranslation } from '@orderly.network/i18n';
3
3
  import { Flex, Text, cn, ChevronDownIcon, ScrollArea, Divider, Icon, ExtensionSlot, ExtensionPositionEnum, ChevronLeftIcon, ChevronRightIcon } from '@orderly.network/ui';
4
- import { AnnouncementType, EMPTY_LIST } from '@orderly.network/types';
4
+ import { AnnouncementType } from '@orderly.network/types';
5
5
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
6
- import { UTCDate, UTCDateMini } from '@date-fns/utc';
7
- import { differenceInHours, differenceInMinutes, format } from 'date-fns';
8
- import { produce } from 'immer';
9
- import { useOrderlyContext, useLocalStorage, useWS, useMaintenanceStatus, useQuery, MaintenanceStatus } from '@orderly.network/hooks';
6
+ import { UTCDateMini } from '@date-fns/utc';
7
+ import { format } from 'date-fns';
8
+ import { useWS, useOrderlyContext, useLocalStorage, useMaintenanceStatus, useQuery, MaintenanceStatus } from '@orderly.network/hooks';
10
9
  import { useAppContext } from '@orderly.network/react-app';
11
10
 
12
11
  // src/components/announcementCenter/announcementCenter.ui.tsx
@@ -327,29 +326,8 @@ var CampaignContentCard = ({ message, coverImage, url, onItemClick }) => {
327
326
  };
328
327
  var MaintenanceContentCard = ({ message, startTime, endTime }) => {
329
328
  const { t } = useTranslation();
330
- const formattedMessage = useMemo(() => {
331
- const hours = differenceInHours(endTime, startTime);
332
- const minutes = differenceInMinutes(endTime, startTime) - hours * 60;
333
- const startUtc = new UTCDate(startTime);
334
- const endUtc = new UTCDate(endTime);
335
- const startTimeFormatted = format(startUtc, "HH:mm");
336
- const endTimeFormatted = format(endUtc, "hh:mm a");
337
- if (hours > 0) {
338
- return t("notification.maintenanceDuration.hours", {
339
- hours: minutes > 0 ? (hours + minutes / 60).toFixed(1) : hours,
340
- startTimeFormatted,
341
- endTimeFormatted
342
- });
343
- }
344
- return t("notification.maintenanceDuration.minutes", {
345
- minutes,
346
- startTimeFormatted,
347
- endTimeFormatted
348
- });
349
- }, [startTime, endTime, t]);
350
329
  return /* @__PURE__ */ jsxs("div", { className: "oui-flex oui-flex-col oui-gap-1", children: [
351
330
  /* @__PURE__ */ jsx(Text, { size: "xs", intensity: 54, children: t("notification.recentlyUpdated") }),
352
- /* @__PURE__ */ jsx(Flex, { itemAlign: "center", children: /* @__PURE__ */ jsx(Text, { size: "xs", weight: "bold", children: formattedMessage }) }),
353
331
  message && /* @__PURE__ */ jsx(Text, { size: "2xs", intensity: 80, as: "div", className: "oui-mt-2", children: message })
354
332
  ] });
355
333
  };
@@ -592,28 +570,34 @@ var getTimeString = (timestamp) => {
592
570
  const time = format(new UTCDateMini(timestamp), "h:mm aa");
593
571
  return `${time} (UTC) on ${date}`;
594
572
  };
595
- var sortDataByUpdatedTime = (ori) => {
596
- return produce(ori, (draft) => {
597
- if (Array.isArray(draft.rows)) {
598
- draft.rows.sort((a, b) => {
599
- if (a.updated_time && b.updated_time) {
600
- return b.updated_time - a.updated_time;
601
- }
602
- return 0;
603
- });
573
+ var sortDataByUpdatedTime = (list) => {
574
+ return list.sort((a, b) => {
575
+ if (a.updated_time && b.updated_time) {
576
+ return b.updated_time - a.updated_time;
604
577
  }
578
+ return 0;
605
579
  });
606
580
  };
581
+ var filterDuplicateArrayById = (list) => {
582
+ const seenIds = /* @__PURE__ */ new Set();
583
+ const newList = [];
584
+ list.forEach((item) => {
585
+ if (!seenIds.has(item.announcement_id)) {
586
+ seenIds.add(item.announcement_id);
587
+ newList.push(item);
588
+ }
589
+ });
590
+ return newList;
591
+ };
607
592
  var useAnnouncementData = () => {
593
+ const { t } = useTranslation();
608
594
  const ws = useWS();
609
- const [announcementStore, setStore] = useLocalStorage(
610
- ORDERLY_ANNOUNCEMENT_KEY,
611
- {}
612
- );
613
- const [tips, setTips] = useState({});
595
+ const { dataAdapter } = useOrderlyContext();
596
+ const [announcementStore, setAnnouncementStore] = useLocalStorage(ORDERLY_ANNOUNCEMENT_KEY, {});
597
+ const [tips, setTips] = useState([]);
598
+ const [maintenances, setMaintenances] = useState([]);
614
599
  const [maintenanceDialogInfo, setMaintenanceDialogInfo] = useState();
615
600
  const { startTime, endTime, status, brokerName } = useMaintenanceStatus();
616
- const { t } = useTranslation();
617
601
  const { data: announcements } = useQuery(
618
602
  `/v2/public/announcement`,
619
603
  {
@@ -625,162 +609,100 @@ var useAnnouncementData = () => {
625
609
  );
626
610
  const getMaintentTipsContent = (brokerName2, startDate, endDate) => t("maintenance.tips.description", { brokerName: brokerName2, startDate, endDate });
627
611
  const getMaintentDialogContent = (brokerName2, endDate) => t("maintenance.dialog.description", { brokerName: brokerName2, endDate });
612
+ useEffect(() => {
613
+ const startDate = startTime ? getTimeString(startTime) : "-";
614
+ const endDate = endTime ? getTimeString(endTime) : "-";
615
+ const dialogContent = status === MaintenanceStatus.Maintenance ? getMaintentDialogContent(brokerName, endDate) : void 0;
616
+ setMaintenanceDialogInfo(dialogContent);
617
+ if (startTime && endTime) {
618
+ setMaintenances([
619
+ {
620
+ announcement_id: maintentanceId,
621
+ type: AnnouncementType.Maintenance,
622
+ startTime,
623
+ endTime,
624
+ updated_time: startTime,
625
+ message: getMaintentTipsContent(brokerName, startDate, endDate)
626
+ }
627
+ ]);
628
+ } else {
629
+ setMaintenances([]);
630
+ }
631
+ }, [startTime, endTime, status, brokerName, t]);
632
+ useEffect(() => {
633
+ let list = [...maintenances ?? [], ...announcements?.rows ?? []];
634
+ if (typeof dataAdapter?.announcementList === "function") {
635
+ list = dataAdapter.announcementList(list);
636
+ }
637
+ const removedDuplicateList = filterDuplicateArrayById(list);
638
+ const sortedList = sortDataByUpdatedTime(removedDuplicateList);
639
+ setTips(sortedList);
640
+ }, [maintenances, announcements, dataAdapter?.announcementList]);
641
+ useEffect(() => {
642
+ if (!announcements?.rows) {
643
+ return;
644
+ }
645
+ const lastUpdateTime = announcements.last_updated_time ?? 0;
646
+ const firstTipTime = tips[0]?.updated_time ?? 0;
647
+ const updatedTime = Math.max(lastUpdateTime, firstTipTime);
648
+ const closedTime = announcementStore.lastUpdateTime ?? 0;
649
+ if (closedTime < updatedTime) {
650
+ setAnnouncementStore({ show: true, lastUpdateTime: updatedTime });
651
+ }
652
+ }, [announcements, tips]);
628
653
  useEffect(() => {
629
654
  const unsubscribe = ws.subscribe("announcement", {
630
655
  onMessage(message) {
631
656
  if (message) {
632
657
  setTips((prev) => {
633
- return produce(prev, (draft) => {
634
- if (!Array.isArray(draft.rows)) {
635
- draft.rows = [];
636
- }
637
- const idx = draft.rows.findIndex(
638
- (tip) => tip.announcement_id === message.announcement_id
639
- );
640
- if (idx !== -1) {
641
- draft.rows.splice(idx, 1);
642
- }
643
- draft.rows.push({
644
- announcement_id: message.announcement_id,
645
- message: message.message,
646
- url: message.url,
647
- i18n: message.i18n,
648
- type: message.type,
649
- updated_time: message.updated_time
650
- });
651
- });
658
+ const list = prev.filter(
659
+ (item) => item.announcement_id !== message.announcement_id
660
+ );
661
+ const newTip = {
662
+ announcement_id: message.announcement_id,
663
+ message: message.message,
664
+ url: message.url,
665
+ i18n: message.i18n,
666
+ type: message.type,
667
+ updated_time: message.updated_time
668
+ };
669
+ return [...list, newTip];
652
670
  });
653
- setStore((prev) => ({ ...prev, show: true }));
671
+ setAnnouncementStore((prev) => ({ ...prev, show: true }));
654
672
  }
655
- },
656
- onError(err) {
657
673
  }
658
674
  });
659
675
  return () => {
660
676
  unsubscribe?.();
661
677
  };
662
678
  }, [ws]);
663
- useEffect(() => {
664
- if (!announcements?.rows) {
665
- return;
666
- }
667
- const apiTime = announcements.last_updated_time ?? 0;
668
- const cachedTime = announcementStore.lastUpdateTime ?? 0;
669
- if (cachedTime < apiTime) {
670
- setTips((prev) => ({ ...prev, rows: announcements?.rows }));
671
- setStore({ show: true, lastUpdateTime: apiTime });
672
- } else {
673
- setTips((prev) => {
674
- return produce(prev, (draft) => {
675
- if (announcements?.rows?.length) {
676
- const existingIds = new Set(
677
- prev.rows?.map((tip) => tip.announcement_id)
678
- );
679
- const maintenanceTip = prev.rows?.find(
680
- (tip) => tip.announcement_id === maintentanceId
681
- );
682
- draft.rows = [];
683
- announcements.rows.forEach((item) => {
684
- if (!existingIds.has(item.announcement_id)) {
685
- draft.rows?.push(item);
686
- }
687
- });
688
- if (maintenanceTip) {
689
- draft.rows.unshift(maintenanceTip);
690
- }
691
- } else {
692
- const idx = draft.rows?.findIndex(
693
- (tip) => tip.announcement_id === maintentanceId
694
- );
695
- if (idx !== void 0 && idx !== -1) {
696
- draft.rows?.splice(idx, 1);
697
- }
698
- }
699
- });
700
- });
701
- }
702
- }, [announcements]);
703
- useEffect(() => {
704
- const startDate = startTime ? getTimeString(startTime) : "-";
705
- const endDate = endTime ? getTimeString(endTime) : "-";
706
- if (status === MaintenanceStatus.Maintenance) {
707
- setMaintenanceDialogInfo(getMaintentDialogContent(brokerName, endDate));
708
- return;
709
- }
710
- setMaintenanceDialogInfo(void 0);
711
- if (startTime && endTime) {
712
- setTips(
713
- (prev) => produce(prev, (draft) => {
714
- if (!Array.isArray(draft.rows)) {
715
- draft.rows = [];
716
- }
717
- draft.rows = [
718
- {
719
- announcement_id: maintentanceId,
720
- type: AnnouncementType.Maintenance,
721
- /** @ts-ignore */
722
- startTime,
723
- /** @ts-ignore */
724
- endTime,
725
- message: getMaintentTipsContent(brokerName, startDate, endDate)
726
- },
727
- ...draft.rows.filter(
728
- (tip) => tip.type !== AnnouncementType.Maintenance
729
- )
730
- ];
731
- })
732
- );
733
- } else {
734
- setTips((prev) => {
735
- return produce(prev, (draft) => {
736
- const index = draft.rows?.findIndex(
737
- (tip) => tip.announcement_id === maintentanceId
738
- );
739
- if (index !== void 0 && index !== -1) {
740
- draft.rows?.splice(index, 1);
741
- }
742
- });
743
- });
744
- }
745
- }, [startTime, endTime, status, brokerName, t]);
746
679
  return {
747
- tips: sortDataByUpdatedTime(tips),
748
- maintenanceDialogInfo
680
+ tips,
681
+ maintenanceDialogInfo,
682
+ announcementStore,
683
+ setAnnouncementStore
749
684
  };
750
685
  };
751
686
  var useAnnouncement = (options) => {
752
687
  const { showAnnouncement, setShowAnnouncement } = useAppContext();
753
- const { dataAdapter } = useOrderlyContext();
754
- const { tips, maintenanceDialogInfo } = useAnnouncementData();
755
- const memoizedTips = useMemo(() => {
756
- if (typeof dataAdapter?.announcementList === "function") {
757
- return dataAdapter.announcementList(
758
- tips?.rows ?? EMPTY_LIST
759
- );
760
- }
761
- return tips?.rows ?? EMPTY_LIST;
762
- }, [dataAdapter?.announcementList, tips?.rows]);
763
- const [announcementStore, setStore] = useLocalStorage(
764
- ORDERLY_ANNOUNCEMENT_KEY,
765
- {}
766
- );
688
+ const {
689
+ tips,
690
+ maintenanceDialogInfo,
691
+ announcementStore,
692
+ setAnnouncementStore
693
+ } = useAnnouncementData();
767
694
  const closeTips = () => {
768
- setStore((prev) => ({ ...prev, show: false }));
695
+ setAnnouncementStore((prev) => ({ ...prev, show: false }));
769
696
  };
770
697
  useEffect(() => {
771
- const len = memoizedTips.length;
698
+ const len = tips.length;
772
699
  setShowAnnouncement(
773
700
  Boolean(len) && announcementStore.show && !options?.hideTips
774
701
  );
775
- }, [
776
- memoizedTips,
777
- announcementStore.show,
778
- options?.hideTips,
779
- setShowAnnouncement
780
- ]);
702
+ }, [tips, announcementStore.show, options?.hideTips, setShowAnnouncement]);
781
703
  return {
782
704
  maintenanceDialogInfo,
783
- tips: memoizedTips,
705
+ tips,
784
706
  closeTips,
785
707
  showAnnouncement
786
708
  };
@@ -813,8 +735,7 @@ var AnnouncementCenterWidget = (props) => {
813
735
  const { dataSource, current, setCurrent } = useAnnouncementCenterScript();
814
736
  const onItemClick = useCallback(
815
737
  (url) => {
816
- if (!url)
817
- return;
738
+ if (!url) return;
818
739
  props.onRouteChange(url);
819
740
  },
820
741
  [props.onRouteChange]
@@ -834,8 +755,7 @@ var AnnouncementCenterPage = (props) => {
834
755
  AnnouncementCenterWidget,
835
756
  {
836
757
  onRouteChange: (url) => {
837
- if (!url)
838
- return;
758
+ if (!url) return;
839
759
  props.routerAdapter?.onRouteChange({
840
760
  href: url,
841
761
  name: url,
@@ -847,5 +767,5 @@ var AnnouncementCenterPage = (props) => {
847
767
  };
848
768
 
849
769
  export { AnnouncementCenterPage, AnnouncementCenterUI, AnnouncementItem, NotificationUI, useAnnouncement };
850
- //# sourceMappingURL=out.js.map
770
+ //# sourceMappingURL=index.mjs.map
851
771
  //# sourceMappingURL=index.mjs.map