1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use wasm_bindgen::JsValue;
use web_sys::window;
use web_sys::Url;

pub struct History;

impl History {
	/// set search param without page reload
	pub fn set_flag(key: &str, value: bool) {
		if value {
			Self::set_param(key, "1");
		} else {
			Self::remove_param(key);
		}
	}
	pub fn set_param(key: &str, value: &str) {
		let url = window().unwrap().location().href().unwrap();
		let url = Url::new(&url).unwrap();
		let params = url.search_params();
		params.set(key, value);
		let url = url.href();
		Self::push(&url);
	}
	pub fn append_param(key: &str, value: &str) {
		let url = window().unwrap().location().href().unwrap();
		let url = Url::new(&url).unwrap();
		let params = url.search_params();
		params.append(key, value);
		let url = url.href();
		Self::push(&url);
	}

	pub fn remove_param(key: &str) {
		let url = window().unwrap().location().href().unwrap();
		let url = Url::new(&url).unwrap();
		let params = url.search_params();
		params.delete(key);
		let url = url.href();
		Self::push(&url);
	}

	pub fn push(path: &str) {
		window()
			.unwrap()
			.history()
			.unwrap()
			.push_state_with_url(&JsValue::UNDEFINED, "", Some(path))
			.unwrap();
	}
	pub fn push_preserve_params(path: &str) {
		let location = window().unwrap().location();
		let href = location.href().unwrap();
		let url = Url::new(&href).unwrap();
		url.set_pathname(path);
		Self::push(url.href().as_str());
	}
	pub fn replace(path: &str) {
		window()
			.unwrap()
			.history()
			.unwrap()
			.replace_state_with_url(&JsValue::UNDEFINED, path, Some(path))
			.unwrap();
	}

	// pub fn replace_params(params:UrlSearchParams){


	// }
}