Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions website/src/components/SearchPage/SearchFullUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const InnerSearchFullUI = ({

const {
state,
setState,
previewedSeqId,
setPreviewedSeqId,
previewHalfScreen,
Expand All @@ -106,6 +107,50 @@ export const InnerSearchFullUI = ({
setAColumnVisibility,
} = useSearchPageState({ initialQueryDict, schema, hiddenFieldValues, filterSchema, referenceGenomesInfo });

const isEmptyQueryState = (q: QueryState | null) => q === null || Object.keys(q).length === 0;
const sessionQueryKey = `${organism}SearchPageQuery`;

const restorableQuery: QueryState | null = useMemo(() => {
// On mount, check if sessionStorage has a query to restore
// sessionStorage is undefined during server-side rendering
if (typeof sessionStorage !== 'undefined') {
const queryValue = sessionStorage.getItem(sessionQueryKey);
if (queryValue) {
try {
return JSON.parse(queryValue) as QueryState;
} catch {
return null;
}
}
}
return null;
}, []);

const [showSessionQueryRestore, setShowSessionQueryRestore] = useState(false);
useEffect(() => {
// Show the restore button if the state is empty and there's a query that can be restored
const isSessionQueryRestorable = isEmptyQueryState(state) && !isEmptyQueryState(restorableQuery);
if (isSessionQueryRestorable) setShowSessionQueryRestore(true);
}, []);

useEffect(() => {
// Update sessionStorage with the new state
if (typeof sessionStorage !== 'undefined') {
if (!isEmptyQueryState(state)) sessionStorage.setItem(sessionQueryKey, JSON.stringify(state));
else sessionStorage.removeItem(sessionQueryKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im a bit confused by this line, when would we want to remove the old sessionQuery is the current query state is empty?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also means that if I click on a sequence after going back to a search page the restore state disappears, I probably would opt to not remove the state, but maybe I am missing sth

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is not ideal behaviour, I will have a further look at this. It should really only be affected by changing the filters

}

// If the state is no longer empty, hide the restore button
if (!isEmptyQueryState(state)) setShowSessionQueryRestore(false);
}, [state]);

const restoreSessionQuery = () => {
if (restorableQuery) {
setState(restorableQuery);
setShowSessionQueryRestore(false);
}
};

const searchVisibilities = useMemo(() => {
return getFieldVisibilitiesFromQuery(schema, state);
}, [schema, state]);
Expand Down Expand Up @@ -317,6 +362,17 @@ export const InnerSearchFullUI = ({
<div className='text-sm text-gray-800 mb-6 justify-between flex flex-col sm:flex-row items-baseline gap-4'>
<div className='mt-auto'>
{buildSequenceCountText(totalSequences, oldCount, initialCount)}
{showSessionQueryRestore && (
<span>
<span className='m-2 text-gray-400'>|</span>
<Button
className='text-sm underline text-primary-700 hover:text-primary-500'
onClick={restoreSessionQuery}
>
Restore previous search
</Button>
</span>
)}
{detailsHook.isPending ||
aggregatedHook.isPending ||
!firstClientSideLoadOfCountCompleted ||
Expand Down
2 changes: 2 additions & 0 deletions website/src/components/SearchPage/useSearchPageState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export function useSearchPageState({
return useMemo(
() => ({
state,
setState,
previewedSeqId,
setPreviewedSeqId,
previewHalfScreen,
Expand All @@ -267,6 +268,7 @@ export function useSearchPageState({
}),
[
state,
setState,
previewedSeqId,
setPreviewedSeqId,
previewHalfScreen,
Expand Down
Loading