@@ -29,13 +29,40 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
2929 setQuery ,
3030 ] = useState ( "" ) ;
3131
32+ const [
33+ page ,
34+ setPage ,
35+ ] = useState < number > ( 0 ) ;
36+
37+ const [
38+ canLoadMore ,
39+ setCanLoadMore ,
40+ ] = useState < boolean > ( true ) ;
41+
42+ const [
43+ context ,
44+ setContext ,
45+ ] = useState < never | undefined > ( undefined ) ;
46+
47+ const [
48+ pageable ,
49+ setPageable ,
50+ ] = useState ( {
51+ page : 0 ,
52+ prevContext : { } ,
53+ data : [ ] ,
54+ values : new Set ( ) ,
55+ } )
56+
3257 const configuredPropsUpTo : Record < string , unknown > = { } ;
3358 for ( let i = 0 ; i < idx ; i ++ ) {
3459 const prop = configurableProps [ i ] ;
3560 configuredPropsUpTo [ prop . name ] = configuredProps [ prop . name ] ;
3661 }
3762 const componentConfigureInput : ComponentConfigureOpts = {
3863 userId,
64+ page,
65+ prevContext : context ,
3966 componentId : component . key ,
4067 propName : prop . name ,
4168 configuredProps : configuredPropsUpTo ,
@@ -55,9 +82,18 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
5582 setError ,
5683 ] = useState < { name : string ; message : string ; } > ( ) ;
5784
85+ const onLoadMore = ( ) => {
86+ setPage ( pageable . page )
87+ setContext ( pageable . prevContext )
88+ setPageable ( {
89+ ...pageable ,
90+ prevContext : { } ,
91+ } )
92+ }
93+
5894 // TODO handle error!
5995 const {
60- isFetching, data , refetch,
96+ isFetching, refetch,
6197 } = useQuery ( {
6298 queryKey : [
6399 "componentConfigure" ,
@@ -67,11 +103,11 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
67103 setError ( undefined ) ;
68104 const res = await client . componentConfigure ( componentConfigureInput ) ;
69105
70- // console.log("res", res)
71106 // XXX look at errors in response here too
72107 const {
73108 options, stringOptions, errors,
74109 } = res ;
110+
75111 if ( errors ?. length ) {
76112 // TODO field context setError? (for validity, etc.)
77113 try {
@@ -84,8 +120,9 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
84120 }
85121 return [ ] ;
86122 }
123+ let _options = [ ]
87124 if ( options ?. length ) {
88- return options ;
125+ _options = options ;
89126 }
90127 if ( stringOptions ?. length ) {
91128 const options = [ ] ;
@@ -95,13 +132,45 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
95132 value : stringOption ,
96133 } ) ;
97134 }
98- return options ;
135+ _options = options ;
136+ }
137+
138+ const newOptions = [ ]
139+ const allValues = new Set ( pageable . values )
140+ for ( const o of _options || [ ] ) {
141+ const value = typeof o === "string"
142+ ? o
143+ : o . value
144+ if ( allValues . has ( value ) ) {
145+ continue
146+ }
147+ allValues . add ( value )
148+ newOptions . push ( o )
149+ }
150+ let data = pageable . data
151+ if ( newOptions . length ) {
152+ data = [
153+ ...pageable . data ,
154+ ...newOptions ,
155+ ]
156+ setPageable ( {
157+ page : page + 1 ,
158+ prevContext : res . context ,
159+ data,
160+ values : allValues ,
161+ } )
162+ } else {
163+ setCanLoadMore ( false )
99164 }
100- return [ ] ;
165+ return data ;
101166 } ,
102167 enabled : ! ! queryEnabled ,
103168 } ) ;
104169
170+ const showLoadMoreButton = ( ) => {
171+ return ! isFetching && ! error && canLoadMore
172+ }
173+
105174 // TODO show error in different spot!
106175 const placeholder = error
107176 ? error . message
@@ -116,7 +185,9 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
116185
117186 return (
118187 < ControlSelect
119- options = { data || [ ] }
188+ showLoadMoreButton = { showLoadMoreButton ( ) }
189+ onLoadMore = { onLoadMore }
190+ options = { pageable . data }
120191 // XXX isSearchable if pageQuery? or maybe in all cases? or maybe NOT when pageQuery
121192 selectProps = { {
122193 isLoading : isFetching ,
0 commit comments