mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-01-31 06:42:03 +00:00
- Replaced console.log and console.error statements with logger methods from @automaker/utils in various UI components, ensuring consistent log formatting and improved readability. - Enhanced error handling by utilizing logger methods to provide clearer context for issues encountered during operations. - Updated multiple views and hooks to integrate the new logging system, improving maintainability and debugging capabilities. This update significantly enhances the observability of UI components, facilitating easier troubleshooting and monitoring.
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { useState, useCallback, useEffect, useRef } from 'react';
|
|
import { createLogger } from '@automaker/utils/logger';
|
|
import { useAppStore } from '@/store/app-store';
|
|
|
|
const logger = createLogger('AgentSession');
|
|
|
|
interface UseAgentSessionOptions {
|
|
projectPath: string | undefined;
|
|
}
|
|
|
|
interface UseAgentSessionResult {
|
|
currentSessionId: string | null;
|
|
handleSelectSession: (sessionId: string | null) => void;
|
|
}
|
|
|
|
export function useAgentSession({ projectPath }: UseAgentSessionOptions): UseAgentSessionResult {
|
|
const { setLastSelectedSession, getLastSelectedSession } = useAppStore();
|
|
const [currentSessionId, setCurrentSessionId] = useState<string | null>(null);
|
|
|
|
// Track if initial session has been loaded
|
|
const initialSessionLoadedRef = useRef(false);
|
|
|
|
// Handle session selection with persistence
|
|
const handleSelectSession = useCallback(
|
|
(sessionId: string | null) => {
|
|
setCurrentSessionId(sessionId);
|
|
// Persist the selection for this project
|
|
if (projectPath) {
|
|
setLastSelectedSession(projectPath, sessionId);
|
|
}
|
|
},
|
|
[projectPath, setLastSelectedSession]
|
|
);
|
|
|
|
// Restore last selected session when switching to Agent view or when project changes
|
|
useEffect(() => {
|
|
if (!projectPath) {
|
|
// No project, reset
|
|
setCurrentSessionId(null);
|
|
initialSessionLoadedRef.current = false;
|
|
return;
|
|
}
|
|
|
|
// Only restore once per project
|
|
if (initialSessionLoadedRef.current) return;
|
|
initialSessionLoadedRef.current = true;
|
|
|
|
const lastSessionId = getLastSelectedSession(projectPath);
|
|
if (lastSessionId) {
|
|
logger.info('Restoring last selected session:', lastSessionId);
|
|
setCurrentSessionId(lastSessionId);
|
|
}
|
|
}, [projectPath, getLastSelectedSession]);
|
|
|
|
// Reset initialSessionLoadedRef when project changes
|
|
useEffect(() => {
|
|
initialSessionLoadedRef.current = false;
|
|
}, [projectPath]);
|
|
|
|
return {
|
|
currentSessionId,
|
|
handleSelectSession,
|
|
};
|
|
}
|