-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
user.js
75 lines (67 loc) · 1.45 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
/** @typedef {import('@wordpress/components').WPCompleter} WPCompleter */
export function getUserLabel( user ) {
const avatar =
user.avatar_urls && user.avatar_urls[ 24 ] ? (
<img
className="editor-autocompleters__user-avatar"
alt=""
src={ user.avatar_urls[ 24 ] }
/>
) : (
<span className="editor-autocompleters__no-avatar"></span>
);
return (
<>
{ avatar }
<span className="editor-autocompleters__user-name">
{ user.name }
</span>
<span className="editor-autocompleters__user-slug">
{ user.slug }
</span>
</>
);
}
/**
* A user mentions completer.
*
* @type {WPCompleter}
*/
export default {
name: 'users',
className: 'editor-autocompleters__user',
triggerPrefix: '@',
useItems( filterValue ) {
const users = useSelect(
( select ) => {
const { getUsers } = select( coreStore );
return getUsers( {
context: 'view',
search: encodeURIComponent( filterValue ),
} );
},
[ filterValue ]
);
const options = useMemo(
() =>
users
? users.map( ( user ) => ( {
key: `user-${ user.slug }`,
value: user,
label: getUserLabel( user ),
} ) )
: [],
[ users ]
);
return [ options ];
},
getOptionCompletion( user ) {
return `@${ user.slug }`;
},
};