Compare commits

...
Author SHA1 Message Date
LudyandGitHub d7be62596c Merge branch 'main' into fix_saas_policy_ui_20260627 2026-07-09 23:13:35 +02:00
LudyandGitHub 4d2f69408b Merge branch 'main' into fix_saas_policy_ui_20260627 2026-07-09 11:05:49 +02:00
LudyandGitHub f723a66021 Merge branch 'main' into fix_saas_policy_ui_20260627 2026-07-04 14:28:16 +02:00
Ludy87 be5faf380d Make policies feature conditional with enabled flag
Add an `enabled` parameter to policies hooks to allow parent components to conditionally disable policy loading and reconciliation. When disabled, usePolicies skips backend API calls via early return in useEffect. Restructure test mocks for clarity and add test coverage for disabled state. This enables granular control over when the policies feature is active without breaking the component hierarchy.
2026-06-27 01:12:21 +02:00
5 changed files with 26 additions and 11 deletions
@@ -7,15 +7,15 @@
* tool list unchanged.
*/
import type { ReactNode } from "react";
import { useMemo, type ReactNode } from "react";
/** Whether the right rail should host the Policies section. False in core. */
export function usePoliciesEnabled(): boolean {
return false;
return useMemo(() => false, []);
}
/** Whether the Policies list should appear for the current user. False in core. */
export function usePoliciesVisible(): boolean {
export function usePoliciesVisible(_enabled = false): boolean {
return false;
}
@@ -66,7 +66,7 @@ export default function RightSidebar() {
} = useToolWorkflow();
const policiesEnabled = usePoliciesEnabled();
const policiesVisible = usePoliciesVisible();
const policiesVisible = usePoliciesVisible(policiesEnabled);
const rawPolicyDetailActive = usePolicyDetailActive();
const fullscreenExpanded = useIsFullscreenExpanded();
const fullscreenGeometry = useToolPanelGeometry({
@@ -90,10 +90,10 @@ export function usePoliciesEnabled(): boolean {
/**
* Whether the right rail should show the Policies section.
*/
export function usePoliciesVisible(): boolean {
const pol = usePolicies();
export function usePoliciesVisible(enabled = POLICIES_ENABLED): boolean {
const pol = usePolicies(enabled);
const { categories } = usePolicyCatalog();
if (!POLICIES_ENABLED) return false;
if (!enabled) return false;
return pol.canConfigure || categories.some((c) => !c.comingSoon);
}
@@ -18,8 +18,6 @@ if (typeof globalThis.crypto?.randomUUID !== "function") {
const api = vi.hoisted(() => ({
store: new Map<string, { id: string }>(),
seq: 0,
}));
vi.mock("@app/services/policyApi", () => ({
listPolicies: vi.fn(async () => [...api.store.values()]),
savePolicy: vi.fn(async (p: { id?: string }) => {
const id = p.id && p.id.length > 0 ? p.id : `be-${++api.seq}`;
@@ -35,6 +33,15 @@ vi.mock("@app/services/policyApi", () => ({
runPolicyPipeline: vi.fn(),
getPolicyRun: vi.fn(),
}));
vi.mock("@app/services/policyApi", () => ({
listPolicies: api.listPolicies,
savePolicy: api.savePolicy,
getPolicy: api.getPolicy,
deletePolicy: api.deletePolicy,
runStoredPolicy: api.runStoredPolicy,
runPolicyPipeline: api.runPolicyPipeline,
getPolicyRun: api.getPolicyRun,
}));
import { usePolicies } from "@app/hooks/usePolicies";
@@ -65,6 +72,7 @@ const wizardResult = {
describe("usePolicies", () => {
beforeEach(() => {
vi.clearAllMocks();
localStorage.clear();
api.store.clear();
api.seq = 0;
@@ -78,6 +86,12 @@ describe("usePolicies", () => {
expect(result.current.policies.security.configured).toBe(false);
});
it("skips the backend reconcile when disabled", async () => {
renderHook(() => usePolicies(false));
await act(async () => {});
expect(api.listPolicies).not.toHaveBeenCalled();
});
it("enabling a policy persists it to the backend + marks it configured", async () => {
const { result } = renderHook(() => usePolicies());
await act(async () => {
@@ -73,7 +73,7 @@ function toStoreRequest(
};
}
export function usePolicies() {
export function usePolicies(enabled = true) {
const [policies, setPolicies] = useState<PoliciesByCategory>(loadPolicies);
const { config, refetch: refetchAppConfig } = useAppConfig();
const { isTeamLeader } = useSaaSTeam();
@@ -88,6 +88,7 @@ export function usePolicies() {
// locally-cached folderId; retry with backoff since the backend may not be up yet.
// On recovery, also re-resolve app config in case its admin/team-leader flags settled false while down.
useEffect(() => {
if (!enabled) return;
let cancelled = false;
let attempt = 0;
let timer: ReturnType<typeof setTimeout> | undefined;
@@ -127,7 +128,7 @@ export function usePolicies() {
cancelled = true;
if (timer) clearTimeout(timer);
};
}, []);
}, [enabled]);
/**
* Enable a new policy from the wizard result: persist it to the backend (the