From e4d6788bfa1160803ef5c4414f78c37dec2fbe6e Mon Sep 17 00:00:00 2001 From: Thomas Mathews Date: Thu, 5 Jan 2023 17:04:56 -0800 Subject: [PATCH] Applied history state pushing. Co-authored-by: nostr-pleb21 --- js/ui/state.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/js/ui/state.js b/js/ui/state.js index 4ea3471..da07f53 100644 --- a/js/ui/state.js +++ b/js/ui/state.js @@ -9,7 +9,16 @@ function view_get_timeline_el() { return find_node("#timeline"); } -function view_timeline_apply_mode(model, mode, opts={}) { +// TODO clean up popstate listener (move to init method or such) +window.addEventListener("popstate", function(event) { + if (event.state && event.state.mode) { + // Update the timeline mode. + // Pass pushState=false to avoid adding another state to the history + view_timeline_apply_mode(DAMUS, event.state.mode, event.state.opts, false); + } +}) + +function view_timeline_apply_mode(model, mode, opts={}, push_state=true) { let xs; const { pubkey, thread_id } = opts; const el = view_get_timeline_el(); @@ -30,6 +39,10 @@ function view_timeline_apply_mode(model, mode, opts={}) { return; } } + + // Push a new state to the browser history stack + if (push_state) + history.pushState({mode, opts}, ''); // Fetch history for certain views if (mode == VM_THREAD) {