Skip to content

Commit 61fa097

Browse files
authored
feat: remove StreamChatRN in favour of a global context ChatConfigContext that allows providing global values (#2703)
1 parent 8b0b4f4 commit 61fa097

File tree

24 files changed

+158
-92
lines changed

24 files changed

+158
-92
lines changed

docusaurus/docs/reactnative/basics/migrating-from-5.x-to-6.x.mdx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,36 @@ The Message Menu design has been revamped to provide a better user experience. T
9090
The previous overlay design has been replaced with a bottom sheet modal design.
9191
:::
9292

93+
#### Remove `StreamChatRN` and introduce `ChatConfigContext`
94+
95+
The `StreamChatRN` global config is removed in favour of the `ChatConfigContext`. The `ChatConfigContext` is a more versatile and feature-rich context that can be used to provide any global configuration to the chat application.
96+
97+
The `resizableCDNHosts` can be configured by wrapping the root of your Chat application with `ChatConfigProvider` as follows:
98+
99+
```tsx
100+
import { Chat, ChatConfigProvider, OverlayProvider } from 'stream-chat-react-native';
101+
102+
const App = () => {
103+
return (
104+
<ChatConfigProvider
105+
value={{
106+
resizableCDNHosts: ['cdn.example.com'],
107+
}}
108+
>
109+
<OverlayProvider>
110+
<Chat client={client}>
111+
<ChannelList />
112+
</Chat>
113+
</OverlayProvider>
114+
</ChatConfigProvider>
115+
);
116+
};
117+
```
118+
119+
:::note
120+
If nothing is provided, the `resizableCDNHosts` will default to `['.stream-io-cdn.com']`.
121+
:::
122+
93123
#### Removed `MessageOverlayContext` and `MessageOverlayProvider`
94124

95125
The `MessageOverlayContext` and `MessageOverlayProvider` have been removed.

docusaurus/docs/reactnative/contexts/chat-context.mdx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,5 @@ Array of users muted by current user.
6161
| ----- |
6262
| array |
6363

64-
### `resizableCDNHosts`
65-
66-
This option allows you to specify a list of CDNs that offer image resizing.
67-
68-
| Type | Default |
69-
| ---------- | ------------------------ |
70-
| `string[]` | `['.stream-io-cdn.com']` |
71-
7264
<!-- ### setActiveChannel -->
7365
<!-- ### channel -->

docusaurus/docs/reactnative/core-components/chat.mdx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ Themes are not hoisted though, therefore a theme provided to `Chat` will not cha
110110
| ------ |
111111
| Object |
112112

113-
### `resizableCDNHosts`
114-
115-
This option allows you to specify a list of CDNs that offer image resizing.
116-
117-
| Type | Default |
118-
| ---------- | ------------------------ |
119-
| `string[]` | `['.stream-io-cdn.com']` |
120-
121113
## UI Component Props
122114

123115
### `ImageComponent`

docusaurus/docs/reactnative/customization/global_config.mdx

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,42 @@ title: Global Config
55
---
66

77
The core of Stream Chat for React Native is it's ability to be customized to your needs.
8-
The new `Global Config` feature allows you to enable and disable features of the library and alter the default behavior of the SDK.
9-
10-
:::info
11-
This will be not be the recommended way of setting the config in the next major version of the SDK that is v6.
12-
13-
Use the `resizableCDNHosts` prop in the [`Chat`](../core-components/chat.mdx) component instead.
14-
:::
8+
The new `Global Chat Config` feature allows you to enable and disable features of the library and alter the default behavior of the SDK.
159

1610
## When to use global config
1711

18-
When you want to alter the default behavior of the SDK or it's components.
19-
Please check the available options below to see if there is a config option that fits your needs.
12+
When you want to alter the default behavior of the SDK such as changing the CDN hosts.
2013

2114
## How to use and alter the global config
2215

23-
Altering the global config can be done by calling the `setConfig` function on the `StreamChatRN` object.
24-
Make sure to call this function before any other components are rendered at the root of your application (App.tsx in most cases).
16+
The `ChatConfigProvider` is a context provider that can be used to provide global configuration to the chat application.
2517

26-
```tsx
27-
import { StreamChatRN } from 'stream-chat-react-native';
18+
The `resizableCDNHosts` can be configured by wrapping the root of your Chat application with `ChatConfigProvider` as follows:
2819

29-
StreamChatRN.setConfig({
30-
resizableCDNHosts: ['https://my-company-cdn.com'],
31-
// future config options
32-
});
20+
```tsx
21+
import { Chat, ChatConfigProvider, OverlayProvider } from 'stream-chat-react-native';
22+
23+
const App = () => {
24+
return (
25+
<ChatConfigProvider
26+
value={{
27+
resizableCDNHosts: ['cdn.example.com'],
28+
}}
29+
>
30+
<OverlayProvider>
31+
<Chat client={client}>
32+
<ChannelList />
33+
</Chat>
34+
</OverlayProvider>
35+
</ChatConfigProvider>
36+
);
37+
};
3338
```
3439

40+
:::note
41+
If nothing is provided, the `resizableCDNHosts` will default to `['.stream-io-cdn.com']`.
42+
:::
43+
3544
## Available config features/Options
3645

3746
### `resizableCDNHosts`

package/src/components/Attachment/Gallery.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getGalleryImageBorderRadius } from './utils/getGalleryImageBorderRadius
1313
import { openUrlSafely } from './utils/openUrlSafely';
1414

1515
import type { MessageType } from '../../components/MessageList/hooks/useMessageList';
16+
import { useChatConfigContext } from '../../contexts/chatConfigContext/ChatConfigContext';
1617
import {
1718
ImageGalleryContextValue,
1819
useImageGalleryContext,
@@ -104,6 +105,7 @@ const GalleryWithContext = <
104105
VideoThumbnail,
105106
} = props;
106107

108+
const { resizableCDNHosts } = useChatConfigContext();
107109
const {
108110
theme: {
109111
messageSimple: {
@@ -139,6 +141,7 @@ const GalleryWithContext = <
139141
() =>
140142
buildGallery({
141143
images: imagesAndVideos,
144+
resizableCDNHosts,
142145
sizeConfig,
143146
}),
144147
// eslint-disable-next-line react-hooks/exhaustive-deps

package/src/components/Attachment/utils/buildGallery/buildGallery.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import type { GallerySizeAndThumbnailGrid, GallerySizeConfig } from './types';
1010

1111
import type { DefaultStreamChatGenerics } from '../../../../types/types';
1212

13+
import {
14+
ChatConfigContextValue,
15+
chatConfigContextDefaultvalue,
16+
} from '../../../../contexts/chatConfigContext/ChatConfigContext';
17+
1318
/**
1419
* Builds and returns a gallery of optimized images to be rendered on UI.
1520
* This function take a object parameter with following properties:
@@ -30,28 +35,32 @@ export function buildGallery<
3035
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
3136
>({
3237
images,
38+
resizableCDNHosts = chatConfigContextDefaultvalue.resizableCDNHosts,
3339
sizeConfig,
34-
}: {
40+
}: Pick<ChatConfigContextValue, 'resizableCDNHosts'> & {
3541
images: Attachment<StreamChatGenerics>[];
3642
sizeConfig: GallerySizeConfig;
3743
}): GallerySizeAndThumbnailGrid {
3844
if (images.length === 1) {
3945
return buildGalleryOfSingleImage({
4046
image: images[0],
47+
resizableCDNHosts,
4148
sizeConfig,
4249
});
4350
}
4451

4552
if (images.length === 2) {
4653
return buildGalleryOfTwoImages({
4754
images,
55+
resizableCDNHosts,
4856
sizeConfig,
4957
});
5058
}
5159

5260
if (images.length === 3) {
5361
return buildGalleryOfThreeImages({
5462
images,
63+
resizableCDNHosts,
5564
sizeConfig,
5665
});
5766
}
@@ -72,6 +81,7 @@ export function buildGallery<
7281
],
7382
images: images.slice(0, 4),
7483
invertedDirections: true,
84+
resizableCDNHosts,
7585
sizeConfig,
7686
});
7787
}

package/src/components/Attachment/utils/buildGallery/buildGalleryOfSingleImage.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { GallerySizeAndThumbnailGrid, GallerySizeConfig } from './types';
66

77
import type { DefaultStreamChatGenerics } from '../../../../types/types';
88
import { getAspectRatio } from '../getAspectRatio';
9+
import { ChatConfigContextValue } from '../../../../contexts/chatConfigContext/ChatConfigContext';
910

1011
/**
1112
* Bound a number to a range.
@@ -67,8 +68,9 @@ export function buildGalleryOfSingleImage<
6768
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
6869
>({
6970
image,
71+
resizableCDNHosts,
7072
sizeConfig,
71-
}: {
73+
}: Pick<ChatConfigContextValue, 'resizableCDNHosts'> & {
7274
image: Attachment<StreamChatGenerics>;
7375
sizeConfig: GallerySizeConfig;
7476
}): GallerySizeAndThumbnailGrid {
@@ -79,6 +81,7 @@ export function buildGalleryOfSingleImage<
7981

8082
const thumbnail = buildThumbnail({
8183
image,
84+
resizableCDNHosts,
8285
...container,
8386
});
8487

package/src/components/Attachment/utils/buildGallery/buildGalleryOfThreeImages.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { GallerySizeAndThumbnailGrid, GallerySizeConfig } from './types';
66

77
import type { DefaultStreamChatGenerics } from '../../../../types/types';
88
import { getAspectRatio } from '../getAspectRatio';
9+
import { ChatConfigContextValue } from '../../../../contexts/chatConfigContext/ChatConfigContext';
910

1011
/** function to move item to the front of the array */
1112
function moveToFront<T>(array: T[], item: T): T[] {
@@ -23,8 +24,9 @@ export function buildGalleryOfThreeImages<
2324
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2425
>({
2526
images,
27+
resizableCDNHosts,
2628
sizeConfig,
27-
}: {
29+
}: Pick<ChatConfigContextValue, 'resizableCDNHosts'> & {
2830
images: Attachment<StreamChatGenerics>[];
2931
sizeConfig: GallerySizeConfig;
3032
}): GallerySizeAndThumbnailGrid {
@@ -77,6 +79,7 @@ export function buildGalleryOfThreeImages<
7779
grid: [[1], [1, 1]],
7880
images: landscapeImage ? moveToFront(images, landscapeImage) : images,
7981
invertedDirections: true,
82+
resizableCDNHosts,
8083
sizeConfig,
8184
});
8285
} else {
@@ -93,6 +96,7 @@ export function buildGalleryOfThreeImages<
9396
grid: [[1], [1, 1]],
9497
images: portraitImage ? moveToFront(images, portraitImage) : images,
9598
invertedDirections: false,
99+
resizableCDNHosts,
96100
sizeConfig,
97101
});
98102
}

package/src/components/Attachment/utils/buildGallery/buildGalleryOfTwoImages.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import type { GallerySizeAndThumbnailGrid, GallerySizeConfig } from './types';
66

77
import type { DefaultStreamChatGenerics } from '../../../../types/types';
88
import { getAspectRatio } from '../getAspectRatio';
9+
import { ChatConfigContextValue } from '../../../../contexts/chatConfigContext/ChatConfigContext';
910

1011
export function buildGalleryOfTwoImages<
1112
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
1213
>({
1314
images,
15+
resizableCDNHosts,
1416
sizeConfig,
15-
}: {
17+
}: Pick<ChatConfigContextValue, 'resizableCDNHosts'> & {
1618
images: Attachment<StreamChatGenerics>[];
1719
sizeConfig: GallerySizeConfig;
1820
}): GallerySizeAndThumbnailGrid {
@@ -36,6 +38,7 @@ export function buildGalleryOfTwoImages<
3638
grid: [[1], [1]],
3739
images,
3840
invertedDirections: true,
41+
resizableCDNHosts,
3942
sizeConfig,
4043
});
4144
}
@@ -53,6 +56,7 @@ export function buildGalleryOfTwoImages<
5356
grid: [[1, 1]],
5457
images,
5558
invertedDirections: true,
59+
resizableCDNHosts,
5660
sizeConfig,
5761
});
5862
}
@@ -61,6 +65,7 @@ export function buildGalleryOfTwoImages<
6165
grid: [[2, 1]],
6266
images: isLandscape1 ? images : images.reverse(),
6367
invertedDirections: true,
68+
resizableCDNHosts,
6469
sizeConfig,
6570
});
6671
}

package/src/components/Attachment/utils/buildGallery/buildThumbnail.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@ import type { DefaultStreamChatGenerics } from '../../../../types/types';
88

99
import { getResizedImageUrl } from '../../../../utils/getResizedImageUrl';
1010
import { getUrlOfImageAttachment } from '../../../../utils/getUrlOfImageAttachment';
11+
import { ChatConfigContextValue } from '../../../../contexts/chatConfigContext/ChatConfigContext';
12+
13+
export type BuildThumbnailProps<
14+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
15+
> = Pick<ChatConfigContextValue, 'resizableCDNHosts'> & {
16+
height: number;
17+
image: Attachment<StreamChatGenerics>;
18+
width: number;
19+
resizeMode?: ImageResizeMode;
20+
};
1121

1222
export function buildThumbnail<
1323
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
1424
>({
1525
height,
1626
image,
27+
resizableCDNHosts,
1728
resizeMode,
1829
width,
19-
}: {
20-
height: number;
21-
image: Attachment<StreamChatGenerics>;
22-
width: number;
23-
resizeMode?: ImageResizeMode;
24-
}): Thumbnail {
30+
}: BuildThumbnailProps<StreamChatGenerics>): Thumbnail {
2531
const { original_height: originalImageHeight, original_width: originalImageWidth } = image;
2632

2733
// Only resize if the original image is larger than the thumbnail container size.
@@ -41,6 +47,7 @@ export function buildThumbnail<
4147
url: shouldResize
4248
? getResizedImageUrl({
4349
height,
50+
resizableCDNHosts,
4451
url: imageUrl,
4552
width,
4653
})

0 commit comments

Comments
 (0)