Skip to content

Commit

Permalink
Factor out address rendering into common pages lib
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Nov 10, 2024
1 parent 7b545e5 commit a744418
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
55 changes: 45 additions & 10 deletions lib/ui/src/Cardano/Wallet/UI/Common/Html/Pages/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ module Cardano.Wallet.UI.Common.Html.Pages.Lib
, Striped (..)
, onStriped
, box
, addressH
)
where

import Prelude

import Cardano.Wallet.Deposit.Pure.API.Address
( encodeAddress
)
import Cardano.Wallet.Deposit.Read
( Address
)
import Cardano.Wallet.UI.Common.Html.Htmx
( hxExt_
, hxGet_
Expand All @@ -40,7 +47,9 @@ import Cardano.Wallet.UI.Common.Html.Htmx
, hxTrigger_
)
import Cardano.Wallet.UI.Common.Html.Lib
( linkText
( WithCopy
, linkText
, truncatableText
)
import Cardano.Wallet.UI.Lib.ListOf
( Cons (..)
Expand Down Expand Up @@ -141,30 +150,40 @@ onStriped s a b = case s of

-- | Render a list of 'AssocRow' as a table. We use 'listOf' to allow 'do' notation
-- in the definition of the rows
record :: Maybe Int -> Width -> Striped -> ListOf (AssocRow m) -> Monad m => HtmlT m ()
record
:: Maybe Int
-> Width
-> Striped
-> ListOf (AssocRow m)
-> Monad m
=> HtmlT m ()
record n w s xs =
table_
[ class_ $ "border-top table table-hover mb-0" <> onStriped s " table-striped" ""
[ class_
$ "border-top table table-hover mb-0" <> onStriped s " table-striped" ""
, style_
$ onWidth w "width: auto" ""
]
$ mapM_ (assocRowH n)
$ listOf xs

-- | Create an 'AssocRow' from a key and a value.
field :: [Attribute] -> HtmlT m () -> HtmlT m () -> ListOf (AssocRow m)
field
:: [Attribute] -> HtmlT m () -> HtmlT m () -> ListOf (AssocRow m)
field attrs key val = singleton $ Elem $ AssocRow attrs key val

-- | Create a simple 'AssocRow' from a key and a value. where the key is a 'Text'.
simpleField :: Monad m => Text -> HtmlT m () -> ListOf (AssocRow m)
simpleField = field [] . toHtml

-- | Create an 'AssocRow' from a key and a value where the value is an 'Html'.
fieldHtml :: Monad m => [Attribute] -> Text -> HtmlT m () -> ListOf (AssocRow m)
fieldHtml
:: Monad m => [Attribute] -> Text -> HtmlT m () -> ListOf (AssocRow m)
fieldHtml as = field as . toHtml

-- | Create an 'AssocRow' from a key and a value where the value is a 'Show' instance.
fieldShow :: (Show a, Monad m) => [Attribute] -> Text -> a -> ListOf (AssocRow m)
fieldShow
:: (Show a, Monad m) => [Attribute] -> Text -> a -> ListOf (AssocRow m)
fieldShow attrs key val = field attrs (toHtml key) (toHtml $ show val)

fadeInId :: Monad m => HtmlT m ()
Expand All @@ -178,14 +197,14 @@ fadeInId =
-- whenever some specific events are received from an SSE endpoint.
-- It also self populate on load.
sseH
:: Link
:: Monad m
=> Link
-- ^ Link to fetch data from
-> Text
-- ^ Target element
-> [Text]
-- ^ Events to trigger onto
-> Monad m
=> HtmlT m ()
-> HtmlT m ()
sseH link target events = do
do
div_
Expand Down Expand Up @@ -254,7 +273,23 @@ showThousandDots = reverse . showThousandDots' . reverse . show
in
a <> if null b then [] else "." <> showThousandDots' b

box :: Monad m => HtmlT m () -> HtmlT m () -> HtmlT m () -> HtmlT m ()
addressH :: Monad m => WithCopy -> Address -> HtmlT m ()
addressH copy addr =
truncatableText copy ("address-text-" <> encodedAddr)
$ toHtml encodedAddr
where
encodedAddr = encodeAddress addr

-- | A box with a title, a subtitle and a content.
box
:: Monad m
=> HtmlT m ()
-- ^ title
-> HtmlT m ()
-- ^ subtitle
-> HtmlT m ()
-- ^ content
-> HtmlT m ()
box x y z = div_ [class_ "bg-body-secondary pb-1"] $ do
nav_ [class_ "navbar p-1 justify-content-center pb-0"]
$ do
Expand Down
17 changes: 0 additions & 17 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Html/Pages/Addresses.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import Prelude
import Cardano.Wallet.Deposit.IO
( WalletPublicIdentity (..)
)
import Cardano.Wallet.Deposit.Read
( Address
)
import Cardano.Wallet.UI.Common.Html.Htmx
( hxInclude_
, hxPost_
Expand All @@ -21,9 +18,7 @@ import Cardano.Wallet.UI.Common.Html.Htmx
)
import Cardano.Wallet.UI.Common.Html.Lib
( AlertH
, WithCopy
, linkText
, truncatableText
)
import Cardano.Wallet.UI.Common.Html.Pages.Lib
( AssocRow
Expand All @@ -45,9 +40,6 @@ import Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
( WalletPresent (..)
, onWalletPresentH
)
import Cardano.Wallet.UI.Lib.Address
( encodeMainnetAddress
)
import Cardano.Wallet.UI.Lib.ListOf
( ListOf
)
Expand All @@ -65,8 +57,6 @@ import Data.Time
)
import Lucid
( Html
, HtmlT
, ToHtml (..)
, class_
, div_
, id_
Expand All @@ -90,13 +80,6 @@ addressesH :: WHtml ()
addressesH = do
sseH addressesLink "addresses" ["wallet"]

customerAddressH :: Monad m => WithCopy -> Address -> HtmlT m ()
customerAddressH copy addr =
truncatableText copy ("address-text-" <> encodedAddr)
$ toHtml encodedAddr
where
encodedAddr = encodeMainnetAddress addr

addressElementH
:: UTCTime -> UTCTime -> AlertH -> WalletPresent -> Html ()
addressElementH now origin = onWalletPresentH $ \case
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Server/Addresses.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import Cardano.Wallet.UI.Common.Html.Lib
( WithCopy (..)
)
import Cardano.Wallet.UI.Common.Html.Pages.Lib
( alertH
( addressH
, alertH
)
import Cardano.Wallet.UI.Common.Layer
( UILayer (..)
Expand All @@ -44,7 +45,6 @@ import Cardano.Wallet.UI.Deposit.Handlers.Addresses.Transactions
)
import Cardano.Wallet.UI.Deposit.Html.Pages.Addresses
( addressElementH
, customerAddressH
)
import Cardano.Wallet.UI.Deposit.Html.Pages.Addresses.Transactions
( customerHistoryH
Expand Down Expand Up @@ -80,7 +80,7 @@ serveGetAddress
serveGetAddress ul c = withSessionLayer ul $ \l -> do
getCustomerAddress
l
(renderSmoothHtml . customerAddressH WithCopy)
(renderSmoothHtml . addressH WithCopy)
alert
c

Expand Down

0 comments on commit a744418

Please sign in to comment.