From c6c651f0b4324c1de7d8e56bcad53d9537e685ab Mon Sep 17 00:00:00 2001 From: Aaron Gutierrez Date: Thu, 11 Jan 2024 10:36:10 -0800 Subject: [PATCH] Better history state management Pop the history when closing the image viewer instead of building up a long list of images. This makes it easier to navigate back to the home page with the browser back button --- src/components/root.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/components/root.tsx b/src/components/root.tsx index 5e090df..5993db9 100644 --- a/src/components/root.tsx +++ b/src/components/root.tsx @@ -161,8 +161,12 @@ export class Root extends React.PureComponent { }; private _onImageSelected = (img: Model.Image) => { + if (this.state.selectedImage) { + window.history.replaceState(null, "", `#${img.src}`); + } else { + window.history.pushState(null, "", `#${img.src}`); + } this.setState({ selectedImage: img }); - window.history.pushState(null, "", `#${img.src}`); }; private _onSetSelected = (set: Model.ImageSet) => { @@ -188,6 +192,7 @@ export class Root extends React.PureComponent { private _showGrid = () => { this.setState({ selectedImage: null }); + window.history.go(-1); this._onSetSelected(this.state.selectedSet as Model.ImageSet); };