Skip to content

Commit 06c16b3

Browse files
authored
bad use selector (keybase#23470)
* WIP * WIP * WIP
1 parent fba9a54 commit 06c16b3

24 files changed

Lines changed: 243 additions & 186 deletions

File tree

‎shared/actions/chat2/index.tsx‎

Lines changed: 80 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,15 @@ const onIncomingMessage = (
346346

347347
const {containsLatestMessageMap} = state.chat2
348348
const shouldAddMessage = containsLatestMessageMap.get(conversationIDKey) || false
349-
const message = Constants.uiMessageToMessage(state, conversationIDKey, cMsg)
349+
350+
const {username, getLastOrdinal, devicename} = Constants.getMessageStateExtras(state, conversationIDKey)
351+
const message = Constants.uiMessageToMessage(
352+
conversationIDKey,
353+
cMsg,
354+
username,
355+
getLastOrdinal,
356+
devicename
357+
)
350358
if (message) {
351359
// The attachmentuploaded call is like an 'edit' of an attachment. We get the placeholder, then its replaced by the actual image
352360
if (
@@ -374,7 +382,13 @@ const onIncomingMessage = (
374382
switch (body.messageType) {
375383
case RPCChatTypes.MessageType.edit:
376384
if (modifiedMessage) {
377-
const modMessage = Constants.uiMessageToMessage(state, conversationIDKey, modifiedMessage)
385+
const modMessage = Constants.uiMessageToMessage(
386+
conversationIDKey,
387+
modifiedMessage,
388+
username,
389+
getLastOrdinal,
390+
devicename
391+
)
378392
if (modMessage) {
379393
actions.push(Chat2Gen.createMessagesAdd({context: {type: 'incoming'}, messages: [modMessage]}))
380394
}
@@ -537,6 +551,8 @@ const ephemeralPurgeToActions = (info: RPCChatTypes.EphemeralPurgeNotifInfo) =>
537551

538552
const messagesUpdatedToActions = (state: Container.TypedState, info: RPCChatTypes.MessagesUpdated) => {
539553
const conversationIDKey = Types.conversationIDToKey(info.convID)
554+
555+
const {username, getLastOrdinal, devicename} = Constants.getMessageStateExtras(state, conversationIDKey)
540556
const messages = (info.updates ?? []).reduce<
541557
Array<{
542558
message: Types.Message
@@ -547,7 +563,7 @@ const messagesUpdatedToActions = (state: Container.TypedState, info: RPCChatType
547563
if (!messageID) {
548564
return l
549565
}
550-
const uiMsg = Constants.uiMessageToMessage(state, conversationIDKey, msg)
566+
const uiMsg = Constants.uiMessageToMessage(conversationIDKey, msg, username, getLastOrdinal, devicename)
551567
if (!uiMsg) {
552568
return l
553569
}
@@ -1115,32 +1131,35 @@ function* loadMoreMessages(
11151131

11161132
const loadingKey = Constants.waitingKeyThreadLoad(conversationIDKey)
11171133
let calledClear = false
1118-
const onGotThread = (thread: string) => {
1119-
if (!thread) {
1120-
return
1121-
}
1134+
const onGotThread = (thread: string) =>
1135+
Saga.callUntyped(function*() {
1136+
if (!thread) {
1137+
return
1138+
}
11221139

1123-
const uiMessages: RPCChatTypes.UIMessages = JSON.parse(thread)
1124-
const actions: Array<Saga.PutEffect> = []
1125-
let shouldClearOthers = false
1126-
if ((forceClear || sd === 'none') && !calledClear) {
1127-
shouldClearOthers = true
1128-
calledClear = true
1129-
}
1130-
const messages = (uiMessages.messages ?? []).reduce<Array<Types.Message>>((arr, m) => {
1131-
const message = conversationIDKey ? Constants.uiMessageToMessage(state, conversationIDKey, m) : null
1132-
if (message) {
1133-
arr.push(message)
1140+
const state = yield* Saga.selectState()
1141+
const {username, getLastOrdinal, devicename} = Constants.getMessageStateExtras(state, conversationIDKey)
1142+
const uiMessages: RPCChatTypes.UIMessages = JSON.parse(thread)
1143+
let shouldClearOthers = false
1144+
if ((forceClear || sd === 'none') && !calledClear) {
1145+
shouldClearOthers = true
1146+
calledClear = true
11341147
}
1135-
return arr
1136-
}, [])
1148+
const messages = (uiMessages.messages ?? []).reduce<Array<Types.Message>>((arr, m) => {
1149+
const message = conversationIDKey
1150+
? Constants.uiMessageToMessage(conversationIDKey, m, username, getLastOrdinal, devicename)
1151+
: null
1152+
if (message) {
1153+
arr.push(message)
1154+
}
1155+
return arr
1156+
}, [])
11371157

1138-
const moreToLoad = uiMessages.pagination ? !uiMessages.pagination.last : true
1139-
actions.push(Saga.put(Chat2Gen.createUpdateMoreToLoad({conversationIDKey, moreToLoad})))
1158+
const moreToLoad = uiMessages.pagination ? !uiMessages.pagination.last : true
1159+
yield Saga.put(Chat2Gen.createUpdateMoreToLoad({conversationIDKey, moreToLoad}))
11401160

1141-
if (messages.length) {
1142-
actions.push(
1143-
Saga.put(
1161+
if (messages.length) {
1162+
yield Saga.put(
11441163
Chat2Gen.createMessagesAdd({
11451164
centeredMessageIDs,
11461165
context: {conversationIDKey, type: 'threadLoad'},
@@ -1149,11 +1168,8 @@ function* loadMoreMessages(
11491168
shouldClearOthers,
11501169
})
11511170
)
1152-
)
1153-
}
1154-
1155-
return actions
1156-
}
1171+
}
1172+
})
11571173

11581174
const onGotThreadLoadStatus = (status: RPCChatTypes.UIChatThreadStatus) => [
11591175
Saga.put(Chat2Gen.createSetThreadLoadStatus({conversationIDKey, status})),
@@ -1425,18 +1441,29 @@ const messageRetry = (action: Chat2Gen.MessageRetryPayload) => {
14251441
}
14261442

14271443
function* loadAttachmentView(
1428-
state: Container.TypedState,
1444+
_: Container.TypedState,
14291445
action: Chat2Gen.LoadAttachmentViewPayload,
14301446
logger: Saga.SagaLogger
14311447
) {
14321448
const {conversationIDKey, viewType, fromMsgID} = action.payload
14331449

1434-
const onHit = (hit: RPCChatTypes.MessageTypes['chat.1.chatUi.chatLoadGalleryHit']['inParam']) => {
1435-
const message = Constants.uiMessageToMessage(state, conversationIDKey, hit.message)
1436-
return message
1437-
? Saga.put(Chat2Gen.createAddAttachmentViewMessage({conversationIDKey, message, viewType}))
1438-
: []
1439-
}
1450+
const onHit = (hit: RPCChatTypes.MessageTypes['chat.1.chatUi.chatLoadGalleryHit']['inParam']) =>
1451+
Saga.callUntyped(function*() {
1452+
const state = yield* Saga.selectState()
1453+
const {username, getLastOrdinal, devicename} = Constants.getMessageStateExtras(state, conversationIDKey)
1454+
1455+
const message = Constants.uiMessageToMessage(
1456+
conversationIDKey,
1457+
hit.message,
1458+
username,
1459+
getLastOrdinal,
1460+
devicename
1461+
)
1462+
if (message) {
1463+
yield Saga.put(Chat2Gen.createAddAttachmentViewMessage({conversationIDKey, message, viewType}))
1464+
}
1465+
})
1466+
14401467
try {
14411468
const res = yield RPCChatTypes.localLoadGalleryRpcSaga({
14421469
incomingCallMap: {'chat.1.chatUi.chatLoadGalleryHit': onHit},
@@ -1467,15 +1494,29 @@ function* threadSearch(
14671494
logger: Saga.SagaLogger
14681495
) {
14691496
const {conversationIDKey, query} = action.payload
1497+
1498+
const {username, getLastOrdinal, devicename} = Constants.getMessageStateExtras(state, conversationIDKey)
14701499
const onHit = (hit: RPCChatTypes.MessageTypes['chat.1.chatUi.chatSearchHit']['inParam']) => {
1471-
const message = Constants.uiMessageToMessage(state, conversationIDKey, hit.searchHit.hitMessage)
1500+
const message = Constants.uiMessageToMessage(
1501+
conversationIDKey,
1502+
hit.searchHit.hitMessage,
1503+
username,
1504+
getLastOrdinal,
1505+
devicename
1506+
)
14721507
return message
14731508
? Saga.put(Chat2Gen.createThreadSearchResults({clear: false, conversationIDKey, messages: [message]}))
14741509
: false
14751510
}
14761511
const onInboxHit = (resp: RPCChatTypes.MessageTypes['chat.1.chatUi.chatSearchInboxHit']['inParam']) => {
14771512
const messages = (resp.searchHit.hits || []).reduce<Array<Types.Message>>((l, h) => {
1478-
const uiMsg = Constants.uiMessageToMessage(state, conversationIDKey, h.hitMessage)
1513+
const uiMsg = Constants.uiMessageToMessage(
1514+
conversationIDKey,
1515+
h.hitMessage,
1516+
username,
1517+
getLastOrdinal,
1518+
devicename
1519+
)
14791520
if (uiMsg) {
14801521
l.push(uiMsg)
14811522
}

‎shared/chat/audio/audio-recorder.native.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ const AudioRecorder = (props: Props) => {
3030
const ampTracker = React.useRef(new AmpTracker()).current
3131
const [visible, setVisible] = React.useState(false)
3232
const [closingDown, setClosingDown] = React.useState(false)
33-
const {audioRecording} = Container.useSelector(state => ({
34-
audioRecording: state.chat2.audioRecording.get(conversationIDKey),
35-
}))
33+
const audioRecording = Container.useSelector(state => state.chat2.audioRecording.get(conversationIDKey))
3634
const closingDownRef = React.useRef(false)
3735

3836
// dispatch

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ const AudioSend = (props: Props) => {
1515
// props
1616
const {conversationIDKey} = props
1717
// state
18-
const {audioRecording} = Container.useSelector(state => ({
19-
audioRecording: state.chat2.audioRecording.get(conversationIDKey),
20-
}))
18+
const audioRecording = Container.useSelector(state => state.chat2.audioRecording.get(conversationIDKey))
2119
// dispatch
2220
const dispatch = Container.useDispatch()
2321
const onCancel = () => {

‎shared/chat/conversation/attachment-fullscreen/container.tsx‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ type OwnProps = Container.RouteProps<{conversationIDKey: Types.ConversationIDKey
1717
const Connected = (props: OwnProps) => {
1818
const conversationIDKey = Container.getRouteProps(props, 'conversationIDKey', Constants.noConversationIDKey)
1919
const inOrdinal = Container.getRouteProps(props, 'ordinal', 0)
20-
const state = Container.useSelector(s => s)
2120
const [ordinal, setOrdinal] = React.useState(inOrdinal)
2221
const [autoPlay, setAutoPlay] = React.useState(true)
2322
const dispatch = Container.useDispatch()
24-
const m = Constants.getMessage(state, conversationIDKey, ordinal)
23+
const m = Container.useSelector(state => Constants.getMessage(state, conversationIDKey, ordinal))
24+
const lastOrdinal = Container.useSelector(
25+
state => [...(state.chat2.messageOrdinals.get(conversationIDKey) ?? [])].pop() ?? Types.numberToOrdinal(0)
26+
)
27+
const getLastOrdinal = () => lastOrdinal
28+
const username = Container.useSelector(state => state.config.username)
29+
const currentDeviceName = Container.useSelector(state => state.config.deviceName ?? '')
2530
const message = m?.type === 'attachment' ? m : blankMessage
2631
const {previewHeight, previewWidth, title, fileURL, previewURL, downloadPath, transferProgress} = message
2732
const {id} = message
@@ -50,7 +55,13 @@ const Connected = (props: OwnProps) => {
5055
],
5156
result => {
5257
if (result.message) {
53-
const goodMessage = Constants.uiMessageToMessage(state, conversationIDKey, result.message)
58+
const goodMessage = Constants.uiMessageToMessage(
59+
conversationIDKey,
60+
result.message,
61+
username,
62+
getLastOrdinal,
63+
currentDeviceName
64+
)
5465
if (goodMessage && goodMessage.type === 'attachment') {
5566
setAutoPlay(false)
5667
addToMessageMap(goodMessage)

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

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -64,58 +64,46 @@ const InstallBotPopup = (props: Props) => {
6464
const [installWithRestrict, setInstallWithRestrict] = React.useState(true)
6565
const [installInConvs, setInstallInConvs] = React.useState<string[]>([])
6666
const [disableDone, setDisableDone] = React.useState(false)
67-
const {
68-
commands,
69-
featured,
70-
inTeam,
71-
inTeamUnrestricted,
72-
isBot,
73-
readOnly,
74-
settings,
75-
teamID,
76-
teamName,
77-
} = Container.useSelector((state: Container.TypedState) => {
78-
let inTeam: boolean | undefined
79-
let isBot: boolean | undefined
80-
let teamRole: TeamTypes.TeamRoleType | null | undefined
81-
let teamName: string | null | undefined
82-
let teamID: TeamTypes.TeamID = TeamTypes.noTeamID
83-
let readOnly = false
67+
68+
const meta = Container.useSelector(state =>
69+
conversationIDKey ? state.chat2.metaMap.get(conversationIDKey) : undefined
70+
)
71+
72+
// TODO will thrash every time
73+
const commands = Container.useSelector(state => {
8474
let commands: Array<string> = []
85-
if (conversationIDKey) {
86-
const meta = state.chat2.metaMap.get(conversationIDKey)
75+
if (conversationIDKey && meta) {
8776
commands = Constants.getBotCommands(state, conversationIDKey)
8877
.filter(c => c.username === botUsername)
8978
.map(c => c.name)
90-
if (meta && meta.teamname) {
91-
teamID = meta.teamID
92-
teamName = meta.teamname
93-
readOnly = !TeamConstants.getCanPerformByID(state, meta.teamID).manageBots
94-
}
95-
teamRole = state.chat2.botTeamRoleInConvMap.get(conversationIDKey)?.get(botUsername)
96-
if (teamRole !== undefined) {
97-
inTeam = !!teamRole
98-
}
99-
100-
if (teamRole === 'bot' || teamRole === 'restrictedbot') {
101-
isBot = true
102-
}
10379
}
10480
const convCommands: Types.BotPublicCommands = {commands, loadError: false}
105-
return {
106-
commands: commands.length > 0 ? convCommands : state.chat2.botPublicCommands.get(botUsername),
107-
featured: state.chat2.featuredBotsMap.get(botUsername),
108-
inTeam,
109-
inTeamUnrestricted: inTeam && teamRole === 'bot',
110-
isBot,
111-
readOnly,
112-
settings: conversationIDKey
113-
? state.chat2.botSettings.get(conversationIDKey)?.get(botUsername) ?? undefined
114-
: undefined,
115-
teamID,
116-
teamName,
117-
}
81+
return commands.length > 0 ? convCommands : state.chat2.botPublicCommands.get(botUsername)
11882
})
83+
84+
const featured = Container.useSelector(state => state.chat2.featuredBotsMap.get(botUsername))
85+
const teamRole = Container.useSelector(state =>
86+
conversationIDKey ? state.chat2.botTeamRoleInConvMap.get(conversationIDKey)?.get(botUsername) : undefined
87+
)
88+
const inTeam = teamRole !== undefined ? !!teamRole : undefined
89+
const inTeamUnrestricted = inTeam && teamRole === 'bot'
90+
const isBot = teamRole === 'bot' || teamRole === 'restrictedbot' ? true : undefined
91+
92+
const readOnly = Container.useSelector(state =>
93+
meta && meta.teamname ? !TeamConstants.getCanPerformByID(state, meta.teamID).manageBots : false
94+
)
95+
const settings = Container.useSelector(state =>
96+
conversationIDKey
97+
? state.chat2.botSettings.get(conversationIDKey)?.get(botUsername) ?? undefined
98+
: undefined
99+
)
100+
let teamname: string | null | undefined
101+
let teamID: TeamTypes.TeamID = TeamTypes.noTeamID
102+
if (meta && meta.teamname) {
103+
teamID = meta.teamID
104+
teamname = meta.teamname
105+
}
106+
119107
const channelMetas = useAllChannelMetas(teamID)
120108
const error = Container.useAnyErrors(Constants.waitingKeyBotAdd, Constants.waitingKeyBotRemove)
121109
// dispatch
@@ -305,19 +293,19 @@ const InstallBotPopup = (props: Props) => {
305293
onCheck={() => setInstallWithMentions(!installWithMentions)}
306294
/>
307295
</Kb.Box2>
308-
{teamID && teamName && channelMetas && (
296+
{teamID && teamname && channelMetas && (
309297
<Kb.Box2 direction="vertical" fullWidth={true} gap="tiny">
310298
<Kb.Text type="BodyBig">In these channels:</Kb.Text>
311299
<Kb.DropdownButton
312300
selected={
313301
<Kb.Box2 direction="horizontal" alignItems="center">
314302
<Kb.Avatar
315303
size={16}
316-
teamname={teamName}
304+
teamname={teamname}
317305
style={{marginRight: Styles.globalMargins.tiny}}
318306
/>
319307
<Kb.Text type="BodySemibold">
320-
{teamName}{' '}
308+
{teamname}{' '}
321309
{installInConvs.length === 1
322310
? `(#${channelMetas.get(installInConvs[0])?.channelname ?? ''})`
323311
: `(${installInConvs.length > 0 ? installInConvs.length : 'all'} channels)`}
@@ -349,7 +337,7 @@ const InstallBotPopup = (props: Props) => {
349337
</Kb.Box2>
350338
)
351339

352-
const channelPickerContent = channelPickerScreen && teamID && teamName && channelMetas && (
340+
const channelPickerContent = channelPickerScreen && teamID && teamname && channelMetas && (
353341
<Kb.Box2 direction="vertical" fullWidth={true} gap="small">
354342
<ChannelPicker
355343
channelMetas={channelMetas}
@@ -358,7 +346,7 @@ const InstallBotPopup = (props: Props) => {
358346
setDisableDone={setDisableDone}
359347
setInstallInConvs={setInstallInConvs}
360348
teamID={teamID}
361-
teamName={teamName}
349+
teamName={teamname}
362350
/>
363351
</Kb.Box2>
364352
)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ const SearchBotPopup = (props: Props) => {
2626

2727
// state
2828
const [lastQuery, setLastQuery] = React.useState('')
29-
const {featuredBotsMap, results} = Container.useSelector((state: Container.TypedState) => ({
30-
featuredBotsMap: state.chat2.featuredBotsMap,
31-
results: state.chat2.botSearchResults,
32-
}))
29+
const featuredBotsMap = Container.useSelector(state => state.chat2.featuredBotsMap)
30+
const results = Container.useSelector(state => state.chat2.botSearchResults)
3331
const waiting = Container.useAnyWaiting(
3432
Constants.waitingKeyBotSearchUsers,
3533
Constants.waitingKeyBotSearchFeatured

0 commit comments

Comments
 (0)