Skip to content

Commit

Permalink
feat:Remember home settings over sessions (#18)
Browse files Browse the repository at this point in the history
* feat:Remember home settings over sessions

* fix:linting errors
  • Loading branch information
rsanteri committed Nov 3, 2021
1 parent 111ae26 commit 977d431
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/pages/Home/Home.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export function isDefaultParams(params: Record<string, string>, checkTimerange:
if (
params._order === defaultHomeParameters._order &&
params._limit === defaultHomeParameters._limit &&
params.status === defaultHomeParameters.status &&
params.status?.indexOf('completed') > -1 &&
params.status?.indexOf('running') > -1 &&
params.status?.indexOf('failed') > -1 &&
(checkTimerange ? params.timerange_start === getTimeFromPastByDays(30, timezone).toString() : true)
) {
return true;
Expand Down
70 changes: 43 additions & 27 deletions src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ const Home: React.FC = () => {

useEffect(() => {
HomeStateCache.active = false;
}, []);

// Try to use same params as last time when on frontpage. But only try this if
// user is coming to default frontpage. We don't want to interrupt direct links from working
const lastUsedParams = JSON.parse(localStorage.getItem('home-params') || '');
if (lastUsedParams && isDefaultParams(rawParams, false)) {
setQp(lastUsedParams);
}
}, []); // eslint-disable-line

//
// QueryParams
Expand All @@ -59,8 +66,9 @@ const Home: React.FC = () => {
dispatch({
type: 'setParams',
params: rawParams,
cachedResult: historyAction === 'POP' && !HomeStateCache.active && HomeStateCache.scroll > 0,
cachedResult: shouldUseCachedResult(historyAction),
});
localStorage.setItem('home-params', JSON.stringify(rawParams));
}, [rawParamsString]); // eslint-disable-line

//
Expand All @@ -82,31 +90,6 @@ const Home: React.FC = () => {
isScrolledFromTop: historyAction === 'POP' && HomeStateCache.scroll > 100,
});

//
// Cache
//

useEffect(() => {
const scrollValue = HomeStateCache.scroll;
if (historyAction === 'POP' && scrollValue > 0 && initialised) {
HomeStateCache.active = true;
} else if (historyAction !== 'POP' || scrollValue === 0) {
HomeStateCache.active = true;
}

if (historyAction !== 'POP') {
// We want to add timerange filter if we are rolling with default params
// but not in back event. In back event we should keep state we had
if (isDefaultParams(rawParams, false, timezone)) {
setQp({ timerange_start: getTimeFromPastByDays(30, timezone).toString() });
}
}
}, [historyAction, initialised]); // eslint-disable-line

useEffect(() => {
HomeStateCache.page = page;
}, [page]);

//
// Data
//
Expand Down Expand Up @@ -149,6 +132,33 @@ const Home: React.FC = () => {
},
});

//
// Cache
//

useEffect(() => {
const scrollValue = HomeStateCache.scroll;
if (historyAction === 'POP' && scrollValue > 0 && initialised) {
HomeStateCache.active = true;
} else if (historyAction !== 'POP' || scrollValue === 0) {
HomeStateCache.active = true;
}

if (historyAction !== 'POP' && initialised) {
if (isDefaultParams(rawParams, false, timezone)) {
// We want to add timerange filter if we are rolling with default params
// but not in back event. In back event we should keep state we had
setQp({ timerange_start: getTimeFromPastByDays(30, timezone).toString() });
}
}
}, [historyAction, initialised]); // eslint-disable-line

// Update cache page on page change
useEffect(() => {
HomeStateCache.page = page;
}, [page]);

// Update cache data on data changes
useEffect(() => {
HomeStateCache.data = rungroups;
}, [rungroups]);
Expand Down Expand Up @@ -267,4 +277,10 @@ const Home: React.FC = () => {
);
};

function shouldUseCachedResult(historyAction: string) {
// We should use cached result on init if route action was POP (back), we haven't yet used cache and cache scroll is
// creater than 0. If scroll state of cache is 0, we might as well load new results.
return historyAction === 'POP' && !HomeStateCache.active && HomeStateCache.scroll > 0;
}

export default Home;

0 comments on commit 977d431

Please sign in to comment.