From 58fd28f1a3afbff2c6b978efd3e351d4535ba51c Mon Sep 17 00:00:00 2001 From: msyfls123 Date: Wed, 24 Apr 2024 22:36:11 +0800 Subject: [PATCH] feat: preview file link in upload result --- client/src/component/uploader.rs | 67 ++++++++++++++++++++++---------- client/src/container/home.rs | 1 - client/src/css/index.styl | 15 ++++++- server/templates/index.html.hbs | 1 + 4 files changed, 61 insertions(+), 23 deletions(-) diff --git a/client/src/component/uploader.rs b/client/src/component/uploader.rs index cc098c7..e535f4c 100644 --- a/client/src/component/uploader.rs +++ b/client/src/component/uploader.rs @@ -2,7 +2,7 @@ use serde_wasm_bindgen::from_value; use wasm_bindgen_futures::{spawn_local, JsFuture}; use web_sys::{console, Event, HtmlInputElement}; use wasm_bindgen::{closure::Closure, JsCast, JsValue}; -use yew::{ html, prelude::function_component, use_callback, use_context, use_memo, use_state, Callback, Html, Properties}; +use yew::{ html, prelude::function_component, use_callback, use_context, use_memo, use_state, Callback, Html, Properties, UseStateHandle}; use js_sys::{Promise, JSON::stringify_with_replacer_and_space}; use serde_json::{Value}; use serde::{Deserialize}; @@ -15,6 +15,11 @@ pub struct Progress { total: Value } +#[derive(Deserialize, PartialEq, Clone)] +pub struct UploadResult { + Location: Value, +} + #[derive(Properties, PartialEq)] pub struct UploaderProps { pub on_upload_start: Callback<()>, @@ -27,9 +32,11 @@ pub fn uploader(props: &UploaderProps) -> Html { let app_ctx = use_context::().expect("no ctx found"); let cos_upload = app_ctx.upload_file; // state - let upload_result = use_state(|| None); + let upload_result: UseStateHandle> = use_state(|| None); let is_uploading = use_state(|| false); + let filename = use_state(|| None); let progress = use_state(|| 0f64); + // computed let percentage = use_memo(progress.clone(), |progress| { let progress = *progress.clone(); format!("{:.1}%", progress * 100.0f64) @@ -38,12 +45,26 @@ pub fn uploader(props: &UploaderProps) -> Html { let progress = *progress.clone(); format!("--percentage: {:.1};", progress * 100.0f64) }); + let location = use_memo(upload_result.clone(), |res| { + match res.as_ref() { + Some(v) => { + let parsed: UploadResult = from_value(v.to_owned()).unwrap(); + "https://".to_string() + parsed.Location.as_str().unwrap_or_default() + }, + None => "".to_string() + } + }); // callbacks let upload_start = use_callback( - (props.on_upload_start.clone(), is_uploading.clone()), - move |_, (on_start, uploading)| { + (props.on_upload_start.clone(), is_uploading.clone(), filename.clone()), + move |e: Event, (on_start, uploading, filename)| { on_start.emit(()); uploading.set(true); + + let target = e.target().unwrap(); + let input_el: HtmlInputElement = target.dyn_into().unwrap(); + let option_file = input_el.files().unwrap().item(0); + filename.set(option_file.map(|file| file.name())); } ); let upload_end = use_callback( @@ -57,7 +78,7 @@ pub fn uploader(props: &UploaderProps) -> Html { let on_upload = use_callback( (upload_end.clone(), progress.clone()), move |e: Event, (upload_cb, progress_state)| { - upload_start.emit(()); + upload_start.emit(e.clone()); let progress_state = progress_state.clone(); let closure = Closure::wrap(Box::new(move |payload: JsValue| { @@ -99,13 +120,17 @@ pub fn uploader(props: &UploaderProps) -> Html { html! {
- - - {if let Some(data) = upload_result.as_ref() { - let upload_text = stringify_with_replacer_and_space(&data, &JsValue::NULL, &JsValue::from_f64(4.0)).unwrap(); - html! {
- { "Upload success" } -
{upload_text.as_string().unwrap_or_default()}
+ {if let Some(_) = upload_result.as_ref() { + // let upload_text = stringify_with_replacer_and_space(&data, &JsValue::NULL, &JsValue::from_f64(4.0)).unwrap(); + html! { } } else if *is_uploading { html! { @@ -136,14 +161,16 @@ pub fn uploader(props: &UploaderProps) -> Html { } } else { html! { - +
+ +
} }}
diff --git a/client/src/container/home.rs b/client/src/container/home.rs index c38dedc..161e0ca 100644 --- a/client/src/container/home.rs +++ b/client/src/container/home.rs @@ -80,7 +80,6 @@ impl Component for HomeInner {

{"Upload"}

{ list.into_iter().map(|i| html! {
- {i.clone()} Bard +

hello {{ name }}