Skip to content

Commit 06c630a

Browse files
committed
fix: how delete for me action is handled on the SDK
1 parent cfc81ab commit 06c630a

File tree

8 files changed

+62
-8
lines changed

8 files changed

+62
-8
lines changed

examples/SampleApp/src/utils/messageActions.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function channelMessageActions({
2121
t: TranslationContextValue['t'];
2222
colors?: typeof Colors;
2323
}) {
24-
const { dismissOverlay, updateMessage } = params;
24+
const { dismissOverlay, deleteForMeMessage } = params;
2525
const actions = messageActions(params);
2626

2727
// We cannot use the useMessageReminder hook here because it is a hook.
@@ -99,10 +99,7 @@ export function channelMessageActions({
9999
{
100100
text: 'Delete',
101101
onPress: async () => {
102-
const { message: deletedMessage } = await chatClient.deleteMessage(params.message.id, {
103-
deleteForMe: true,
104-
});
105-
updateMessage(deletedMessage);
102+
await deleteForMeMessage?.action();
106103
dismissOverlay();
107104
},
108105
style: 'destructive',

package/src/components/Channel/Channel.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Channel as ChannelClass,
1010
ChannelState,
1111
Channel as ChannelType,
12+
DeleteMessageOptions,
1213
EventHandler,
1314
LocalMessage,
1415
localMessageToNewMessagePayload,
@@ -322,6 +323,7 @@ export type ChannelPropsWithContext = Pick<ChannelContextValue, 'channel'> &
322323
| 'handleBan'
323324
| 'handleCopy'
324325
| 'handleDelete'
326+
| 'handleDeleteForMe'
325327
| 'handleEdit'
326328
| 'handleFlag'
327329
| 'handleMarkUnread'
@@ -580,6 +582,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
580582
handleBan,
581583
handleCopy,
582584
handleDelete,
585+
handleDeleteForMe,
583586
handleEdit,
584587
handleFlag,
585588
handleMarkUnread,
@@ -1509,7 +1512,15 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
15091512
});
15101513

15111514
const deleteMessage: MessagesContextValue['deleteMessage'] = useStableCallback(
1512-
async (message, hardDelete = false) => {
1515+
async (message, optionsOrHardDelete = false) => {
1516+
let options: DeleteMessageOptions = {};
1517+
if (typeof optionsOrHardDelete === 'boolean') {
1518+
options = optionsOrHardDelete ? { hardDelete: true } : {};
1519+
} else if (optionsOrHardDelete?.deleteForMe) {
1520+
options = { deleteForMe: true };
1521+
} else if (optionsOrHardDelete?.hardDelete) {
1522+
options = { hardDelete: true };
1523+
}
15131524
if (!channel.id) {
15141525
throw new Error('Channel has not been initialized yet');
15151526
}
@@ -1528,7 +1539,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
15281539

15291540
threadInstance?.upsertReplyLocally({ message: updatedMessage });
15301541

1531-
const data = await client.deleteMessage(message.id, hardDelete);
1542+
const data = await client.deleteMessage(message.id, options);
15321543

15331544
if (data?.message) {
15341545
updateMessage({ ...data.message });
@@ -1837,6 +1848,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
18371848
handleBan,
18381849
handleCopy,
18391850
handleDelete,
1851+
handleDeleteForMe,
18401852
handleEdit,
18411853
handleFlag,
18421854
handleMarkUnread,

package/src/components/Channel/hooks/useCreateMessagesContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const useCreateMessagesContext = ({
3333
handleBan,
3434
handleCopy,
3535
handleDelete,
36+
handleDeleteForMe,
3637
handleEdit,
3738
handleFlag,
3839
handleMarkUnread,
@@ -150,6 +151,7 @@ export const useCreateMessagesContext = ({
150151
handleBan,
151152
handleCopy,
152153
handleDelete,
154+
handleDeleteForMe,
153155
handleEdit,
154156
handleFlag,
155157
handleMarkUnread,

package/src/components/Message/Message.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export type MessagePressableHandlerPayload = PressableHandlerPayload & {
121121
export type MessageActionHandlers = {
122122
copyMessage: () => void;
123123
deleteMessage: () => void;
124+
deleteForMeMessage: () => void;
124125
editMessage: () => void;
125126
flagMessage: () => void;
126127
markUnread: () => Promise<void>;
@@ -155,6 +156,7 @@ export type MessagePropsWithContext = Pick<
155156
| 'handleBan'
156157
| 'handleCopy'
157158
| 'handleDelete'
159+
| 'handleDeleteForMe'
158160
| 'handleEdit'
159161
| 'handleFlag'
160162
| 'handleMarkUnread'
@@ -229,6 +231,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
229231
handleBan,
230232
handleCopy,
231233
handleDelete,
234+
handleDeleteForMe,
232235
handleEdit,
233236
handleFlag,
234237
handleMarkUnread,
@@ -487,6 +490,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
487490
const {
488491
handleCopyMessage,
489492
handleDeleteMessage,
493+
handleDeleteForMeMessage,
490494
handleEditMessage,
491495
handleFlagMessage,
492496
handleMarkUnreadMessage,
@@ -514,6 +518,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
514518
banUser,
515519
copyMessage,
516520
deleteMessage,
521+
deleteForMeMessage,
517522
editMessage,
518523
flagMessage,
519524
handleReaction,
@@ -534,6 +539,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
534539
handleBan,
535540
handleCopy,
536541
handleDelete,
542+
handleDeleteForMe,
537543
handleEdit,
538544
handleFlag,
539545
handleMarkUnread,
@@ -565,6 +571,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
565571
: messageActionsProp({
566572
banUser,
567573
copyMessage,
574+
deleteForMeMessage,
568575
deleteMessage,
569576
dismissOverlay,
570577
editMessage,
@@ -587,6 +594,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
587594

588595
const actionHandlers: MessageActionHandlers = {
589596
copyMessage: handleCopyMessage,
597+
deleteForMeMessage: handleDeleteForMeMessage,
590598
deleteMessage: handleDeleteMessage,
591599
editMessage: handleEditMessage,
592600
flagMessage: handleFlagMessage,

package/src/components/Message/hooks/useMessageActionHandlers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ export const useMessageActionHandlers = ({
6666
);
6767
};
6868

69+
const handleDeleteForMeMessage = async () => {
70+
if (!message.id) {
71+
return;
72+
}
73+
74+
await deleteMessage(message, { deleteForMe: true });
75+
};
76+
6977
const handleToggleMuteUser = async () => {
7078
if (!message.user?.id) {
7179
return;
@@ -182,6 +190,7 @@ export const useMessageActionHandlers = ({
182190

183191
return {
184192
handleCopyMessage,
193+
handleDeleteForMeMessage,
185194
handleDeleteMessage,
186195
handleEditMessage,
187196
handleFlagMessage,

package/src/components/Message/hooks/useMessageActions.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export type MessageActionsHookProps = Pick<
3939
| 'handleBan'
4040
| 'handleCopy'
4141
| 'handleDelete'
42+
| 'handleDeleteForMe'
4243
| 'handleEdit'
4344
| 'handleFlag'
4445
| 'handleQuotedReply'
@@ -73,6 +74,7 @@ export const useMessageActions = ({
7374
handleBan,
7475
handleCopy,
7576
handleDelete,
77+
handleDeleteForMe,
7678
handleEdit,
7779
handleFlag,
7880
handleMarkUnread,
@@ -101,6 +103,7 @@ export const useMessageActions = ({
101103
const {
102104
handleCopyMessage,
103105
handleDeleteMessage,
106+
handleDeleteForMeMessage,
104107
handleEditMessage,
105108
handleFlagMessage,
106109
handleMarkUnreadMessage,
@@ -182,6 +185,19 @@ export const useMessageActions = ({
182185
titleStyle: { color: accent_red },
183186
};
184187

188+
const deleteForMeMessage: MessageActionType = {
189+
action: () => {
190+
dismissOverlay();
191+
if (handleDeleteForMe) {
192+
handleDeleteForMe(message);
193+
}
194+
handleDeleteForMeMessage();
195+
},
196+
actionType: 'deleteForMeMessage',
197+
icon: <Delete fill={accent_red} size={24} />,
198+
title: t('Delete for me'),
199+
};
200+
185201
const editMessage: MessageActionType = {
186202
action: () => {
187203
dismissOverlay();
@@ -319,6 +335,7 @@ export const useMessageActions = ({
319335
return {
320336
banUser,
321337
copyMessage,
338+
deleteForMeMessage,
322339
deleteMessage,
323340
editMessage,
324341
flagMessage,

package/src/components/Message/utils/messageActions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export type MessageActionsParams = {
2626
showMessageReactions: boolean;
2727
threadReply: MessageActionType;
2828
unpinMessage: MessageActionType;
29+
// Optional Actions
30+
deleteForMeMessage?: MessageActionType;
2931
} & Pick<MessageContextValue, 'message' | 'isMyMessage'> &
3032
Pick<MessagesContextValue, 'updateMessage'>;
3133

package/src/contexts/messagesContext/MessagesContext.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
Channel,
88
ChannelState,
99
CommandSuggestion,
10+
DeleteMessageOptions,
1011
LocalMessage,
1112
MessageResponse,
1213
} from 'stream-chat';
@@ -114,7 +115,11 @@ export type MessagesContextValue = Pick<MessageContextValue, 'isMessageAIGenerat
114115
* Defaults to: [DateHeader](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/MessageList/DateHeader.tsx)
115116
**/
116117
DateHeader: React.ComponentType<DateHeaderProps>;
117-
deleteMessage: (message: LocalMessage, hardDelete?: boolean) => Promise<void>;
118+
// FIXME: Remove the signature with optionsOrHardDelete boolean with the next major release
119+
deleteMessage: (
120+
message: LocalMessage,
121+
optionsOrHardDelete?: boolean | DeleteMessageOptions,
122+
) => Promise<void>;
118123
deleteReaction: (type: string, messageId: string) => Promise<void>;
119124

120125
/** Should keyboard be dismissed when messaged is touched */
@@ -404,6 +409,8 @@ export type MessagesContextValue = Pick<MessageContextValue, 'isMessageAIGenerat
404409
handleBan?: (message: LocalMessage) => Promise<void>;
405410
/** Handler to access when a copy message action is invoked */
406411
handleCopy?: (message: LocalMessage) => Promise<void>;
412+
/** Handler to access when a delete for me message action is invoked */
413+
handleDeleteForMe?: (message: LocalMessage) => Promise<void>;
407414
/** Handler to access when a delete message action is invoked */
408415
handleDelete?: (message: LocalMessage) => Promise<void>;
409416
/** Handler to access when an edit message action is invoked */

0 commit comments

Comments
 (0)