-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #624 from Enterprise-CMCS/master
Release to val
- Loading branch information
Showing
7 changed files
with
216 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useEffect, useState } from "react"; | ||
import { | ||
Button, | ||
Dialog, | ||
DialogContent, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
} from "@/components"; | ||
import { Auth } from "aws-amplify"; | ||
import { useIdle } from "@/hooks/useIdle"; | ||
import { useGetUser } from "@/api"; | ||
import { useCountdown } from "@/hooks/useCountdown"; | ||
import { intervalToDuration } from "date-fns"; | ||
import pluralize from "pluralize"; | ||
|
||
const TWENTY_MINS_IN_MILS = 1000 * 60 * 20; | ||
const TEN_MINS_IN_MILS = 60 * 10; | ||
|
||
export const TimeoutModal = () => { | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
const isIdleForTwentyMins = useIdle(TWENTY_MINS_IN_MILS, { | ||
initialState: false, | ||
}); | ||
|
||
const [timeoutModalCountdown, { startCountdown, resetCountdown }] = | ||
useCountdown(TEN_MINS_IN_MILS); | ||
const { data: user } = useGetUser(); | ||
|
||
const onLogOut = () => { | ||
Auth.signOut(); | ||
}; | ||
|
||
const onExtendSession = () => { | ||
setIsModalOpen(false); | ||
|
||
// artificial delay hiding the countdown reset after modal dismissal | ||
setTimeout(() => { | ||
resetCountdown(); | ||
}, 500); | ||
}; | ||
|
||
useEffect(() => { | ||
if (timeoutModalCountdown === 0) { | ||
onLogOut(); | ||
} | ||
}, [timeoutModalCountdown]); | ||
|
||
useEffect(() => { | ||
if (user?.user && isIdleForTwentyMins) { | ||
startCountdown(); | ||
setIsModalOpen(true); | ||
} | ||
}, [isIdleForTwentyMins]); | ||
|
||
const duration = intervalToDuration({ | ||
start: 0, | ||
end: timeoutModalCountdown * 1000, | ||
}); | ||
|
||
return ( | ||
<Dialog open={isModalOpen} onOpenChange={onExtendSession}> | ||
<DialogContent className="sm:max-w-[425px]"> | ||
<DialogHeader> | ||
<DialogTitle>Session expiring soon</DialogTitle> | ||
</DialogHeader> | ||
<div className="py-4"> | ||
<span> | ||
Your session will expire in <strong>{duration.minutes}</strong>{" "} | ||
{pluralize("minute", duration.minutes)} and{" "} | ||
<strong>{duration.seconds}</strong>{" "} | ||
{pluralize("second", duration.seconds)}. | ||
</span> | ||
</div> | ||
<DialogFooter> | ||
<Button type="submit" onClick={onExtendSession}> | ||
Yes, extend session | ||
</Button> | ||
<Button type="button" variant="outline" onClick={onLogOut}> | ||
No, sign out | ||
</Button> | ||
</DialogFooter> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// credit: https://usehooks-ts.com/react-hook/use-countdown | ||
|
||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
type CountdownControllers = { | ||
startCountdown: () => void; | ||
stopCountdown: () => void; | ||
resetCountdown: () => void; | ||
}; | ||
|
||
export const useCountdown = ( | ||
countStart: number, | ||
): [number, CountdownControllers] => { | ||
const [count, setCount] = useState<number>(countStart); | ||
const [isCountdownRunning, setIsCountdownRunning] = useState<boolean>(false); | ||
|
||
const startCountdown = () => { | ||
setIsCountdownRunning(true); | ||
}; | ||
|
||
const stopCountdown = () => { | ||
setIsCountdownRunning(false); | ||
}; | ||
|
||
// Will set running false and reset the seconds to initial value | ||
const resetCountdown = useCallback(() => { | ||
stopCountdown(); | ||
setCount(countStart); | ||
}, [stopCountdown]); | ||
|
||
const countdownCallback = useCallback(() => { | ||
if (count === 0) { | ||
stopCountdown(); | ||
return; | ||
} | ||
|
||
setCount((oldCount) => oldCount - 1); | ||
}, [count, stopCountdown]); | ||
|
||
useEffect(() => { | ||
if (isCountdownRunning === false) { | ||
return; | ||
} | ||
|
||
const id = setInterval(() => { | ||
countdownCallback(); | ||
}, 1000); | ||
|
||
return () => { | ||
clearInterval(id); | ||
}; | ||
}, [isCountdownRunning, countdownCallback]); | ||
|
||
return [count, { startCountdown, stopCountdown, resetCountdown }]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// credit: https://github.com/mantinedev/mantine/blob/master/packages/@mantine/hooks/src/use-idle/use-idle.ts | ||
|
||
import { useEffect, useRef, useState } from "react"; | ||
|
||
const DEFAULT_EVENTS: (keyof DocumentEventMap)[] = [ | ||
"keypress", | ||
"mousemove", | ||
"touchmove", | ||
"click", | ||
"scroll", | ||
]; | ||
const DEFAULT_OPTIONS = { | ||
events: DEFAULT_EVENTS, | ||
initialState: true, | ||
}; | ||
|
||
export function useIdle( | ||
timeout: number, | ||
options?: Partial<{ | ||
events: (keyof DocumentEventMap)[]; | ||
initialState: boolean; | ||
}>, | ||
) { | ||
const { events, initialState } = { ...DEFAULT_OPTIONS, ...options }; | ||
const [idle, setIdle] = useState<boolean>(initialState); | ||
const timer = useRef<number>(); | ||
|
||
// start tracking idleness on useIdle mount | ||
useEffect(() => { | ||
timer.current = window.setTimeout(() => { | ||
setIdle(true); | ||
}, timeout); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const handleEvents = () => { | ||
setIdle(false); | ||
|
||
if (timer.current) { | ||
window.clearTimeout(timer.current); | ||
} | ||
|
||
timer.current = window.setTimeout(() => { | ||
setIdle(true); | ||
}, timeout); | ||
}; | ||
|
||
events.forEach((event) => document.addEventListener(event, handleEvents)); | ||
|
||
return () => { | ||
events.forEach((event) => | ||
document.removeEventListener(event, handleEvents), | ||
); | ||
}; | ||
}, [timeout]); | ||
|
||
return idle; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters