Dialog.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { ReactElement, useContext } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { Typography } from '@material-ui/core';
  4. import { rootContext } from '../context/Root';
  5. import { CollectionView } from '../pages/collections/Types';
  6. import { PartitionView } from '../pages/partitions/Types';
  7. import { CollectionData } from '../pages/overview/collectionCard/Types';
  8. import { LOADING_STATE } from '../consts/Milvus';
  9. // handle release and load dialog
  10. export interface LoadAndReleaseDialogHookProps {
  11. type: 'partition' | 'collection';
  12. }
  13. export const useLoadAndReleaseDialogHook = (
  14. props: LoadAndReleaseDialogHookProps
  15. ) => {
  16. const { type } = props;
  17. const { setDialog } = useContext(rootContext);
  18. const { t: dialogTrans } = useTranslation('dialog');
  19. const { t: btnTrans } = useTranslation('btn');
  20. const { t: partitionTrans } = useTranslation('partition');
  21. const { t: collectionTrans } = useTranslation('collection');
  22. const name =
  23. type === 'collection'
  24. ? collectionTrans('collection')
  25. : partitionTrans('partition');
  26. const actionsMap = {
  27. release: {
  28. title: dialogTrans('releaseTitle', { type: name }),
  29. component: (
  30. <Typography className="dialog-content">
  31. {dialogTrans('releaseContent', { type: name })}
  32. </Typography>
  33. ),
  34. confirmLabel: btnTrans('release'),
  35. },
  36. load: {
  37. title: dialogTrans('loadTitle', { type: name }),
  38. component: (
  39. <Typography className="dialog-content">
  40. {dialogTrans('loadContent', { type: name })}
  41. </Typography>
  42. ),
  43. confirmLabel: btnTrans('load'),
  44. },
  45. };
  46. const handleAction = (
  47. data: PartitionView | CollectionView | CollectionData,
  48. cb: (data: any) => Promise<any>
  49. ) => {
  50. const actionType: 'release' | 'load' =
  51. data._status === LOADING_STATE.UNLOADED ? 'load' : 'release';
  52. const { title, component, confirmLabel } = actionsMap[actionType];
  53. setDialog({
  54. open: true,
  55. type: 'notice',
  56. params: {
  57. title,
  58. component,
  59. confirmLabel,
  60. confirm: () => cb(data),
  61. },
  62. });
  63. };
  64. return {
  65. handleAction,
  66. };
  67. };
  68. export const useInsertDialogHook = () => {
  69. const { setDialog } = useContext(rootContext);
  70. const handleInsertDialog = (
  71. // stepper container, contains all contents
  72. component: ReactElement
  73. ) => {
  74. setDialog({
  75. open: true,
  76. type: 'custom',
  77. params: {
  78. component,
  79. },
  80. });
  81. };
  82. return {
  83. handleInsertDialog,
  84. };
  85. };