index.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @author tuonian
  3. * @date 2023/6/26
  4. */
  5. import {useLocation, useParams, useNavigate, Outlet} from "react-router";
  6. import {useEffect, useMemo, useState} from "react";
  7. import {useAppDispatch, useAppSelector} from "../../store";
  8. import './index.less'
  9. import type { MenuProps } from 'antd';
  10. import { Menu } from 'antd';
  11. import {createNginxMenus, serverRoute} from "../../routes/routes";
  12. import {NginxRouteParams} from "./types.ts";
  13. import {NginxActions} from "../../store/slice/nginx.ts";
  14. import {NavLink} from "react-router-dom";
  15. import {BackButton} from "../../components/BackButton.tsx";
  16. import {StopStartButton} from "./components/StopStartButton.tsx";
  17. import './components'
  18. import {useNginx} from "./utils/nginx.ts";
  19. /**
  20. * nginx配置首页
  21. * @param children
  22. * @constructor
  23. */
  24. export const Nginx = ({children}: any)=>{
  25. const location = useLocation();
  26. const params = useParams<NginxRouteParams>()
  27. const navigate = useNavigate()
  28. const [current] = useNginx(params.id);
  29. const server = useAppSelector(state => state.nginx.server);
  30. const servers = useAppSelector(state => state.nginx.servers)
  31. const [activeKey,setActiveKey] = useState<string>('settings')
  32. const [openKeys,setOpenKeys] = useState<string[]>([])
  33. const dispatch = useAppDispatch();
  34. console.log('children',children,params)
  35. useEffect(()=>{
  36. setActiveKey(location.pathname)
  37. console.log('location changed ', location)
  38. },[location.pathname])
  39. useEffect(()=>{
  40. if (!current || !params.sid){
  41. return
  42. }
  43. const routeKey = serverRoute(current.id, params.sid);
  44. if (openKeys.indexOf(routeKey) == -1){
  45. setOpenKeys(openKeys.concat([routeKey]))
  46. }
  47. if (params.sid && params.sid !== String(server?.id)){
  48. const now = servers.find(item=>String(item.id) === params.sid);
  49. if (!now){
  50. console.log('nginx server invalidate sid', server, params)
  51. dispatch(NginxActions.setServer())
  52. }else {
  53. dispatch(NginxActions.setServer(now))
  54. }
  55. }else if (!params.sid){
  56. dispatch(NginxActions.setServer())
  57. }
  58. console.log('params change', params, location, routeKey)
  59. },[params, servers, current])
  60. const onClick: MenuProps['onClick'] = (e) => {
  61. setActiveKey(e.key);
  62. navigate(e.key, {
  63. replace: true,
  64. })
  65. console.log('click ', e);
  66. };
  67. /**
  68. * 仅允许一个打开
  69. * @param e
  70. */
  71. const onOpenChange: MenuProps['onOpenChange'] = (e)=>{
  72. console.log('onOpenChange', e)
  73. setOpenKeys(e)
  74. }
  75. const menuItems: MenuProps['items'] = useMemo(()=>{
  76. if (!current){
  77. return []
  78. }
  79. return createNginxMenus(current, servers);
  80. },[current, servers])
  81. return (<div className="nginx-container">
  82. <div className="nginx-header">
  83. <BackButton to="/"/>
  84. <div>Nginx实例配置:{current?.name}</div>
  85. <div>({current?.ipAddr || '--'})</div>
  86. <div style={{flex: 1}} />
  87. <StopStartButton />
  88. <a target="_blank" style={{fontSize: 14,marginLeft: 10}} href="https://nginx.org/en/docs/">参考文档</a>
  89. </div>
  90. <div className="nginx-conf">
  91. <Menu
  92. onClick={onClick}
  93. style={{ width: 300 }}
  94. mode="inline"
  95. items={menuItems}
  96. activeKey={activeKey}
  97. selectedKeys={[activeKey]}
  98. onOpenChange={onOpenChange}
  99. openKeys={openKeys}
  100. />
  101. <div className="nginx-routes">
  102. {
  103. !current ? (<div className="error">该实例不存在或者已被删除,<NavLink to="/">返回首页</NavLink></div> ): null
  104. }
  105. <Outlet />
  106. </div>
  107. </div>
  108. </div>)
  109. }