@@ -6,66 +6,68 @@ import {TagSelectorEntry, specialTag} from './tagSelectorEntry';
66import { QueryResult } from 'react-apollo' ;
77
88export const useSuggest = (
9- tagResult : QueryResult < Tags , { } > ,
10- inputValue : string ,
11- usedTags : string [ ] ,
12- skipValue = false
9+ tagResult : QueryResult < Tags , { } > ,
10+ inputValue : string ,
11+ usedTags : string [ ] ,
12+ skipValue = false ,
13+ includeInputValueOnNoMatch = true
1314) : TagSelectorEntry [ ] => {
14- const [ tagKeySomeCase , tagValue ] = inputValue . split ( ':' ) ;
15- const tagKey = tagKeySomeCase . toLowerCase ( ) ;
15+ const [ tagKeySomeCase , tagValue ] = inputValue . split ( ':' ) ;
16+ const tagKey = tagKeySomeCase . toLowerCase ( ) ;
1617
17- const exactMatch = ( ( tagResult . data && tagResult . data . tags ) || [ ] ) . find ( ( tag ) => tag . key === tagKey ) ;
18+ const exactMatch = ( ( tagResult . data && tagResult . data . tags ) || [ ] ) . find ( ( tag ) => tag . key === tagKey ) ;
1819
19- const valueResult = useQuery < SuggestTagValue , SuggestTagValueVariables > ( gqlTags . SuggestTagValue , {
20- variables : { tag : tagKey , query : tagValue } ,
21- skip : exactMatch === undefined || skipValue ,
22- } ) ;
20+ const valueResult = useQuery < SuggestTagValue , SuggestTagValueVariables > ( gqlTags . SuggestTagValue , {
21+ variables : { tag : tagKey , query : tagValue } ,
22+ skip : exactMatch === undefined || skipValue ,
23+ } ) ;
2324
24- if ( exactMatch && tagValue !== undefined && usedTags . indexOf ( exactMatch . key ) === - 1 && ! skipValue ) {
25- return suggestTagValue ( exactMatch , tagValue , valueResult ) ;
26- } else {
27- return suggestTag ( exactMatch , tagResult , tagKey , usedTags ) ;
28- }
25+ if ( exactMatch && tagValue !== undefined && usedTags . indexOf ( exactMatch . key ) === - 1 && ! skipValue ) {
26+ return suggestTagValue ( exactMatch , tagValue , valueResult , includeInputValueOnNoMatch ) ;
27+ } else {
28+ return suggestTag ( exactMatch , tagResult , tagKey , usedTags ) ;
29+ }
2930} ;
3031
3132const suggestTag = (
32- exactMatch : TagSelectorEntry [ 'tag' ] | undefined ,
33- tagResult : QueryResult < Tags , { } > ,
34- tagKey : string ,
35- usedTags : string [ ]
33+ exactMatch : TagSelectorEntry [ 'tag' ] | undefined ,
34+ tagResult : QueryResult < Tags , { } > ,
35+ tagKey : string ,
36+ usedTags : string [ ]
3637) => {
37- if ( ! tagResult . data || tagResult . data . tags === null ) {
38- return [ ] ;
39- }
38+ if ( ! tagResult . data || tagResult . data . tags === null ) {
39+ return [ ] ;
40+ }
4041
41- let availableTags = ( tagResult . data . tags || [ ] )
42- . filter ( ( tag ) => usedTags . indexOf ( tag . key ) === - 1 )
43- . filter ( ( tag ) => tag . key . indexOf ( tagKey ) === 0 ) ;
42+ let availableTags = ( tagResult . data . tags || [ ] )
43+ . filter ( ( tag ) => usedTags . indexOf ( tag . key ) === - 1 )
44+ . filter ( ( tag ) => tag . key . indexOf ( tagKey ) === 0 ) ;
4445
45- if ( tagKey && ! exactMatch ) {
46- availableTags = [ specialTag ( tagKey , 'new' ) , ...availableTags ] ;
47- }
46+ if ( tagKey && ! exactMatch ) {
47+ availableTags = [ specialTag ( tagKey , 'new' ) , ...availableTags ] ;
48+ }
4849
49- if ( usedTags . indexOf ( tagKey ) !== - 1 ) {
50- availableTags = [ specialTag ( tagKey , 'used' ) , ...availableTags ] ;
51- }
50+ if ( usedTags . indexOf ( tagKey ) !== - 1 ) {
51+ availableTags = [ specialTag ( tagKey , 'used' ) , ...availableTags ] ;
52+ }
5253
53- return availableTags
54- . sort ( ( a , b ) => b . usages - a . usages )
55- . slice ( 0 , 5 )
56- . map ( ( tag ) => ( { tag, value : '' } ) ) ;
54+ return availableTags
55+ . sort ( ( a , b ) => b . usages - a . usages )
56+ . slice ( 0 , 5 )
57+ . map ( ( tag ) => ( { tag, value : '' } ) ) ;
5758} ;
5859
5960const suggestTagValue = (
60- exactMatch : TagSelectorEntry [ 'tag' ] ,
61- tagValue : string ,
62- valueResult : QueryResult < SuggestTagValue , SuggestTagValueVariables >
61+ exactMatch : TagSelectorEntry [ 'tag' ] ,
62+ tagValue : string ,
63+ valueResult : QueryResult < SuggestTagValue , SuggestTagValueVariables > ,
64+ includeInputValueOnNoMatch : boolean
6365) : TagSelectorEntry [ ] => {
64- let someValues = ( valueResult . data && valueResult . data . values ) || [ ] ;
66+ let someValues = ( valueResult . data && valueResult . data . values ) || [ ] ;
6567
66- if ( someValues . indexOf ( tagValue ) === - 1 ) {
67- someValues = [ tagValue , ...someValues ] ;
68- }
68+ if ( includeInputValueOnNoMatch && someValues . indexOf ( tagValue ) === - 1 ) {
69+ someValues = [ tagValue , ...someValues ] ;
70+ }
6971
70- return someValues . map ( ( val ) => ( { tag : exactMatch , value : val } ) ) ;
72+ return someValues . map ( ( val ) => ( { tag : exactMatch , value : val } ) ) ;
7173} ;
0 commit comments