diff --git a/apps/frontend/src/data/decoders/nature.gleam b/apps/frontend/src/data/decoders/kind.gleam similarity index 83% rename from apps/frontend/src/data/decoders/nature.gleam rename to apps/frontend/src/data/decoders/kind.gleam index f4b13f2..0c31fd2 100644 --- a/apps/frontend/src/data/decoders/nature.gleam +++ b/apps/frontend/src/data/decoders/kind.gleam @@ -1,14 +1,14 @@ import gleam/dynamic import gleam/result -pub type Nature { +pub type Kind { Function TypeDefinition TypeAlias Constant } -pub fn decode_nature(dyn) { +pub fn decode_kind(dyn) { use str <- result.try(dynamic.string(dyn)) case str { "function" -> Ok(Function) @@ -19,8 +19,8 @@ pub fn decode_nature(dyn) { } } -pub fn display_nature(nature) { - case nature { +pub fn display_kind(kind) { + case kind { Function -> "Function" TypeDefinition -> "Type" TypeAlias -> "Type Alias" diff --git a/apps/frontend/src/data/decoders/search_result.gleam b/apps/frontend/src/data/decoders/search_result.gleam index 809c308..d5c116f 100644 --- a/apps/frontend/src/data/decoders/search_result.gleam +++ b/apps/frontend/src/data/decoders/search_result.gleam @@ -1,5 +1,5 @@ import data/decoders/implementations.{type Implementations, Implementations} -import data/decoders/nature.{type Nature} +import data/decoders/kind.{type Kind} import data/decoders/signature.{type Signature} import gleam/dynamic import gleam/option.{type Option} @@ -38,7 +38,7 @@ pub type SearchResult { documentation: String, module_name: String, name: String, - nature: Nature, + kind: Kind, package_name: String, json_signature: Signature, metadata: Metadata, @@ -58,7 +58,7 @@ pub fn decode_search_result(dyn) { dynamic.field("documentation", dynamic.string), dynamic.field("module_name", dynamic.string), dynamic.field("name", dynamic.string), - dynamic.field("kind", nature.decode_nature), + dynamic.field("kind", kind.decode_kind), dynamic.field("package_name", dynamic.string), dynamic.field("json_signature", signature.decode_signature), dynamic.field("metadata", decode_metadata), diff --git a/apps/frontend/src/frontend.gleam b/apps/frontend/src/frontend.gleam index 4d7c10a..d981956 100644 --- a/apps/frontend/src/frontend.gleam +++ b/apps/frontend/src/frontend.gleam @@ -40,11 +40,9 @@ pub fn main() { |> options.timeout(5000) |> grille_pain.setup() - let apply_debugger = fn(fun: fn(a, tardis.Instance) -> a) { - fn(app: a) { - result.map(debugger_, fun(app, _)) - |> result.unwrap(app) - } + let apply_debugger = fn(app: a, fun: fn(a, tardis.Instance) -> a) { + result.map(debugger_, fun(app, _)) + |> result.unwrap(app) } let assert Ok(_) = @@ -58,35 +56,53 @@ pub fn main() { fn update(model: Model, msg: Msg) { case msg { - msg.UpdateInput(content) -> - model - |> model.update_input(content) - |> update.none() - msg.SubmitSearch -> { - use <- bool.guard(when: model.input == "", return: #(model, effect.none())) - http.expect_json(search_result.decode_search_results, msg.SearchResults) - |> http.get("http://localhost:3000/search?q=" <> model.input, _) - |> pair.new(model, _) - } - msg.Reset -> - model - |> model.reset() - |> update.none() - msg.SearchResults(search_results) -> { - let toast = - search_results - |> result.map_error(fn(error) { - error - |> toast_error.describe_http_error() - |> option.map(toast.error) - }) - |> result.unwrap_error(option.None) - |> option.unwrap(effect.none()) - search_results - |> result.map(model.update_search_results(model, _)) - |> result.unwrap(model) - |> pair.new(toast) - } + msg.UpdateInput(content) -> update_input(model, content) + msg.SubmitSearch -> submit_search(model) + msg.Reset -> reset(model) msg.None -> update.none(model) + msg.SearchResults(search_results) -> + handle_search_results(model, search_results) } } + +fn update_input(model: Model, content: String) { + model + |> model.update_input(content) + |> update.none() +} + +fn reset(model: Model) { + model + |> model.reset() + |> update.none() +} + +fn submit_search(model: Model) { + use <- bool.guard(when: model.input == "", return: #(model, effect.none())) + http.expect_json(search_result.decode_search_results, msg.SearchResults) + |> http.get("http://localhost:3000/search?q=" <> model.input, _) + |> pair.new(model, _) +} + +fn handle_search_results( + model: Model, + search_results: Result(search_result.SearchResults, http.HttpError), +) { + let toast = display_toast(search_results) + search_results + |> result.map(model.update_search_results(model, _)) + |> result.unwrap(model) + |> pair.new(toast) +} + +fn display_toast( + search_results: Result(search_result.SearchResults, http.HttpError), +) { + search_results + |> result.map_error(fn(error) { + toast_error.describe_http_error(error) + |> option.map(toast.error) + }) + |> result.unwrap_error(option.None) + |> option.unwrap(effect.none()) +} diff --git a/apps/frontend/src/frontend/documentation.gleam b/apps/frontend/src/frontend/documentation.gleam index da6002f..cac8ce4 100644 --- a/apps/frontend/src/frontend/documentation.gleam +++ b/apps/frontend/src/frontend/documentation.gleam @@ -1,5 +1,3 @@ -import frontend/documentation/styles as s -import gleam/io import gleam/list import gleam/string import lustre/attribute as a @@ -8,20 +6,20 @@ import lustre/element/html as h @external(javascript, "../markdown.ffi.mjs", "convert") fn converter(content: String) -> String +fn remove_leading_space(str: String) { + case str { + " " <> rest -> rest + str -> str + } +} + pub fn view(document: String) { let content = document |> string.split("\n") - |> list.map(fn(s) { - case s { - " " <> rest -> rest - _ -> s - } - }) + |> list.map(remove_leading_space) |> string.join("\n") |> converter() - h.div( - [a.attribute("dangerous-unescaped-html", content), a.class("documentation")], - [], - ) + |> a.attribute("dangerous-unescaped-html", _) + h.div([content, a.class("documentation")], []) } diff --git a/apps/frontend/src/frontend/view.gleam b/apps/frontend/src/frontend/view.gleam index 89a2380..378efaa 100644 --- a/apps/frontend/src/frontend/view.gleam +++ b/apps/frontend/src/frontend/view.gleam @@ -1,4 +1,4 @@ -import data/decoders/nature +import data/decoders/kind import data/decoders/search_result import data/decoders/signature.{type Parameter, type Type, Parameter} import data/model.{type Model} @@ -10,7 +10,6 @@ import frontend/styles as s import frontend/types as t import gleam/bool import gleam/int -import gleam/io import gleam/list import gleam/option.{None, Some} import gleam/string @@ -323,7 +322,7 @@ fn view_search_results(search_results: List(search_result.SearchResult)) { use item <- list.map(search_results) h.div([s.search_result()], [ h.div([s.search_details()], [ - h.div([], [h.text(nature.display_nature(item.nature))]), + h.div([], [h.text(kind.display_kind(item.kind))]), h.div([], [ t.dark_white(item.package_name <> "@" <> item.version), t.dark_white("."),