@@ -5,7 +5,6 @@ import * as Kb from '@/common-adapters'
55import debounce from 'lodash/debounce'
66import { isMobile } from '@/constants/platform'
77import chunk from 'lodash/chunk'
8- import { memoize } from '@/util/memoize'
98import {
109 emojiDataToRenderableEmoji ,
1110 getEmojiStr ,
@@ -39,41 +38,37 @@ const chunkEmojis = (emojis: Array<EmojiData>, emojisPerLine: number): Array<Row
3938// same.
4039const 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
7873const singleEmojiWidth = isMobile ? 32 : 26
7974const emojiPadding = 5
@@ -104,10 +99,6 @@ type Props = {
10499 waitingForEmoji ?: boolean
105100}
106101
107- type State = {
108- activeSectionKey : string
109- }
110-
111102type 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+ }
162154const emptyCustomEmojiIndex = { filter : ( ) => [ ] , get : ( ) => undefined }
163155
164156const getResultFilter = ( emojiGroups ?: ReadonlyArray < T . RPCChat . EmojiGroup > ) => {
@@ -171,66 +163,64 @@ const getResultFilter = (emojiGroups?: ReadonlyArray<T.RPCChat.EmojiGroup>) => {
171163
172164const 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
235225const 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