mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 01:07:46 +00:00
feat: Introduce session context and add session duration stat for /stats command (#854)
This commit is contained in:
29
packages/cli/src/ui/contexts/SessionContext.test.tsx
Normal file
29
packages/cli/src/ui/contexts/SessionContext.test.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { render } from 'ink-testing-library';
|
||||
import { Text } from 'ink';
|
||||
import { SessionProvider, useSession } from './SessionContext.js';
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
const TestComponent = () => {
|
||||
const { startTime } = useSession();
|
||||
return <Text>{startTime.toISOString()}</Text>;
|
||||
};
|
||||
|
||||
describe('SessionContext', () => {
|
||||
it('should provide a start time', () => {
|
||||
const { lastFrame } = render(
|
||||
<SessionProvider>
|
||||
<TestComponent />
|
||||
</SessionProvider>,
|
||||
);
|
||||
|
||||
const frameText = lastFrame();
|
||||
// Check if the output is a valid ISO string, which confirms it's a Date object.
|
||||
expect(new Date(frameText!).toString()).not.toBe('Invalid Date');
|
||||
});
|
||||
});
|
||||
38
packages/cli/src/ui/contexts/SessionContext.tsx
Normal file
38
packages/cli/src/ui/contexts/SessionContext.tsx
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user