forked from CredenceOrg/Credence-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionTimeoutModal.tsx
More file actions
71 lines (63 loc) · 1.96 KB
/
Copy pathSessionTimeoutModal.tsx
File metadata and controls
71 lines (63 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { useEffect, useState } from 'react'
import ConfirmDialog from './ConfirmDialog'
export interface SessionTimeoutModalProps {
open: boolean
onStayLoggedIn: () => void
onLogout: () => void
timeLeftSeconds: number
}
/**
* Modal shown when the user's session is about to expire due to inactivity.
*/
export default function SessionTimeoutModal({
open,
onStayLoggedIn,
onLogout,
timeLeftSeconds,
}: SessionTimeoutModalProps) {
const [internalTimeLeft, setInternalTimeLeft] = useState(timeLeftSeconds)
useEffect(() => {
setInternalTimeLeft(timeLeftSeconds)
}, [timeLeftSeconds])
useEffect(() => {
if (!open || internalTimeLeft <= 0) return
const timer = setInterval(() => {
setInternalTimeLeft((prev) => Math.max(0, prev - 1))
}, 1000)
return () => clearInterval(timer)
}, [open, internalTimeLeft])
if (!open) return null
return (
<ConfirmDialog
open={open}
title="Session Timeout Warning"
subtitle={`Your session will expire in ${internalTimeLeft} seconds due to inactivity.`}
onConfirm={onStayLoggedIn}
onCancel={onLogout}
confirmLabel="Stay logged in"
confirmPhrase="STAY"
confirmHint="Press the button above to extend your session."
variant="info"
confirmInputLabel={
<>
Type <strong>STAY</strong> to remain logged in
</>
}
>
<div
style={{
padding: 'var(--credence-space-4)',
background: 'var(--credence-color-warning-surface)',
border: '1px solid var(--credence-color-warning-border)',
borderRadius: 'var(--credence-radius-md)',
color: 'var(--credence-color-warning-text)',
fontSize: 'var(--credence-font-size-sm)',
marginBottom: 'var(--credence-space-4)',
}}
>
For your security, you are automatically logged out after a period of inactivity. Any
unsaved changes may be lost.
</div>
</ConfirmDialog>
)
}