Skip to content

Commit

Permalink
Make the back button take the user back to the entry list or previous…
Browse files Browse the repository at this point in the history
…ly selected entry
  • Loading branch information
myieye committed Jan 7, 2025
1 parent 5ccf3be commit 21e381c
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions frontend/viewer/src/ProjectView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,30 @@
let showOptionsDialog = false;
let pickedEntry = false;
let navigateToEntryId = getSearchParam(ViewerSearchParam.EntryId);
// Makes the back button work for going back to the list view
// todo use a lib for this?
window.addEventListener('popstate', () => {
const currEntryId = getSearchParam(ViewerSearchParam.EntryId);
if (!currEntryId) {
pickedEntry = false;
$selectedEntry = undefined;
} else {
if (currEntryId !== $selectedEntry?.id) {
navigateToEntryId = currEntryId;
refreshSelection();
}
}
});
const selectedEntry = writable<IEntry | undefined>(undefined);
setContext('selectedEntry', selectedEntry);
// For some reason reactive syntax doesn't pick up every change, so we need to manually subscribe
// and we need the extra call to updateEntryIdSearchParam in refreshSelection
const unsubSelectedEntry = selectedEntry.subscribe(updateEntryIdSearchParam);
$: { pickedEntry; updateEntryIdSearchParam(); }
function updateEntryIdSearchParam() {
updateSearchParam(ViewerSearchParam.EntryId, navigateToEntryId ?? (pickedEntry ? $selectedEntry?.id : undefined), true);
updateSearchParam(ViewerSearchParam.EntryId, navigateToEntryId ?? (pickedEntry ? $selectedEntry?.id : undefined));
}
$: {
Expand All @@ -176,6 +192,7 @@
const entry = $entries.find(e => e.id === navigateToEntryId);
if (entry) {
$selectedEntry = entry;
pickedEntry = true;
}
} else if ($selectedEntry !== undefined) {
const entry = $entries.find(e => e.id === $selectedEntry!.id);
Expand All @@ -184,9 +201,7 @@
}
}
if ($selectedEntry) {
pickedEntry = true;
} else {
if (!$selectedEntry) {
pickedEntry = false;
if ($entries?.length > 0)
$selectedEntry = $entries[0];
Expand Down

0 comments on commit 21e381c

Please sign in to comment.