@@ -2,6 +2,7 @@ import React, { useEffect } from 'react';
22import Downshift , { DownshiftProps } from 'downshift' ;
33import css from '@styled-system/css' ;
44import { Input , List , ListAction } from '@codesandbox/components' ;
5+ import { useOvermind } from 'app/overmind' ;
56
67type User = {
78 id : string ;
@@ -11,17 +12,25 @@ type User = {
1112
1213interface IUserAutoComplete {
1314 inputValue : string ;
15+ allowSelf ?: boolean ;
1416 children : ( answer : {
1517 users : User [ ] ;
1618 loading : boolean ;
1719 error : Error | null ;
1820 } ) => JSX . Element ;
1921}
2022
21- const UserAutoComplete = ( { inputValue, children } : IUserAutoComplete ) => {
23+ const UserAutoComplete = ( {
24+ inputValue,
25+ children,
26+ allowSelf = false ,
27+ } : IUserAutoComplete ) => {
2228 const [ users , setUsers ] = React . useState < User [ ] > ( [ ] ) ;
2329 const [ loading , setLoading ] = React . useState < boolean > ( true ) ;
2430 const [ error , setError ] = React . useState < Error | null > ( null ) ;
31+ const {
32+ state : { user } ,
33+ } = useOvermind ( ) ;
2534 useEffect ( ( ) => {
2635 setLoading ( true ) ;
2736 setError ( null ) ;
@@ -34,7 +43,10 @@ const UserAutoComplete = ({ inputValue, children }: IUserAutoComplete) => {
3443 fetch ( `/api/v1/users/search?username=${ inputValue } ` )
3544 . then ( x => x . json ( ) )
3645 . then ( x => {
37- setUsers ( x ) ;
46+ const fetchedUsers = allowSelf
47+ ? x
48+ : x . filter ( member => member . username !== user ?. username ) ;
49+ setUsers ( fetchedUsers ) ;
3850 setLoading ( false ) ;
3951 } )
4052 . catch ( e => {
@@ -50,14 +62,15 @@ const UserAutoComplete = ({ inputValue, children }: IUserAutoComplete) => {
5062 clearTimeout ( timeoutId ) ;
5163 }
5264 } ;
53- } , [ inputValue ] ) ;
65+ } , [ allowSelf , inputValue , user ] ) ;
5466
5567 return children ( { users, loading, error } ) ;
5668} ;
5769
5870interface IUserSearchInputProps {
5971 onInputValueChange : DownshiftProps < string > [ 'onInputValueChange' ] ;
6072 inputValue : string ;
73+ allowSelf ?: boolean ;
6174 [ key : string ] : any ;
6275}
6376
@@ -67,6 +80,7 @@ const InputWithoutTypes = Input as any;
6780export const UserSearchInput = ( {
6881 onInputValueChange,
6982 inputValue,
83+ allowSelf = false ,
7084 ...props
7185} : IUserSearchInputProps ) => (
7286 < Downshift
@@ -98,7 +112,7 @@ export const UserSearchInput = ({
98112 />
99113 </ div >
100114
101- < UserAutoComplete inputValue = { currentInputValue } >
115+ < UserAutoComplete allowSelf = { allowSelf } inputValue = { currentInputValue } >
102116 { ( { users, error } ) =>
103117 users . length > 0 && ! error && isOpen ? (
104118 < List
0 commit comments