Skip to content

Commit ca6b990

Browse files
committed
fix: move image gallery inside channel component
1 parent dfb1bfa commit ca6b990

File tree

5 files changed

+102
-108
lines changed

5 files changed

+102
-108
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ import { CreatePollIcon as DefaultCreatePollIcon } from '../../components/Poll/c
5050
import {
5151
AttachmentPickerContextValue,
5252
AttachmentPickerProvider,
53+
ImageGalleryProvider,
54+
ImageGalleryProviderProps,
5355
MessageContextValue,
56+
OverlayContextValue,
57+
useOverlayContext,
5458
} from '../../contexts';
5559
import {
5660
AudioPlayerContextProps,
@@ -140,6 +144,7 @@ import { ImageOverlaySelectedComponent as DefaultImageOverlaySelectedComponent }
140144
import { AutoCompleteSuggestionHeader as AutoCompleteSuggestionHeaderDefault } from '../AutoCompleteInput/AutoCompleteSuggestionHeader';
141145
import { AutoCompleteSuggestionItem as AutoCompleteSuggestionItemDefault } from '../AutoCompleteInput/AutoCompleteSuggestionItem';
142146
import { AutoCompleteSuggestionList as AutoCompleteSuggestionListDefault } from '../AutoCompleteInput/AutoCompleteSuggestionList';
147+
import { ImageGallery } from '../ImageGallery/ImageGallery';
143148
import { EmptyStateIndicator as EmptyStateIndicatorDefault } from '../Indicators/EmptyStateIndicator';
144149
import {
145150
LoadingErrorIndicator as LoadingErrorIndicatorDefault,
@@ -263,6 +268,8 @@ const debounceOptions = {
263268
};
264269

265270
export type ChannelPropsWithContext = Pick<ChannelContextValue, 'channel'> &
271+
Partial<ImageGalleryProviderProps> &
272+
Pick<OverlayContextValue, 'overlay' | 'overlayOpacity'> &
266273
Partial<
267274
Pick<AttachmentPickerContextValue, 'bottomInset' | 'topInset' | 'disableAttachmentPicker'>
268275
> &
@@ -575,7 +582,14 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
575582
attachmentPickerErrorText,
576583
numberOfAttachmentImagesToLoadPerCall = 60,
577584
numberOfAttachmentPickerImageColumns = 3,
578-
585+
autoPlayVideo = false,
586+
giphyVersion = 'fixed_height',
587+
imageGalleryCustomComponents,
588+
imageGalleryGridHandleHeight = 40,
589+
imageGalleryGridSnapPoints,
590+
numberOfImageGalleryGridColumns = 3,
591+
overlay,
592+
overlayOpacity,
579593
bottomInset = 0,
580594
CameraSelectorIcon = DefaultCameraSelectorIcon,
581595
FileSelectorIcon = DefaultFileSelectorIcon,
@@ -620,7 +634,6 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
620634
Gallery = GalleryDefault,
621635
getMessagesGroupStyles,
622636
Giphy = GiphyDefault,
623-
giphyVersion = 'fixed_height',
624637
handleAttachButtonPress,
625638
handleBan,
626639
handleCopy,
@@ -1747,6 +1760,25 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
17471760
],
17481761
);
17491762

1763+
const imageGalleryProviderProps = useMemo(
1764+
() => ({
1765+
autoPlayVideo,
1766+
giphyVersion,
1767+
imageGalleryCustomComponents,
1768+
imageGalleryGridHandleHeight,
1769+
imageGalleryGridSnapPoints,
1770+
numberOfImageGalleryGridColumns,
1771+
}),
1772+
[
1773+
autoPlayVideo,
1774+
giphyVersion,
1775+
imageGalleryCustomComponents,
1776+
imageGalleryGridHandleHeight,
1777+
imageGalleryGridSnapPoints,
1778+
numberOfImageGalleryGridColumns,
1779+
],
1780+
);
1781+
17501782
const attachmentPickerContext = useMemo(
17511783
() => ({
17521784
bottomInset,
@@ -2044,37 +2076,40 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
20442076
}
20452077

20462078
return (
2047-
<KeyboardCompatibleView
2048-
behavior={keyboardBehavior}
2049-
enabled={!disableKeyboardCompatibleView}
2050-
keyboardVerticalOffset={keyboardVerticalOffset}
2051-
{...additionalKeyboardAvoidingViewProps}
2052-
>
2053-
<ChannelProvider value={channelContext}>
2054-
<OwnCapabilitiesProvider value={ownCapabilitiesContext}>
2055-
<TypingProvider value={typingContext}>
2056-
<PaginatedMessageListProvider value={messageListContext}>
2057-
<MessagesProvider value={messagesContext}>
2058-
<ThreadProvider value={threadContext}>
2059-
<AttachmentPickerProvider value={attachmentPickerContext}>
2060-
<MessageComposerProvider value={messageComposerContext}>
2061-
<MessageInputProvider value={inputMessageInputContext}>
2062-
<AudioPlayerProvider value={audioPlayerContext}>
2063-
<View style={{ height: '100%' }}>{children}</View>
2064-
{!disableAttachmentPicker && (
2065-
<AttachmentPicker ref={bottomSheetRef} {...attachmentPickerProps} />
2066-
)}
2067-
</AudioPlayerProvider>
2068-
</MessageInputProvider>
2069-
</MessageComposerProvider>
2070-
</AttachmentPickerProvider>
2071-
</ThreadProvider>
2072-
</MessagesProvider>
2073-
</PaginatedMessageListProvider>
2074-
</TypingProvider>
2075-
</OwnCapabilitiesProvider>
2076-
</ChannelProvider>
2077-
</KeyboardCompatibleView>
2079+
<ImageGalleryProvider value={imageGalleryProviderProps}>
2080+
<KeyboardCompatibleView
2081+
behavior={keyboardBehavior}
2082+
enabled={!disableKeyboardCompatibleView}
2083+
keyboardVerticalOffset={keyboardVerticalOffset}
2084+
{...additionalKeyboardAvoidingViewProps}
2085+
>
2086+
<ChannelProvider value={channelContext}>
2087+
<OwnCapabilitiesProvider value={ownCapabilitiesContext}>
2088+
<TypingProvider value={typingContext}>
2089+
<PaginatedMessageListProvider value={messageListContext}>
2090+
<MessagesProvider value={messagesContext}>
2091+
<ThreadProvider value={threadContext}>
2092+
<AttachmentPickerProvider value={attachmentPickerContext}>
2093+
<MessageComposerProvider value={messageComposerContext}>
2094+
<MessageInputProvider value={inputMessageInputContext}>
2095+
<AudioPlayerProvider value={audioPlayerContext}>
2096+
<View style={{ height: '100%' }}>{children}</View>
2097+
{!disableAttachmentPicker && (
2098+
<AttachmentPicker ref={bottomSheetRef} {...attachmentPickerProps} />
2099+
)}
2100+
</AudioPlayerProvider>
2101+
</MessageInputProvider>
2102+
</MessageComposerProvider>
2103+
</AttachmentPickerProvider>
2104+
</ThreadProvider>
2105+
</MessagesProvider>
2106+
</PaginatedMessageListProvider>
2107+
</TypingProvider>
2108+
</OwnCapabilitiesProvider>
2109+
</ChannelProvider>
2110+
</KeyboardCompatibleView>
2111+
{overlay === 'gallery' && <ImageGallery overlayOpacity={overlayOpacity} />}
2112+
</ImageGalleryProvider>
20782113
);
20792114
};
20802115

@@ -2094,6 +2129,7 @@ export type ChannelProps = Partial<Omit<ChannelPropsWithContext, 'channel' | 'th
20942129
export const Channel = (props: PropsWithChildren<ChannelProps>) => {
20952130
const { client, enableOfflineSupport, isOnline, isMessageAIGenerated } = useChatContext();
20962131
const { t } = useTranslationContext();
2132+
const { overlay, overlayOpacity } = useOverlayContext();
20972133

20982134
const threadFromProps = props?.thread;
20992135
const threadInstance = (threadFromProps as ThreadType)?.threadInstance as Thread;
@@ -2125,6 +2161,8 @@ export const Channel = (props: PropsWithChildren<ChannelProps>) => {
21252161
{...{
21262162
isMessageAIGenerated,
21272163
isOnline,
2164+
overlay,
2165+
overlayOpacity,
21282166
setThreadMessages,
21292167
thread,
21302168
threadMessages,

package/src/components/ImageGallery/ImageGallery.tsx

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
import { useImageGalleryGestures } from './hooks/useImageGalleryGestures';
3737

3838
import { useImageGalleryContext } from '../../contexts/imageGalleryContext/ImageGalleryContext';
39-
import { OverlayProviderProps } from '../../contexts/overlayContext/OverlayContext';
4039
import { useTheme } from '../../contexts/themeContext/ThemeContext';
4140
import { useStateStore } from '../../hooks';
4241
import { useViewport } from '../../hooks/useViewport';
@@ -100,30 +99,25 @@ const imageGallerySelector = (state: ImageGalleryState) => ({
10099
currentIndex: state.currentIndex,
101100
});
102101

103-
type Props = ImageGalleryCustomComponents & {
102+
type Props = {
104103
overlayOpacity: SharedValue<number>;
105-
} & Pick<
106-
OverlayProviderProps,
107-
| 'imageGalleryGridSnapPoints'
108-
| 'imageGalleryGridHandleHeight'
109-
| 'numberOfImageGalleryGridColumns'
110-
>;
104+
};
111105

112106
export const ImageGallery = (props: Props) => {
113-
const {
114-
imageGalleryCustomComponents,
115-
imageGalleryGridHandleHeight = 40,
116-
imageGalleryGridSnapPoints,
117-
numberOfImageGalleryGridColumns,
118-
overlayOpacity,
119-
} = props;
107+
const { overlayOpacity } = props;
120108
const {
121109
theme: {
122110
colors: { white_snow },
123111
imageGallery: { backgroundColor, pager, slide },
124112
},
125113
} = useTheme();
126-
const { imageGalleryStateStore } = useImageGalleryContext();
114+
const {
115+
imageGalleryStateStore,
116+
imageGalleryCustomComponents,
117+
imageGalleryGridHandleHeight,
118+
imageGalleryGridSnapPoints,
119+
numberOfImageGalleryGridColumns,
120+
} = useImageGalleryContext();
127121
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
128122
const { assets, videoPlayerPool } = imageGalleryStateStore;
129123

@@ -136,7 +130,7 @@ export const ImageGallery = (props: Props) => {
136130
const halfScreenHeight = fullWindowHeight / 2;
137131
const quarterScreenHeight = fullWindowHeight / 4;
138132
const snapPoints = React.useMemo(
139-
() => [(fullWindowHeight * 3) / 4, fullWindowHeight - imageGalleryGridHandleHeight],
133+
() => [(fullWindowHeight * 3) / 4, fullWindowHeight - (imageGalleryGridHandleHeight ?? 0)],
140134
// eslint-disable-next-line react-hooks/exhaustive-deps
141135
[],
142136
);

package/src/contexts/imageGalleryContext/ImageGalleryContext.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@ import React, { PropsWithChildren, useContext, useEffect, useMemo, useState } fr
22

33
import { Attachment } from 'stream-chat';
44

5+
import { ImageGalleryCustomComponents } from '../../components/ImageGallery/ImageGallery';
56
import { ImageGalleryStateStore } from '../../state-store/image-gallery-state-store';
67
import { DEFAULT_BASE_CONTEXT_VALUE } from '../utils/defaultBaseContextValue';
78

89
import { isTestEnvironment } from '../utils/isTestEnvironment';
910

10-
export type ImageGalleryContextValue = {
11+
export type ImageGalleryProviderProps = ImageGalleryCustomComponents & {
1112
autoPlayVideo?: boolean;
12-
imageGalleryStateStore: ImageGalleryStateStore;
13+
/**
14+
* The giphy version to render - check the keys of the [Image Object](https://developers.giphy.com/docs/api/schema#image-object) for possible values. Uses 'fixed_height' by default
15+
* */
16+
giphyVersion?: keyof NonNullable<Attachment['giphy']>;
17+
imageGalleryGridHandleHeight?: number;
18+
imageGalleryGridSnapPoints?: [string | number, string | number];
19+
numberOfImageGalleryGridColumns?: number;
1320
};
1421

15-
export type ImageGalleryProviderProps = {
16-
autoPlayVideo?: boolean;
17-
giphyVersion?: keyof NonNullable<Attachment['giphy']>;
22+
export type ImageGalleryContextValue = ImageGalleryProviderProps & {
23+
imageGalleryStateStore: ImageGalleryStateStore;
1824
};
1925

2026
export const ImageGalleryContext = React.createContext(
@@ -38,8 +44,9 @@ export const ImageGalleryProvider = ({
3844
() => ({
3945
autoPlayVideo: value?.autoPlayVideo,
4046
imageGalleryStateStore,
47+
...value,
4148
}),
42-
[value?.autoPlayVideo, imageGalleryStateStore],
49+
[imageGalleryStateStore, value],
4350
);
4451

4552
return (

package/src/contexts/overlayContext/OverlayContext.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import React, { useContext } from 'react';
22

3-
import type { Attachment } from 'stream-chat';
4-
5-
import type { ImageGalleryCustomComponents } from '../../components/ImageGallery/ImageGallery';
3+
import { SharedValue } from 'react-native-reanimated';
64

75
import type { Streami18n } from '../../utils/i18n/Streami18n';
86
import type { DeepPartial } from '../themeContext/ThemeContext';
@@ -15,6 +13,7 @@ export type Overlay = 'alert' | 'gallery' | 'none';
1513

1614
export type OverlayContextValue = {
1715
overlay: Overlay;
16+
overlayOpacity: SharedValue<number>;
1817
setOverlay: React.Dispatch<React.SetStateAction<Overlay>>;
1918
style?: DeepPartial<Theme>;
2019
};
@@ -23,17 +22,9 @@ export const OverlayContext = React.createContext(
2322
DEFAULT_BASE_CONTEXT_VALUE as OverlayContextValue,
2423
);
2524

26-
export type OverlayProviderProps = ImageGalleryCustomComponents & {
27-
autoPlayVideo?: boolean;
28-
/**
29-
* The giphy version to render - check the keys of the [Image Object](https://developers.giphy.com/docs/api/schema#image-object) for possible values. Uses 'fixed_height' by default
30-
* */
31-
giphyVersion?: keyof NonNullable<Attachment['giphy']>;
25+
export type OverlayProviderProps = {
3226
/** https://github.com/GetStream/stream-chat-react-native/wiki/Internationalization-(i18n) */
3327
i18nInstance?: Streami18n;
34-
imageGalleryGridHandleHeight?: number;
35-
imageGalleryGridSnapPoints?: [string | number, string | number];
36-
numberOfImageGalleryGridColumns?: number;
3728
value?: Partial<OverlayContextValue>;
3829
};
3930

package/src/contexts/overlayContext/OverlayProvider.tsx

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import React, { PropsWithChildren, useEffect, useMemo, useState } from 'react';
1+
import React, { PropsWithChildren, useEffect, useState } from 'react';
22

33
import { BackHandler } from 'react-native';
44

55
import { cancelAnimation, useSharedValue, withTiming } from 'react-native-reanimated';
66

77
import { OverlayContext, OverlayProviderProps } from './OverlayContext';
88

9-
import { ImageGallery } from '../../components/ImageGallery/ImageGallery';
10-
119
import { useStreami18n } from '../../hooks/useStreami18n';
1210

13-
import {
14-
ImageGalleryProvider,
15-
ImageGalleryProviderProps,
16-
} from '../imageGalleryContext/ImageGalleryContext';
1711
import { ThemeProvider } from '../themeContext/ThemeContext';
1812
import {
1913
DEFAULT_USER_LANGUAGE,
@@ -41,17 +35,7 @@ import {
4135
* @example ./OverlayProvider.md
4236
*/
4337
export const OverlayProvider = (props: PropsWithChildren<OverlayProviderProps>) => {
44-
const {
45-
autoPlayVideo,
46-
children,
47-
giphyVersion,
48-
i18nInstance,
49-
imageGalleryCustomComponents,
50-
imageGalleryGridHandleHeight = 40,
51-
imageGalleryGridSnapPoints,
52-
numberOfImageGalleryGridColumns,
53-
value,
54-
} = props;
38+
const { children, i18nInstance, value } = props;
5539

5640
const [overlay, setOverlay] = useState(value?.overlay || 'none');
5741

@@ -85,37 +69,17 @@ export const OverlayProvider = (props: PropsWithChildren<OverlayProviderProps>)
8569
// eslint-disable-next-line react-hooks/exhaustive-deps
8670
}, [overlay]);
8771

88-
const imageGalleryProviderProps: ImageGalleryProviderProps = useMemo(
89-
() => ({
90-
autoPlayVideo,
91-
giphyVersion,
92-
}),
93-
[autoPlayVideo, giphyVersion],
94-
);
95-
9672
const overlayContext = {
9773
overlay,
74+
overlayOpacity,
9875
setOverlay,
9976
style: value?.style,
10077
};
10178

10279
return (
10380
<TranslationProvider value={{ ...translators, userLanguage: DEFAULT_USER_LANGUAGE }}>
10481
<OverlayContext.Provider value={overlayContext}>
105-
<ImageGalleryProvider value={imageGalleryProviderProps}>
106-
<ThemeProvider style={overlayContext.style}>
107-
{children}
108-
{overlay === 'gallery' && (
109-
<ImageGallery
110-
imageGalleryCustomComponents={imageGalleryCustomComponents}
111-
imageGalleryGridHandleHeight={imageGalleryGridHandleHeight}
112-
imageGalleryGridSnapPoints={imageGalleryGridSnapPoints}
113-
numberOfImageGalleryGridColumns={numberOfImageGalleryGridColumns}
114-
overlayOpacity={overlayOpacity}
115-
/>
116-
)}
117-
</ThemeProvider>
118-
</ImageGalleryProvider>
82+
<ThemeProvider style={overlayContext.style}>{children}</ThemeProvider>
11983
</OverlayContext.Provider>
12084
</TranslationProvider>
12185
);

0 commit comments

Comments
 (0)