@@ -11,9 +11,11 @@ import qualified Data.ByteString.Lazy as BL
11
11
import qualified Data.Text.Encoding as T
12
12
import qualified Functora.Bolt11 as B11
13
13
import Functora.Miso.Prelude
14
+ import qualified Functora.Miso.Widgets.Field as Field
14
15
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
15
16
import qualified Functora.Miso.Widgets.Header as Header
16
17
import qualified Functora.Prelude as Prelude
18
+ import Type.Reflection
17
19
import qualified Prelude
18
20
19
21
bolt11 :: Model -> [View Action ]
@@ -22,10 +24,10 @@ bolt11 st =
22
24
<> (if isRight ln then parserWidget rawLn rh else mempty )
23
25
<> parserWidget rawR r
24
26
<> fromRight mempty (verifierWidget [rawLn, rawR] <$> rh <*> r)
25
- <> either (const mempty ) invoiceWidget ln
27
+ <> either (const mempty ) ( invoiceWidget st) ln
26
28
<> either
27
29
(const mempty )
28
- (\ bsR -> if bsR == mempty then mempty else preimageWidget rawR bsR)
30
+ (\ bsR -> if bsR == mempty then mempty else preimageWidget st rawR bsR)
29
31
r
30
32
where
31
33
rawLn :: MisoString
@@ -56,10 +58,11 @@ verifierWidget src rh r =
56
58
then success " The preimage matches the invoice"
57
59
else failure " The preimage does not match the invoice"
58
60
59
- invoiceWidget :: B11. Bolt11 -> [View Action ]
60
- invoiceWidget ln =
61
+ invoiceWidget :: Model -> B11. Bolt11 -> [View Action ]
62
+ invoiceWidget st ln =
61
63
Header. headerViewer " Invoice Details"
62
64
<> pairs
65
+ st
63
66
( [ pair " Network"
64
67
$ case B11. bolt11HrpNet $ B11. bolt11Hrp ln of
65
68
B11. BitcoinMainnet -> " Bitcoin Mainnet"
@@ -75,7 +78,7 @@ invoiceWidget ln =
75
78
$ B11. bolt11Timestamp ln
76
79
]
77
80
<> ( B11. bolt11Tags ln
78
- >>= invoiceTagWidget ln
81
+ >>= invoiceTag ln
79
82
)
80
83
<> [ pair " Signature"
81
84
. B11. inspectHex
@@ -88,8 +91,8 @@ invoiceWidget ln =
88
91
where
89
92
sig = B11. bolt11Signature ln
90
93
91
- invoiceTagWidget :: B11. Bolt11 -> B11. Tag -> [FieldPair DynamicField Identity ]
92
- invoiceTagWidget ln = \ case
94
+ invoiceTag :: B11. Bolt11 -> B11. Tag -> [FieldPair DynamicField Identity ]
95
+ invoiceTag ln = \ case
93
96
B11. PaymentHash x -> hex " Preimage Hash" x
94
97
B11. PaymentSecret x -> hex " Payment Secret" x
95
98
B11. Description x -> pure . pair " Description" $ inspect x
@@ -129,10 +132,11 @@ invoiceTagWidget ln = \case
129
132
. pair x
130
133
. B11. inspectHex
131
134
132
- preimageWidget :: MisoString -> ByteString -> [View Action ]
133
- preimageWidget rawR r =
135
+ preimageWidget :: Model -> MisoString -> ByteString -> [View Action ]
136
+ preimageWidget st rawR r =
134
137
Header. headerViewer " Preimage Details"
135
138
<> pairs
139
+ st
136
140
[ pair " Preimage" rawR,
137
141
pair " Preimage Hash" . inspect @ ByteString $ sha256Hash r
138
142
]
@@ -157,17 +161,62 @@ pair x =
157
161
newFieldPairId x
158
162
. DynamicFieldText
159
163
160
- pairs :: (Foldable1 f ) => [FieldPair DynamicField f ] -> [View Action ]
161
- pairs raw =
162
- FieldPairs. fieldPairsViewer
163
- FieldPairs. Args
164
- { FieldPairs. argsModel = xs,
165
- FieldPairs. argsOptic = id ,
166
- FieldPairs. argsAction =
167
- \ fun -> PushUpdate . Instant $ \ next -> do
168
- void $ fun xs
169
- pure next
170
- }
164
+ pairs ::
165
+ ( Typeable model ,
166
+ Foldable1 f
167
+ ) =>
168
+ model ->
169
+ [FieldPair DynamicField f ] ->
170
+ [View Action ]
171
+ pairs st raw =
172
+ case typeOf st `eqTypeRep` typeRep @ Model of
173
+ Just HRefl ->
174
+ FieldPairs. fieldPairsViewer
175
+ FieldPairs. Args
176
+ { FieldPairs. argsModel = st,
177
+ FieldPairs. argsOptic = constTraversal xs,
178
+ FieldPairs. argsAction = PushUpdate . Instant
179
+ }
180
+ ( \ idx ->
181
+ Field. defViewerOpts
182
+ { Field. viewerOptsQrOptic =
183
+ Just
184
+ $ # modelState
185
+ . # stDoc
186
+ . # stDocLnInvoiceViewer
187
+ . at idx
188
+ . non
189
+ StViewer
190
+ { stViewerQr = Closed ,
191
+ stViewerTruncate = Closed
192
+ }
193
+ . # stViewerQr,
194
+ Field. viewerOptsTruncateOptic =
195
+ Just
196
+ $ # modelState
197
+ . # stDoc
198
+ . # stDocLnInvoiceViewer
199
+ . at idx
200
+ . non
201
+ StViewer
202
+ { stViewerQr = Closed ,
203
+ stViewerTruncate = Closed
204
+ }
205
+ . # stViewerTruncate
206
+ }
207
+ )
208
+ Nothing ->
209
+ FieldPairs. fieldPairsViewer
210
+ FieldPairs. Args
211
+ { FieldPairs. argsModel = st,
212
+ FieldPairs. argsOptic = constTraversal xs,
213
+ FieldPairs. argsAction =
214
+ \ fun -> PushUpdate . Instant $ \ next -> do
215
+ void $ fun st
216
+ pure next
217
+ }
218
+ ( const Field. defViewerOpts
219
+ )
171
220
where
172
221
xs =
173
222
filter
@@ -180,12 +229,12 @@ pairs raw =
180
229
success :: MisoString -> [View Action ]
181
230
success msg =
182
231
css " app-success"
183
- $ pairs [newFieldPairId mempty $ DynamicFieldText msg]
232
+ $ pairs () [newFieldPairId mempty $ DynamicFieldText msg]
184
233
185
234
failure :: MisoString -> [View Action ]
186
235
failure msg =
187
236
css " app-failure"
188
- $ pairs [newFieldPairId mempty $ DynamicFieldText msg]
237
+ $ pairs () [newFieldPairId mempty $ DynamicFieldText msg]
189
238
190
239
css :: MisoString -> [View action ] -> [View action ]
191
240
css x = fmap $ \ case
0 commit comments