Root.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { useState, useCallback, useEffect } from 'react';
  2. import React from 'react';
  3. import { ThemeProvider, makeStyles } from '@material-ui/core/styles';
  4. import { SwipeableDrawer } from '@material-ui/core';
  5. import {
  6. RootContextType,
  7. DialogType,
  8. SnackBarType,
  9. OpenSnackBarType,
  10. } from './Types';
  11. import CustomSnackBar from '../components/customSnackBar/CustomSnackBar';
  12. import CustomDialog from '../components/customDialog/CustomDialog';
  13. import { theme } from '../styles/theme';
  14. import { MilvusHttp } from 'insight_src/http/Milvus';
  15. const DefaultDialogConfigs: DialogType = {
  16. open: false,
  17. type: 'notice',
  18. params: {
  19. title: '',
  20. component: <></>,
  21. confirm: () => new Promise((res, rej) => res(true)),
  22. cancel: () => new Promise((res, rej) => res(true)),
  23. },
  24. };
  25. export const rootContext = React.createContext<RootContextType>({
  26. openSnackBar: (
  27. message,
  28. type = 'success',
  29. autoHideDuration,
  30. position = { vertical: 'top', horizontal: 'right' }
  31. ) => {},
  32. dialog: DefaultDialogConfigs,
  33. setDialog: params => {},
  34. handleCloseDialog: () => {},
  35. setDrawer: (params: any) => {},
  36. versionInfo: { attu: '1', sdk: '1' },
  37. });
  38. const { Provider } = rootContext;
  39. // Dialog has two type : normal | custom;
  40. // notice type mean it's a notice dialog you need to set props like title, content, actions
  41. // custom type could have own state, you could set a complete component in dialog.
  42. export const RootProvider = (props: { children: React.ReactNode }) => {
  43. const classes = makeStyles({
  44. paper: {
  45. minWidth: '300px',
  46. borderRadius: '0px',
  47. },
  48. paperAnchorRight: {
  49. width: '40vw',
  50. },
  51. })();
  52. const [snackBar, setSnackBar] = useState<SnackBarType>({
  53. open: false,
  54. type: 'success',
  55. message: '',
  56. vertical: 'top',
  57. horizontal: 'right',
  58. autoHideDuration: 3000,
  59. });
  60. const [dialog, setDialog] = useState<DialogType>(DefaultDialogConfigs);
  61. const [drawer, setDrawer]: any = useState({
  62. anchor: 'right',
  63. open: false,
  64. child: <></>,
  65. });
  66. const [versionInfo, setVersionInfo] = useState({ attu: '', sdk: '' });
  67. const handleSnackBarClose = () => {
  68. setSnackBar(v => ({ ...v, open: false }));
  69. };
  70. const openSnackBar: OpenSnackBarType = useCallback(
  71. (
  72. message = '',
  73. type = 'success',
  74. autoHideDuration: number | null | undefined = 5000,
  75. position = { vertical: 'top', horizontal: 'right' }
  76. ) => {
  77. setSnackBar({
  78. open: true,
  79. message,
  80. type,
  81. autoHideDuration,
  82. ...position,
  83. });
  84. },
  85. []
  86. );
  87. const handleCloseDialog = () => {
  88. setDialog({
  89. ...dialog,
  90. open: false,
  91. });
  92. };
  93. const toggleDrawer =
  94. (open: boolean) => (event: React.KeyboardEvent | React.MouseEvent) => {
  95. if (
  96. event.type === 'keydown' &&
  97. ((event as React.KeyboardEvent).key === 'Tab' ||
  98. (event as React.KeyboardEvent).key === 'Shift')
  99. ) {
  100. return;
  101. }
  102. setDrawer({ ...drawer, open: open });
  103. };
  104. useEffect(() => {
  105. const fetchVersion = async () => {
  106. const res = await MilvusHttp.getVersion();
  107. console.log(res);
  108. setVersionInfo(res);
  109. };
  110. fetchVersion();
  111. }, []);
  112. return (
  113. <Provider
  114. value={{
  115. openSnackBar,
  116. dialog,
  117. setDialog,
  118. handleCloseDialog,
  119. setDrawer,
  120. versionInfo,
  121. }}
  122. >
  123. <ThemeProvider theme={theme}>
  124. <CustomSnackBar {...snackBar} onClose={handleSnackBarClose} />
  125. {props.children}
  126. <CustomDialog {...dialog} onClose={handleCloseDialog} />
  127. <SwipeableDrawer
  128. anchor={drawer.anchor}
  129. open={drawer.open}
  130. onClose={toggleDrawer(false)}
  131. onOpen={toggleDrawer(true)}
  132. classes={{ paperAnchorRight: classes.paperAnchorRight }}
  133. >
  134. {drawer.child}
  135. </SwipeableDrawer>
  136. </ThemeProvider>
  137. </Provider>
  138. );
  139. };