Skip to content

Commit 696cccb

Browse files
authored
Merge pull request #530 from GetStream/reduce-expensive-merge
Reuse expensive merge in theme
2 parents 79e4151 + 4d7c41b commit 696cccb

File tree

3 files changed

+73
-42
lines changed

3 files changed

+73
-42
lines changed

src/components/MessageList/MessageList.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef, useState } from 'react';
1+
import React, { useEffect, useMemo, useRef, useState } from 'react';
22
import {
33
ActivityIndicator,
44
FlatListProps,
@@ -42,6 +42,7 @@ import {
4242
useThreadContext,
4343
} from '../../contexts/threadContext/ThreadContext';
4444
import {
45+
mergeThemes,
4546
ThemeProvider,
4647
useTheme,
4748
} from '../../contexts/themeContext/ThemeContext';
@@ -295,12 +296,17 @@ const MessageListWithContext = <
295296
TypingIndicatorContainer,
296297
} = props;
297298

299+
const { theme } = useTheme();
300+
298301
const {
299-
theme: {
300-
colors: { accent_blue, white_snow },
301-
messageList: { container, listContainer },
302-
},
303-
} = useTheme();
302+
colors: { accent_blue, white_snow },
303+
messageList: { container, listContainer },
304+
} = theme;
305+
306+
const modifiedTheme = useMemo(
307+
() => mergeThemes({ style: myMessageTheme, theme }),
308+
[myMessageTheme, theme],
309+
);
304310

305311
const messageList = useMessageList<At, Ch, Co, Ev, Me, Re, Us>({
306312
inverted,
@@ -507,7 +513,7 @@ const MessageListWithContext = <
507513
if (wrapMessageInTheme) {
508514
return (
509515
<>
510-
<ThemeProvider style={myMessageTheme}>
516+
<ThemeProvider mergedStyle={modifiedTheme}>
511517
<Message
512518
goToMessage={goToMessage}
513519
groupStyles={

src/components/MessageOverlay/MessageOverlay.tsx

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React, { useEffect, useMemo, useRef, useState } from 'react';
22
import {
33
Keyboard,
44
Platform,
@@ -43,6 +43,7 @@ import {
4343
useOverlayContext,
4444
} from '../../contexts/overlayContext/OverlayContext';
4545
import {
46+
mergeThemes,
4647
ThemeProvider,
4748
useTheme,
4849
} from '../../contexts/themeContext/ThemeContext';
@@ -145,24 +146,40 @@ const MessageOverlayWithContext = <
145146
visible,
146147
} = props;
147148

149+
const { theme } = useTheme();
150+
148151
const {
149-
theme: {
150-
colors: {
151-
blue_alice,
152-
grey_gainsboro,
153-
grey_whisper,
154-
transparent,
155-
white_smoke,
156-
},
157-
messageSimple: {
158-
content: {
159-
container: { borderRadiusL, borderRadiusS },
160-
containerInner,
161-
replyContainer,
162-
},
152+
colors: {
153+
blue_alice,
154+
grey_gainsboro,
155+
grey_whisper,
156+
transparent,
157+
white_smoke,
158+
},
159+
messageSimple: {
160+
content: {
161+
container: { borderRadiusL, borderRadiusS },
162+
containerInner,
163+
replyContainer,
163164
},
164165
},
165-
} = useTheme();
166+
} = theme;
167+
168+
const myMessageTheme = messagesContext?.myMessageTheme;
169+
const [myMessageThemeString, setMyMessageThemeString] = useState(
170+
JSON.stringify(myMessageTheme),
171+
);
172+
173+
useEffect(() => {
174+
if (myMessageTheme) {
175+
setMyMessageThemeString(JSON.stringify(myMessageTheme));
176+
}
177+
}, [myMessageTheme]);
178+
179+
const modifiedTheme = useMemo(
180+
() => mergeThemes({ style: myMessageTheme, theme }),
181+
[myMessageThemeString, theme],
182+
);
166183

167184
const scrollViewRef = useRef<ScrollView>(null);
168185

@@ -312,20 +329,14 @@ const MessageOverlayWithContext = <
312329

313330
const hasThreadReplies = !!message?.reply_count;
314331

315-
const {
316-
Attachment,
317-
FileAttachmentGroup,
318-
Gallery,
319-
MessageAvatar,
320-
myMessageTheme,
321-
Reply,
322-
} = messagesContext || {};
332+
const { Attachment, FileAttachmentGroup, Gallery, MessageAvatar, Reply } =
333+
messagesContext || {};
323334

324335
const wrapMessageInTheme = clientId === message?.user?.id && !!myMessageTheme;
325336

326337
return (
327338
<MessagesProvider<At, Ch, Co, Ev, Me, Re, Us> value={messagesContext}>
328-
<ThemeProvider style={wrapMessageInTheme ? myMessageTheme : undefined}>
339+
<ThemeProvider mergedStyle={wrapMessageInTheme ? modifiedTheme : theme}>
329340
<Animated.View
330341
pointerEvents={visible ? 'auto' : 'none'}
331342
style={StyleSheet.absoluteFillObject}

src/contexts/themeContext/ThemeContext.tsx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,40 @@ export type DeepPartial<T> = {
88
};
99

1010
export type ThemeProviderInputValue = {
11+
mergedStyle?: Theme;
1112
style?: DeepPartial<Theme>;
1213
};
1314

15+
export type MergedThemesParams = {
16+
style?: DeepPartial<Theme>;
17+
theme?: Theme;
18+
};
19+
20+
export const mergeThemes = (params: MergedThemesParams) => {
21+
const { style, theme } = params;
22+
const finalTheme = (!theme || Object.keys(theme).length === 0
23+
? JSON.parse(JSON.stringify(defaultTheme))
24+
: JSON.parse(JSON.stringify(theme))) as Theme;
25+
26+
if (style) {
27+
merge(finalTheme, style);
28+
}
29+
30+
return finalTheme;
31+
};
32+
1433
export const ThemeContext = React.createContext({} as Theme);
1534

1635
export const ThemeProvider: React.FC<ThemeProviderInputValue> = (props) => {
17-
const { children, style } = props;
36+
const { children, mergedStyle, style } = props;
1837
const { theme } = useTheme();
1938
const modifiedTheme = useMemo(() => {
20-
const finalTheme =
21-
Object.keys(theme).length === 0
22-
? JSON.parse(JSON.stringify(defaultTheme))
23-
: JSON.parse(JSON.stringify(theme));
24-
25-
if (style) {
26-
merge(finalTheme, style);
39+
if (mergedStyle) {
40+
return mergedStyle;
2741
}
2842

29-
return finalTheme;
30-
}, [theme, style]);
43+
return mergeThemes({ style, theme });
44+
}, [mergedStyle, style, theme]);
3145

3246
return (
3347
<ThemeContext.Provider value={modifiedTheme}>

0 commit comments

Comments
 (0)