Skip to content

Commit 5d5f585

Browse files
authored
refactor: Code refactor and optimisation
1 parent eba465f commit 5d5f585

19 files changed

+279
-174
lines changed

src/components/BellIcon.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { FC } from 'react';
22
import React from 'react';
33

4-
interface IconProps {
5-
color?: string;
6-
size?: number
7-
}
4+
import type { IconProps } from '../types';
5+
86

97
const BellIcon: FC<IconProps> = ({ color = '#232326', size = 30 }: { color?: string, size?: number}) => {
108
return (

src/components/Card.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { CSSProperties } from "react";
2-
import React, { type FC, useState } from "react";
1+
import type { CSSProperties , FC} from "react";
2+
import React, { useState } from "react";
33

44
import CloseIcon from "./CloseIcon";
55
import { useSirenContext } from "./SirenProvider";
@@ -32,17 +32,19 @@ import useSiren from "../utils/sirenHook";
3232
* <Card
3333
* notification={notification}
3434
* cardProps={{ hideAvatar: false }}
35+
* darkMode={false}
3536
* styles={customStyles}
3637
* onCardClick={(notification) => console.log('Notification clicked', notification)}
37-
* deleteById={(id) => console.log('Notification deleted', id)}
38+
* deleteNotificationById={(id) => console.log('Notification deleted', id)}
3839
* />
3940
*
4041
* @param {NotificationCardProps} props - The properties passed to the Card component.
4142
* @param {Object} props.notification - The notification data to display in the card.
4243
* @param {Object} [props.cardProps] - Optional properties to customize the appearance of the card.
4344
* @param {Object} props.styles - Custom styles applied to the card and its elements.
45+
* @param {boolean} props.darkMode - Flag to determine the dark mode status.
4446
* @param {Function} [props.onCardClick] - Callback function executed when the card is clicked.
45-
* @param {Function} [props.deleteById] - Callback function executed when the delete action is triggered.
47+
* @param {Function} [props.deleteNotificationById] - Callback function executed when the delete action is triggered.
4648
* @returns {ReactElement} The rendered Card component.
4749
*/
4850

@@ -63,6 +65,8 @@ const Card: FC<NotificationCardProps> = ({
6365
const { id } = useSirenContext();
6466

6567
const [deleteAnimationStyle, setDeleteAnimationStyle] = useState('');
68+
const [imageLoaded, setImageLoaded] = useState(true);
69+
const [imageSource, setImageSource] = useState(thumbnailUrl ?? '');
6670

6771
const defaultAvatar = darkMode ? defaultAvatarDark : defaultAvatarLight;
6872
const failedImage = darkMode ? failedImageDark: failedImageLight;
@@ -113,10 +117,6 @@ const Card: FC<NotificationCardProps> = ({
113117
event.stopPropagation();
114118
};
115119

116-
const [imageLoaded, setImageLoaded] = useState(true); // Initially assume image is loaded
117-
118-
const [imageSource, setImageSource] = useState(thumbnailUrl ? thumbnailUrl : '');
119-
120120
const onErrorMedia = (): void => {
121121
setImageLoaded(false);
122122
setImageSource(failedImage);
@@ -133,6 +133,7 @@ const Card: FC<NotificationCardProps> = ({
133133
onClick={handleNotificationCardClick}
134134
aria-label={`siren-notification-card-${notification.id}`}
135135
data-testid={`card-${notification.id}`}
136+
role="button"
136137
>
137138
{!hideAvatar && (
138139
<div
@@ -145,6 +146,7 @@ const Card: FC<NotificationCardProps> = ({
145146
}}
146147
aria-label={`siren-notification-avatar-${notification.id}`}
147148
onClick={handleAvatarClick}
149+
role="button"
148150
/>
149151
)}
150152
<div className="siren-sdk-card-content-wrapper">
@@ -168,7 +170,9 @@ const Card: FC<NotificationCardProps> = ({
168170
className="siren-sdk-card-thumbnail-container"
169171
style={{...(onMediaThumbnailClick && { cursor: "pointer" }),
170172
backgroundColor: darkMode ? '#4C4C4C' : '#F0F2F5'}}
171-
onClick={handleMediaClick}>
173+
onClick={handleMediaClick}
174+
role="button"
175+
>
172176
<img
173177
className={`siren-sdk-card-thumbnail-image ${thumbnailUrl && imageLoaded ? 'siren-sdk-card-thumbnail-with-image' : ''}`}
174178
src={imageSource}
@@ -192,6 +196,7 @@ const Card: FC<NotificationCardProps> = ({
192196
className="siren-sdk-delete-button"
193197
onClick={onDelete}
194198
aria-label={`siren-notification-delete-${notification.id}`}
199+
role="button"
195200
>
196201
<CloseIcon
197202
color={styles?.deleteIcon.color}

src/components/ClearAllIcon.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { FC } from 'react';
22
import React from 'react';
33

4-
interface IconProps {
5-
color?: string;
6-
size?: number;
7-
}
4+
import type { IconProps } from '../types';
5+
86

97
const ClearAllIcon: FC<IconProps> = ({ color = '#667185', size = 24 }) => {
108
return (

src/components/CloseIcon.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { FC } from 'react';
22
import React from 'react';
33

4-
interface IconProps {
5-
color?: string;
6-
size?: number;
7-
}
4+
import type { IconProps } from '../types';
5+
86

97
const CloseIcon: FC<IconProps> = ({ color = '#98A2B3', size= 20 }) => {
108
return (

src/components/EmptyList.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import type { FC } from "react";
12
import React from "react";
23

34
import darkModeIcon from "../assets/dark/emptyIconDark.svg";
45
import lightModeIcon from "../assets/light/emptyIconLight.svg";
5-
import type { SirenStyleProps } from "../types";
6+
import type { EmptyListProps } from "../types";
67
import { Constants } from "../utils";
78
import { LIST_EMPTY_SUB_TEXT, LIST_EMPTY_TEXT } from "../utils/constants";
89
import "../styles/emptyList.css";
@@ -14,14 +15,18 @@ import "../styles/emptyList.css";
1415
* @example
1516
* <EmptyList
1617
* styles={customStyles}
18+
* darkMode={false}
1719
* />
1820
*
19-
* @param {Object} props - The properties passed to the EmptyList component.
21+
* @param {EmptyListProps} props - The properties passed to the EmptyList component.
2022
* @param {Object} props.styles - The styles object to customize the appearance of the empty list.
23+
* @param {boolean} [props.darkMode] - Flag indicating if the component is in dark mode
2124
* @returns {ReactElement} The rendered EmptyList component.
2225
*/
23-
const EmptyList = (props: { styles: SirenStyleProps, darkMode: boolean }) => {
24-
const { styles, darkMode } = props;
26+
const EmptyList: FC<EmptyListProps> = ({
27+
styles,
28+
darkMode
29+
}) => {
2530

2631
const containerStyle = { backgroundColor: darkMode ? Constants.COLORS.dark.iconColor : Constants.COLORS.light.iconColor };
2732

src/components/ErrorWindow.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import type { FC } from "react";
12
import React from "react";
23

34
import errorIcon from "../assets/errorIcon.svg";
4-
import type { SirenStyleProps } from "../types";
5+
import type { ErrorWindowProps } from "../types";
56
import { Constants } from "../utils";
67
import { ERROR_SUB_TEXT, ERROR_TEXT } from "../utils/constants";
78
import "../styles/errorWindow.css";
@@ -13,20 +14,22 @@ import "../styles/errorWindow.css";
1314
* @example
1415
* <ErrorWindow
1516
* styles={customStyles}
17+
* error="An error occurred while fetching data."
18+
* darkMode={false}
1619
* onRefresh={() => console.log('Refresh button clicked')}
1720
* />
1821
*
19-
* @param {Object} props - The properties passed to the ErrorWindow component.
22+
* @param {ErrorWindowProps} props - The properties passed to the ErrorWindow component.
2023
* @param {Object} props.styles - The styles object to customize the appearance of the error window.
24+
* @param {Object} props.error - The error message to display in the window.
2125
* @param {boolean} props.darkMode - Flag for whether the selected theme is dark mode
2226
* @returns {ReactElement} The rendered ErrorWindow component.
2327
*/
24-
const ErrorWindow = (props: {
25-
styles: SirenStyleProps;
26-
error: string;
27-
darkMode: boolean;
28+
const ErrorWindow: FC<ErrorWindowProps> = ({
29+
styles,
30+
error,
31+
darkMode
2832
}) => {
29-
const { styles, error, darkMode } = props;
3033
const containerStyle = {
3134
backgroundColor: darkMode
3235
? Constants.COLORS.dark.iconColor

src/components/Header.tsx

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { type FC } from "react";
1+
import React, { type FC, useMemo } from "react";
22

33
import ClearAllIcon from "./ClearAllIcon";
44
import type { HeaderProps } from "../types";
@@ -16,14 +16,18 @@ const { CLEAR_ALL_LABEL } = Constants;
1616
* title="Notifications"
1717
* styles={customStyles}
1818
* enableClearAll={true}
19+
* hideClearAll={false}
1920
* handleClearAllNotification={() => console.log('Clear all notifications')}
21+
* fullScreen={false}
2022
* />
2123
*
2224
* @param {HeaderProps} props - The properties passed to the Header component.
2325
* @param {string} props.title - The title to be displayed in the header.
2426
* @param {Object} props.styles - The styles object to customize the appearance of the header.
2527
* @param {boolean} props.enableClearAll - Whether to enable the "Clear All" action in the header.
28+
* @param {boolean} props.hideClearAll - Flag indicating if the "Clear All" action should be hidden.
2629
* @param {Function} props.handleClearAllNotification - Callback function executed when the "Clear All" action is clicked.
30+
* @param {boolean} props.fullScreen - Flag indicating if the component is in full screen mode.
2731
* @returns {ReactElement} The rendered Header component.
2832
*/
2933
const Header: FC<HeaderProps> = ({
@@ -34,37 +38,41 @@ const Header: FC<HeaderProps> = ({
3438
handleClearAllNotification,
3539
fullScreen
3640
}) => {
37-
return (
38-
<>
39-
<div
40-
style={{...(!fullScreen && styles.windowTopBorder),...styles.headerContainer}}
41-
className="siren-sdk-header-container"
42-
data-testid="header"
43-
>
44-
<p style={styles.headerTitle} className="siren-sdk-text-break">{title}</p>
45-
{!hideClearAll && (
46-
<div
47-
className="siren-sdk-header-right-container"
48-
style={{
49-
opacity: !enableClearAll ? 0.5 : 1,
50-
cursor: enableClearAll ? "pointer" : "default",
51-
}}
52-
onClick={handleClearAllNotification}
53-
data-testid="clear-all"
54-
aria-disabled={!enableClearAll}
55-
aria-label="siren-header-clear-all"
41+
42+
const headerRightContainerStyle = useMemo(() => ({
43+
...(!enableClearAll && {
44+
opacity: 0.5,
45+
cursor: 'default'
46+
})
47+
}), [enableClearAll]);
48+
49+
return (
50+
<div
51+
style={{...(!fullScreen && styles.windowTopBorder),...styles.headerContainer}}
52+
className="siren-sdk-header-container"
53+
data-testid="header"
54+
>
55+
<p style={styles.headerTitle} className="siren-sdk-text-break">{title}</p>
56+
{!hideClearAll && (
57+
<div
58+
className="siren-sdk-header-right-container"
59+
style={headerRightContainerStyle}
60+
onClick={handleClearAllNotification}
61+
data-testid="clear-all"
62+
aria-disabled={!enableClearAll}
63+
aria-label="siren-header-clear-all"
64+
role="button"
65+
>
66+
<ClearAllIcon color={styles.clearIcon.color} size={styles.clearIcon.size}/>
67+
<p
68+
className="siren-sdk-header-clear-all-text"
69+
style={styles.headerAction}
5670
>
57-
<ClearAllIcon color={styles.clearIcon.color} size={styles.clearIcon.size}/>
58-
<p
59-
className="siren-sdk-header-clear-all-text"
60-
style={styles.headerAction}
61-
>
62-
{CLEAR_ALL_LABEL}
63-
</p>
64-
</div>
65-
)}
66-
</div>
67-
</>
71+
{CLEAR_ALL_LABEL}
72+
</p>
73+
</div>
74+
)}
75+
</div>
6876
);
6977
};
7078

src/components/Loader.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ import React, { type FC } from "react";
33
import "../styles/loader.css";
44
import type { LoaderProps } from "../types";
55

6+
/**
7+
* Loader component represents a skeleton loader for the notification card.
8+
*
9+
* @component
10+
* @example
11+
* <Loader
12+
* hideAvatar={false}
13+
* styles={customStyles}
14+
* />
15+
*
16+
* @param {LoaderProps} props - The properties passed to the Loader component.
17+
* @param {boolean} props.hideAvatar - Flag to determine if the avatar should be hidden.
18+
* @param {Object} props.styles - Custom styles applied to the loader.
19+
* @returns {ReactElement} The rendered Loader component.
20+
*/
21+
622
const Loader : FC<LoaderProps> = ({
723
hideAvatar,
824
styles,

src/components/ShowMore.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ import React from "react";
33
import "../styles/showMore.css";
44
import type { LoadMoreProps } from "../types";
55

6+
/**
7+
* ShowMoreButton component represents the button to load more items in the notification panel.
8+
*
9+
* @component
10+
* @example
11+
* <ShowMoreButton
12+
* styles={customStyles}
13+
* customComponent={<button>Load More</button>}
14+
* onClick={() => console.log('Load more clicked')}
15+
* loadMoreLabel="Load More"
16+
* />
17+
*
18+
* @param {LoadMoreProps} props - The properties passed to the ShowMoreButton component.
19+
* @param {Object} props.styles - The styles object to customize the appearance of the button.
20+
* @param {ReactElement} props.customComponent - Custom component to be rendered as the load more button.
21+
* @param {Function} props.onClick - Callback function executed when the button is clicked.
22+
* @param {string} props.loadMoreLabel - The label to be displayed on the button.
23+
* @returns {ReactElement} The rendered ShowMoreButton component.
24+
*/
25+
626
const ShowMoreButton = (props: LoadMoreProps) => {
727
const { styles, customComponent, onClick, loadMoreLabel } = props;
828

@@ -23,7 +43,7 @@ const ShowMoreButton = (props: LoadMoreProps) => {
2343
style={styles.loadMoreButton}
2444
aria-label="siren-load-more"
2545
>
26-
{loadMoreLabel || "Load More"}
46+
{loadMoreLabel ?? "Load More"}
2747
</button>
2848
);
2949
};

0 commit comments

Comments
 (0)