@@ -4,36 +4,49 @@ use crate::encoding::{
44} ;
55use crate :: i18n:: { Language , get_translations} ;
66use dioxus:: prelude:: * ;
7+ use dioxus_router:: prelude:: navigator;
78
89#[ component]
910pub fn Home ( ) -> Element {
1011 let language = use_context :: < Signal < Language > > ( ) ;
1112 let t = get_translations ( language ( ) ) ;
13+ let nav = navigator ( ) ;
14+ let mut app_context =
15+ use_context :: < Signal < crate :: AppContext > > ( ) ;
1216
1317 let mut note_text = use_signal ( || String :: new ( ) ) ;
1418 let mut encryption =
1519 use_signal ( || Option :: < CipherType > :: None ) ;
1620 let mut password = use_signal ( || String :: new ( ) ) ;
17- let mut generated_url =
18- use_signal ( || Option :: < String > :: None ) ;
19- let mut qr_code_svg =
20- use_signal ( || Option :: < String > :: None ) ;
2121 let mut error_message =
2222 use_signal ( || Option :: < String > :: None ) ;
2323
24+ use_effect ( move || {
25+ let ctx = app_context. read ( ) ;
26+ if let Some ( content) = & ctx. content {
27+ note_text. set ( content. clone ( ) ) ;
28+ }
29+ if !ctx. password . is_empty ( ) {
30+ password. set ( ctx. password . clone ( ) ) ;
31+ }
32+ if let Some ( cipher) = ctx. cipher {
33+ encryption. set ( Some ( cipher) ) ;
34+ }
35+ } ) ;
36+
2437 let generate_note = move |_| {
2538 let t = get_translations ( language ( ) ) ;
2639 error_message. set ( None ) ;
27- generated_url. set ( None ) ;
28- qr_code_svg. set ( None ) ;
2940
3041 let note_content = note_text. read ( ) . clone ( ) ;
3142 let enc_option = encryption. read ( ) . clone ( ) ;
43+ let pwd = password. read ( ) . clone ( ) ;
3244
3345 let note_data = match enc_option {
34- None => NoteData :: PlainText ( note_content) ,
46+ None => {
47+ NoteData :: PlainText ( note_content. clone ( ) )
48+ }
3549 Some ( cipher) => {
36- let pwd = password. read ( ) . clone ( ) ;
3750 if pwd. is_empty ( ) {
3851 error_message. set ( Some (
3952 t. password_required . to_string ( ) ,
@@ -57,28 +70,33 @@ pub fn Home() -> Element {
5770 }
5871 } ;
5972
60- let base_url = web_sys:: window ( )
61- . and_then ( |w| w. location ( ) . href ( ) . ok ( ) )
62- . unwrap_or_else ( || {
63- "http://localhost:8080" . to_string ( )
64- } )
65- . split ( '#' )
66- . next ( )
67- . unwrap_or ( "http://localhost:8080" )
68- . to_string ( )
69- + "view" ;
70-
71- match build_url ( & base_url, & note_data) {
72- Ok ( url) => {
73- match generate_qr_code ( & url) {
74- Ok ( svg) => qr_code_svg. set ( Some ( svg) ) ,
73+ if let Some ( window) = web_sys:: window ( ) {
74+ if let Ok ( origin) = window. location ( ) . origin ( ) {
75+ let view_url = format ! ( "{}/view" , origin) ;
76+ match build_url ( & view_url, & note_data) {
77+ Ok ( url) => match generate_qr_code ( & url)
78+ {
79+ Ok ( qr) => {
80+ app_context. set (
81+ crate :: AppContext {
82+ content : Some (
83+ note_content,
84+ ) ,
85+ password : pwd,
86+ cipher : enc_option,
87+ share_url : Some ( url) ,
88+ qr_code : Some ( qr) ,
89+ } ,
90+ ) ;
91+
92+ nav. push ( "/share" ) ;
93+ }
94+ Err ( e) => error_message
95+ . set ( Some ( e. localized ( & t) ) ) ,
96+ } ,
7597 Err ( e) => error_message
7698 . set ( Some ( e. localized ( & t) ) ) ,
7799 }
78- generated_url. set ( Some ( url) ) ;
79- }
80- Err ( e) => {
81- error_message. set ( Some ( e. localized ( & t) ) )
82100 }
83101 }
84102 } ;
@@ -158,33 +176,5 @@ pub fn Home() -> Element {
158176 }
159177 }
160178 }
161-
162-
163- if let Some ( url) = generated_url( ) {
164- section {
165- h2 { "{t.share_title}" }
166- div {
167- input {
168- r#type: "text" ,
169- readonly: true ,
170- value: "{url}" ,
171- onclick: move |_| {
172- if let Some ( window) = web_sys:: window( ) {
173- let clipboard = window. navigator( ) . clipboard( ) ;
174- let _ = clipboard. write_text( & url) ;
175- }
176- } ,
177- }
178- p { "{t.click_to_copy}" }
179- }
180-
181- if let Some ( svg) = qr_code_svg( ) {
182- figure {
183- h3 { "{t.qr_code}" }
184- div { dangerous_inner_html: "{svg}" }
185- }
186- }
187- }
188- }
189179 }
190180}
0 commit comments