Skip to content

Commit

Permalink
fix history when browsing library
Browse files Browse the repository at this point in the history
  • Loading branch information
g3gg0 committed Aug 20, 2023
1 parent 73d0dd2 commit 45d7a39
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions contrib/data/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,10 @@ <h2>Client certificate upload</h2>
super(props);
this.state = {
path: '/',
mode: 'content',
files: [],
selectedFiles: [],
tonies: [],
mode: "content",
showSuccessMessage: false,
showSuccessState: false,
opacity: 1
Expand All @@ -805,25 +805,27 @@ <h2>Client certificate upload</h2>
componentDidMount() {
// Extract path from the URL if present
const urlParams = new URLSearchParams(window.location.search);
const initialPath = urlParams.get('path') || '/';
const initialMode = urlParams.get('mode') || 'content';
const path = urlParams.get('path') || this.state.path;
const mode = urlParams.get('mode') || this.state.mode;

this.setState({ path: initialPath, mode: initialMode });
this.fetchFileIndex(initialPath, initialMode);
this.setState({ path: path, mode: mode });
this.fetchFileIndex(path, mode);

fetch("/tonies.json")
.then(response => response.json())
.then(data => this.setState({ tonies: data }));

window.addEventListener('popstate', this.handlePopState);

window.history.replaceState({ path, mode }, '', `?path=${path}&mode=${mode}`);
}

componentWillUnmount() {
window.removeEventListener('popstate', this.handlePopState);
}

handlePopState = (event) => {
if (event.state && event.state.path) {
if (event.state && event.state.path && event.state.mode) {
this.fetchFileIndex(event.state.path, event.state.mode, true);
}
};
Expand Down Expand Up @@ -889,7 +891,14 @@ <h2>Client certificate upload</h2>

async fetchFileIndex(path, mode, scroll = false) {
try {
window.history.pushState({ path }, '', `?path=${path}&mode=${mode}`);
const urlParams = new URLSearchParams(window.location.search);
const currentPath = urlParams.get('path') || '';
const currentMode = urlParams.get('mode') || '';

if (path !== currentPath || mode !== currentMode) {
window.history.pushState({ path, mode }, '', `?path=${path}&mode=${mode}`);
}

const response = await fetch(`/api/fileIndex?path=${encodeURIComponent(path)}&special=${mode}`);
if (response.ok) {
const data = await response.json();
Expand Down

0 comments on commit 45d7a39

Please sign in to comment.