Initial commit
This commit is contained in:
24
public/hooks/useSession.ts
Normal file
24
public/hooks/useSession.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
function generateSessionId() {
|
||||
return Math.random().toString(36).substring(2, 15);
|
||||
}
|
||||
|
||||
export function useSession() {
|
||||
const [sessionId, setSessionId] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const url = new URL(window.location.href);
|
||||
let id = url.searchParams.get('sessionId');
|
||||
|
||||
if (!id) {
|
||||
id = generateSessionId();
|
||||
url.searchParams.set('sessionId', id);
|
||||
window.history.replaceState({}, '', url.toString());
|
||||
}
|
||||
|
||||
setSessionId(id);
|
||||
}, []);
|
||||
|
||||
return sessionId;
|
||||
}
|
||||
Reference in New Issue
Block a user