Skip to content

Commit 2ca048c

Browse files
authored
chat fixes and reduce some memory pressure (keybase#26294)
1 parent f88a7ae commit 2ca048c

14 files changed

Lines changed: 204 additions & 229 deletions

File tree

‎shared/chat/audio/audio-player.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import {formatAudioRecordDuration} from '@/util/timestamp'
66
import {isMobile} from '@/constants/platform'
77

88
type VisProps = {
9-
amps: ReadonlyArray<number>
9+
amps: undefined | ReadonlyArray<number>
1010
playedRatio: number
1111
height: number
1212
maxWidth?: number
1313
}
1414

1515
const AudioVis = (props: VisProps) => {
1616
const {amps, playedRatio, maxWidth} = props
17-
let threshold = Math.floor(amps.length * playedRatio)
17+
let threshold = Math.floor((amps?.length ?? 0) * playedRatio)
1818
if (threshold > 0) {
1919
threshold += 2 // metering actually lags so compensate while actually playing
2020
}
2121

2222
let maxHeight = 0
23-
const content = amps.map((inamp, index) => {
23+
const content = amps?.map((inamp, index) => {
2424
const amp = isNaN(inamp) || inamp < 0 ? 0 : inamp
2525
const prop = Math.min(1.0, Math.max(Math.sqrt(amp), 0.05))
2626
const height = Math.floor(prop * props.height)
@@ -64,7 +64,7 @@ type Props = {
6464
duration: number
6565
maxWidth?: number
6666
url: string
67-
visAmps: ReadonlyArray<number>
67+
visAmps: undefined | ReadonlyArray<number>
6868
}
6969

7070
const AudioPlayer = (props: Props) => {

‎shared/chat/conversation/info-panel/attachments.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ export const useAttachmentSections = (
546546
onClick: () => onMediaClick(m),
547547
previewURL: m.previewURL,
548548
typ:
549-
m.audioAmps.length > 0
549+
(m.audioAmps?.length ?? 0) > 0
550550
? ThumbTyp.AUDIO
551551
: m.videoDuration
552552
? ThumbTyp.VIDEO
@@ -630,7 +630,7 @@ export const useAttachmentSections = (
630630
if (m.type !== 'text') {
631631
return l
632632
}
633-
if (!m.unfurls.size) {
633+
if (!m.unfurls?.size) {
634634
l.push({
635635
author: m.author,
636636
ctime: m.timestamp,

‎shared/chat/conversation/list-area/index.desktop.tsx‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,13 @@ const useScrolling = (p: {
135135
}) => {
136136
const conversationIDKey = C.useChatContext(s => s.id)
137137
const {listRef, containsLatestMessage, messageOrdinals, centeredOrdinal} = p
138+
const numOrdinals = messageOrdinals.length
138139
const editingOrdinal = C.useChatContext(s => s.editing)
139140
const loadNewerMessagesDueToScroll = C.useChatContext(s => s.dispatch.loadNewerMessagesDueToScroll)
140-
const newestOrdinal = messageOrdinals[messageOrdinals.length - 1] ?? T.Chat.numberToOrdinal(-1)
141-
const oldestOrdinal = messageOrdinals[0] ?? T.Chat.numberToOrdinal(-1)
142141
const loadNewerMessages = C.useThrottledCallback(
143142
React.useCallback(() => {
144-
loadNewerMessagesDueToScroll(newestOrdinal)
145-
}, [loadNewerMessagesDueToScroll, newestOrdinal]),
143+
loadNewerMessagesDueToScroll(numOrdinals)
144+
}, [loadNewerMessagesDueToScroll, numOrdinals]),
146145
200
147146
)
148147
const conversationIDKeyChanged = C.Chat.useCIDChanged(conversationIDKey)
@@ -179,7 +178,7 @@ const useScrolling = (p: {
179178
const list = listRef.current
180179
if (list) {
181180
if (list.scrollTop < listEdgeSlopTop) {
182-
loadOlderMessages(oldestOrdinal)
181+
loadOlderMessages(numOrdinals)
183182
} else if (
184183
!containsLatestMessage &&
185184
!isLockedToBottom() &&
@@ -188,7 +187,7 @@ const useScrolling = (p: {
188187
loadNewerMessages()
189188
}
190189
}
191-
}, [listRef, containsLatestMessage, loadNewerMessages, loadOlderMessages, isLockedToBottom, oldestOrdinal]) //,
190+
}, [listRef, containsLatestMessage, loadNewerMessages, loadOlderMessages, isLockedToBottom, numOrdinals])
192191

193192
const scrollToBottom = React.useCallback(() => {
194193
lockedToBottomRef.current = true

‎shared/chat/conversation/list-area/index.native.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const useScrolling = (p: {
3535
conversationIDKey: T.Chat.ConversationIDKey
3636
listRef: React.MutableRefObject</*FlashList<ItemType> |*/ FlatList<ItemType> | null>
3737
}) => {
38-
const {messageOrdinals, cidChanged, listRef, centeredOrdinal} = p
39-
const oldestOrdinal = messageOrdinals.at(-1) ?? T.Chat.numberToOrdinal(-1)
38+
const {cidChanged, listRef, centeredOrdinal, messageOrdinals} = p
39+
const numOrdinals = messageOrdinals.length
4040
const loadOlderMessages = C.useChatContext(s => s.dispatch.loadOlderMessagesDueToScroll)
4141
const scrollToBottom = React.useCallback(() => {
4242
listRef.current?.scrollToOffset({animated: false, offset: 0})
@@ -67,8 +67,8 @@ const useScrolling = (p: {
6767
})
6868

6969
const onEndReached = React.useCallback(() => {
70-
loadOlderMessages(oldestOrdinal)
71-
}, [loadOlderMessages, oldestOrdinal])
70+
loadOlderMessages(numOrdinals)
71+
}, [loadOlderMessages, numOrdinals])
7272

7373
return {
7474
onEndReached,

‎shared/chat/conversation/messages/message-popup/text.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const PopText = (ownProps: OwnProps) => {
3333
return `created a new team repository called ${m.repo}`
3434
case T.RPCGen.GitPushType.default:
3535
return m.refs
36-
.map(ref => {
36+
?.map(ref => {
3737
const commits =
3838
ref.commits?.map(
3939
c =>

‎shared/chat/conversation/messages/reaction-tooltip.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ const ReactionTooltip = (p: OwnProps) => {
6464
}))
6565
}, [navigateAppend, onHidden, ordinal])
6666

67-
let reactions = [..._reactions.keys()]
67+
let reactions = [...(_reactions?.keys() ?? [])]
6868
.map(emoji => ({
6969
emoji,
70-
users: [...(_reactions.get(emoji)?.users ?? new Set())]
70+
users: [...(_reactions?.get(emoji)?.users ?? new Set())]
7171
// Earliest users go at the top
7272
.sort((a, b) => a.timestamp - b.timestamp)
7373
.map(r => ({

‎shared/chat/conversation/messages/system-added-to-team/index.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {indefiniteArticle} from '@/util/string'
88
type Props = {
99
addee: string
1010
adder: string
11-
bulkAdds: ReadonlyArray<string>
11+
bulkAdds?: ReadonlyArray<string>
1212
role: T.Teams.MaybeTeamRoleType
1313
onManageNotifications: () => void
1414
onViewBot: () => void
@@ -77,7 +77,7 @@ const AddedToTeam = (props: Props) => {
7777
<UserNotice>
7878
<Kb.Text type="BodySmall">
7979
{youOrUsername({capitalize: true, username: props.adder, you: props.you})}added{' '}
80-
{getAddedUsernames(props.bulkAdds.length === 0 ? [props.addee] : props.bulkAdds)}
80+
{getAddedUsernames(props.bulkAdds?.length ? props.bulkAdds : [props.addee])}
8181
{props.isTeam && ' to the team'}
8282
{role && ` as ${indefiniteArticle(role)} ${role}`}. <ManageComponent {...props} />
8383
</Kb.Text>

‎shared/chat/conversation/messages/system-git-push/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const GitPush = React.memo(function GitPush(p: Props) {
103103
case 'default':
104104
return (
105105
<>
106-
{refs.map(ref => {
106+
{refs?.map(ref => {
107107
const branchName = C.Chat.systemGitBranchName(ref)
108108
return (
109109
<GitPushCommon key={branchName}>

‎shared/chat/conversation/messages/system-joined/container.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const JoinedContainer = React.memo(function JoinedContainer(p: OwnProps) {
2929
}
3030

3131
const joiners2 = React.useMemo(() => {
32-
return !joiners.length && !leavers.length ? [author] : joiners
32+
return !joiners?.length && !leavers?.length ? [author] : joiners
3333
}, [joiners, leavers, author])
3434

3535
const props = {

‎shared/chat/conversation/messages/system-joined/index.tsx‎

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,57 @@ type Props = {
88
channelname: string
99
isAdHoc: boolean
1010
isBigTeam: boolean
11-
joiners: ReadonlyArray<string>
12-
leavers: ReadonlyArray<string>
11+
joiners?: ReadonlyArray<string>
12+
leavers?: ReadonlyArray<string>
1313
onAuthorClick: (username: string) => void
1414
onManageChannels: () => void
1515
onManageNotifications: () => void
1616
teamname: string
1717
timestamp: number
1818
}
1919

20+
const none = new Array<string>()
2021
const Joined = (props: Props) => (
2122
<Kb.Box2 direction="vertical" style={styles.container} fullWidth={true} alignSelf="flex-start">
22-
{props.joiners.length ? <MultiUserJoinedNotice {...props} joiners={props.joiners} leavers={[]} /> : null}
23-
{props.leavers.length ? <MultiUserJoinedNotice {...props} joiners={[]} leavers={props.leavers} /> : null}
23+
{props.joiners?.length ? (
24+
<MultiUserJoinedNotice {...props} joiners={props.joiners} leavers={none} />
25+
) : null}
26+
{props.leavers?.length ? (
27+
<MultiUserJoinedNotice {...props} joiners={none} leavers={props.leavers} />
28+
) : null}
2429
</Kb.Box2>
2530
)
2631

27-
const MultiUserJoinedNotice = (props: Props) => (
28-
<Kb.Box2 direction="vertical" fullWidth={true} alignSelf="flex-start">
29-
<UserNotice>
30-
<Kb.Text type="BodySmall" style={{paddingBottom: Kb.Styles.globalMargins.xxtiny}} lineClamp={2}>
31-
{props.joiners.length > 0 ? getAddedUsernames(props.joiners) : getAddedUsernames(props.leavers)}
32-
{` ${props.leavers.length > props.joiners.length ? 'left' : 'joined'} ${
33-
props.isBigTeam ? `#${props.channelname}.` : `${props.teamname}.`
34-
}`}
35-
{props.timestamp ? (
36-
<Kb.Text type="BodyTiny" style={styles.timestamp}>
37-
{' ' + formatTimeForChat(props.timestamp)}
38-
</Kb.Text>
39-
) : null}
40-
</Kb.Text>
41-
<Kb.Box2 direction="horizontal" alignSelf="flex-start" style={styles.avatarLine}>
42-
<Kb.AvatarLine
43-
usernames={props.joiners.length > 0 ? props.joiners : props.leavers}
44-
maxShown={3}
45-
size={32}
46-
layout="horizontal"
47-
alignSelf="flex-start"
48-
/>
49-
</Kb.Box2>
50-
</UserNotice>
51-
</Kb.Box2>
52-
)
32+
const MultiUserJoinedNotice = (props: Props) => {
33+
return (
34+
<Kb.Box2 direction="vertical" fullWidth={true} alignSelf="flex-start">
35+
<UserNotice>
36+
<Kb.Text type="BodySmall" style={{paddingBottom: Kb.Styles.globalMargins.xxtiny}} lineClamp={2}>
37+
{(props.joiners?.length ?? 0) > 0
38+
? getAddedUsernames(props.joiners)
39+
: getAddedUsernames(props.leavers)}
40+
{` ${(props.leavers?.length ?? 0) > (props.joiners?.length ?? 0) ? 'left' : 'joined'} ${
41+
props.isBigTeam ? `#${props.channelname}.` : `${props.teamname}.`
42+
}`}
43+
{props.timestamp ? (
44+
<Kb.Text type="BodyTiny" style={styles.timestamp}>
45+
{' ' + formatTimeForChat(props.timestamp)}
46+
</Kb.Text>
47+
) : null}
48+
</Kb.Text>
49+
<Kb.Box2 direction="horizontal" alignSelf="flex-start" style={styles.avatarLine}>
50+
<Kb.AvatarLine
51+
usernames={((props.joiners?.length ?? 0) > 0 ? props.joiners : props.leavers) ?? none}
52+
maxShown={3}
53+
size={32}
54+
layout="horizontal"
55+
alignSelf="flex-start"
56+
/>
57+
</Kb.Box2>
58+
</UserNotice>
59+
</Kb.Box2>
60+
)
61+
}
5362

5463
const styles = Kb.Styles.styleSheetCreate(
5564
() =>

0 commit comments

Comments
 (0)