Files
ai-safety-console-web/src/auth/activity.ts
T
baozaotumao2025 1b90e552a5 first commit
2026-07-18 21:10:39 +08:00

25 lines
1.0 KiB
TypeScript

const key = 'safety-last-activity-at'
const throttleMs = 30_000
export const activity = {
get: () => Number(localStorage.getItem(key) || 0),
record(force = false) {
const now = Date.now()
if (force || now - this.get() >= throttleMs) localStorage.setItem(key, String(now))
},
clear: () => localStorage.removeItem(key),
start(checkRefresh: () => void) {
const onActivity = () => { this.record(); checkRefresh() }
const onVisible = () => { if (document.visibilityState === 'visible') checkRefresh() }
const events = ['click', 'keydown', 'touchstart'] as const
events.forEach((event) => window.addEventListener(event, onActivity, { passive: true }))
window.addEventListener('popstate', onActivity)
document.addEventListener('visibilitychange', onVisible)
return () => {
events.forEach((event) => window.removeEventListener(event, onActivity))
window.removeEventListener('popstate', onActivity)
document.removeEventListener('visibilitychange', onVisible)
}
},
}