AccessTestProvider.tsx 867 Bytes
import { useLayoutEffect, useRef, useState, type ReactNode } from 'react';
import { AccessProvider, useAccess } from '@/access';

const defaultRoles = ['VIEWER'];

interface Props {
  permCodes: string[];
  roleCodes?: string[];
  children: ReactNode;
}

function ApplyTestLogin({ permCodes, roleCodes = defaultRoles, children }: Props) {
  const { applyLogin } = useAccess();
  const login = useRef(applyLogin);
  login.current = applyLogin;
  const [ready, setReady] = useState(false);
  useLayoutEffect(() => {
    login.current({ permCodes, roleCodes });
    setReady(true);
  }, [permCodes, roleCodes]);
  return ready ? children : null;
}

/** Exercise the same permission entry point used after a real login. */
export function AccessTestProvider(props: Props) {
  return (
    <AccessProvider>
      <ApplyTestLogin {...props} />
    </AccessProvider>
  );
}