nak/src/main/scala/Main.scala

123 lines
3.3 KiB
Scala
Raw Normal View History

2023-03-18 20:42:41 -04:00
import cats.effect.*
import cats.effect.syntax.all.*
import cats.syntax.all.*
import fs2.concurrent.*
import fs2.dom.{Event => _, *}
import io.circe.parser.*
import io.circe.syntax.*
2023-03-18 19:41:54 -04:00
import calico.*
import calico.html.io.{*, given}
import calico.syntax.*
import scoin.*
2023-03-24 06:25:31 -04:00
import snow.*
import Utils.*
2023-03-24 19:46:59 -04:00
import Components.*
2023-03-18 17:07:07 -04:00
2023-03-18 19:41:54 -04:00
object Main extends IOWebApp {
2023-03-23 20:04:53 -04:00
def render: Resource[IO, HtmlDivElement[IO]] = Store(window).flatMap {
store =>
div(
2023-03-23 20:04:53 -04:00
cls := "flex w-full h-full flex-col items-center justify-center",
div(
cls := "w-4/5",
2023-04-05 15:55:12 -04:00
h1(
cls := "px-1 py-2 text-center text-xl",
img(
cls := "inline-block w-8 mr-2",
src := "/favicon.ico"
),
"nostr army knife"
),
div(
2023-03-24 06:25:31 -04:00
cls := "flex my-3",
input(store),
2023-03-24 06:25:31 -04:00
actions(store)
),
result(store)
)
2023-03-23 20:04:53 -04:00
)
}
2023-03-24 06:25:31 -04:00
def actions(store: Store): Resource[IO, HtmlDivElement[IO]] =
div(
cls := "flex flex-col space-y-1 my-3",
store.result.map {
case Right(_: Event) =>
button(
Styles.button,
"format",
onClick --> (_.foreach(_ =>
store.input.update(original =>
parse(original).toOption
.map(_.printWith(jsonPrinter))
.getOrElse(original)
)
))
2023-03-24 06:25:31 -04:00
)
case _ => div("")
},
2023-03-24 06:25:31 -04:00
button(
Styles.button,
"generate event",
onClick --> (_.foreach(_ =>
store.input.set(
Event(
kind = 1,
content = "hello world"
).sign(keyOne)
2023-03-24 06:25:31 -04:00
.asJson
.printWith(jsonPrinter)
)
))
2023-03-25 08:49:33 -04:00
),
button(
Styles.button,
"generate keypair",
onClick --> (_.foreach(_ =>
store.input.set(
NIP19.encode(PrivateKey(randomBytes32()))
)
))
2023-03-24 06:25:31 -04:00
)
)
2023-03-23 20:04:53 -04:00
def input(store: Store): Resource[IO, HtmlDivElement[IO]] =
div(
cls := "w-full grow",
2023-03-23 20:04:53 -04:00
div(
cls := "w-full flex justify-center",
textArea.withSelf { self =>
(
cls := "w-full max-h-96 p-3 rounded",
2023-03-24 06:25:31 -04:00
styleAttr := "min-height: 280px; font-family: monospace",
2023-03-24 19:46:59 -04:00
spellCheck := false,
2023-03-25 13:42:28 -04:00
placeholder := "paste something nostric (event JSON, nprofile, npub, nevent etc or hex key or id)",
2023-03-23 20:04:53 -04:00
onInput --> (_.foreach(_ =>
self.value.get.flatMap(store.input.set)
)),
value <-- store.input
)
2023-03-23 20:04:53 -04:00
}
)
)
2023-03-23 20:04:53 -04:00
def result(store: Store): Resource[IO, HtmlDivElement[IO]] =
div(
cls := "w-full flex my-5",
2023-03-24 21:12:11 -04:00
store.result.map {
case Left(msg) => div(msg)
case Right(bytes: ByteVector32) => render32Bytes(bytes)
case Right(event: Event) => renderEvent(event, store)
case Right(pp: ProfilePointer) => renderProfilePointer(pp)
case Right(evp: EventPointer) => renderEventPointer(evp)
2023-03-24 21:12:11 -04:00
case Right(sk: PrivateKey) =>
renderProfilePointer(
ProfilePointer(pubkey = sk.publicKey.xonly),
Some(sk)
)
case Right(addr: AddressPointer) => renderAddressPointer(addr)
}
2023-03-23 20:04:53 -04:00
)
2023-03-18 19:41:54 -04:00
}