Files
openchamber/packages/ui/src/hooks/useSessionStatusBootstrap.ts
T

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-12-07 19:32:53 +02:00
import React from 'react';
import { opencodeClient } from '@/lib/opencode/client';
import { useSessionStore } from '@/stores/useSessionStore';
type SessionStatusPayload = {
type: 'idle' | 'busy' | 'retry';
attempt?: number;
message?: string;
next?: number;
};
export const useSessionStatusBootstrap = (options?: { enabled?: boolean }) => {
const enabled = options?.enabled ?? true;
2025-12-07 19:32:53 +02:00
React.useEffect(() => {
if (!enabled) {
return;
}
2025-12-07 19:32:53 +02:00
let cancelled = false;
const bootstrap = async () => {
try {
// Use global status to detect busy sessions across all directories,
// including sessions started externally (e.g., via CLI) before UI opened
const statusMap = await opencodeClient.getGlobalSessionStatus();
2025-12-07 19:32:53 +02:00
if (cancelled || !statusMap) return;
const nextStatus = new Map<string, SessionStatusPayload>();
2025-12-07 19:32:53 +02:00
Object.entries(statusMap).forEach(([sessionId, raw]) => {
if (!sessionId || !raw) return;
const status = raw as SessionStatusPayload;
nextStatus.set(sessionId, status);
2025-12-07 19:32:53 +02:00
});
if (nextStatus.size > 0) {
useSessionStore.setState({ sessionStatus: nextStatus });
2025-12-07 19:32:53 +02:00
}
} catch { /* ignored */ }
};
void bootstrap();
return () => {
cancelled = true;
};
}, [enabled]);
2025-12-07 19:32:53 +02:00
};