Spaces:
Paused
Paused
File size: 2,407 Bytes
4e7644b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import { useEffect } from "react";
import { useAgentStore } from "./agentStore";
import { useCodingModeStore } from "./codingModeStore";
import { useProjectDirectoryStore } from "./projectDirectoryStore";
import { codingModeApi } from "../api/modules/codingMode";
import { projectDirectoryApi } from "../api/modules/projectDirectory";
/**
* Pull Coding tools and Agent project directory state independently.
* selectedAgent change. Backend (agent.json) is the source of truth — the
* store is in-memory only, so without this hook the UI would show stale or
* empty state across reloads and tabs.
*
* Mount once at a top-level component (e.g. MainLayout) so every route
* sees a populated store.
*/
export function useSyncCodingMode(): void {
const { selectedAgent } = useAgentStore();
const setCodingMode = useCodingModeStore((s) => s.setCodingMode);
const setProjectDir = useProjectDirectoryStore((s) => s.setProjectDir);
useEffect(() => {
if (!selectedAgent) return;
let cancelled = false;
const startRevision =
useCodingModeStore.getState().codingModeRevisionByAgent[selectedAgent] ??
0;
void Promise.all([codingModeApi.get(), projectDirectoryApi.get()])
.then(([mode, project]) => {
if (cancelled) return;
const currentRevision =
useCodingModeStore.getState().codingModeRevisionByAgent[
selectedAgent
] ?? 0;
if (currentRevision === startRevision) {
setCodingMode(selectedAgent, mode.enabled);
}
setProjectDir(
selectedAgent,
project.is_workspace_default ? null : project.path,
);
})
.catch((err) => {
if (cancelled) return;
// Log so a misconfigured backend is visible — then mark the
// agent initialized with safe defaults. Without this the
// Agent configuration stays disabled
// forever on any GET failure.
console.warn("Failed to sync coding mode state:", err);
const currentRevision =
useCodingModeStore.getState().codingModeRevisionByAgent[
selectedAgent
] ?? 0;
if (currentRevision === startRevision) {
setCodingMode(selectedAgent, false);
}
setProjectDir(selectedAgent, null);
});
return () => {
cancelled = true;
};
}, [selectedAgent, setCodingMode, setProjectDir]);
}
|