malliaridis commented on code in PR #5257: URL: https://github.com/apache/solr/pull/5257#discussion_r4623039356
########## ui/shared/src/commonMain/kotlin/org/apache/solr/ui/components/indexquery/data/HttpIndexQueryRepository.kt: ########## @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.ui.components.indexquery.data + +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.get +import io.ktor.http.isSuccess +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import org.apache.solr.ui.domain.RequestHandlerOption + +class HttpIndexQueryRepository(private val httpClient: HttpClient) : IndexQueryRepository { + override suspend fun listCollections(): Result<List<String>> { + val response = httpClient.get("api/collections") + return when { + response.status.isSuccess() -> Result.success(response.body<CollectionsResponse>().collections) + else -> Result.failure(Exception("Unknown Error")) + } + } + + override suspend fun listRequestHandlers(collection: String): Result<List<RequestHandlerOption>> { + val response = httpClient.get("api/collections/$collection/config/requestHandler") + return when { + response.status.isSuccess() -> { + val handlers = response.body<RequestHandlersResponse>().config.requestHandler + Result.success( + handlers.map { (name, handler) -> RequestHandlerOption(name, handler.className) }.sortedBy { it.name }, + ) + } + else -> Result.failure(Exception("Unknown Error")) + } + } +} + +@Serializable +private class CollectionsResponse(val collections: List<String> = emptyList()) + +@Serializable +private class RequestHandlersResponse(val config: ConfigResponse) + +@Serializable +private class ConfigResponse(val requestHandler: Map<String, RequestHandlerResponse> = emptyMap()) + +@Serializable +private class RequestHandlerResponse(@SerialName("class") val className: String = "") Review Comment: We haven't defined how or where to put these data classes, so perhaps a small note here for myself and whoever stumbles upon this: In general, putting these private classes in the same file is fine, unless they are needed somewhere else. These classes are tightly coupled with the implementation class, and right now only used by it. However, if the classes are used by other repositories, then we should consider reusing them by marking them internal and putting them in their own file. One more thing: For convention and convenience, we should use `data class` for all DTO classes, including these. It may generate some redundant code, but makes life easier and the cost is low. If we later see issues of size, we may come back to this, but that will likely not be the case. ########## ui/shared/src/commonTest/kotlin/org/apache/solr/ui/components/indexquery/data/HttpIndexQueryRepositoryTest.kt: ########## @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.ui.components.indexquery.data + +import io.ktor.client.engine.mock.respond +import io.ktor.http.ContentType +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode +import io.ktor.http.fullPath +import io.ktor.http.headersOf +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlinx.coroutines.test.runTest +import org.apache.solr.ui.createMockEngine +import org.apache.solr.ui.domain.RequestHandlerOption +import org.apache.solr.ui.testHttpClient + +class HttpIndexQueryRepositoryTest { + + @Test + fun `lists collections from the collections endpoint`() = runTest { + val engine = createMockEngine( + { request -> + assertEquals("/api/collections", request.url.fullPath) + respond( + content = """{"collections":["films","books"]}""", + headers = jsonHeaders, + ) + }, + ) + val httpClient = testHttpClient(engine) + + try { + val result = HttpIndexQueryRepository(httpClient).listCollections() + + assertEquals(listOf("films", "books"), result.getOrThrow()) + assertEquals(1, engine.requestHistory.size) + } finally { + httpClient.close() + } Review Comment: Prefer the kotlin-way here and in all other tests (make sure it compiles on wasm as well): ```kotlin httpClient.use { ... } ``` instead of `try`. And if you go even further, you may introduce a helper to reduce a few lines of code: ```kotlin private suspend fun withRepository( handler: MockRequestHandler, block: suspend (HttpIndexQueryRepository, MockEngine) -> Unit, ) { val engine = createMockEngine(handler) testHttpClient(engine).use { block(HttpIndexQueryRepository(it), engine) } } ``` ########## ui/shared/src/commonTest/kotlin/org/apache/solr/ui/components/indexquery/IndexQueryStateHolderTest.kt: ########## @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.ui.components.indexquery + +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.apache.solr.ui.components.indexquery.data.IndexQueryRepository +import org.apache.solr.ui.components.indexquery.viewmodel.IndexQueryStateHolder +import org.apache.solr.ui.domain.RequestHandlerOption +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull +import kotlin.test.assertTrue + +private val selectHandler = RequestHandlerOption(name = "/select", className = "solr.SearchHandler") + +private class FakeIndexQueryRepository( + var collections: Result<List<String>> = Result.success(emptyList()), + var handlers: Map<String, Result<List<RequestHandlerOption>>> = emptyMap(), +) : IndexQueryRepository { + override suspend fun listCollections(): Result<List<String>> = collections + + override suspend fun listRequestHandlers(collection: String): Result<List<RequestHandlerOption>> = + handlers[collection] ?: Result.success(emptyList()) +} + +class IndexQueryStateHolderTest { + + @Test + fun testLoadCollectionsPopulatesList() = runTest { + val repository = FakeIndexQueryRepository(collections = Result.success(listOf("films", "shop"))) + val holder = IndexQueryStateHolder(repository, this) + + holder.loadCollections() + advanceUntilIdle() + + assertEquals(listOf("films", "shop"), holder.uiState.value.collections) + assertFalse(holder.uiState.value.collectionsLoading) + } + + @Test + fun testSelectingCollectionLoadsItsHandlersAndClearsHandlerChoice() = runTest { + val repository = FakeIndexQueryRepository( + handlers = mapOf("films" to Result.success(listOf(selectHandler))), + ) + val holder = IndexQueryStateHolder(repository, this) + + holder.selectCollection("films") + advanceUntilIdle() + + assertEquals(listOf(selectHandler), holder.uiState.value.handlers) + assertNull(holder.uiState.value.selectedHandler) + assertFalse(holder.uiState.value.handlersLoading) + } Review Comment: Clearing handler choise may be an unwanted behavior, especially if the user is using a default handler, forgot to choose the right collection, switches to the right collection and all its input is lost. ########## ui/shared/src/commonMain/kotlin/org/apache/solr/ui/components/indexquery/viewmodel/IndexQueryStateHolder.kt: ########## @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.ui.components.indexquery.viewmodel + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import org.apache.solr.ui.components.indexquery.data.IndexQueryRepository +import org.apache.solr.ui.domain.RequestHandlerOption + +data class IndexQueryUiState( + val collections: List<String> = emptyList(), + val collectionsLoading: Boolean = false, + val selectedCollection: String? = null, + val handlers: List<RequestHandlerOption> = emptyList(), + val handlersLoading: Boolean = false, + val selectedHandler: String? = null, + val error: Boolean = false, +) + +class IndexQueryStateHolder( Review Comment: A state holder is only used right now for cases where the state is shared across multiple components and kept in sync. Right now, the Index and Query does not have such case, at least none I am aware of. If there is, please correct me. If you do not have any other use of the index-query state, skip the state holder and use directly a ViewModel that will be added anyway. And if you add a viewmodel, you can and should integrate the UI element into the current UI as well, since it would be ready to use. ########## ui/shared/src/commonMain/kotlin/org/apache/solr/ui/views/components/HeaderDropdown.kt: ########## @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.ui.views.components + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.widthIn +import androidx.compose.material3.DropdownMenuItem +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.ExposedDropdownMenuAnchorType +import androidx.compose.material3.ExposedDropdownMenuBox +import androidx.compose.material3.ExposedDropdownMenuDefaults +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp + +/** An option shown in a [HeaderDropdown]. */ +data class HeaderDropdownOption( + val key: String, + val label: String, + val subtitle: String? = null, +) + +/** The available options, loading state, selection, and selection callback for a [HeaderDropdown]. */ +data class HeaderDropdownState( + val options: List<HeaderDropdownOption>, + val isLoading: Boolean, + val selectedKey: String?, + val onSelect: (String) -> Unit, + val isEnabled: Boolean = true, +) + +/** + * A reusable dropdown field for compact headers, with an optional subtitle for each option. + * + * @param label Label shown on the field. + * @param state Options, loading state, selection, and callback for the dropdown. + * @param testTag Test tag applied to the field. + * @param modifier Modifier applied to the dropdown container. + * @param placeholder Optional text shown when there is no selection. + */ +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun HeaderDropdown( Review Comment: Nicely implemented the component in a reusable fashion. For a follow-up PR it makes sense to remove the code from other screens that do exactly the same, like the `ConfigsetsDropdown`. ########## ui/shared/src/commonMain/kotlin/org/apache/solr/ui/views/indexquery/IndexQueryHeader.kt: ########## @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.ui.views.indexquery + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.width +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.unit.dp +import org.apache.solr.ui.shared.generated.resources.Res +import org.apache.solr.ui.shared.generated.resources.label_collection_core +import org.apache.solr.ui.shared.generated.resources.label_request_handler +import org.apache.solr.ui.shared.generated.resources.no_collections_available +import org.apache.solr.ui.shared.generated.resources.no_request_handlers +import org.apache.solr.ui.shared.generated.resources.placeholder_select_request_handler +import org.apache.solr.ui.views.components.HeaderDropdown +import org.apache.solr.ui.views.components.HeaderDropdownState +import org.jetbrains.compose.resources.stringResource + +/** + * Header of the Index & Query section with a collection dropdown and a request handler dropdown. + * + * The header only renders what it is given. Loading and selection happen in the caller. + * + * @param collectionDropdown Dropdown state for the available collections. + * @param requestHandlers Dropdown state for the selected collection's request handlers. + * @param modifier Modifier to apply to the root column. + */ +@Composable +fun IndexQueryHeader( + collectionDropdown: HeaderDropdownState, + requestHandlers: HeaderDropdownState, + modifier: Modifier = Modifier, +) { + val requestHandlersDropdown = requestHandlers.copy( + isEnabled = requestHandlers.isEnabled && + !collectionDropdown.isLoading && + collectionDropdown.selectedKey != null, Review Comment: Can be moved into a variable like `val hasLoadedData = !collectionDropdown.isLoading && collectionDropdown.options.isEmpty()` and be reused for improving readability. In theory `collectionDropdown.selectedKey` can also be just `val hasSelection = collectionDropdown.selectedKey != null`, again for readability. ########## ui/shared/src/jvmMain/kotlin/org/apache/solr/ui/preview/indexquery/PreviewIndexQueryHeader.kt: ########## @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.ui.preview.indexquery + +import androidx.compose.runtime.Composable +import androidx.compose.ui.tooling.preview.Preview +import org.apache.solr.ui.preview.PreviewContainer +import org.apache.solr.ui.views.components.HeaderDropdownOption +import org.apache.solr.ui.views.components.HeaderDropdownState +import org.apache.solr.ui.views.indexquery.IndexQueryHeader + +@Composable +@Preview +internal fun PreviewBasicIndexQueryHeader() = PreviewContainer { + IndexQueryHeader( + collectionDropdown = HeaderDropdownState( + options = listOf("dog", "cat").map { HeaderDropdownOption(key = it, label = it) }, + isLoading = false, + selectedKey = "cat", + onSelect = {}, + ), + requestHandlers = HeaderDropdownState( + options = listOf(HeaderDropdownOption(key = "Handle", label = "Handle", subtitle = "solr.myRequestHandler")), + isLoading = false, + selectedKey = null, + onSelect = {}, + ), + ) +} + +@Composable +@Preview +internal fun PreviewLoadingIndexQueryHeader() = PreviewContainer { + IndexQueryHeader( + collectionDropdown = HeaderDropdownState( + options = listOf("dog", "cat").map { HeaderDropdownOption(key = it, label = it) }, + isLoading = true, + selectedKey = "cat", + onSelect = {}, + ), + requestHandlers = HeaderDropdownState( + options = listOf(HeaderDropdownOption(key = "Handle", label = "Handle", subtitle = "solr.myRequestHandler")), + isLoading = true, + selectedKey = "Handle", + onSelect = {}, + ), + ) +} + +@Composable +@Preview +internal fun PreviewNoCollectionsIndexQueryHeader() = PreviewContainer { + IndexQueryHeader( + collectionDropdown = HeaderDropdownState( + options = emptyList(), + isLoading = false, + selectedKey = null, + onSelect = {}, + ), + requestHandlers = HeaderDropdownState( + options = emptyList(), + isLoading = false, + selectedKey = null, + onSelect = {}, + ), + ) +} + +@Composable +@Preview +internal fun PreviewNoRequestHandlersIndexQueryHeader() = PreviewContainer { + IndexQueryHeader( + collectionDropdown = HeaderDropdownState( + options = listOf(HeaderDropdownOption(key = "dog", label = "dog")), Review Comment: Would be better to use more suitable, real-world placeholder texts, ideally ones that fit the Solr context or come from examples, like `techproducts`, `films` etc. ########## ui/shared/src/commonMain/kotlin/org/apache/solr/ui/views/indexquery/IndexQueryHeader.kt: ########## @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.ui.views.indexquery + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.width +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.unit.dp +import org.apache.solr.ui.shared.generated.resources.Res +import org.apache.solr.ui.shared.generated.resources.label_collection_core +import org.apache.solr.ui.shared.generated.resources.label_request_handler +import org.apache.solr.ui.shared.generated.resources.no_collections_available +import org.apache.solr.ui.shared.generated.resources.no_request_handlers +import org.apache.solr.ui.shared.generated.resources.placeholder_select_request_handler +import org.apache.solr.ui.views.components.HeaderDropdown +import org.apache.solr.ui.views.components.HeaderDropdownState +import org.jetbrains.compose.resources.stringResource + +/** + * Header of the Index & Query section with a collection dropdown and a request handler dropdown. + * + * The header only renders what it is given. Loading and selection happen in the caller. + * + * @param collectionDropdown Dropdown state for the available collections. + * @param requestHandlers Dropdown state for the selected collection's request handlers. + * @param modifier Modifier to apply to the root column. + */ +@Composable +fun IndexQueryHeader( + collectionDropdown: HeaderDropdownState, + requestHandlers: HeaderDropdownState, + modifier: Modifier = Modifier, +) { + val requestHandlersDropdown = requestHandlers.copy( + isEnabled = requestHandlers.isEnabled && + !collectionDropdown.isLoading && + collectionDropdown.selectedKey != null, + ) + + Column(modifier = modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { + Row(horizontalArrangement = Arrangement.spacedBy(16.dp)) { + HeaderDropdown( Review Comment: Short comments infront of elements like `HeaderDropdown` with what data it holds prooved to be useful for reviewers. Here for example you could use `// collections dropdown` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
