Connect.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { Theme, makeStyles, Typography } from '@material-ui/core';
  2. import { useTranslation } from 'react-i18next';
  3. import { ITextfieldConfig } from '../../components/customInput/Types';
  4. import icons from '../../components/icons/Icons';
  5. import ConnectContainer from './ConnectContainer';
  6. import CustomInput from '../../components/customInput/CustomInput';
  7. import { useContext, useMemo, useState } from 'react';
  8. import { formatForm } from '../../utils/Form';
  9. import { useFormValidation } from '../../hooks/Form';
  10. import CustomButton from '../../components/customButton/CustomButton';
  11. import { useHistory } from 'react-router-dom';
  12. import { authContext } from '../../context/Auth';
  13. import { MilvusHttp } from '../../http/Milvus';
  14. import { rootContext } from '../../context/Root';
  15. import { MILVUS_ADDRESS } from '../../consts/Localstorage';
  16. import { formatAddress } from '../../utils/Format';
  17. // import { io } from "socket.io-client";
  18. const useStyles = makeStyles((theme: Theme) => ({
  19. wrapper: {
  20. display: 'flex',
  21. flexDirection: 'column',
  22. alignItems: 'flex-end',
  23. padding: theme.spacing(0, 3),
  24. },
  25. titleWrapper: {
  26. display: 'flex',
  27. alignItems: 'center',
  28. padding: theme.spacing(3),
  29. margin: '0 auto',
  30. '& .title': {
  31. margin: 0,
  32. color: '#323232',
  33. fontWeight: 'bold',
  34. },
  35. },
  36. logo: {
  37. width: '42px',
  38. height: 'auto',
  39. marginRight: theme.spacing(2),
  40. },
  41. input: {
  42. margin: theme.spacing(3, 0, 0.5),
  43. },
  44. }));
  45. const MILVUS_URL =
  46. ((window as any)._env_ && (window as any)._env_.MILVUS_URL) || '';
  47. const Connect = () => {
  48. const history = useHistory();
  49. const { setAddress } = useContext(authContext);
  50. const { openSnackBar } = useContext(rootContext);
  51. const classes = useStyles();
  52. const { t: commonTrans } = useTranslation();
  53. const { t: warningTrans } = useTranslation('warning');
  54. const milvusTrans: { [key in string]: string } = commonTrans('milvus');
  55. const { t: btnTrans } = useTranslation('btn');
  56. const { t: successTrans } = useTranslation('success');
  57. const [form, setForm] = useState({
  58. address: MILVUS_URL,
  59. });
  60. const checkedForm = useMemo(() => {
  61. const { address } = form;
  62. return formatForm({ address });
  63. }, [form]);
  64. const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
  65. const Logo = icons.milvus;
  66. const handleInputChange = (value: string) => {
  67. setForm({ address: value });
  68. };
  69. const handleConnect = async () => {
  70. const address = formatAddress(form.address);
  71. await MilvusHttp.connect(address);
  72. openSnackBar(successTrans('connect'));
  73. setAddress(address);
  74. window.localStorage.setItem(MILVUS_ADDRESS, address);
  75. history.push('/');
  76. };
  77. const addressInputConfig: ITextfieldConfig = {
  78. label: milvusTrans.address,
  79. key: 'address',
  80. onChange: handleInputChange,
  81. variant: 'filled',
  82. className: classes.input,
  83. placeholder: milvusTrans.address,
  84. fullWidth: true,
  85. validations: [
  86. {
  87. rule: 'require',
  88. errorText: warningTrans('required', { name: milvusTrans.address }),
  89. },
  90. ],
  91. defaultValue: form.address,
  92. };
  93. // test code for socket
  94. // useEffect(() => {
  95. // const socket = io('http://localhost:3002');
  96. // socket.on('connect', function () {
  97. // console.log('Connected');
  98. // socket.emit('identity', 0, (res: any) =>
  99. // console.log(res));
  100. // socket.emit('events', { test: 'events' });
  101. // socket.emit('senddata', { test: 'senddata' });
  102. // });
  103. // socket.on('events', (data: any) => {
  104. // console.log('event', data);
  105. // });
  106. // socket.on('senddata', (data: any) => {
  107. // console.log('senddata', data);
  108. // });
  109. // socket.on('exception', (data: any) => {
  110. // console.log('event', data);
  111. // });
  112. // socket.on('disconnect', () => {
  113. // console.log('Disconnected');
  114. // });
  115. // }, []);
  116. return (
  117. <ConnectContainer>
  118. <section className={classes.wrapper}>
  119. <div className={classes.titleWrapper}>
  120. <Logo classes={{ root: classes.logo }} />
  121. <Typography variant="h2" className="title">
  122. {milvusTrans.admin}
  123. </Typography>
  124. </div>
  125. <CustomInput
  126. type="text"
  127. textConfig={addressInputConfig}
  128. checkValid={checkIsValid}
  129. validInfo={validation}
  130. />
  131. <CustomButton
  132. variant="contained"
  133. disabled={form.address ? false : disabled}
  134. onClick={handleConnect}
  135. >
  136. {btnTrans('connect')}
  137. </CustomButton>
  138. </section>
  139. </ConnectContainer>
  140. );
  141. };
  142. export default Connect;