mirror of
https://github.com/PrimalHQ/primal-web-app.git
synced 2024-10-01 17:31:13 +00:00
Add app states
This commit is contained in:
parent
44f67d2551
commit
3175a76cf3
@ -10,10 +10,12 @@ import {
|
||||
|
||||
export type AppContextStore = {
|
||||
isInactive: boolean,
|
||||
appState: 'sleep' | 'waking' | 'woke',
|
||||
}
|
||||
|
||||
const initialData = {
|
||||
const initialData: AppContextStore = {
|
||||
isInactive: false,
|
||||
appState: 'woke',
|
||||
};
|
||||
|
||||
export const AppContext = createContext<AppContextStore>();
|
||||
@ -38,10 +40,33 @@ export const AppProvider = (props: { children: JSXElement }) => {
|
||||
|
||||
onMount(() => {
|
||||
document.addEventListener('mousemove', monitorActivity);
|
||||
document.addEventListener('scroll', monitorActivity);
|
||||
document.addEventListener('keydown', monitorActivity);
|
||||
});
|
||||
|
||||
onCleanup(() => {
|
||||
document.removeEventListener('mousemove', monitorActivity);
|
||||
document.removeEventListener('scroll', monitorActivity);
|
||||
document.removeEventListener('keydown', monitorActivity);
|
||||
});
|
||||
|
||||
let wakingTimeout = 0;
|
||||
|
||||
createEffect(() => {
|
||||
if (store.isInactive) {
|
||||
updateStore('appState', () => 'sleep');
|
||||
clearTimeout(wakingTimeout);
|
||||
}
|
||||
else {
|
||||
// Set this state in order to make sure that we reload page
|
||||
// when user requests future notes because we didn't fetch them yet
|
||||
updateStore('appState', () => 'waking');
|
||||
|
||||
// Give time for future notes fetching to fire before changing state
|
||||
wakingTimeout = setTimeout(() => {
|
||||
updateStore('appState', () => 'woke');
|
||||
}, 36_000);
|
||||
}
|
||||
});
|
||||
|
||||
// STORES ---------------------------------------
|
||||
|
@ -110,7 +110,7 @@ const Home: Component = () => {
|
||||
});
|
||||
|
||||
const loadNewContent = () => {
|
||||
if (newNotesCount() > 100) {
|
||||
if (newNotesCount() > 100 || app?.appState === 'waking') {
|
||||
location.reload();
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user