-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathuseChannelActionItems.tsx
More file actions
253 lines (225 loc) · 6.8 KB
/
useChannelActionItems.tsx
File metadata and controls
253 lines (225 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import React, { useMemo } from 'react';
import { Alert } from 'react-native';
import type { Channel } from 'stream-chat';
import { ChannelActions, getOtherUserInDirectChannel } from './useChannelActions';
import { useChannelActions } from './useChannelActions';
import { useChannelMembershipState } from './useChannelMembershipState';
import { useChannelMuteActive } from './useChannelMuteActive';
import { useIsDirectChat } from './useIsDirectChat';
import { useTheme, useTranslationContext } from '../../../contexts';
import type { TranslationContextValue } from '../../../contexts/translationContext/TranslationContext';
import { Archive, IconProps, Mute, BlockUser, Delete } from '../../../icons';
import { ArrowBoxLeft } from '../../../icons/ArrowBoxLeft';
export type ChannelActionHandler = () => Promise<void> | void;
export type ChannelActionItem = {
action: ChannelActionHandler;
Icon: React.ComponentType<IconProps>;
id: 'mute' | 'block' | 'archive' | 'leave' | 'deleteChannel' | string;
label: string;
placement: 'both' | 'sheet' | 'swipe';
type: 'destructive' | 'standard';
};
export type ChannelActionItemsParams = {
actions: ChannelActions;
channel: Channel;
isArchived: boolean;
isDirectChat: boolean;
isPinned: boolean;
muteActive: boolean;
t: TranslationContextValue['t'];
};
export type BuildDefaultChannelActionItems = (
channelActionItemsParams: ChannelActionItemsParams,
) => ChannelActionItem[];
const ChannelActionsIcon = ({
Icon,
...rest
}: { Icon: React.ComponentType<IconProps> } & IconProps) => {
const {
theme: { semantics },
} = useTheme();
return <Icon stroke={semantics.textSecondary} width={20} height={20} {...rest} />;
};
export const buildDefaultChannelActionItems: BuildDefaultChannelActionItems = (
channelActionItemsParams,
) => {
const {
actions: {
archive,
deleteChannel,
leave,
unarchive,
muteChannel,
unmuteChannel,
muteUser,
unmuteUser,
blockUser,
unblockUser,
},
isArchived,
isDirectChat,
muteActive,
t,
channel,
} = channelActionItemsParams;
const ownUserId = channel.getClient().userID;
const client = channel.getClient();
const isBlocked = isDirectChat
? new Set(client.blockedUsers.getLatestValue().userIds).has(
getOtherUserInDirectChannel(channel)?.user?.id ?? '',
)
: undefined;
const actionItems: ChannelActionItem[] = [
// isPinned
// ? {
// action: unpin,
// Icon: <View />,
// id: 'unpin',
// label: '',
// placement: 'both',
// type: 'standard',
// }
// : {
// action: pin,
// Icon: <View />,
// id: 'pin',
// label: '',
// placement: 'both',
// type: 'standard',
// },
{
action: isDirectChat
? muteActive
? unmuteUser
: muteUser
: muteActive
? unmuteChannel
: muteChannel,
Icon: (props) => (
<Mute
width={20}
height={20}
{...props}
stroke={undefined}
fill={props.fill ?? props.stroke}
/>
),
id: 'mute',
label: isDirectChat
? muteActive
? t('Unmute User')
: t('Mute User')
: muteActive
? t('Unmute Group')
: t('Mute Group'),
placement: isDirectChat ? 'sheet' : 'both',
type: 'standard',
},
];
if (isDirectChat) {
actionItems.push({
action: isBlocked ? unblockUser : blockUser,
Icon: (props) => <ChannelActionsIcon Icon={BlockUser} {...props} />,
id: 'block',
label: isBlocked ? t('Unblock User') : t('Block User'),
placement: 'sheet',
type: 'standard',
});
}
actionItems.push({
action: isArchived ? unarchive : archive,
Icon: (props) => <ChannelActionsIcon Icon={Archive} {...props} />,
id: 'archive',
label: isDirectChat
? isArchived
? t('Unarchive Chat')
: t('Archive Chat')
: isArchived
? t('Unarchive Group')
: t('Archive Group'),
placement: isDirectChat ? 'sheet' : 'both',
type: 'standard',
});
actionItems.push({
action: leave,
Icon: (props) => <ChannelActionsIcon Icon={ArrowBoxLeft} {...props} />,
id: 'leave',
label: isDirectChat ? t('Leave Chat') : t('Leave Group'),
placement: 'sheet',
type: 'destructive',
});
if (channel.data?.created_by?.id === ownUserId) {
actionItems.push({
action: () => {
const title = isDirectChat ? t('Delete chat') : t('Delete group');
const message = isDirectChat
? t("Are you sure you want to delete this chat? This can't be undone.")
: t("Are you sure you want to delete this group? This can't be undone.");
Alert.alert(title, message, [
{
style: 'cancel',
text: t('Cancel'),
},
{
onPress: async () => {
await deleteChannel();
},
style: 'destructive',
text: t('Delete'),
},
]);
},
Icon: (props) => <ChannelActionsIcon Icon={Delete} {...props} />,
id: 'deleteChannel',
label: isDirectChat ? t('Delete Chat') : t('Delete Group'),
placement: 'sheet',
type: 'destructive',
});
}
return actionItems;
};
export type GetChannelActionItems = (params: {
context: ChannelActionItemsParams;
defaultItems: ChannelActionItem[];
}) => ChannelActionItem[];
export const getChannelActionItems: GetChannelActionItems = ({ defaultItems }) => defaultItems;
type UseChannelActionItemsParams = {
channel: Channel;
getChannelActionItems?: GetChannelActionItems;
};
export const useChannelActionItems = ({
channel,
getChannelActionItems: getChannelActionItemsProp = getChannelActionItems,
}: UseChannelActionItemsParams) => {
const { t } = useTranslationContext();
const membership = useChannelMembershipState(channel);
const channelActions = useChannelActions(channel);
const isDirectChat = useIsDirectChat(channel);
const isPinned = Boolean(membership?.pinned_at);
const isArchived = Boolean(membership?.archived_at);
const muteActive = useChannelMuteActive(channel);
const channelActionItemsParams = useMemo(
() => ({
actions: channelActions,
channel,
isArchived,
isDirectChat,
isPinned,
muteActive,
t,
}),
[channel, muteActive, channelActions, isArchived, isDirectChat, isPinned, t],
);
const defaultItems = useMemo(
() => buildDefaultChannelActionItems(channelActionItemsParams),
[channelActionItemsParams],
);
return useMemo(
() =>
getChannelActionItemsProp({
context: channelActionItemsParams,
defaultItems,
}),
[channelActionItemsParams, defaultItems, getChannelActionItemsProp],
);
};