Skip to content

Commit

Permalink
better tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrimpuff committed Oct 20, 2024
1 parent 9866a8f commit 5ea8ec3
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ printpdf = { git = "https://github.com/Qrimpuff/printpdf", branch = "reuse_image
] }
image = { version = "0.25.2", features = ["png", "webp"] }
futures = "0.3.31"
wasm-bindgen = { version = "0.2.95", features = ["serde"] }
4 changes: 2 additions & 2 deletions assets/untrack.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<body></body>
<script>
let disabled = localStorage.getItem("umami.disabled");
if (disabled == "1") {
localStorage.setItem("umami.disabled", "0");
if (disabled) {
localStorage.removeItem("umami.disabled");
} else {
localStorage.setItem("umami.disabled", "1");
}
Expand Down
6 changes: 6 additions & 0 deletions assets/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export function track_event(event, data) {
if (window.umami && window.umami.track) {
window.umami.track(event, data);
}
}
73 changes: 70 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use dioxus::prelude::*;
use dioxus_logger::tracing::{info, Level};
use gloo::{
file::{Blob, BlobContents},
utils::document,
utils::{document, format::JsValueSerdeExt},
};
use serde::Serialize;
use sources::*;
use web_sys::{wasm_bindgen::JsCast, Url};
use wasm_bindgen::prelude::*;
use web_sys::Url;

fn main() {
// Init logger
Expand Down Expand Up @@ -186,6 +188,15 @@ pub fn UnknownImport(
mut common_deck: Signal<Option<CommonDeck>>,
map: Signal<CardsInfoMap>,
) -> Element {
#[derive(Serialize)]
struct EventData {
format: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
file_format: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<String>,
}

let mut deck_error = use_signal(String::new);
let mut deck_success = use_signal(String::new);
let mut file_name = use_signal(String::new);
Expand All @@ -208,6 +219,14 @@ pub fn UnknownImport(
*common_deck.write() =
Some(holodelta::Deck::to_common_deck(deck, &map.read()));
*deck_success.write() = "Deck file format: holoDelta".into();
track_convert_event(
EventType::Import,
EventData {
format: "Unknown",
file_format: Some("holoDelta"),
error: None,
},
);
return;
}

Expand All @@ -218,6 +237,14 @@ pub fn UnknownImport(
*common_deck.write() =
Some(holoduel::Deck::to_common_deck(deck, &map.read()));
*deck_success.write() = "Deck file format: HoloDuel".into();
track_convert_event(
EventType::Import,
EventData {
format: "Unknown",
file_format: Some("HoloDuel"),
error: None,
},
);
return;
}

Expand All @@ -229,10 +256,26 @@ pub fn UnknownImport(
Some(tabletop_sim::Deck::to_common_deck(deck, &map.read()));
*deck_success.write() =
"Deck file format: Tabletop Simulator (by Noodlebrain)".into();
track_convert_event(
EventType::Import,
EventData {
format: "Unknown",
file_format: Some("Tabletop Sim"),
error: None,
},
);
return;
}

*deck_error.write() = "Cannot parse deck file".into();
track_convert_event(
EventType::Import,
EventData {
format: "Unknown",
file_format: None,
error: Some("Cannot parse deck file".into()),
},
);
}
}
}
Expand Down Expand Up @@ -282,7 +325,7 @@ fn DeckPreview() -> Element {
let map = CARDS_INFO.read();

let Some(deck) = deck.as_ref() else {
return rsx! { };
return rsx! {};
};

let oshi = rsx! {
Expand Down Expand Up @@ -405,3 +448,27 @@ pub fn done_loading() {
loading.remove();
}
}

#[wasm_bindgen(module = "/assets/utils.js")]
extern "C" {
fn track_event(event: &str, data: JsValue);
}

pub enum EventType {
Import,
Export,
}

pub fn track_convert_event<T>(event: EventType, data: T)
where
T: serde::ser::Serialize,
{
let event = match event {
EventType::Import => "import",
EventType::Export => "export",
};

let data = JsValue::from_serde(&data).unwrap();

track_event(event, data);
}
80 changes: 70 additions & 10 deletions src/sources/deck_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use dioxus_logger::tracing::info;
use reqwest::{Client, ClientBuilder};
use serde::{Deserialize, Serialize};

use crate::{track_convert_event, EventType};

use super::{
CardsInfoMap, CommonCards, CommonCardsConversion, CommonDeck, CommonDeckConversion,
MergeCommonCards,
Expand Down Expand Up @@ -102,7 +104,7 @@ impl Deck {
Ok(serde_json::from_str(&content).map_err(|_| content)?)
}

pub async fn publish(&self, game_title_id: u32) -> Result<String, Box<dyn Error>> {
pub async fn publish(&mut self, game_title_id: u32) -> Result<String, Box<dyn Error>> {
let mut req = PublishDeckRequest(self.clone());
req.0.game_title_id = game_title_id;

Expand All @@ -118,13 +120,10 @@ impl Deck {

let res: ViewDeckResult = serde_json::from_str(&content).map_err(|_| content)?;

let deck = Deck {
game_title_id,
deck_id: res.deck_id,
..Deck::default()
};
self.game_title_id = game_title_id;
self.deck_id = res.deck_id;

Ok(deck.view_url())
Ok(self.view_url())
}
}

Expand Down Expand Up @@ -193,6 +192,17 @@ fn http_client() -> &'static Client {

#[component]
pub fn Import(mut common_deck: Signal<Option<CommonDeck>>, map: Signal<CardsInfoMap>) -> Element {
#[derive(Serialize)]
struct EventData {
format: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
game_title_id: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
deck_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<String>,
}

let mut deck_error = use_signal(String::new);
let mut import_url_code = use_signal(String::new);
let mut is_url = use_signal(|| false);
Expand Down Expand Up @@ -244,9 +254,29 @@ pub fn Import(mut common_deck: Signal<Option<CommonDeck>>, map: Signal<CardsInfo
match deck {
Ok(deck) => {
*deck_log_url.write() = deck.view_url();
track_convert_event(
EventType::Import,
EventData {
format: "Deck Log",
game_title_id: Some(deck.game_title_id),
deck_id: Some(deck.deck_id.clone()),
error: None,
},
);
*common_deck.write() = Some(Deck::to_common_deck(deck, &map.read()));
}
Err(e) => *deck_error.write() = e.to_string(),
Err(e) => {
*deck_error.write() = e.to_string();
track_convert_event(
EventType::Import,
EventData {
format: "Deck Log",
game_title_id: None,
deck_id: None,
error: Some(e.to_string()),
},
);
}
}

*loading.write() = false;
Expand Down Expand Up @@ -297,6 +327,16 @@ static PUBLISH_CACHE: GlobalSignal<HashMap<(u32, u64), String>> = Signal::global

#[component]
pub fn Export(mut common_deck: Signal<Option<CommonDeck>>, map: Signal<CardsInfoMap>) -> Element {
#[derive(Serialize)]
struct EventData {
format: &'static str,
game_title_id: u32,
#[serde(skip_serializing_if = "Option::is_none")]
deck_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<String>,
}

let mut deck_error = use_signal(String::new);
let mut game_title_id = use_signal(|| 9); // default to Deck Log JP
let mut deck_log_url = use_signal(String::new);
Expand Down Expand Up @@ -327,15 +367,35 @@ pub fn Export(mut common_deck: Signal<Option<CommonDeck>>, map: Signal<CardsInfo
return;
}

let deck = Deck::from_common_deck(common_deck.clone(), &map.read());
let mut deck = Deck::from_common_deck(common_deck.clone(), &map.read());
match deck.publish(*game_title_id.read()).await {
Ok(url) => {
*deck_log_url.write() = url.clone();
PUBLISH_CACHE
.write()
.insert((*game_title_id.read(), common_deck.calculate_hash()), url);
track_convert_event(
EventType::Export,
EventData {
format: "Deck Log",
game_title_id: *game_title_id.read(),
deck_id: Some(deck.deck_id.clone()),
error: None,
},
);
}
Err(e) => {
*deck_error.write() = e.to_string();
track_convert_event(
EventType::Export,
EventData {
format: "Deck Log",
game_title_id: *game_title_id.read(),
deck_id: None,
error: Some(e.to_string()),
},
);
}
Err(e) => *deck_error.write() = e.to_string(),
}

*loading.write() = false;
Expand Down
Loading

0 comments on commit 5ea8ec3

Please sign in to comment.