Skip to content

Commit

Permalink
Format code and rsx
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed Aug 22, 2024
1 parent 75401e2 commit b27944e
Show file tree
Hide file tree
Showing 131 changed files with 1,773 additions and 3,557 deletions.
14 changes: 4 additions & 10 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use std::{fs, path::PathBuf};

const STYLES_IN: &str = "public/styles";
const STYLES_OUT: &str = "public/css-out";

fn main() {
println!("cargo:rerun-if-changed=src/styles");

let files = fs::read_dir(STYLES_IN).expect("It should read a dir");

_ = fs::create_dir(STYLES_OUT);

for file in files {
let file = file.expect("It should read a file");
let path = file.path();
Expand All @@ -19,12 +14,11 @@ fn main() {
.expect("It should parse a file name")
.strip_suffix(".scss")
.expect("It should remove a suffix .scss");

let out_path = PathBuf::from(STYLES_OUT).join(format!("{}.css", raw_name));

let scss_options = grass::Options::default().style(grass::OutputStyle::Compressed);
let css =
grass::from_path(path, &scss_options).expect("It should convert a .scss block to css");
let scss_options = grass::Options::default()
.style(grass::OutputStyle::Compressed);
let css = grass::from_path(path, &scss_options)
.expect("It should convert a .scss block to css");
fs::write(out_path, css).expect("It should write the output of a css");
}
}
10 changes: 9 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
tag:
build:
dx build --release

tag: check build
#!/usr/bin/env nu
git tag (open Cargo.toml | get package.version)

check:
dx fmt --check --all-code
dx check
cargo clippy -- -D warnings
25 changes: 6 additions & 19 deletions src/components/atoms/account.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
use dioxus::prelude::*;

#[derive(PartialEq, Props, Clone)]
pub struct AccountProps {
title: String,
description: String,
on_click: EventHandler<()>,
}

pub fn AccountButton(props: AccountProps) -> Element {
rsx!(
button {
class: "account",
onclick: move |_| {
props.on_click.call(())
},
div {
class: "account__wrapper",
h3 {
class: "account__title",
{props.title}
}
p {
class: "account__description",
{props.description}
}
}
}
button { class: "account", onclick: move |_| { props.on_click.call(()) },
div { class: "account__wrapper",
h3 { class: "account__title", { props.title } }
p { class: "account__description", { props.description } }
}
}
)
}
14 changes: 4 additions & 10 deletions src/components/atoms/action_request.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
use dioxus::prelude::*;

use super::dropdown::ElementSize;

#[derive(PartialEq, Props, Clone)]
pub struct RequestProps {
name: String,
details: Option<String>,
#[props(default = ElementSize::Medium)]
size: ElementSize,
}

pub fn ActionRequest(props: RequestProps) -> Element {
let size = match props.size {
ElementSize::Big => "action-request--big",
ElementSize::Medium => "action-request--medium",
ElementSize::Small => "action-request--small",
};

rsx!(
div {
class: "action-request {size}",
div { class: "action-request {size}",
span { class: "action-request__title",
{props.name}
{
props.name }
}
if let Some(details) = props.details {
span { class: "action-request__details",
{details}
}
span { class: "action-request__details", { details } }
}
}
)
Expand Down
148 changes: 58 additions & 90 deletions src/components/atoms/attach.rs
Original file line number Diff line number Diff line change
@@ -1,198 +1,164 @@
use std::ops::Deref;

use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
use futures_util::TryFutureExt;
use wasm_bindgen::JsCast;
use web_sys::HtmlElement;

use crate::{
components::atoms::{
button::Variant as ButtonVariant, dropdown::ElementSize,
icon_button::Variant as IconButtonVariant, Button, Close, Icon, IconButton,
},
hooks::use_attach::{use_attach, AttachFile},
};

#[derive(Clone, Debug)]
pub enum AttachError {
NotFound,
UncoverType,
UnknownContent,
Size,
}

#[derive(Clone, Debug)]
pub struct FeedAttachError {
explanation: String,
details: String,
}

#[derive(PartialEq, Debug, Clone)]
pub struct AttachEvent {
pub value: Vec<u8>,
}

#[derive(PartialEq, Props, Clone)]
pub struct AttachProps {
// value: Vec<u8>,
label: Option<String>,
supported_types: Vec<String>,
cta_text: String,
on_change: EventHandler<AttachFile>,
}

const MAX_FILE_SIZE: u64 = 2 * 1024 * 1024;

pub fn Attach(props: AttachProps) -> Element {
let i18 = use_i18();
let mut attach = use_attach();
let mut textarea_ref = use_signal::<Option<Box<HtmlElement>>>(|| None);
let mut error = use_signal::<Option<FeedAttachError>>(|| None);

let supported_types = props
.supported_types
.iter()
.map(|t| t.parse::<mime::Mime>().expect("Supported mime"))
.collect::<Vec<mime::Mime>>();

let on_handle_attach = move |_| {
if let Some(input_element) = textarea_ref() {
input_element.click();
}
};

let on_handle_input = move |event: Event<FormData>| {
let supported_types = supported_types.clone();
spawn({
async move {
let files = &event.files().ok_or(AttachError::NotFound)?;
let fs = files.files();

let existing_file = fs.get(0).ok_or(AttachError::NotFound)?;
let name = existing_file.clone();

let content = files
.read_file(existing_file)
.await
.ok_or(AttachError::NotFound)?;
let infered_type = infer::get(content.deref()).ok_or(AttachError::UncoverType)?;

let content_type: Result<mime::Mime, _> = infered_type.mime_type().parse();
let content_type = content_type.map_err(|_| AttachError::UnknownContent)?;

let infered_type = infer::get(content.deref())
.ok_or(AttachError::UncoverType)?;
let content_type: Result<mime::Mime, _> = infered_type
.mime_type()
.parse();
let content_type = content_type
.map_err(|_| AttachError::UnknownContent)?;
if !supported_types.contains(&content_type) {
return Err(AttachError::UncoverType);
}

let blob = match content_type.type_() {
mime::IMAGE => gloo::file::Blob::new(content.deref()),
mime::VIDEO => gloo::file::Blob::new_with_options(
content.deref(),
Some(infered_type.mime_type()),
),
mime::VIDEO => {
gloo::file::Blob::new_with_options(
content.deref(),
Some(infered_type.mime_type()),
)
}
_ => gloo::file::Blob::new(content.deref()),
};

let size = blob.size().clone();

if size > MAX_FILE_SIZE {
return Err(AttachError::Size);
}

let object_url = gloo::file::ObjectUrl::from(blob);

let attach_file = AttachFile {
name: existing_file.to_string(),
preview_url: object_url,
data: content.clone(),
content_type,
size,
};

attach.set(Some(attach_file.clone()));

props.on_change.call(attach_file);

Ok::<(), AttachError>(())
}
.unwrap_or_else(move |e: AttachError| {
let message_error = match e {
AttachError::NotFound => FeedAttachError {
explanation: translate!(i18, "errors.attach.not_found.explanation"),
details: translate!(i18, "errors.attach.not_found.details"),
},
AttachError::Size => FeedAttachError {
explanation: translate!(i18, "errors.attach.size.explanation"),
details: translate!(i18, "errors.attach.size.details"),
},
AttachError::UncoverType | AttachError::UnknownContent => FeedAttachError {
explanation: translate!(i18, "errors.attach.mime.explanation"),
details: translate!(i18, "errors.attach.mime.details"),
},
};

error.set(Some(message_error))
})
.unwrap_or_else(move |e: AttachError| {
let message_error = match e {
AttachError::NotFound => {
FeedAttachError {
explanation: translate!(
i18, "errors.attach.not_found.explanation"
),
details: translate!(i18, "errors.attach.not_found.details"),
}
}
AttachError::Size => {
FeedAttachError {
explanation: translate!(
i18, "errors.attach.size.explanation"
),
details: translate!(i18, "errors.attach.size.details"),
}
}
AttachError::UncoverType | AttachError::UnknownContent => {
FeedAttachError {
explanation: translate!(
i18, "errors.attach.mime.explanation"
),
details: translate!(i18, "errors.attach.mime.details"),
}
}
};
error.set(Some(message_error))
})
});
};

rsx!(
section {
class: "attach",
section { class: "attach",
if let Some(value) = props.label {
label { class: "input__label", "{value}" }
}
if let Some(e) = error() {
div {
class: "attach__wrapper attach__wrapper--error",
div { class: "attach__wrapper attach__wrapper--error",
div { class: "attach__error__header",
h4 { class: "attach__error__title",
{ translate!(i18, "errors.attach.title") }
}
div {
class: "attach__close",
h4 { class: "attach__error__title", { translate!(i18, "errors.attach.title") } }
div { class: "attach__close",
IconButton {
variant: IconButtonVariant::Round,
size: ElementSize::Big,
class: "button--avatar bg--transparent",
body: rsx!(
Icon {
icon: Close,
height: 28,
width: 28,
fill: "var(--state-destructive-active)"
}
Icon { icon : Close, height : 28, width : 28, fill :
"var(--state-destructive-active)" }
),
on_click: move |_| {
error.set(None)
}
on_click: move |_| { error.set(None) }
}
}
}
p { class: "attach__error__explanation",
"{e.explanation}"
}
p { class: "attach__error__details",
"{e.details}"
}
p { class: "attach__error__explanation", "{e.explanation}" }
p { class: "attach__error__details", "{e.details}" }
}
} else {
div {
class: "attach__wrapper",
{
attach.get_file().ok().map(|url| {
rsx!(
img {
class: "attach__preview",
src: "{url}"
}
)
})
}

div {
class: "attach__cta",
div { class: "attach__wrapper",
{ attach.get_file().ok().map(| url | { rsx!(img { class :
"attach__preview", src : "{url}" }) }) },
div { class: "attach__cta",
Button {
text: "{props.cta_text}",
status: None,
Expand All @@ -206,7 +172,9 @@ pub fn Attach(props: AttachProps) -> Element {
r#type: "file",
class: "attach__input",
onmounted: move |event| {
event.data.downcast::<web_sys::Element>()
event
.data
.downcast::<web_sys::Element>()
.and_then(|element| element.clone().dyn_into::<web_sys::HtmlElement>().ok())
.map(|html_element| textarea_ref.set(Some(Box::new(html_element.clone()))));
},
Expand Down
Loading

0 comments on commit b27944e

Please sign in to comment.