Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
- Use WASM for web instead of JS
  • Loading branch information
DRSchlaubi committed Dec 7, 2024
1 parent 99663be commit 96a9840
Show file tree
Hide file tree
Showing 34 changed files with 162 additions and 195 deletions.
2 changes: 1 addition & 1 deletion .idea/caches/deviceStreaming.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions app/desktop/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.org.jline.utils.OSUtils

plugins {
alias(libs.plugins.kotlin.compose)
Expand Down Expand Up @@ -49,8 +48,8 @@ compose.desktop {
"java.naming" // required by logback
)
when {
OSUtils.IS_WINDOWS -> targetFormats(TargetFormat.AppImage)
OSUtils.IS_OSX -> targetFormats(TargetFormat.Pkg)
HostManager.hostIsMingw -> targetFormats(TargetFormat.AppImage)
HostManager.hostIsMac -> targetFormats(TargetFormat.Pkg)
else -> targetFormats(TargetFormat.Deb)
}

Expand Down Expand Up @@ -113,7 +112,7 @@ tasks {
archiveExtension = "tar.gz"
}

if (OSUtils.IS_WINDOWS) {
if (true) {
registerMsixTasks()
registerMsixTasks("MSStore", msStore = true)
}
Expand Down Expand Up @@ -169,7 +168,7 @@ fun TaskContainerScope.registerMsixTasks(prefix: String = "", msStore: Boolean =

afterEvaluate {
"packageReleaseDistributionForCurrentOS" {
if (OSUtils.IS_WINDOWS) {
if (HostManager.hostIsMingw) {
dependsOn(finalizeMsixWorkspace)
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/desktop/src/nonWindowsMain/kotlin/AuthorizationServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ fun Application.authModule(onAuth: () -> Unit, scope: CoroutineScope) {
install(CORS) {
allowMethod(HttpMethod.Post)
val baseUrl = getUrl()
allowHost(baseUrl.host, listOf(baseUrl.protocol.name))
allowHost(baseUrl.hostWithPortIfSpecified, listOf(baseUrl.protocol.name))
}
routing {
post("login") {
get("login") {
val token = call.parameters["token"] ?: throw BadRequestException("Missing token")

saveConfig(Config(token))
Expand Down
2 changes: 1 addition & 1 deletion app/desktop/src/nonWindowsMain/kotlin/ConfigFile.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:OptIn(ExperimentalSerializationApi::class)
@file:OptIn(ExperimentalSerializationApi::class, ExperimentalSerializationApi::class)

package dev.schlaubi.tonbrett.app.desktop

Expand Down
4 changes: 2 additions & 2 deletions app/desktop/uwp_helper/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import org.jetbrains.kotlin.org.jline.utils.OSUtils
import org.jetbrains.kotlin.konan.target.HostManager

plugins {
java
Expand Down Expand Up @@ -27,7 +27,7 @@ tasks {
outputs.dir(jextractOutput)

// I cannot figure out how to change the path on GitHub Actions
val command = if (OSUtils.IS_WINDOWS) {
val command = if (HostManager.hostIsMingw) {
"jextract.bat"
} else {
"jextract"
Expand Down
8 changes: 2 additions & 6 deletions app/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ kotlin {
withApple()
withJvm()
withJs()
withWasmJs()
}

group("mobile") {
Expand All @@ -37,6 +38,7 @@ kotlin {
withJs()
withApple()
withAndroidTarget()
withWasmJs()
}
}
}
Expand Down Expand Up @@ -66,12 +68,6 @@ kotlin {
}
}

jsMain {
dependencies {
api("org.jetbrains.kotlin:kotlinx-atomicfu-runtime:2.0.20")
}
}

named("desktopMain") {
dependencies {
implementation(libs.kmongo.id.serialization)
Expand Down
4 changes: 3 additions & 1 deletion app/shared/src/commonMain/kotlin/components/SearchBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ fun SearchBar(updateSounds: SoundUpdater) {
},
colors = TextFieldDefaults.colors(
unfocusedTextColor = ColorScheme.current.textColor,
focusedTextColor = ColorScheme.current.textColor
focusedTextColor = ColorScheme.current.textColor,
focusedContainerColor = ColorScheme.current.searchBarColor,
unfocusedContainerColor = ColorScheme.current.searchBarColor,
)
)
},
Expand Down
17 changes: 12 additions & 5 deletions app/web/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl

plugins {
alias(libs.plugins.kotlin.compose)
kotlin("multiplatform")
Expand All @@ -9,8 +11,13 @@ repositories {
}

kotlin {
js(IR) {
browser()
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
browser {
webpackTask {
mainOutputFileName = ".proxy/web.js"
}
}
binaries.executable()
compilations.all {
packageJson {
Expand Down Expand Up @@ -41,7 +48,7 @@ kotlin {
implementation(compose.material)
implementation(compose.components.resources)
implementation(compose.materialIconsExtended)
implementation(npm("@discord/embedded-app-sdk", "1.4.2"))
implementation(npm("@discord/embedded-app-sdk", "1.7.1"))
}
}
}
Expand All @@ -50,11 +57,11 @@ kotlin {
tasks {
val copyIcon by creating(Copy::class) {
from(rootProject.file("icons/logo.ico"))
into(layout.buildDirectory.dir("distributions"))
into(layout.buildDirectory.dir("dist/wasmJs/productionExecutable"))
rename { "favicon.ico" }
}

named("jsMainClasses") {
named("wasmJsMainClasses") {
dependsOn(copyIcon)
}
}

This file was deleted.

14 changes: 13 additions & 1 deletion app/web/src/templates/discord-activity.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@
<head>
<meta charset="UTF-8">
<title>Tonbrett</title>
<script src="/.proxy/api/soundboard/ui/skiko.js"> </script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
// glue code for Kotlin/WASM
// noinspection JSUnusedGlobalSymbols
function authorize(discordSdk, appId, state) {
return discordSdk.commands.authorize({
client_id: appId,
scope: ["identify"],
response_type: "code",
state: state
});
}
</script>
<style>
body {
background-color: rgb(17, 18, 20);
Expand Down
2 changes: 1 addition & 1 deletion app/web/src/templates/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<link rel="icon" type="image/png" href="/soundboard/ui/favicon.ico"/>
<title>Tonbrett</title>
<script src="/soundboard/ui/skiko.js"> </script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<style>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.ProvideTextStyle
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.CheckCircle
Expand All @@ -26,35 +21,34 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import dev.schlaubi.tonbrett.app.LocalStrings
import dev.schlaubi.tonbrett.app.ColorScheme
import dev.schlaubi.tonbrett.app.LocalStrings
import dev.schlaubi.tonbrett.common.authServerPort
import io.ktor.client.fetch.*
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.browser.window
import kotlinx.coroutines.await
import kotlinx.coroutines.delay
import io.github.oshai.kotlinlogging.KotlinLogging
import org.w3c.dom.url.URLSearchParams
import org.w3c.fetch.RequestInit
import org.w3c.xhr.XMLHttpRequest
import kotlin.time.Duration.Companion.seconds

private val LOG = KotlinLogging.logger { }

private fun post(): RequestInit = js("{method: 'POST'}")

@Composable
fun AuthorizationScreen(cli: Boolean, protocol: Boolean) {
val strings = LocalStrings.current
val token =
remember { URLSearchParams(window.location.search).get("token") ?: error("Missing token") }
remember { URLSearchParams(window.location.search.toJsString()).get("token") ?: error("Missing token") }
if (!cli) {
LaunchedEffect(Unit) {
try {
if (protocol) {
window.location.href = "tonbrett://login?token=$token"
} else {
fetch(
"http://localhost:$authServerPort/login?token=$token",
object : RequestInit {
override var method: String? = "POST"
}).await()
window.fetch("http://localhost:$authServerPort/login?token=$token").await()
}
} catch (e: Throwable) {
LOG.error(e) { "Could not propagate token" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@file:JsModule("@discord/embedded-app-sdk")

package dev.schlaubi.tonbrett.app.web

import kotlin.js.Promise

external interface AuthorizeInput : JsAny {
@JsName("client_id")
val clientId: JsString
val scope: JsArray<JsString>

@JsName("response_type")
val responseType: JsString

@JsName("code_challenge")
val codeChallenge: JsString?
val state: JsString?
val prompt: JsString?

@JsName("code_challenge_method")
val codeChallengeMethod: JsString?
}

external class AuthorizeResponse : JsAny {
val code: JsString
}

external class DiscordSDK constructor(oauthClientId: String) : JsAny {
val commands: Commands
fun ready(): Promise<JsAny?>

class Commands {
fun authorize(input: AuthorizeInput): Promise<AuthorizeResponse>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ import dev.schlaubi.tonbrett.app.api.getUrl
import dev.schlaubi.tonbrett.app.title
import dev.schlaubi.tonbrett.common.AuthRefreshResponse
import dev.schlaubi.tonbrett.common.Route
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.plugins.resources.Resources
import io.ktor.client.plugins.resources.post
import io.ktor.http.takeFrom
import io.ktor.serialization.kotlinx.json.json
import io.ktor.util.generateNonce
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.resources.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.util.*
import kotlinx.coroutines.await
import org.jetbrains.skiko.wasm.onWasmReady

private val apiClient by lazy {
HttpClient {
Expand All @@ -49,13 +47,13 @@ private enum class State {

private val context = AppContext()

fun discordActivity() = onWasmReady {
fun discordActivity() {
try {
CanvasBasedWindow(title) {
var state by remember { mutableStateOf(State.INITIALIZING) }

LaunchedEffect(Unit) {
discordSdk.ready().await()
discordSdk.ready().await<JsAny?>()

state = if (context.isSignedIn) State.RUNNING else State.AUTHORIZING
println("Switching state to $state")
Expand Down Expand Up @@ -84,14 +82,9 @@ fun discordActivity() = onWasmReady {

private suspend fun requestToken(): AuthRefreshResponse {
val state = generateNonce()
val input = Any().asDynamic()
input.client_id = appId
input.scope = arrayOf("identify")
input.response_type = "code"
input.state = state

val response = discordSdk.commands.authorize(input).await()
return apiClient.exchangeToken(response.code, state)
val response = discordSdk.authorize(appId, state).await<AuthorizeResponse>()
return apiClient.exchangeToken(response.code.toString(), state)
}

/**
Expand Down
Loading

0 comments on commit 96a9840

Please sign in to comment.