Skip to content

Commit c4b9350

Browse files
authored
Rest of memoize remove (keybase#26354)
1 parent 3aa6454 commit c4b9350

15 files changed

Lines changed: 572 additions & 557 deletions

File tree

‎shared/chat/conversation/bot/channel-picker.tsx‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as React from 'react'
22
import * as Kb from '@/common-adapters'
33
import * as Styles from '@/styles'
44
import type * as T from '@/constants/types'
5-
import {memoize} from '@/util/memoize'
65
import {makeInsertMatcher} from '@/util/string'
76

87
type Props = {
@@ -15,24 +14,25 @@ type Props = {
1514
teamName: string
1615
}
1716

18-
const getChannels = memoize(
19-
(channelMetas: Map<T.Chat.ConversationIDKey, T.Chat.ConversationMeta>, searchText: string) => {
20-
const matcher = makeInsertMatcher(searchText)
21-
const regex = new RegExp(searchText, 'i')
22-
return [...channelMetas.values()]
23-
.filter(({channelname, description}) => {
24-
if (!searchText) {
25-
return true // no search text means show all
26-
}
27-
return (
28-
// match channel name for search as subsequence (like the identity modal)
29-
// match channel desc by strict substring (less noise in results)
30-
channelname.search(matcher) !== -1 || description.search(regex) !== -1
31-
)
32-
})
33-
.sort((a, b) => a.channelname.localeCompare(b.channelname))
34-
}
35-
)
17+
const getChannels = (
18+
channelMetas: Map<T.Chat.ConversationIDKey, T.Chat.ConversationMeta>,
19+
searchText: string
20+
) => {
21+
const matcher = makeInsertMatcher(searchText)
22+
const regex = new RegExp(searchText, 'i')
23+
return [...channelMetas.values()]
24+
.filter(({channelname, description}) => {
25+
if (!searchText) {
26+
return true // no search text means show all
27+
}
28+
return (
29+
// match channel name for search as subsequence (like the identity modal)
30+
// match channel desc by strict substring (less noise in results)
31+
channelname.search(matcher) !== -1 || description.search(regex) !== -1
32+
)
33+
})
34+
.sort((a, b) => a.channelname.localeCompare(b.channelname))
35+
}
3636

3737
const toggleChannel = (convID: string, installInConvs: ReadonlyArray<string>) => {
3838
if (installInConvs.includes(convID)) {
@@ -99,7 +99,11 @@ const ChannelPicker = (props: Props) => {
9999
setDisableDone(false)
100100
}, [allSelected, installInConvs, setDisableDone])
101101

102-
const rows = getChannels(props.channelMetas, searchText).map(meta => (
102+
const channels = React.useMemo(
103+
() => getChannels(props.channelMetas, searchText),
104+
[props.channelMetas, searchText]
105+
)
106+
const rows = channels.map(meta => (
103107
<Row
104108
disabled={allSelected}
105109
key={meta.conversationIDKey}

‎shared/chat/conversation/bot/install.tsx‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ import {useAllChannelMetas} from '@/teams/common/channel-hooks'
1010
const RestrictedItem = '---RESTRICTED---'
1111

1212
export const useBotConversationIDKey = (inConvIDKey?: T.Chat.ConversationIDKey, teamID?: T.Teams.TeamID) => {
13-
const [conversationIDKey, setConversationIDKey] = React.useState(inConvIDKey)
13+
const cleanInConvIDKey = T.Chat.isValidConversationIDKey(inConvIDKey ?? '') ? inConvIDKey : undefined
14+
const [conversationIDKey, setConversationIDKey] = React.useState(cleanInConvIDKey)
1415
const generalConvID = C.useChatState(s => teamID && s.teamIDToGeneralConvID.get(teamID))
1516
const findGeneralConvIDFromTeamID = C.useChatState(s => s.dispatch.findGeneralConvIDFromTeamID)
1617
React.useEffect(() => {
17-
if (!conversationIDKey && teamID) {
18+
if (!cleanInConvIDKey && teamID) {
1819
if (!generalConvID) {
1920
findGeneralConvIDFromTeamID(teamID)
2021
} else {
2122
setConversationIDKey(generalConvID)
2223
}
2324
}
24-
}, [conversationIDKey, findGeneralConvIDFromTeamID, generalConvID, teamID])
25+
}, [cleanInConvIDKey, findGeneralConvIDFromTeamID, generalConvID, teamID])
2526
return conversationIDKey
2627
}
2728

@@ -36,9 +37,9 @@ const InstallBotPopupLoader = (props: LoaderProps) => {
3637
const inConvIDKey = props.conversationIDKey
3738
const teamID = props.teamID
3839
const conversationIDKey = useBotConversationIDKey(inConvIDKey, teamID)
39-
if (!inConvIDKey) return null
40+
if (!conversationIDKey) return null
4041
return (
41-
<C.ChatProvider id={inConvIDKey}>
42+
<C.ChatProvider id={conversationIDKey}>
4243
<InstallBotPopup botUsername={botUsername} conversationIDKey={conversationIDKey} />
4344
</C.ChatProvider>
4445
)

‎shared/chat/conversation/bot/search.page.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type OwnProps = C.Chat.ChatProviderProps<C.ViewPropsToPageProps<typeof Search>>
77
const Screen = (p: OwnProps) => {
88
const {conversationIDKey, ...rest} = p.route.params
99
return (
10-
<C.Chat.ProviderScreen rp={p}>
10+
<C.Chat.ProviderScreen rp={p} canBeNull={true}>
1111
<Search {...rest} />
1212
</C.Chat.ProviderScreen>
1313
)

‎shared/chat/emoji-picker/index.tsx‎

Lines changed: 99 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as Kb from '@/common-adapters'
55
import debounce from 'lodash/debounce'
66
import {isMobile} from '@/constants/platform'
77
import chunk from 'lodash/chunk'
8-
import {memoize} from '@/util/memoize'
98
import {
109
emojiDataToRenderableEmoji,
1110
getEmojiStr,
@@ -39,41 +38,37 @@ const chunkEmojis = (emojis: Array<EmojiData>, emojisPerLine: number): Array<Row
3938
// same.
4039
const removeObsolete = (emojis: Array<EmojiData>) => emojis.filter(e => !e.obsoleted_by)
4140

42-
const getEmojiSections = memoize(
43-
(emojisPerLine: number): Array<Section> =>
44-
_getData().categories.map(
45-
c =>
46-
({
47-
data: chunkEmojis(removeObsolete(c.emojis), emojisPerLine),
48-
key: c.category,
49-
title: c.category,
50-
}) as const
51-
)
52-
)
41+
const getEmojiSections = (emojisPerLine: number): Array<Section> =>
42+
_getData().categories.map(
43+
c =>
44+
({
45+
data: chunkEmojis(removeObsolete(c.emojis), emojisPerLine),
46+
key: c.category,
47+
title: c.category,
48+
}) as const
49+
)
5350

54-
const getFrequentSection = memoize(
55-
(
56-
topReacjis: ReadonlyArray<T.RPCGen.UserReacji>,
57-
customEmojiGroups: ReadonlyArray<T.RPCChat.EmojiGroup>,
58-
emojisPerLine: number
59-
): Section => {
60-
const {emojiNameMap} = _getData()
61-
const customEmojiIndex = getCustomEmojiIndex(customEmojiGroups)
62-
const emojis = topReacjis.reduce<Array<EmojiData>>((arr, top) => {
63-
const shortNameNoColons = top.name.replace(/:/g, '')
64-
const emoji = emojiNameMap[shortNameNoColons] || customEmojiIndex.get(shortNameNoColons)
65-
if (emoji) {
66-
arr.push(emoji)
67-
}
68-
return arr
69-
}, [])
70-
return {
71-
data: chunkEmojis(emojis, emojisPerLine).slice(0, 4),
72-
key: 'Frequently Used',
73-
title: 'Frequently used',
51+
const getFrequentSection = (
52+
topReacjis: ReadonlyArray<T.RPCGen.UserReacji>,
53+
customEmojiGroups: ReadonlyArray<T.RPCChat.EmojiGroup>,
54+
emojisPerLine: number
55+
): Section => {
56+
const {emojiNameMap} = _getData()
57+
const customEmojiIndex = getCustomEmojiIndex(customEmojiGroups)
58+
const emojis = topReacjis.reduce<Array<EmojiData>>((arr, top) => {
59+
const shortNameNoColons = top.name.replace(/:/g, '')
60+
const emoji = emojiNameMap[shortNameNoColons] || customEmojiIndex.get(shortNameNoColons)
61+
if (emoji) {
62+
arr.push(emoji)
7463
}
64+
return arr
65+
}, [])
66+
return {
67+
data: chunkEmojis(emojis, emojisPerLine).slice(0, 4),
68+
key: 'Frequently Used',
69+
title: 'Frequently used',
7570
}
76-
)
71+
}
7772

7873
const singleEmojiWidth = isMobile ? 32 : 26
7974
const emojiPadding = 5
@@ -104,10 +99,6 @@ type Props = {
10499
waitingForEmoji?: boolean
105100
}
106101

107-
type State = {
108-
activeSectionKey: string
109-
}
110-
111102
type Bookmark = {
112103
coveredSectionKeys: ReadonlySet<string>
113104
iconType: Kb.IconType
@@ -125,16 +116,17 @@ const emojiGroupsToEmojiArrayArray = (
125116
name: emojiGroup.name,
126117
}))
127118

128-
const getCustomEmojiSections = memoize(
129-
(emojiGroups: ReadonlyArray<T.RPCChat.EmojiGroup>, emojisPerLine: number): Array<Section> =>
130-
emojiGroupsToEmojiArrayArray(emojiGroups).map(group => ({
131-
data: chunkEmojis(group.emojis, emojisPerLine),
132-
key: group.name,
133-
title: group.name,
134-
}))
135-
)
119+
const getCustomEmojiSections = (
120+
emojiGroups: ReadonlyArray<T.RPCChat.EmojiGroup>,
121+
emojisPerLine: number
122+
): Array<Section> =>
123+
emojiGroupsToEmojiArrayArray(emojiGroups).map(group => ({
124+
data: chunkEmojis(group.emojis, emojisPerLine),
125+
key: group.name,
126+
title: group.name,
127+
}))
136128

137-
const getCustomEmojiIndex = memoize((emojiGroups: ReadonlyArray<T.RPCChat.EmojiGroup>) => {
129+
const getCustomEmojiIndex = (emojiGroups: ReadonlyArray<T.RPCChat.EmojiGroup>) => {
138130
const mapper = new Map<string, EmojiData>()
139131
emojiGroupsToEmojiArrayArray(emojiGroups).forEach(emojiGroup =>
140132
emojiGroup.emojis.forEach(emoji => {
@@ -158,7 +150,7 @@ const getCustomEmojiIndex = memoize((emojiGroups: ReadonlyArray<T.RPCChat.EmojiG
158150
}, new Array<EmojiData>()),
159151
get: (shortName: string): EmojiData | undefined => mapper.get(shortName),
160152
}
161-
})
153+
}
162154
const emptyCustomEmojiIndex = {filter: () => [], get: () => undefined}
163155

164156
const getResultFilter = (emojiGroups?: ReadonlyArray<T.RPCChat.EmojiGroup>) => {
@@ -171,66 +163,64 @@ const getResultFilter = (emojiGroups?: ReadonlyArray<T.RPCChat.EmojiGroup>) => {
171163

172164
const getEmojisPerLine = (width: number) => width && Math.floor(width / emojiWidthWithPadding)
173165

174-
const getSectionsAndBookmarks = memoize(
175-
(
176-
width: number,
177-
topReacjis: ReadonlyArray<T.RPCGen.UserReacji>,
178-
hideTopReacjis: boolean,
179-
customEmojiGroups?: ReadonlyArray<T.RPCChat.EmojiGroup>
180-
) => {
181-
if (!width) {
182-
return {bookmarks: [], sections: []}
183-
}
166+
const getSectionsAndBookmarks = (
167+
width: number,
168+
topReacjis: ReadonlyArray<T.RPCGen.UserReacji>,
169+
hideTopReacjis: boolean,
170+
customEmojiGroups?: ReadonlyArray<T.RPCChat.EmojiGroup>
171+
) => {
172+
if (!width) {
173+
return {bookmarks: [], sections: []}
174+
}
175+
176+
const emojisPerLine = getEmojisPerLine(width)
177+
const sections: Array<Section> = []
178+
const bookmarks: Array<Bookmark> = []
184179

185-
const emojisPerLine = getEmojisPerLine(width)
186-
const sections: Array<Section> = []
187-
const bookmarks: Array<Bookmark> = []
180+
if (topReacjis.length && !hideTopReacjis) {
181+
const frequentSection = getFrequentSection(topReacjis, customEmojiGroups || emptyArray, emojisPerLine)
182+
bookmarks.push({
183+
coveredSectionKeys: new Set([frequentSection.key]),
184+
iconType: 'iconfont-clock',
185+
sectionIndex: sections.length,
186+
})
187+
sections.push(frequentSection)
188+
}
188189

189-
if (topReacjis.length && !hideTopReacjis) {
190-
const frequentSection = getFrequentSection(topReacjis, customEmojiGroups || emptyArray, emojisPerLine)
190+
getEmojiSections(emojisPerLine).forEach(section => {
191+
const cat = Data.categoryIcons as {[key: string]: Kb.IconType}
192+
const categoryIcon = cat[section.title]
193+
categoryIcon &&
191194
bookmarks.push({
192-
coveredSectionKeys: new Set([frequentSection.key]),
193-
iconType: 'iconfont-clock',
195+
coveredSectionKeys: new Set([section.key]),
196+
iconType: categoryIcon,
194197
sectionIndex: sections.length,
195198
})
196-
sections.push(frequentSection)
197-
}
199+
sections.push(section)
200+
})
198201

199-
getEmojiSections(emojisPerLine).forEach(section => {
200-
const cat = Data.categoryIcons as {[key: string]: Kb.IconType}
201-
const categoryIcon = cat[section.title]
202-
categoryIcon &&
203-
bookmarks.push({
204-
coveredSectionKeys: new Set([section.key]),
205-
iconType: categoryIcon,
206-
sectionIndex: sections.length,
207-
})
202+
if (customEmojiGroups?.length) {
203+
const coveredSectionKeys = new Set<string>()
204+
getCustomEmojiSections(customEmojiGroups, emojisPerLine).forEach(section => {
205+
coveredSectionKeys.add(section.key)
208206
sections.push(section)
209207
})
208+
const bookmark = {
209+
coveredSectionKeys,
210+
iconType: 'iconfont-keybase',
211+
sectionIndex: sections.length,
212+
} as Bookmark
213+
bookmarks.push(bookmark)
214+
}
210215

211-
if (customEmojiGroups?.length) {
212-
const coveredSectionKeys = new Set<string>()
213-
getCustomEmojiSections(customEmojiGroups, emojisPerLine).forEach(section => {
214-
coveredSectionKeys.add(section.key)
215-
sections.push(section)
216-
})
217-
const bookmark = {
218-
coveredSectionKeys,
219-
iconType: 'iconfont-keybase',
220-
sectionIndex: sections.length,
221-
} as Bookmark
222-
bookmarks.push(bookmark)
223-
}
224-
225-
sections.push({
226-
data: [],
227-
key: 'not-found',
228-
title: 'not-found',
229-
})
216+
sections.push({
217+
data: [],
218+
key: 'not-found',
219+
title: 'not-found',
220+
})
230221

231-
return {bookmarks, sections}
232-
}
233-
)
222+
return {bookmarks, sections}
223+
}
234224

235225
const EmojiRow = React.memo(function EmojiRow(p: {
236226
row: Row
@@ -246,12 +236,17 @@ const EmojiRow = React.memo(function EmojiRow(p: {
246236
)
247237
})
248238

249-
class EmojiPicker extends React.PureComponent<Props, State> {
250-
state = {activeSectionKey: ''}
239+
const EmojiPicker = React.memo(function EmojiPicker(p: Props) {
240+
const [activeSectionKey, setActiveSectionKey] = React.useState('')
241+
const props = {...p, activeSectionKey, setActiveSectionKey}
242+
return <EmojiPicker2 {...props} />
243+
})
251244

252-
private mounted = true
245+
class EmojiPicker2 extends React.PureComponent<
246+
Props & {activeSectionKey: string; setActiveSectionKey: (s: string) => void}
247+
> {
253248
componentWillUnmount() {
254-
this.mounted = false
249+
this.onSectionChange.cancel()
255250
}
256251

257252
private getEmojiSingle = (emoji: EmojiData, skinTone?: T.Chat.EmojiSkinTone) => {
@@ -284,8 +279,8 @@ class EmojiPicker extends React.PureComponent<Props, State> {
284279
const content = (
285280
<Kb.Box2 key="bookmark" direction="horizontal" fullWidth={true} style={styles.bookmarkContainer}>
286281
{bookmarks.map((bookmark, bookmarkIndex) => {
287-
const isActive = this.state.activeSectionKey
288-
? bookmark.coveredSectionKeys.has(this.state.activeSectionKey)
282+
const isActive = this.props.activeSectionKey
283+
? bookmark.coveredSectionKeys.has(this.props.activeSectionKey)
289284
: bookmarkIndex === 0
290285
return (
291286
<Kb.Box
@@ -325,10 +320,7 @@ class EmojiPicker extends React.PureComponent<Props, State> {
325320
</Kb.Box2>
326321
)
327322

328-
private onSectionChange = debounce(
329-
(section: Section) => this.mounted && this.setState({activeSectionKey: section.key}),
330-
200
331-
)
323+
private onSectionChange = debounce((section: Section) => this.props.setActiveSectionKey(section.key), 200)
332324

333325
private makeNotFound = () => (
334326
<Kb.Box2 direction="vertical" fullWidth={true} centerChildren={true} style={styles.notFoundContainer}>

0 commit comments

Comments
 (0)