4
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
6
*/
7
+ import { mergeSx } from '@gridsuite/commons-ui' ;
7
8
import { Avatar , Box , Theme , Tooltip } from '@mui/material' ;
8
9
10
+ const FERMAT_PRIME = 65537 ;
11
+ // This function is a copy/paste of the MUI demo sample here :
12
+ // https://mui.com/material-ui/react-avatar/#letter-avatars
13
+ // hash function improved to generate more distinct values for similar strings using FERMAT_PRIME
14
+ // Use hsl to manage saturation and softer colors
15
+ function stringToColor ( string : string ) {
16
+ let hash = 0 ;
17
+ /* eslint-disable no-bitwise */
18
+ const stringUniqueHash = [ ...string ] . reduce ( ( acc , char ) => {
19
+ hash = char . charCodeAt ( 0 ) + ( ( acc << 5 ) - acc ) * FERMAT_PRIME ;
20
+ return hash & hash ;
21
+ } , 0 ) ;
22
+ /* eslint-enable no-bitwise */
23
+ return `hsl(${ stringUniqueHash % 360 } , 50%, 50%)` ;
24
+ }
25
+
9
26
function getAbbreviationFromUserName ( name : string ) {
10
27
// notice : == null means null or undefined
11
28
if ( name == null || name . trim ( ) === '' ) {
@@ -24,6 +41,7 @@ const styles = {
24
41
height : '32px' ,
25
42
width : '32px' ,
26
43
fontSize : theme . typography . fontSize ,
44
+ backgroundColor : theme . row . hover as string ,
27
45
} ) ,
28
46
} ;
29
47
@@ -33,7 +51,9 @@ export function UserCellRenderer({ value }: Readonly<UserCellRendererProps>) {
33
51
return (
34
52
< Box sx = { { display : 'inline-flex' , verticalAlign : 'middle' } } >
35
53
< Tooltip title = { value } placement = "right" >
36
- < Avatar sx = { styles . avatar } > { getAbbreviationFromUserName ( value ) } </ Avatar >
54
+ < Avatar sx = { mergeSx ( styles . avatar , value ? { backgroundColor : stringToColor ( value ) } : null ) } >
55
+ { getAbbreviationFromUserName ( value ) }
56
+ </ Avatar >
37
57
</ Tooltip >
38
58
</ Box >
39
59
) ;
0 commit comments