You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 3.8 kB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import FullScreenFrame from '@/components/FullScreenFrame';
  2. import { getKnowledgeGraphUrl, getLabelStudioUrl } from '@/services/developmentEnvironment';
  3. import Loading from '@/utils/loading';
  4. import { to } from '@/utils/promise';
  5. import SessionStorage from '@/utils/sessionStorage';
  6. import { FloatButton } from 'antd';
  7. import classNames from 'classnames';
  8. import { useEffect, useState } from 'react';
  9. import './index.less';
  10. export enum IframePageType {
  11. DatasetAnnotation = 'DatasetAnnotation', // 数据标注
  12. AppDevelopment = 'AppDevelopment', // 应用开发
  13. DevEnv = 'DevEnv', // 开发环境
  14. GitLink = 'GitLink', // git link
  15. Aim = 'Aim', // 实验对比
  16. Knowledge = 'Knowledge', // 知识图谱
  17. TensorBoard = 'TensorBoard', // 可视化结果
  18. }
  19. const getRequestAPI = (type: IframePageType): (() => Promise<any>) => {
  20. switch (type) {
  21. case IframePageType.DatasetAnnotation: // 数据标注
  22. return getLabelStudioUrl;
  23. case IframePageType.AppDevelopment: // 应用开发
  24. return () => Promise.resolve({ code: 200, data: 'http://172.20.32.197:30080/' });
  25. case IframePageType.DevEnv: // 开发环境
  26. return () =>
  27. Promise.resolve({
  28. code: 200,
  29. data: SessionStorage.getItem(SessionStorage.editorUrlKey) || '',
  30. });
  31. case IframePageType.GitLink: // git link
  32. return () => Promise.resolve({ code: 200, data: 'http://172.20.32.201:4000' });
  33. case IframePageType.Aim: // Aim
  34. return () =>
  35. Promise.resolve({
  36. code: 200,
  37. data: SessionStorage.getItem(SessionStorage.aimUrlKey),
  38. });
  39. case IframePageType.Knowledge:
  40. // 知识图谱
  41. return getKnowledgeGraphUrl;
  42. case IframePageType.TensorBoard:
  43. // TensorBoard
  44. return () =>
  45. Promise.resolve({
  46. code: 200,
  47. data: SessionStorage.getItem(SessionStorage.tensorBoardUrlKey),
  48. });
  49. }
  50. };
  51. type IframePageProps = {
  52. /** 子系统 */
  53. type?: IframePageType;
  54. /** url */
  55. url?: string;
  56. /** 是否可以在页签上打开 */
  57. openInTab?: boolean;
  58. /** 是否显示加载 */
  59. showLoading?: boolean;
  60. /** 自定义样式类名 */
  61. className?: string;
  62. /** 自定义样式 */
  63. style?: React.CSSProperties;
  64. };
  65. /** 系统内嵌 iframe,目前系统有数据标注、应用开发、开发环境、GitLink 四个子系统,使用时可以添加其他子系统 */
  66. function IframePage({
  67. type,
  68. url,
  69. showLoading = true,
  70. openInTab = false,
  71. className,
  72. style,
  73. }: IframePageProps) {
  74. const [iframeUrl, setIframeUrl] = useState('');
  75. // const [loading, setLoading] = useState(false);
  76. useEffect(() => {
  77. const requestIframeUrl = async (type: IframePageType) => {
  78. if (showLoading) {
  79. Loading.show();
  80. }
  81. const [res] = await to(getRequestAPI(type)());
  82. if (res && res.data) {
  83. setIframeUrl(res.data);
  84. } else {
  85. if (showLoading) {
  86. Loading.hide();
  87. }
  88. }
  89. };
  90. if (type) {
  91. requestIframeUrl(type);
  92. } else if (url) {
  93. if (showLoading) {
  94. Loading.show();
  95. }
  96. setIframeUrl(url);
  97. }
  98. }, [type, url, showLoading]);
  99. const handleLoad = () => {
  100. if (showLoading) {
  101. Loading.hide();
  102. }
  103. };
  104. const handleError = (error?: React.SyntheticEvent<HTMLIFrameElement, Event>) => {
  105. console.log('error', error);
  106. if (showLoading) {
  107. Loading.hide();
  108. }
  109. };
  110. return (
  111. <div className={classNames('kf-iframe-page', className)} style={style}>
  112. {/* {loading && createPortal(<KFSpin size="large" />, document.body)} */}
  113. {iframeUrl && <FullScreenFrame url={iframeUrl} onLoad={handleLoad} onError={handleError} />}
  114. {openInTab && <FloatButton onClick={() => window.open(iframeUrl, '_blank')} />}
  115. </div>
  116. );
  117. }
  118. export default IframePage;