Root.tsx 4.2 KB

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