VisResponse.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { ChatContext } from '@/app/chat-context';
  2. import { LoadingOutlined } from '@ant-design/icons';
  3. import { GPTVis } from '@antv/gpt-vis';
  4. import JsonView from '@uiw/react-json-view';
  5. import { githubDarkTheme } from '@uiw/react-json-view/githubDark';
  6. import { githubLightTheme } from '@uiw/react-json-view/githubLight';
  7. import { Alert, Spin } from 'antd';
  8. import classNames from 'classnames';
  9. import React, { useContext, useMemo } from 'react';
  10. import rehypeRaw from 'rehype-raw';
  11. import remarkGfm from 'remark-gfm';
  12. import markdownComponents from './config';
  13. interface VisResponseProps {
  14. name: string;
  15. args: any;
  16. status: string;
  17. logo: string;
  18. result: string;
  19. err_msg: any;
  20. }
  21. const VisResponse: React.FC<{ data: VisResponseProps }> = ({ data }) => {
  22. const { mode } = useContext(ChatContext);
  23. const type = useMemo(() => {
  24. switch (data.status) {
  25. case 'complete':
  26. return 'success';
  27. case 'failed':
  28. return 'error';
  29. case 'running':
  30. return 'warning';
  31. default:
  32. return undefined;
  33. }
  34. }, [data]);
  35. if (!data) return null;
  36. const theme = mode === 'dark' ? githubDarkTheme : githubLightTheme;
  37. return (
  38. <div className='flex flex-1 flex-col'>
  39. <Alert
  40. className={classNames('mb-4', {
  41. 'bg-[#fafafa] border-[transparent]': !type,
  42. })}
  43. message={data.name}
  44. type={type}
  45. {...(type && { showIcon: true })}
  46. {...(type === 'warning' && {
  47. icon: <Spin indicator={<LoadingOutlined spin />} />,
  48. })}
  49. />
  50. {data.result && (
  51. <JsonView
  52. style={{ ...theme, width: '100%', padding: 10 }}
  53. className={classNames({
  54. 'bg-[#fafafa]': mode === 'light',
  55. })}
  56. value={JSON.parse(data.result || '{}')}
  57. enableClipboard={false}
  58. displayDataTypes={false}
  59. objectSortKeys={false}
  60. />
  61. )}
  62. {data.err_msg && (
  63. <GPTVis components={markdownComponents} remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeRaw]}>
  64. {data.err_msg}
  65. </GPTVis>
  66. )}
  67. </div>
  68. );
  69. };
  70. export default VisResponse;