91 lines
2.3 KiB
TypeScript
91 lines
2.3 KiB
TypeScript
import type { User, Session } from 'better-auth/types';
|
|
|
|
/**
|
|
* Central application state composable
|
|
* Manages navigation context and critical app-level state
|
|
* This is the single source of truth for "what am I viewing"
|
|
*/
|
|
export const useAppState = () => {
|
|
// Current navigation context
|
|
const activeAgentId = useState<string | null>('appState:activeAgentId', () => null);
|
|
const activeTopicId = useState<string | null>('appState:activeTopicId', () => null);
|
|
|
|
// User data
|
|
const user = useState<User | null>('appState:user', () => null);
|
|
const session = useState<Session | null>('appState:session', () => null);
|
|
|
|
// Loading states
|
|
const isInitializing = useState<boolean>('appState:isInitializing', () => true);
|
|
const generationInProgress = useState<{ generationId: string } | null>('appState:generationInProgress', () => null);
|
|
|
|
/**
|
|
* Set the active agent and clear the topic
|
|
*/
|
|
const setActiveAgent = (agentId: string | null | undefined) => {
|
|
activeAgentId.value = agentId || null;
|
|
// Clear topic when switching agents
|
|
activeTopicId.value = null;
|
|
};
|
|
|
|
/**
|
|
* Set the active topic
|
|
*/
|
|
const setActiveTopic = (topicId: string | null | undefined) => {
|
|
activeTopicId.value = topicId || null;
|
|
};
|
|
|
|
/**
|
|
* Set user session data
|
|
*/
|
|
const setUser = (userData: User | null) => {
|
|
user.value = userData;
|
|
};
|
|
|
|
/**
|
|
* Set session
|
|
*/
|
|
const setSession = (sessionData: Session | null) => {
|
|
session.value = sessionData;
|
|
};
|
|
|
|
/**
|
|
* Mark initialization complete
|
|
*/
|
|
const markInitialized = () => {
|
|
isInitializing.value = false;
|
|
};
|
|
|
|
/**
|
|
* Start a generation
|
|
*/
|
|
const startGeneration = (generationId: string) => {
|
|
generationInProgress.value = { generationId };
|
|
};
|
|
|
|
/**
|
|
* End current generation
|
|
*/
|
|
const endGeneration = () => {
|
|
generationInProgress.value = null;
|
|
};
|
|
|
|
return {
|
|
// State
|
|
activeAgentId,
|
|
activeTopicId,
|
|
user,
|
|
session,
|
|
isInitializing,
|
|
generationInProgress,
|
|
|
|
// Actions
|
|
setActiveAgent,
|
|
setActiveTopic,
|
|
setUser,
|
|
setSession,
|
|
markInitialized,
|
|
startGeneration,
|
|
endGeneration
|
|
};
|
|
};
|