feat: Introduce session context and add session duration stat for /stats command (#854)

This commit is contained in:
Abhi
2025-06-08 18:01:02 -04:00
committed by GitHub
parent 9104ac02f7
commit 7868ef8229
7 changed files with 146 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import React, { createContext, useContext, useState, useMemo } from 'react';
interface SessionContextType {
startTime: Date;
}
const SessionContext = createContext<SessionContextType | null>(null);
export const SessionProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [startTime] = useState(new Date());
const value = useMemo(
() => ({
startTime,
}),
[startTime],
);
return (
<SessionContext.Provider value={value}>{children}</SessionContext.Provider>
);
};
export const useSession = () => {
const context = useContext(SessionContext);
if (!context) {
throw new Error('useSession must be used within a SessionProvider');
}
return context;
};