From 1ed5f241fd01a707b7a3cf73b7b6aa6edf721e7d Mon Sep 17 00:00:00 2001
From: youxingzhi <xingzhi.you@shopee.com>
Date: Fri, 5 Apr 2024 10:33:34 +0800
Subject: [PATCH] blog-2

---
 Cargo.lock                        |  2 ++
 examples/hello-world/src/main.tsx |  1 +
 packages/react/Cargo.toml         |  4 +++-
 packages/react/src/lib.rs         | 38 +++++++++++++++++++++++++++----
 packages/shared/src/lib.rs        |  6 +----
 5 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 26bfbea..bfe3995 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -68,6 +68,8 @@ name = "react"
 version = "0.1.0"
 dependencies = [
  "console_error_panic_hook",
+ "js-sys",
+ "log",
  "shared",
  "wasm-bindgen",
  "wasm-bindgen-test",
diff --git a/examples/hello-world/src/main.tsx b/examples/hello-world/src/main.tsx
index cdf3f95..78a90af 100644
--- a/examples/hello-world/src/main.tsx
+++ b/examples/hello-world/src/main.tsx
@@ -1,5 +1,6 @@
 import {createRoot} from 'react-dom'
 
+
 const comp = <div>hello world</div>
 console.log(comp)
 console.log(createRoot(document.getElementById("root")))
diff --git a/packages/react/Cargo.toml b/packages/react/Cargo.toml
index e2e2cfc..225b9c8 100644
--- a/packages/react/Cargo.toml
+++ b/packages/react/Cargo.toml
@@ -19,7 +19,9 @@ shared = { path = "../shared" }
 # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
 # code size when deploying.
 console_error_panic_hook = { version = "0.1.7", optional = true }
-web-sys = {  version = "0.3.69", features = ["console"] }
+web-sys = { version = "0.3.69", features = ["console"] }
+js-sys = "0.3.69"
+log = "0.4.21"
 
 [dev-dependencies]
 wasm-bindgen-test = "0.3.34"
diff --git a/packages/react/src/lib.rs b/packages/react/src/lib.rs
index 9c4be9a..03d41f8 100644
--- a/packages/react/src/lib.rs
+++ b/packages/react/src/lib.rs
@@ -1,6 +1,36 @@
+use js_sys::{Object, Reflect};
 use wasm_bindgen::prelude::*;
 
-#[wasm_bindgen]
-pub fn jsxDEV() -> String {
-    "hello world".to_string()
-}
\ No newline at end of file
+use shared::REACT_ELEMENT_TYPE;
+
+#[wasm_bindgen(js_name = jsxDEV)]
+pub fn jsx_dev(_type: &JsValue, config: &JsValue, key: &JsValue) -> JsValue {
+    let react_element = Object::new();
+    Reflect::set(
+        &react_element,
+        &"&&typeof".into(),
+        &JsValue::from_str(REACT_ELEMENT_TYPE),
+    )
+        .expect("$$typeof panic");
+    Reflect::set(&react_element, &"type".into(), _type).expect("_type panic");
+    Reflect::set(&react_element, &"key".into(), key).expect("key panic");
+
+    let conf = config.dyn_ref::<Object>().unwrap();
+    let props = Object::new();
+    for prop in Object::keys(conf) {
+        let val = Reflect::get(conf, &prop);
+        match prop.as_string() {
+            None => {}
+            Some(k) => {
+                if k == "ref" && val.is_ok() {
+                    Reflect::set(&react_element, &"ref".into(), &val.unwrap()).expect("ref panic");
+                } else if val.is_ok() {
+                    Reflect::set(&props, &JsValue::from(k), &val.unwrap()).expect("props panic");
+                }
+            }
+        }
+    }
+
+    Reflect::set(&react_element, &"props".into(), &props).expect("props panic");
+    react_element.into()
+}
diff --git a/packages/shared/src/lib.rs b/packages/shared/src/lib.rs
index 779fc2e..433e5d7 100644
--- a/packages/shared/src/lib.rs
+++ b/packages/shared/src/lib.rs
@@ -1,5 +1 @@
-pub struct REACT_ELEMENT_TYPE;
-
-// pub enum ElementType {
-//
-// }
\ No newline at end of file
+pub static REACT_ELEMENT_TYPE: &str = "react.element";
\ No newline at end of file