mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 16:19:07 -05:00
keep parse result on Store.
This commit is contained in:
parent
b9fc27ea2f
commit
8961eddc43
|
@ -1,4 +1,3 @@
|
||||||
import cats.data.*
|
|
||||||
import cats.effect.*
|
import cats.effect.*
|
||||||
import cats.effect.syntax.all.*
|
import cats.effect.syntax.all.*
|
||||||
import cats.syntax.all.*
|
import cats.syntax.all.*
|
||||||
|
@ -15,40 +14,6 @@ import snow.*
|
||||||
import Utils.*
|
import Utils.*
|
||||||
import Components.*
|
import Components.*
|
||||||
|
|
||||||
object Store {
|
|
||||||
def apply(window: Window[IO]): Resource[IO, Store] = {
|
|
||||||
val key = "nak-input"
|
|
||||||
|
|
||||||
for {
|
|
||||||
inputRef <- SignallingRef[IO].of("").toResource
|
|
||||||
|
|
||||||
_ <- Resource.eval {
|
|
||||||
OptionT(window.localStorage.getItem(key))
|
|
||||||
.foreachF(inputRef.set(_))
|
|
||||||
}
|
|
||||||
|
|
||||||
_ <- window.localStorage
|
|
||||||
.events(window)
|
|
||||||
.foreach {
|
|
||||||
case Storage.Event.Updated(`key`, _, value, _) =>
|
|
||||||
inputRef.set(value)
|
|
||||||
case _ => IO.unit
|
|
||||||
}
|
|
||||||
.compile
|
|
||||||
.drain
|
|
||||||
.background
|
|
||||||
|
|
||||||
_ <- inputRef.discrete
|
|
||||||
.foreach(input => IO.cede *> window.localStorage.setItem(key, input))
|
|
||||||
.compile
|
|
||||||
.drain
|
|
||||||
.background
|
|
||||||
} yield new Store(inputRef)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case class Store(input: SignallingRef[IO, String])
|
|
||||||
|
|
||||||
object Main extends IOWebApp {
|
object Main extends IOWebApp {
|
||||||
def render: Resource[IO, HtmlDivElement[IO]] = Store(window).flatMap {
|
def render: Resource[IO, HtmlDivElement[IO]] = Store(window).flatMap {
|
||||||
store =>
|
store =>
|
||||||
|
@ -120,23 +85,18 @@ object Main extends IOWebApp {
|
||||||
def result(store: Store): Resource[IO, HtmlDivElement[IO]] =
|
def result(store: Store): Resource[IO, HtmlDivElement[IO]] =
|
||||||
div(
|
div(
|
||||||
cls := "w-full flex my-5",
|
cls := "w-full flex my-5",
|
||||||
store.input.map { input =>
|
store.result.map {
|
||||||
if input.trim() == "" then div("")
|
case Left(msg) => div(msg)
|
||||||
else
|
case Right(event: Event) =>
|
||||||
Parser.parseInput(input) match {
|
renderEvent(event)
|
||||||
case Left(msg) => div(msg)
|
case Right(pp: ProfilePointer) => renderProfilePointer(pp)
|
||||||
case Right(event: Event) =>
|
case Right(evp: EventPointer) => renderEventPointer(evp)
|
||||||
renderEvent(event)
|
case Right(sk: PrivateKey) =>
|
||||||
case Right(pp: ProfilePointer) => renderProfilePointer(pp)
|
renderProfilePointer(
|
||||||
case Right(evp: EventPointer) => renderEventPointer(evp)
|
ProfilePointer(pubkey = sk.publicKey.xonly),
|
||||||
case Right(sk: PrivateKey) =>
|
Some(sk)
|
||||||
renderProfilePointer(
|
)
|
||||||
ProfilePointer(pubkey = sk.publicKey.xonly),
|
case Right(addr: AddressPointer) => renderAddressPointer(addr)
|
||||||
Some(sk)
|
|
||||||
)
|
|
||||||
case Right(addr: AddressPointer) => renderAddressPointer(addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,13 +3,13 @@ import cats.syntax.all.*
|
||||||
import scoin.*
|
import scoin.*
|
||||||
import snow.*
|
import snow.*
|
||||||
|
|
||||||
import Components.*
|
type Result = Either[
|
||||||
|
String,
|
||||||
|
Event | PrivateKey | AddressPointer | EventPointer | ProfilePointer
|
||||||
|
]
|
||||||
|
|
||||||
object Parser {
|
object Parser {
|
||||||
def parseInput(input: String): Either[
|
def parseInput(input: String): Result =
|
||||||
String,
|
|
||||||
Event | PrivateKey | AddressPointer | EventPointer | ProfilePointer
|
|
||||||
] =
|
|
||||||
NIP19.decode(input) match {
|
NIP19.decode(input) match {
|
||||||
case Right(pp: ProfilePointer) => Right(pp)
|
case Right(pp: ProfilePointer) => Right(pp)
|
||||||
case Right(evp: EventPointer) => Right(evp)
|
case Right(evp: EventPointer) => Right(evp)
|
||||||
|
|
45
src/main/scala/Store.scala
Normal file
45
src/main/scala/Store.scala
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
import cats.data.*
|
||||||
|
import cats.effect.*
|
||||||
|
import cats.effect.syntax.all.*
|
||||||
|
import cats.syntax.all.*
|
||||||
|
import fs2.concurrent.*
|
||||||
|
import fs2.dom.{Event => _, *}
|
||||||
|
|
||||||
|
case class Store(
|
||||||
|
input: SignallingRef[IO, String],
|
||||||
|
result: SignallingRef[IO, Result]
|
||||||
|
)
|
||||||
|
|
||||||
|
object Store {
|
||||||
|
def apply(window: Window[IO]): Resource[IO, Store] = {
|
||||||
|
val key = "nak-input"
|
||||||
|
|
||||||
|
for {
|
||||||
|
input <- SignallingRef[IO].of("").toResource
|
||||||
|
result <- SignallingRef[IO, Result](Left("")).toResource
|
||||||
|
|
||||||
|
_ <- Resource.eval {
|
||||||
|
OptionT(window.localStorage.getItem(key))
|
||||||
|
.foreachF(input.set(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
_ <- window.localStorage
|
||||||
|
.events(window)
|
||||||
|
.foreach {
|
||||||
|
case Storage.Event.Updated(`key`, _, value, _) =>
|
||||||
|
input.set(value)
|
||||||
|
case _ => IO.unit
|
||||||
|
}
|
||||||
|
.compile
|
||||||
|
.drain
|
||||||
|
.background
|
||||||
|
|
||||||
|
_ <- input.discrete
|
||||||
|
.evalTap(input => IO.cede *> window.localStorage.setItem(key, input))
|
||||||
|
.evalTap(input => result.set(Parser.parseInput(input)))
|
||||||
|
.compile
|
||||||
|
.drain
|
||||||
|
.background
|
||||||
|
} yield Store(input, result)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user