-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathChannelSwipableWrapper.tsx
More file actions
153 lines (136 loc) · 4.61 KB
/
ChannelSwipableWrapper.tsx
File metadata and controls
153 lines (136 loc) · 4.61 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
import React, { PropsWithChildren, useCallback, useMemo, useState } from 'react';
import { StyleSheet } from 'react-native';
import { SharedValue } from 'react-native-reanimated';
import { Channel } from 'stream-chat';
import { ChannelDetailsBottomSheet as DefaultChannelDetailsBottomSheet } from './ChannelDetailsBottomSheet';
import type { ChannelDetailsBottomSheetProps } from './ChannelDetailsBottomSheet';
import { useTheme } from '../../contexts';
import { useSwipeRegistryContext } from '../../contexts/swipeableContext/SwipeRegistryContext';
import { Archive, MenuPointHorizontal, Mute } from '../../icons';
import { GetChannelActionItems } from '../ChannelList/hooks/useChannelActionItems';
import { useChannelActionItems } from '../ChannelList/hooks/useChannelActionItems';
import { useChannelActionItemsById } from '../ChannelList/hooks/useChannelActionItemsById';
import { useIsDirectChat } from '../ChannelList/hooks/useIsDirectChat';
import {
BottomSheetModal,
RightActions,
SwipableActionItem,
SwipableWrapper,
SwipableWrapperProps,
} from '../UIComponents';
export const OpenChannelDetailsButton = () => {
const {
theme: { semantics },
} = useTheme();
return <MenuPointHorizontal stroke={semantics.textPrimary} />;
};
export const ChannelSwipableWrapper = ({
channel,
getChannelActionItems,
ChannelDetailsBottomSheet: ChannelDetailsBottomSheetComponent = DefaultChannelDetailsBottomSheet,
swipableProps: _swipableProps,
children,
}: PropsWithChildren<{
channel: Channel;
ChannelDetailsBottomSheet?: React.ComponentType<ChannelDetailsBottomSheetProps>;
getChannelActionItems?: GetChannelActionItems;
swipableProps?: SwipableWrapperProps['swipableProps'];
}>) => {
const [channelDetailSheetOpen, setChannelDetailSheetOpen] = useState(false);
const channelActionsById = useChannelActionItemsById({ channel, getChannelActionItems });
const channelActionItems = useChannelActionItems({ channel, getChannelActionItems });
const swipableRegistry = useSwipeRegistryContext();
const {
theme: { semantics },
} = useTheme();
const styles = useStyles();
const isDirectChannel = useIsDirectChat(channel);
const Icon = useCallback(
() =>
isDirectChannel ? (
<Mute width={20} height={20} fill={semantics.textOnAccent} />
) : (
<Archive width={20} height={20} stroke={semantics.textOnAccent} />
),
[isDirectChannel, semantics.textOnAccent],
);
const swipableActions = useMemo<SwipableActionItem[]>(() => {
const items: SwipableActionItem[] = [
{
id: 'openSheet',
action: () => setChannelDetailSheetOpen(true),
contentContainerStyle: [styles.contentContainerStyle, styles.elipsis],
Content: OpenChannelDetailsButton,
},
];
const extraItem = isDirectChannel ? channelActionsById.mute : channelActionsById.archive;
if (extraItem) {
const { id, action } = extraItem;
items.push({
id,
action: () => {
action();
swipableRegistry?.closeAll();
},
Content: Icon,
contentContainerStyle: [styles.contentContainerStyle, styles.standard],
});
}
return items;
}, [
styles.contentContainerStyle,
styles.elipsis,
styles.standard,
isDirectChannel,
channelActionsById.mute,
channelActionsById.archive,
Icon,
swipableRegistry,
]);
const renderRightActions = useCallback(
(_progress: SharedValue<number>, translation: SharedValue<number>) => (
<RightActions items={swipableActions} translation={translation} />
),
[swipableActions],
);
const swipableProps = useMemo(
() => ({ renderRightActions, ..._swipableProps }),
[_swipableProps, renderRightActions],
);
return (
<>
<SwipableWrapper swipeableId={channel.cid} swipableProps={swipableProps}>
{children}
</SwipableWrapper>
<BottomSheetModal
onClose={() => setChannelDetailSheetOpen(false)}
visible={channelDetailSheetOpen}
height={356}
>
<ChannelDetailsBottomSheetComponent channel={channel} items={channelActionItems} />
</BottomSheetModal>
</>
);
};
const useStyles = () => {
const {
theme: { semantics },
} = useTheme();
return useMemo(
() =>
StyleSheet.create({
contentContainerStyle: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
},
elipsis: {
backgroundColor: semantics.backgroundCoreSurface,
},
standard: {
backgroundColor: semantics.accentPrimary,
},
}),
[semantics.accentPrimary, semantics.backgroundCoreSurface],
);
};