|
| 1 | +import React, { useCallback, useEffect, useMemo, useState } from 'react'; |
| 2 | +import { |
| 3 | + ChromeWsEventTypes, |
| 4 | + ChromeWsPayload, |
| 5 | +} from '@redhat-cloud-services/types'; |
| 6 | +import useChrome from '@redhat-cloud-services/frontend-components/useChrome'; |
| 7 | +import BulkSelect from '@redhat-cloud-services/frontend-components/BulkSelect'; |
| 8 | + |
| 9 | +import { |
| 10 | + NotificationDrawerBody, |
| 11 | + NotificationDrawerHeader, |
| 12 | + NotificationDrawerList, |
| 13 | +} from '@patternfly/react-core/dist/dynamic/components/NotificationDrawer'; |
| 14 | +import { Badge } from '@patternfly/react-core/dist/dynamic/components/Badge'; |
| 15 | + |
| 16 | +import orderBy from 'lodash/orderBy'; |
| 17 | +import { useNavigate } from 'react-router-dom'; |
| 18 | +import NotificationItem from './NotificationItem'; |
| 19 | +import { EmptyNotifications } from './EmptyNotifications'; |
| 20 | +import { NotificationData } from '../../types/Drawer'; |
| 21 | +import useNotificationDrawer from '../../hooks/useNotificationDrawer'; |
| 22 | +import { ActionDropdown, FilterDropdown } from './Dropdowns'; |
| 23 | + |
| 24 | +export type DrawerPanelProps = { |
| 25 | + panelRef: React.Ref<unknown>; |
| 26 | + isOrgAdmin: boolean; |
| 27 | + toggleDrawer: () => void; |
| 28 | +}; |
| 29 | + |
| 30 | +const DrawerPanelBase = ({ isOrgAdmin, toggleDrawer }: DrawerPanelProps) => { |
| 31 | + const { addWsEventListener } = useChrome(); |
| 32 | + |
| 33 | + const [isDropdownOpen, setIsDropdownOpen] = useState(false); |
| 34 | + const [isFilterDropdownOpen, setIsFilterDropdownOpen] = useState(false); |
| 35 | + const { |
| 36 | + state, |
| 37 | + addNotification, |
| 38 | + updateNotificationRead, |
| 39 | + updateSelectedStatus, |
| 40 | + updateNotificationsSelected, |
| 41 | + updateNotificationSelected, |
| 42 | + setFilters, |
| 43 | + } = useNotificationDrawer(); |
| 44 | + const navigate = useNavigate(); |
| 45 | + |
| 46 | + const eventType: ChromeWsEventTypes = |
| 47 | + 'com.redhat.console.notifications.drawer'; |
| 48 | + |
| 49 | + const handleWsEvent = useCallback( |
| 50 | + (event: ChromeWsPayload<NotificationData>) => { |
| 51 | + addNotification(event.data as NotificationData); |
| 52 | + }, |
| 53 | + [addNotification] |
| 54 | + ); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + const unregister = addWsEventListener(eventType, handleWsEvent); |
| 58 | + return () => { |
| 59 | + unregister(); |
| 60 | + }; |
| 61 | + }, [addWsEventListener, handleWsEvent]); |
| 62 | + |
| 63 | + const filteredNotifications = useMemo(() => { |
| 64 | + const notificationsByBundle = state.notificationData.reduce( |
| 65 | + (acc, notification) => { |
| 66 | + if (!acc[notification.bundle]) { |
| 67 | + acc[notification.bundle] = []; |
| 68 | + } |
| 69 | + acc[notification.bundle].push(notification); |
| 70 | + return acc; |
| 71 | + }, |
| 72 | + {} |
| 73 | + ); |
| 74 | + |
| 75 | + return (state.filters || []).reduce((acc, chosenFilter) => { |
| 76 | + if (notificationsByBundle[chosenFilter]) { |
| 77 | + acc.push(...notificationsByBundle[chosenFilter]); |
| 78 | + } |
| 79 | + return acc; |
| 80 | + }, [] as NotificationData[]); |
| 81 | + }, [state.filters, state.notificationData]); |
| 82 | + |
| 83 | + const onNotificationsDrawerClose = () => { |
| 84 | + setFilters([]); |
| 85 | + toggleDrawer(); |
| 86 | + }; |
| 87 | + |
| 88 | + const onUpdateSelectedStatus = (read: boolean) => { |
| 89 | + updateSelectedStatus(read); |
| 90 | + setIsDropdownOpen(false); |
| 91 | + }; |
| 92 | + |
| 93 | + const selectAllNotifications = (selected: boolean) => { |
| 94 | + updateNotificationsSelected(selected); |
| 95 | + }; |
| 96 | + |
| 97 | + const selectVisibleNotifications = () => { |
| 98 | + const visibleNotifications = |
| 99 | + state.filters.length > 0 ? filteredNotifications : state.notificationData; |
| 100 | + visibleNotifications.forEach((notification) => |
| 101 | + updateNotificationSelected(notification.id, true) |
| 102 | + ); |
| 103 | + }; |
| 104 | + |
| 105 | + const onFilterSelect = (chosenFilter: string) => { |
| 106 | + state.filters.includes(chosenFilter) |
| 107 | + ? setFilters(state.filters.filter((filter) => filter !== chosenFilter)) |
| 108 | + : setFilters([...state.filters, chosenFilter]); |
| 109 | + }; |
| 110 | + |
| 111 | + const onNavigateTo = (link: string) => { |
| 112 | + navigate(link); |
| 113 | + onNotificationsDrawerClose(); |
| 114 | + }; |
| 115 | + |
| 116 | + const toggleDropdown = () => setIsDropdownOpen(!isDropdownOpen); |
| 117 | + |
| 118 | + const RenderNotifications = () => { |
| 119 | + if (state.notificationData.length === 0) { |
| 120 | + return ( |
| 121 | + <EmptyNotifications |
| 122 | + isOrgAdmin={isOrgAdmin} |
| 123 | + onLinkClick={onNotificationsDrawerClose} |
| 124 | + /> |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + const sortedNotifications = orderBy( |
| 129 | + state.filters?.length > 0 |
| 130 | + ? filteredNotifications |
| 131 | + : state.notificationData, |
| 132 | + ['read', 'created'], |
| 133 | + ['asc', 'asc'] |
| 134 | + ); |
| 135 | + |
| 136 | + return sortedNotifications.map((notification) => ( |
| 137 | + <NotificationItem |
| 138 | + key={notification.id} |
| 139 | + notification={notification} |
| 140 | + onNavigateTo={onNavigateTo} |
| 141 | + updateNotificationSelected={updateNotificationSelected} |
| 142 | + updateNotificationRead={updateNotificationRead} |
| 143 | + /> |
| 144 | + )); |
| 145 | + }; |
| 146 | + |
| 147 | + return ( |
| 148 | + <> |
| 149 | + <NotificationDrawerHeader |
| 150 | + onClose={onNotificationsDrawerClose} |
| 151 | + title="Notifications" |
| 152 | + className="pf-u-align-items-center" |
| 153 | + > |
| 154 | + {state.filters.length > 0 && ( |
| 155 | + <Badge isRead>{state.filters.length}</Badge> |
| 156 | + )} |
| 157 | + <FilterDropdown |
| 158 | + filterConfig={state.filterConfig} |
| 159 | + isDisabled={state.notificationData.length === 0} |
| 160 | + activeFilters={state.filters} |
| 161 | + setActiveFilters={setFilters} |
| 162 | + onFilterSelect={onFilterSelect} |
| 163 | + isFilterDropdownOpen={isFilterDropdownOpen} |
| 164 | + setIsFilterDropdownOpen={setIsFilterDropdownOpen} |
| 165 | + /> |
| 166 | + <BulkSelect |
| 167 | + id="notifications-bulk-select" |
| 168 | + items={[ |
| 169 | + { |
| 170 | + title: 'Select none (0)', |
| 171 | + key: 'select-none', |
| 172 | + onClick: () => selectAllNotifications(false), |
| 173 | + }, |
| 174 | + { |
| 175 | + title: `Select visible (${ |
| 176 | + state.filters.length > 0 |
| 177 | + ? filteredNotifications.length |
| 178 | + : state.notificationData.length |
| 179 | + })`, |
| 180 | + key: 'select-visible', |
| 181 | + onClick: selectVisibleNotifications, |
| 182 | + }, |
| 183 | + { |
| 184 | + title: `Select all (${state.notificationData.length})`, |
| 185 | + key: 'select-all', |
| 186 | + onClick: () => selectAllNotifications(true), |
| 187 | + }, |
| 188 | + ]} |
| 189 | + count={ |
| 190 | + state.notificationData.filter(({ selected }) => selected).length |
| 191 | + } |
| 192 | + checked={ |
| 193 | + state.notificationData.length > 0 && |
| 194 | + state.notificationData.every(({ selected }) => selected) |
| 195 | + } |
| 196 | + /> |
| 197 | + <ActionDropdown |
| 198 | + isDropdownOpen={isDropdownOpen} |
| 199 | + setIsDropdownOpen={toggleDropdown} |
| 200 | + isDisabled={state.notificationData.length === 0} |
| 201 | + onUpdateSelectedStatus={onUpdateSelectedStatus} |
| 202 | + onNavigateTo={onNavigateTo} |
| 203 | + isOrgAdmin={isOrgAdmin} |
| 204 | + hasNotificationsPermissions={state.hasNotificationsPermissions} |
| 205 | + /> |
| 206 | + </NotificationDrawerHeader> |
| 207 | + <NotificationDrawerBody> |
| 208 | + <NotificationDrawerList> |
| 209 | + <RenderNotifications /> |
| 210 | + </NotificationDrawerList> |
| 211 | + </NotificationDrawerBody> |
| 212 | + </> |
| 213 | + ); |
| 214 | +}; |
| 215 | + |
| 216 | +const DrawerPanel = React.forwardRef( |
| 217 | + (props: DrawerPanelProps, panelRef: React.Ref<unknown>) => ( |
| 218 | + <DrawerPanelBase |
| 219 | + isOrgAdmin={props.isOrgAdmin} |
| 220 | + panelRef={panelRef} |
| 221 | + toggleDrawer={props.toggleDrawer} |
| 222 | + /> |
| 223 | + ) |
| 224 | +); |
| 225 | +DrawerPanel.displayName = 'DrawerPanel'; |
| 226 | + |
| 227 | +export default DrawerPanel; |
0 commit comments