noredink-ui/styleguide-app/EllieLink.elm

109 lines
3.1 KiB
Elm
Raw Normal View History

module EllieLink exposing (Config, SectionExample, view)
2022-03-29 19:33:00 +03:00
import Dict exposing (Dict)
import Html.Styled exposing (..)
2022-03-29 19:33:00 +03:00
import Http
import Nri.Ui.ClickableText.V3 as ClickableText
import Url.Builder
2022-03-29 19:33:00 +03:00
type alias Config =
{ packageDependencies : Result Http.Error (Dict String String)
}
type alias SectionExample =
{ name : String
, fullModuleName : String
, sectionName : String
, mainType : String
2022-04-13 03:32:46 +03:00
, extraImports : List String
, code : String
}
2022-03-29 19:33:00 +03:00
view : Config -> SectionExample -> Html msg
view config example =
ClickableText.link ("View " ++ example.sectionName ++ " example on Ellie")
2022-03-29 19:33:00 +03:00
[ ClickableText.linkExternal (generateEllieLink config example)
, ClickableText.small
]
2022-03-29 19:33:00 +03:00
generateEllieLink : Config -> SectionExample -> String
generateEllieLink config example =
let
packageDependencies =
config.packageDependencies
|> Result.map Dict.toList
|> Result.withDefault []
|> List.map toPackageDependencyQueryString
toPackageDependencyQueryString ( name, version ) =
Url.Builder.string "packages" (name ++ "@" ++ version)
in
Url.Builder.crossOrigin "https://ellie-app.com/a/example/v1"
[]
2022-03-29 19:33:00 +03:00
([ Url.Builder.string "title" (example.name ++ " | " ++ example.sectionName)
, Url.Builder.string "elmcode" (generateElmExampleModule config example)
, Url.Builder.string "htmlcode" ellieHtmlSetup
, Url.Builder.string "elmversion" "0.19.1"
]
++ packageDependencies
)
2022-03-29 19:33:00 +03:00
generateElmExampleModule : Config -> SectionExample -> String
generateElmExampleModule config example =
let
maybeErrorMessages =
case config.packageDependencies of
Err httpError ->
[ "{- "
, "Something went wrong fetching the package dependencies!"
, "You will need to install the packages by hand for this code to compile."
, ""
, "Error: "
2022-03-29 19:33:00 +03:00
, Debug.toString httpError
, "-}"
]
2022-03-29 19:33:00 +03:00
Ok _ ->
[]
in
[ "module Main exposing (main)"
, ""
, "import Css exposing (Style)"
, "import Html as RootHtml"
, "import Html.Styled exposing (..)"
, "import Nri.Ui.Colors.V1 as Colors"
, "import Nri.Ui.UiIcon.V1 as UiIcon"
, "import Nri.Ui.Svg.V1 as Svg"
, "import " ++ example.fullModuleName ++ " as " ++ example.name
2022-04-13 03:32:46 +03:00
, String.join "\n" example.extraImports
, ""
2022-03-29 19:33:00 +03:00
, ""
]
2022-03-29 19:33:00 +03:00
++ maybeErrorMessages
++ [ "main : " ++ example.mainType
2022-03-29 19:33:00 +03:00
, "main ="
, " " ++ example.code
, " |> toUnstyled"
]
|> String.join "\n"
|> String.replace "\t" " "
ellieHtmlSetup : String
ellieHtmlSetup =
"""
<html> <head></head>
<body>
<main></main>
<script>
var app = Elm.Main.init({ node: document.querySelector('main') })
</script>
</body>
</html>
"""