Skip to content
Open
33 changes: 32 additions & 1 deletion mobile/src/libs/components/card/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
View,
} from '#libs/components/components';
import { AppColor } from '#libs/enums/enums';
import { useCallback } from '#libs/hooks/hooks';
import { type IconName } from '#libs/types/types';

import { DEFAULT_NUMBER_OF_LINES } from './libs/constants/constants';
Expand All @@ -24,8 +25,12 @@ type Properties = {
onPress: () => void;
iconRight?: IconName;
onIconPress?: () => void;
id?: number;
isModalVisible?: boolean;
};

const rowReferences = new Map();

const Card: React.FC<Properties> = ({
title,
image = imagePlaceholder,
Expand All @@ -34,7 +39,28 @@ const Card: React.FC<Properties> = ({
onPress,
iconRight,
onIconPress,
id,
isModalVisible,
}) => {
const handleSwipeableReference = useCallback(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we prefer using useRef with array without Map outside?

(reference: Swipeable | null) => {
if (reference && !rowReferences.get(id)) {
rowReferences.set(id, reference);
}
},
[id],
);

const handleCloseOtherSwipeables = useCallback(() => {
[...rowReferences.entries()].forEach(([key, reference]) => {
if (key !== id && reference) {
(reference as Swipeable).close();
}
});
}, [id, rowReferences]);

!isModalVisible && handleCloseOtherSwipeables();

const renderRightSwipeActions = (): React.ReactNode => {
return (
Boolean(iconRight) && (
Expand All @@ -46,7 +72,12 @@ const Card: React.FC<Properties> = ({
};

return (
<Swipeable renderRightActions={renderRightSwipeActions}>
<Swipeable
renderRightActions={renderRightSwipeActions}
key={id}
ref={handleSwipeableReference}
onSwipeableWillOpen={handleCloseOtherSwipeables}
>
<Pressable onPress={onPress} style={styles.container}>
{iconName && iconColor ? (
<View style={styles.iconContainer}>
Expand Down
2 changes: 2 additions & 0 deletions mobile/src/screens/chat-list/chat-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const ChatList: React.FC = () => {
return (
<Card
key={item.id}
id={item.id}
title={item.name}
onPress={(): void => {
handleSelectChat(item.name, item.id.toString());
Expand All @@ -122,6 +123,7 @@ const ChatList: React.FC = () => {
onIconPress={(): void => {
handleShowDeleteModal(item.id);
}}
isModalVisible={isDeleteModalVisible}
/>
);
})}
Expand Down
11 changes: 7 additions & 4 deletions mobile/src/screens/journal/journal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const Journal: React.FC = () => {
const navigation =
useNavigation<NativeStackNavigationProp<JournalNavigationParameterList>>();

const [isModalVisible, setIsModalVisible] = useState<boolean>(false);
const [isDeleteModalVisible, setIsDeleteModalVisible] =
useState<boolean>(false);

const { allJournalEntries, selectedJournalEntry } = useAppSelector(
({ journal }) => {
Expand All @@ -57,11 +58,11 @@ const Journal: React.FC = () => {

const handleShowModal = (id: number): void => {
dispatch(journalActions.setSelectedJournalEntry(id));
setIsModalVisible(true);
setIsDeleteModalVisible(true);
};

const hanleCloseModal = (): void => {
setIsModalVisible(false);
setIsDeleteModalVisible(false);
};

const handleDeleteNote = (): void => {
Expand Down Expand Up @@ -100,7 +101,7 @@ const Journal: React.FC = () => {
return (
<LinearGradient>
<Modal
isVisible={isModalVisible}
isVisible={isDeleteModalVisible}
onClose={hanleCloseModal}
onDelete={handleDeleteNote}
type="Note"
Expand All @@ -115,6 +116,7 @@ const Journal: React.FC = () => {
return (
<Card
key={item.id}
id={item.id}
title={item.title}
onPress={(): void => {
handleSelectJournalEntry(item.id);
Expand All @@ -123,6 +125,7 @@ const Journal: React.FC = () => {
onIconPress={(): void => {
handleShowModal(item.id);
}}
isModalVisible={isDeleteModalVisible}
/>
);
})}
Expand Down