Skip to content

Commit e8c5180

Browse files
committed
fix: improve state usage
1 parent 578646d commit e8c5180

File tree

9 files changed

+62
-52
lines changed

9 files changed

+62
-52
lines changed

package/src/components/ImageGallery/ImageGallery.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export const ImageGallery = (props: Props) => {
125125
} = useTheme();
126126
const { imageGalleryStateStore } = useImageGalleryContext();
127127
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
128-
const assets = imageGalleryStateStore.assets;
128+
const { assets, videoPlayerPool } = imageGalleryStateStore;
129129

130130
const { vh, vw } = useViewport();
131131

@@ -230,11 +230,12 @@ export const ImageGallery = (props: Props) => {
230230

231231
// If you change the current index, pause the active video player.
232232
useEffect(() => {
233-
const actvivePlayer = imageGalleryStateStore.videoPlayerPool.getActivePlayer();
234-
if (actvivePlayer) {
235-
actvivePlayer.pause();
233+
const activePlayer = videoPlayerPool.getActivePlayer();
234+
235+
if (activePlayer) {
236+
activePlayer.pause();
236237
}
237-
}, [currentIndex, imageGalleryStateStore.videoPlayerPool]);
238+
}, [currentIndex, videoPlayerPool]);
238239

239240
const { doubleTap, pan, pinch, singleTap } = useImageGalleryGestures({
240241
currentImageHeight,

package/src/components/ImageGallery/components/AnimatedGalleryVideo.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { VideoPlayerState } from '../../../state-store/video-player';
1717
import { ONE_SECOND_IN_MILLISECONDS } from '../../../utils/constants';
1818
import { Spinner } from '../../UIComponents/Spinner';
1919
import { useAnimatedGalleryStyle } from '../hooks/useAnimatedGalleryStyle';
20-
import { useVideoPlayer } from '../hooks/useVideoPlayer';
20+
import { useImageGalleryVideoPlayer } from '../hooks/useImageGalleryVideoPlayer';
2121
import { Photo } from '../ImageGallery';
2222

2323
const oneEighth = 1 / 8;
@@ -49,10 +49,7 @@ const styles = StyleSheet.create({
4949
});
5050

5151
const videoPlayerSelector = (state: VideoPlayerState) => ({
52-
duration: state.duration,
5352
isPlaying: state.isPlaying,
54-
position: state.position,
55-
progress: state.progress,
5653
});
5754

5855
export const AnimatedGalleryVideo = React.memo(
@@ -77,7 +74,7 @@ export const AnimatedGalleryVideo = React.memo(
7774

7875
const videoRef = useRef<VideoType>(null);
7976

80-
const videoPlayer = useVideoPlayer({
77+
const videoPlayer = useImageGalleryVideoPlayer({
8178
id: attachmentId,
8279
});
8380

package/src/components/ImageGallery/components/ImageGalleryVideoControl.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Pause, Play } from '../../../icons';
1010
import { VideoPlayerState } from '../../../state-store/video-player';
1111
import { getDurationLabelFromDuration } from '../../../utils/utils';
1212
import { ProgressControl } from '../../ProgressControl/ProgressControl';
13-
import { useVideoPlayer } from '../hooks/useVideoPlayer';
13+
import { useImageGalleryVideoPlayer } from '../hooks/useImageGalleryVideoPlayer';
1414

1515
const styles = StyleSheet.create({
1616
durationTextStyle: {
@@ -52,7 +52,7 @@ const videoPlayerSelector = (state: VideoPlayerState) => ({
5252
export const ImageGalleryVideoControl = React.memo((props: ImageGalleryFooterVideoControlProps) => {
5353
const { attachmentId } = props;
5454

55-
const videoPlayer = useVideoPlayer({
55+
const videoPlayer = useImageGalleryVideoPlayer({
5656
id: attachmentId,
5757
});
5858

package/src/components/ImageGallery/hooks/useVideoPlayer.ts renamed to package/src/components/ImageGallery/hooks/useImageGalleryVideoPlayer.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ import { useMemo } from 'react';
33
import { useImageGalleryContext } from '../../../contexts/imageGalleryContext/ImageGalleryContext';
44
import { VideoPlayerOptions } from '../../../state-store/video-player';
55

6-
export type UseVideoPlayerProps = VideoPlayerOptions;
6+
export type UseImageGalleryVideoPlayerProps = VideoPlayerOptions;
77

88
/**
99
* Hook to get the video player instance.
1010
* @param options - The options for the video player.
1111
* @returns The video player instance.
1212
*/
13-
export const useVideoPlayer = (options: UseVideoPlayerProps) => {
14-
const { imageGalleryStateStore } = useImageGalleryContext();
13+
export const useImageGalleryVideoPlayer = (options: UseImageGalleryVideoPlayerProps) => {
14+
const { autoPlayVideo, imageGalleryStateStore } = useImageGalleryContext();
1515
const videoPlayer = useMemo(() => {
16-
return imageGalleryStateStore.videoPlayerPool.getOrAddPlayer(options);
17-
}, [imageGalleryStateStore, options]);
16+
return imageGalleryStateStore.videoPlayerPool.getOrAddPlayer({
17+
...options,
18+
autoPlay: autoPlayVideo,
19+
});
20+
}, [autoPlayVideo, imageGalleryStateStore, options]);
1821

1922
return videoPlayer;
2023
};

package/src/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ export * from './ImageGallery/components/ImageGalleryHeader';
8181
export * from './ImageGallery/components/ImageGalleryOverlay';
8282
export * from './ImageGallery/components/ImageGrid';
8383
export * from './ImageGallery/components/ImageGridHandle';
84+
export * from './ImageGallery/components/ImageGalleryVideoControl';
85+
export * from './ImageGallery/hooks/useImageGalleryVideoPlayer';
8486

8587
export * from './Indicators/EmptyStateIndicator';
8688
export * from './Indicators/LoadingDot';

package/src/contexts/imageGalleryContext/ImageGalleryContext.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
import React, { PropsWithChildren, useContext, useEffect, useState } from 'react';
1+
import React, { PropsWithChildren, useContext, useEffect, useMemo, useState } from 'react';
2+
3+
import { Attachment } from 'stream-chat';
24

35
import { ImageGalleryStateStore } from '../../state-store/image-gallery-state-store';
4-
import type { UnknownType } from '../../types/types';
56
import { DEFAULT_BASE_CONTEXT_VALUE } from '../utils/defaultBaseContextValue';
67

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

910
export type ImageGalleryContextValue = {
11+
autoPlayVideo?: boolean;
1012
imageGalleryStateStore: ImageGalleryStateStore;
1113
};
1214

15+
export type ImageGalleryProviderProps = {
16+
autoPlayVideo?: boolean;
17+
giphyVersion?: keyof NonNullable<Attachment['giphy']>;
18+
};
19+
1320
export const ImageGalleryContext = React.createContext(
1421
DEFAULT_BASE_CONTEXT_VALUE as ImageGalleryContextValue,
1522
);
1623

17-
export const ImageGalleryProvider = ({ children }: PropsWithChildren<UnknownType>) => {
18-
const [imageGalleryStateStore] = useState(
19-
() => new ImageGalleryStateStore({ autoPlayVideo: false, giphyVersion: 'fixed_height' }),
20-
);
24+
export const ImageGalleryProvider = ({
25+
children,
26+
value,
27+
}: PropsWithChildren<{ value: ImageGalleryProviderProps }>) => {
28+
const [imageGalleryStateStore] = useState(() => new ImageGalleryStateStore(value));
2129

2230
useEffect(() => {
2331
const unsubscribe = imageGalleryStateStore.registerSubscriptions();
@@ -26,13 +34,17 @@ export const ImageGalleryProvider = ({ children }: PropsWithChildren<UnknownType
2634
};
2735
}, [imageGalleryStateStore]);
2836

37+
const imageGalleryContextValue = useMemo(
38+
() => ({
39+
autoPlayVideo: value?.autoPlayVideo,
40+
imageGalleryStateStore,
41+
}),
42+
[value?.autoPlayVideo, imageGalleryStateStore],
43+
);
44+
2945
return (
3046
<ImageGalleryContext.Provider
31-
value={
32-
{
33-
imageGalleryStateStore,
34-
} as unknown as ImageGalleryContextValue
35-
}
47+
value={imageGalleryContextValue as unknown as ImageGalleryContextValue}
3648
>
3749
{children}
3850
</ImageGalleryContext.Provider>

package/src/contexts/overlayContext/OverlayProvider.tsx

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

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

@@ -10,7 +10,10 @@ import { ImageGallery } from '../../components/ImageGallery/ImageGallery';
1010

1111
import { useStreami18n } from '../../hooks/useStreami18n';
1212

13-
import { ImageGalleryProvider } from '../imageGalleryContext/ImageGalleryContext';
13+
import {
14+
ImageGalleryProvider,
15+
ImageGalleryProviderProps,
16+
} from '../imageGalleryContext/ImageGalleryContext';
1417
import { ThemeProvider } from '../themeContext/ThemeContext';
1518
import {
1619
DEFAULT_USER_LANGUAGE,
@@ -82,6 +85,14 @@ export const OverlayProvider = (props: PropsWithChildren<OverlayProviderProps>)
8285
// eslint-disable-next-line react-hooks/exhaustive-deps
8386
}, [overlay]);
8487

88+
const imageGalleryProviderProps: ImageGalleryProviderProps = useMemo(
89+
() => ({
90+
autoPlayVideo,
91+
giphyVersion,
92+
}),
93+
[autoPlayVideo, giphyVersion],
94+
);
95+
8596
const overlayContext = {
8697
overlay,
8798
setOverlay,
@@ -91,13 +102,11 @@ export const OverlayProvider = (props: PropsWithChildren<OverlayProviderProps>)
91102
return (
92103
<TranslationProvider value={{ ...translators, userLanguage: DEFAULT_USER_LANGUAGE }}>
93104
<OverlayContext.Provider value={overlayContext}>
94-
<ImageGalleryProvider>
105+
<ImageGalleryProvider value={imageGalleryProviderProps}>
95106
<ThemeProvider style={overlayContext.style}>
96107
{children}
97108
{overlay === 'gallery' && (
98109
<ImageGallery
99-
autoPlayVideo={autoPlayVideo}
100-
giphyVersion={giphyVersion}
101110
imageGalleryCustomComponents={imageGalleryCustomComponents}
102111
imageGalleryGridHandleHeight={imageGalleryGridHandleHeight}
103112
imageGalleryGridSnapPoints={imageGalleryGridSnapPoints}

package/src/state-store/image-gallery-state-store.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class ImageGalleryStateStore {
4848
options: ImageGalleryOptions;
4949
videoPlayerPool: VideoPlayerPool;
5050

51-
constructor(options: Partial<ImageGalleryOptions>) {
51+
constructor(options: Partial<ImageGalleryOptions> = {}) {
5252
this.options = { ...INITIAL_IMAGE_GALLERY_OPTIONS, ...options };
5353
this.state = new StateStore<ImageGalleryState>(INITIAL_STATE);
5454
this.videoPlayerPool = new VideoPlayerPool();
@@ -145,33 +145,16 @@ export class ImageGalleryStateStore {
145145
asset.messageId === message?.id &&
146146
stripQueryFromUrl(asset.uri) === stripQueryFromUrl(selectedAttachmentUrl ?? ''),
147147
);
148-
console.log('index', index);
149148
this.state.partialNext({ currentIndex: index === -1 ? 0 : index });
150149
},
151150
);
152151

153152
return unsubscribe;
154153
};
155154

156-
addVideosToPool = () => {
157-
const message = this.message;
158-
const attachments = this.attachments;
159-
const videoAttachments = attachments.filter(
160-
(attachment) => attachment.type === FileTypes.Video,
161-
);
162-
videoAttachments.forEach((attachment) => {
163-
const assetId = this.getAssetId(message?.id ?? '', attachment.asset_url ?? '');
164-
this.videoPlayerPool.getOrAddPlayer({
165-
autoPlay: this.options.autoPlayVideo,
166-
id: assetId,
167-
});
168-
});
169-
};
170-
171155
registerSubscriptions = () => {
172156
const subscriptions: Unsubscribe[] = [];
173157
subscriptions.push(this.subscribeToSelectedAttachmentUrl());
174-
this.addVideosToPool();
175158

176159
return () => {
177160
subscriptions.forEach((subscription) => subscription());

package/src/state-store/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
export * from './audio-player';
21
export * from './in-app-notifications-store';
2+
export * from './audio-player';
33
export * from './audio-player-pool';
4+
export * from './video-player';
5+
export * from './video-player.pool';
6+
export * from './image-gallery-state-store';

0 commit comments

Comments
 (0)