From bfe534d1cebd9624b2a989d884bb3c92a46b34d6 Mon Sep 17 00:00:00 2001 From: d1snin Date: Sun, 5 May 2024 03:55:07 +0300 Subject: [PATCH] fix: translation doesnt apply on space listings --- .../dev/d1s/beam/client/DefaultBeamClient.kt | 6 +-- .../beam/daemon/configuration/StatusPages.kt | 7 +++ .../d1s/beam/daemon/service/SpaceService.kt | 1 + .../beam/ui/component/SpaceCardComponent.kt | 5 +- .../ui/component/SpaceListingComponent.kt | 54 +++++++++---------- .../d1s/beam/ui/util/CurrentTranslation.kt | 3 ++ http/daemon.http | 31 +++++------ 7 files changed, 55 insertions(+), 52 deletions(-) diff --git a/beam-client/src/commonMain/kotlin/dev/d1s/beam/client/DefaultBeamClient.kt b/beam-client/src/commonMain/kotlin/dev/d1s/beam/client/DefaultBeamClient.kt index 57a2ab33..469c516a 100644 --- a/beam-client/src/commonMain/kotlin/dev/d1s/beam/client/DefaultBeamClient.kt +++ b/beam-client/src/commonMain/kotlin/dev/d1s/beam/client/DefaultBeamClient.kt @@ -353,9 +353,9 @@ public class DefaultBeamClient( public override suspend fun getResolvedTranslation(languageCode: LanguageCode): Result = runCatching { - httpClient.get(Paths.GET_RESOLVED_TRANSLATION) { - setLanguageCode(languageCode) - }.body() + val path = Paths.GET_RESOLVED_TRANSLATION.replaceLanguageCodePlaceholder(languageCode) + + httpClient.get(path).body() } override suspend fun getTranslations(): Result = diff --git a/beam-daemon/src/main/kotlin/dev/d1s/beam/daemon/configuration/StatusPages.kt b/beam-daemon/src/main/kotlin/dev/d1s/beam/daemon/configuration/StatusPages.kt index 0de7d228..967a63f3 100644 --- a/beam-daemon/src/main/kotlin/dev/d1s/beam/daemon/configuration/StatusPages.kt +++ b/beam-daemon/src/main/kotlin/dev/d1s/beam/daemon/configuration/StatusPages.kt @@ -22,6 +22,7 @@ import dev.d1s.beam.bundle.util.respondHtml import dev.d1s.beam.commons.Paths import dev.d1s.exkt.ktor.server.koin.configuration.ApplicationConfigurer import dev.d1s.exkt.ktor.server.statuspages.HttpStatusException +import dev.d1s.exkt.ktor.server.statuspages.httpStatusException import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.config.* @@ -47,6 +48,12 @@ object StatusPages : ApplicationConfigurer, KoinComponent { handleExceptions() handleStatuses() handleNotFoundStatus() + + httpStatusException { call, exception -> + val status = exception.status + val message = exception.toMessage(status) + call.respond(status, message) + } } } diff --git a/beam-daemon/src/main/kotlin/dev/d1s/beam/daemon/service/SpaceService.kt b/beam-daemon/src/main/kotlin/dev/d1s/beam/daemon/service/SpaceService.kt index 3a6c0727..2aa4a37c 100644 --- a/beam-daemon/src/main/kotlin/dev/d1s/beam/daemon/service/SpaceService.kt +++ b/beam-daemon/src/main/kotlin/dev/d1s/beam/daemon/service/SpaceService.kt @@ -112,6 +112,7 @@ class DefaultSpaceService : SpaceService, KoinComponent { val addedSpace = handleUniqueSlugViolation { spaceRepository.addSpace(space).getOrThrow() } + val translatedSpace = translateOptionally(addedSpace, languageCode) val translatedSpaceDto = spaceDtoConverter.convertToDto(translatedSpace) diff --git a/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/component/SpaceCardComponent.kt b/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/component/SpaceCardComponent.kt index 7b971f6d..bbcddc83 100644 --- a/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/component/SpaceCardComponent.kt +++ b/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/component/SpaceCardComponent.kt @@ -81,7 +81,7 @@ class SpaceCardComponent : Component(::Config), KoinC } private suspend fun getSpace() = - config.spaceIdentifier.value?.let { + config.space.value ?: config.spaceIdentifier.value?.let { spaceCache.getOrPut(it) { client.getSpace(it, currentLanguageCode).getOrNull() } @@ -149,7 +149,8 @@ class SpaceCardComponent : Component(::Config), KoinC class Config { - val spaceIdentifier = atomic(null) + val space = atomic(null) + val spaceIdentifier = atomic(null) val bare = atomic(null) diff --git a/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/component/SpaceListingComponent.kt b/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/component/SpaceListingComponent.kt index 647da981..9801f478 100644 --- a/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/component/SpaceListingComponent.kt +++ b/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/component/SpaceListingComponent.kt @@ -43,6 +43,8 @@ import kotlinx.atomicfu.atomic import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import org.koin.core.component.KoinComponent import org.koin.core.component.get import org.koin.core.component.inject @@ -54,40 +56,26 @@ class SpaceListingComponent : Component(::Config), private val beamClient by inject() - private val spaces = ObservableValue(emptyList) - - private val paginator by lazy { - val limit = if (currentSpace != null) PAGE_LIMIT + 1 else PAGE_LIMIT - - Paginator(limit, currentPage = 0) - } - private val renderingScope = CoroutineScope(Dispatchers.Main) - private val initialized = atomic(false) + private var mainPanel: SimplePanel? = null override fun SimplePanel.render(): Effect { val (state, effect) = Effect.lazy() - fun render() { - renderingScope.launch { + renderingScope.launch { + renderMutex.withLock { initialize() renderSpaceContainer(state) } } - currentTranslationObservable.subscribeSkipping { - reset() - render() - } - - render() - return effect } private suspend fun initialize() { if (!initialized.getAndSet(true)) { + mainPanel?.dispose() getMoreSpaces() } } @@ -106,13 +94,6 @@ class SpaceListingComponent : Component(::Config), } } - private fun SimplePanel.reset() { - getChildren().forEach { it.dispose() } - spaces.value = emptyList - paginator.currentPage = 0 - initialized.value = false - } - private fun SimplePanel.renderBoundContainer( effectState: MutableLazyEffectState, block: SimplePanel.(FetchedSpaces) -> Unit @@ -146,7 +127,7 @@ class SpaceListingComponent : Component(::Config), val spaceCard = get>(Qualifier.SpaceCardComponent) render(spaceCard) { - this.spaceIdentifier.value = space.id + this.space.value = space this.fullHeight.value = true } } @@ -170,8 +151,8 @@ class SpaceListingComponent : Component(::Config), } } - private suspend fun getSpaces(offset: Int = paginator.offset): FetchedSpaces? = - beamClient.getSpaces(paginator.limit, offset, currentLanguageCode).getOrNull()?.let { page -> + private suspend fun getSpaces(): FetchedSpaces? = + beamClient.getSpaces(paginator.limit, paginator.offset, currentLanguageCode).getOrNull()?.let { page -> val spaces = page.elements val filteredSpaces = spaces.filterSpaces() val totalCount = page.count() @@ -232,10 +213,23 @@ class SpaceListingComponent : Component(::Config), val singleColumn = atomic(false) } - private companion object { + companion object { private const val PAGE_LIMIT = 4 - private val emptyList: FetchedSpaces = listOf() to 0 + private val spaces = ObservableValue(listOf() to 0) + private val initialized = atomic(false) + + private val renderMutex = Mutex() + + private val paginator by lazy { + Paginator(PAGE_LIMIT, currentPage = 0) + } + + fun reset() { + spaces.value = listOf() to 0 + paginator.currentPage = 0 + initialized.value = false + } } } \ No newline at end of file diff --git a/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/util/CurrentTranslation.kt b/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/util/CurrentTranslation.kt index 4869b35d..fc84001c 100644 --- a/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/util/CurrentTranslation.kt +++ b/beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/util/CurrentTranslation.kt @@ -19,6 +19,7 @@ package dev.d1s.beam.ui.util import dev.d1s.beam.client.BeamClient import dev.d1s.beam.commons.* import dev.d1s.beam.ui.Qualifier +import dev.d1s.beam.ui.component.SpaceListingComponent import dev.d1s.beam.ui.state.CurrentSpaceContentChangeObservable import dev.d1s.beam.ui.state.Observable import dev.d1s.beam.ui.state.SpaceContentChange @@ -70,6 +71,8 @@ fun setCurrentTranslation(translation: Translation) { actualizeCurrentTranslation() + SpaceListingComponent.reset() + (currentSpaceContentChangeObservable as? CurrentSpaceContentChangeObservable)?.let { observable -> currentSpace?.id?.let { observable.setCurrentSpaceContent(currentRows with currentBlocks) diff --git a/http/daemon.http b/http/daemon.http index 8ea848bb..e6f0909c 100644 --- a/http/daemon.http +++ b/http/daemon.http @@ -6,15 +6,15 @@ Authorization: Bearer {{token}} Content-Type: application/json { - "slug": "test-1", + "slug": "test-2", "metadata": {}, "view": { "theme": "catppuccin-mocha", "icon": null, "favicon": null, "preview": null, - "title": "test", - "description": "test", + "title": "${test-1.title}", + "description": "${test-1.description}", "remark": null } } @@ -37,7 +37,7 @@ Content-Type: application/json } ### GET Space -GET http://localhost:8573/api/spaces/917824b0-3b07-42f3-951a-dc81e30fde18?language=en +GET http://localhost:8573/api/spaces/test-1?language=en ### GET Spaces GET http://localhost:8573/api/spaces?limit=10&offset=0 @@ -96,15 +96,15 @@ Content-Type: application/json "size": "EXTRA_LARGE", "entities": [ { - "type": "space", + "type": "text", "parameters": { - "identifier": "root" + "value": "${test-1.title}" } } ], "metadata": { }, - "spaceId": "root" + "spaceId": "test-1" } ### GET Blocks @@ -156,17 +156,17 @@ Content-Type: application/json } ### POST Translation -POST http://localhost:8573/api/translations/en +POST http://localhost:8573/api/translations/ru Authorization: Bearer {{token}} Content-Type: application/json { - "languageName": "English", + "languageName": null, "default": false, "translations": { "ui.icon-alt": "Space icon", "ui.default-title": "Beam", - "ui.default-remark": "Running [Beam][https://github.com/d1snin/beam] VERSION ", + "ui.default-remark": "Running [Beam][https://github.com/d1snin/beam] v", "ui.failure-card.not-found.icon-alt": "404 icon", "ui.failure-card.not-found.message": "We ended up with nothing.", "ui.failure-card.empty-space.icon-alt": "Empty space icon", @@ -177,9 +177,8 @@ Content-Type: application/json "ui.space-listing.message": "Explore other spaces:", "ui.space-listing.fetch-more-button": "Fetch more", "ui.block.collapsed-content-entity.button.message": "More", - "test-1.title": "en title", - "test-1.description": "en description", - "random.text": "random text emmm eeee" + "test-1.title": "ru title", + "test-1.description": "ru description" } } @@ -203,7 +202,7 @@ Content-Type: application/json "translations": { "ui.icon-alt": "Space icon", "ui.default-title": "Beam", - "ui.default-remark": "Running [Beam][https://github.com/d1snin/beam] VERSION ", + "ui.default-remark": "Running [Beam][https://github.com/d1snin/beam] v", "ui.failure-card.not-found.icon-alt": "404 icon", "ui.failure-card.not-found.message": "We ended up with nothing.", "ui.failure-card.empty-space.icon-alt": "Empty space icon", @@ -213,9 +212,7 @@ Content-Type: application/json "ui.footer.language-switcher.message": "Language...", "ui.space-listing.message": "Explore other spaces:", "ui.space-listing.fetch-more-button": "Fetch more", - "ui.block.collapsed-content-entity.button.message": "More", - "test-1.title": "en title", - "test-1.description": "en description" + "ui.block.collapsed-content-entity.button.message": "More" } }