useSetupCheck.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import { useContext, useEffect } from 'react';
  2. import { useLocation } from 'react-router-dom';
  3. import { StatusContext } from '../context/Status';
  4. /**
  5. * 自定义Hook:检查系统setup状态并进行重定向
  6. * @param {Object} options - 配置选项
  7. * @param {boolean} options.autoRedirect - 是否自动重定向,默认true
  8. * @param {string} options.setupPath - setup页面路径,默认'/setup'
  9. * @returns {Object} 返回setup状态信息
  10. */
  11. export function useSetupCheck(options = {}) {
  12. const { autoRedirect = true, setupPath = '/setup' } = options;
  13. const [statusState] = useContext(StatusContext);
  14. const location = useLocation();
  15. const isSetupComplete = statusState?.status?.setup !== false;
  16. const needsSetup = !isSetupComplete && location.pathname !== setupPath;
  17. useEffect(() => {
  18. if (autoRedirect && needsSetup) {
  19. window.location.href = setupPath;
  20. }
  21. }, [autoRedirect, needsSetup, setupPath]);
  22. return {
  23. isSetupComplete,
  24. needsSetup,
  25. statusState,
  26. currentPath: location.pathname
  27. };
  28. }