@@ -19,32 +19,132 @@ import { ProIconButton } from '../../buttons/ProButton';
19
19
import { useMessageIdFromContext } from '../../../contexts/MessageIdContext' ;
20
20
import { useMessageDirection } from '../../../state/selectors' ;
21
21
import { useShowUserDetailsCbFromConversation } from '../../menuAndSettingsHooks/useShowUserDetailsCb' ;
22
+ import { assertUnreachable } from '../../../types/sqlSharedTypes' ;
22
23
23
- const boldProfileNameCtx : Array < ContactNameContext > = [
24
- 'conversation-list-item' ,
25
- 'quoted-message-composition' ,
26
- 'message-author' ,
27
- 'message-info-author' ,
28
- 'member-list-item' ,
29
- 'contact-list-row' ,
30
- ] ;
31
-
32
- const showPubkeyCtx : Array < ContactNameContext > = [ 'message-author' ] ;
33
-
34
- const ntsIsYouCtx : Array < ContactNameContext > = [
35
- 'message-author' ,
36
- 'quote-author' ,
37
- 'quoted-message-composition' ,
38
- 'message-search-result' ,
39
- 'react-list-modal' ,
40
- 'member-list-item' ,
41
- ] ;
42
-
43
- const forceSingleLineCtx : Array < ContactNameContext > = [
44
- 'message-info-author' ,
45
- 'member-list-item' ,
46
- 'contact-list-row' ,
47
- ] ;
24
+ /**
25
+ * In some contexts, we want to bold the name of the contact.
26
+ */
27
+ function isBoldProfileNameCtx ( ctx : ContactNameContext ) {
28
+ // We are doing this as a switch instead of an array so we have to be explicit anytime we add a new context,
29
+ // thanks to the assertUnreachable below
30
+ switch ( ctx ) {
31
+ case 'conversation-list-item' :
32
+ case 'quoted-message-composition' :
33
+ case 'message-author' :
34
+ case 'message-info-author' :
35
+ case 'member-list-item' :
36
+ case 'contact-list-row' :
37
+ return true ;
38
+ case 'quote-author' :
39
+ case 'conversation-list-item-search' :
40
+ case 'react-list-modal' :
41
+ case 'message-search-result' :
42
+ return false ;
43
+ default :
44
+ assertUnreachable ( ctx , 'isBoldProfileNameCtx' ) ;
45
+ throw new Error ( 'isBoldProfileNameCtx: unreachable' ) ;
46
+ }
47
+ }
48
+
49
+ /**
50
+ * In some contexts, we want to show the pubkey of the contact.
51
+ */
52
+ function isShowPubkeyCtx ( ctx : ContactNameContext ) {
53
+ // We are doing this as a switch instead of an array so we have to be explicit anytime we add a new context,
54
+ // thanks to the assertUnreachable below
55
+ switch ( ctx ) {
56
+ case 'message-author' :
57
+ case 'message-info-author' :
58
+ return true ;
59
+ case 'member-list-item' :
60
+ case 'contact-list-row' :
61
+ case 'quote-author' :
62
+ case 'conversation-list-item' :
63
+ case 'quoted-message-composition' :
64
+ case 'conversation-list-item-search' :
65
+ case 'react-list-modal' :
66
+ case 'message-search-result' :
67
+ return false ;
68
+ default :
69
+ assertUnreachable ( ctx , 'isShowPubkeyCtx' ) ;
70
+ throw new Error ( 'isShowPubkeyCtx: unreachable' ) ;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * In some contexts, we want to rename "Note To Self" to "You".
76
+ */
77
+ function isShowNtsIsYouCtx ( ctx : ContactNameContext ) {
78
+ // We are doing this as a switch instead of an array so we have to be explicit anytime we add a new context,
79
+ // thanks to the assertUnreachable below
80
+ switch ( ctx ) {
81
+ case 'message-author' :
82
+ case 'quote-author' :
83
+ case 'quoted-message-composition' :
84
+ case 'react-list-modal' :
85
+ case 'message-search-result' :
86
+ case 'member-list-item' :
87
+ return true ;
88
+ case 'message-info-author' :
89
+ case 'contact-list-row' :
90
+ case 'conversation-list-item' :
91
+ case 'conversation-list-item-search' :
92
+ return false ;
93
+ default :
94
+ assertUnreachable ( ctx , 'isShowNtsIsYouCtx' ) ;
95
+ throw new Error ( 'isShowNtsIsYouCtx: unreachable' ) ;
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Usually, we'd allow the name to be multiline. but in some contexts we want to force it to be single line.
101
+ */
102
+ function isForceSingleLineCtx ( ctx : ContactNameContext ) {
103
+ // We are doing this as a switch instead of an array so we have to be explicit anytime we add a new context,
104
+ // thanks to the assertUnreachable below
105
+ switch ( ctx ) {
106
+ case 'message-info-author' :
107
+ case 'member-list-item' :
108
+ case 'contact-list-row' :
109
+ return true ;
110
+ case 'message-author' :
111
+ case 'quote-author' :
112
+ case 'quoted-message-composition' :
113
+ case 'react-list-modal' :
114
+ case 'message-search-result' :
115
+ case 'conversation-list-item' :
116
+ case 'conversation-list-item-search' :
117
+ return false ;
118
+ default :
119
+ assertUnreachable ( ctx , 'isForceSingleLineCtx' ) ;
120
+ throw new Error ( 'isForceSingleLineCtx: unreachable' ) ;
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Usually, we'd allow the name to be multiline. but in some contexts we want to force it to be single line.
126
+ */
127
+ function isShowUPMOnClickCtx ( ctx : ContactNameContext ) {
128
+ // We are doing this as a switch instead of an array so we have to be explicit anytime we add a new context,
129
+ // thanks to the assertUnreachable below
130
+ switch ( ctx ) {
131
+ case 'message-info-author' :
132
+ return true ;
133
+ case 'member-list-item' :
134
+ case 'contact-list-row' :
135
+ case 'message-author' :
136
+ case 'quote-author' :
137
+ case 'quoted-message-composition' :
138
+ case 'react-list-modal' :
139
+ case 'message-search-result' :
140
+ case 'conversation-list-item' :
141
+ case 'conversation-list-item-search' :
142
+ return false ;
143
+ default :
144
+ assertUnreachable ( ctx , 'isForceSingleLineCtx' ) ;
145
+ throw new Error ( 'isForceSingleLineCtx: unreachable' ) ;
146
+ }
147
+ }
48
148
49
149
const commonNameStyles : CSSProperties = {
50
150
minWidth : 0 ,
@@ -62,8 +162,6 @@ const boldStyles: CSSProperties = {
62
162
fontWeight : 'bold' ,
63
163
} ;
64
164
65
- const contactNameContextShowUPM : Array < ContactNameContext > = [ 'message-info-author' ] ;
66
-
67
165
export const ContactName = ( {
68
166
pubkey,
69
167
module,
@@ -99,21 +197,19 @@ export const ContactName = ({
99
197
const msgDirection = useMessageDirection ( msgId ) ;
100
198
101
199
const displayName = isMe
102
- ? // we want to show "You" instead of Note to Self in some places (like quotes)
103
- ntsIsYouCtx . includes ( contactNameContext )
104
- ? tr ( 'you' )
105
- : tr ( 'noteToSelf' )
106
- : // we want to show the nickname in brackets if a nickname is set for search results
200
+ ? // we want to show "You" instead of "Note to Self" in some places (like quotes)
201
+ tr ( isShowNtsIsYouCtx ( contactNameContext ) ? 'you' : 'noteToSelf' )
202
+ : // we want to show the realName in brackets if a nickname is set for search results
107
203
contactNameContext === 'conversation-list-item-search' && nickname && realName
108
204
? `${ nickname } (${ realName } )`
109
205
: noFallback ;
110
206
111
207
const shouldShowShortenPkAsName = ! displayName ;
112
208
113
209
const shouldShowPubkey =
114
- ! shouldShowShortenPkAsName && isPublic && showPubkeyCtx . includes ( contactNameContext ) ;
115
- const boldProfileName = boldProfileNameCtx . includes ( contactNameContext ) ;
116
- const forceSingleLine = forceSingleLineCtx . includes ( contactNameContext ) ;
210
+ ! shouldShowShortenPkAsName && isPublic && isShowPubkeyCtx ( contactNameContext ) ;
211
+ const boldProfileName = isBoldProfileNameCtx ( contactNameContext ) ;
212
+ const forceSingleLine = isForceSingleLineCtx ( contactNameContext ) ;
117
213
118
214
const displayedName = shouldShowShortenPkAsName ? shortPubkey : displayName ;
119
215
@@ -163,10 +259,7 @@ export const ContactName = ({
163
259
maxWidth : '100%' ,
164
260
...style ,
165
261
} }
166
- onClick = {
167
- ( contactNameContextShowUPM . includes ( contactNameContext ) && showConversationSettingsCb ) ||
168
- undefined
169
- }
262
+ onClick = { ( isShowUPMOnClickCtx ( contactNameContext ) && showConversationSettingsCb ) || undefined }
170
263
>
171
264
{ displayedName ? (
172
265
< div style = { mergedNameStyle } className = { `${ prefix } __profile-name` } >
0 commit comments