@@ -8,15 +8,15 @@ mod scheme;
8
8
9
9
use std:: rc:: Rc ;
10
10
11
- use asn1_parser:: { Asn1 , Asn1Decoder } ;
11
+ use asn1_parser:: { Asn1 , Asn1Decoder , Asn1Encoder } ;
12
12
use web_sys:: KeyboardEvent ;
13
13
use yew:: { classes, function_component, html, use_effect_with_deps, use_reducer, use_state, Callback , Html , Reducible } ;
14
- use yew_hooks:: { use_clipboard, use_location} ;
14
+ use yew_hooks:: { use_clipboard, use_local_storage , use_location} ;
15
15
use yew_notifications:: { use_notification, Notification , NotificationType } ;
16
16
17
17
use crate :: asn1:: asn1_viewer:: Asn1Viewer ;
18
18
use crate :: asn1:: hex_view:: HexViewer ;
19
- use crate :: common:: ByteInput ;
19
+ use crate :: common:: { encode_bytes , ByteInput , BytesFormat } ;
20
20
use crate :: url_query_params;
21
21
use crate :: url_query_params:: generate_asn1_link;
22
22
@@ -26,6 +26,7 @@ pub const TEST_ASN1: &[u8] = &[
26
26
48 , 30 , 160 , 2 , 5 , 0 , 161 , 24 , 30 , 22 , 0 , 67 , 0 , 101 , 0 , 114 , 0 , 116 , 0 , 105 , 0 , 102 , 0 , 105 , 0 , 99 , 0 , 97 , 0 , 116 ,
27
27
0 , 101 ,
28
28
] ;
29
+ const ASN1_LOCAL_STORAGE_KEY : & str = "ASN1_DATA" ;
29
30
30
31
pub fn compare_ids ( asn1_node_id : u64 , cur_node : & Option < u64 > ) -> bool {
31
32
matches ! ( cur_node, Some ( node_id) if * node_id == asn1_node_id)
@@ -83,7 +84,7 @@ pub fn asn1_parser_page() -> Html {
83
84
let raw_data = ( * raw_asn1) . clone ( ) ;
84
85
let parse_asn1 = Callback :: from ( move |_| match Asn1 :: decode_buff ( & raw_data) {
85
86
Ok ( asn1) => {
86
- log :: debug!( "parsed!" ) ;
87
+ debug ! ( "parsed!" ) ;
87
88
asn1_setter. set ( asn1. to_owned_with_asn1 ( asn1. inner_asn1 ( ) . to_owned ( ) ) ) ;
88
89
}
89
90
Err ( error) => notifications. spawn ( Notification :: new (
@@ -109,11 +110,25 @@ pub fn asn1_parser_page() -> Html {
109
110
let notifications = notification_manager. clone ( ) ;
110
111
let raw_asn1_setter = raw_asn1. setter ( ) ;
111
112
let asn1_setter = parsed_asn1. setter ( ) ;
113
+ let local_storage = use_local_storage :: < String > ( ASN1_LOCAL_STORAGE_KEY . to_owned ( ) ) ;
112
114
use_effect_with_deps (
113
115
move |_: & [ ( ) ; 0 ] | {
114
116
let query = & location. search ;
115
117
116
118
if query. len ( ) < 2 {
119
+ // URL query params is empty. We try to load ASN1 from local storage.
120
+ if let Some ( raw_asn1) = ( * local_storage) . as_ref ( ) {
121
+ if let Ok ( bytes) = hex:: decode ( raw_asn1) {
122
+ match Asn1 :: decode_buff ( & bytes) {
123
+ Ok ( asn1) => {
124
+ asn1_setter. set ( asn1. to_owned_with_asn1 ( asn1. inner_asn1 ( ) . to_owned ( ) ) ) ;
125
+ }
126
+ Err ( err) => {
127
+ error ! ( "Can not decode asn1: {:?}" , err) ;
128
+ }
129
+ }
130
+ }
131
+ }
117
132
return ;
118
133
}
119
134
@@ -122,7 +137,6 @@ pub fn asn1_parser_page() -> Html {
122
137
let url_query_params:: Asn1 { asn1 : asn1_data } = asn1;
123
138
match Asn1 :: decode_buff ( & asn1_data) {
124
139
Ok ( asn1) => {
125
- log:: debug!( "parsed!" ) ;
126
140
asn1_setter. set ( asn1. to_owned_with_asn1 ( asn1. inner_asn1 ( ) . to_owned ( ) ) ) ;
127
141
}
128
142
Err ( error) => notifications. spawn ( Notification :: new (
@@ -145,6 +159,16 @@ pub fn asn1_parser_page() -> Html {
145
159
[ ] ,
146
160
) ;
147
161
162
+ let local_storage = use_local_storage :: < String > ( ASN1_LOCAL_STORAGE_KEY . to_owned ( ) ) ;
163
+ use_effect_with_deps (
164
+ move |asn1| {
165
+ let mut encoded = vec ! [ 0 ; asn1. needed_buf_size( ) ] ;
166
+ asn1. encode_buff ( & mut encoded) . expect ( "ASN1 encoding should not fail" ) ;
167
+ local_storage. set ( encode_bytes ( encoded, BytesFormat :: Hex ) ) ;
168
+ } ,
169
+ parsed_asn1. clone ( ) ,
170
+ ) ;
171
+
148
172
let clipboard = use_clipboard ( ) ;
149
173
let raw_asn1_data = ( * raw_asn1) . clone ( ) ;
150
174
let share_by_link = Callback :: from ( move |_| {
0 commit comments