prompt-bot.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { sendSpacePostRequest } from '@/utils/request';
  2. import { useRequest } from 'ahooks';
  3. import { ConfigProvider, FloatButton, Form, List, Popover, Select, Tooltip, message } from 'antd';
  4. import { useState } from 'react';
  5. import { useTranslation } from 'react-i18next';
  6. type SelectTableProps = {
  7. data: any;
  8. loading: boolean;
  9. submit: (prompt: string) => void;
  10. close: () => void;
  11. };
  12. const SelectTable: React.FC<SelectTableProps> = ({ data, loading, submit, close }) => {
  13. const { t } = useTranslation();
  14. const handleClick = (content: string) => () => {
  15. submit(content);
  16. close();
  17. };
  18. return (
  19. <div
  20. style={{
  21. maxHeight: 400,
  22. overflow: 'auto',
  23. }}
  24. >
  25. <List
  26. dataSource={data?.data}
  27. loading={loading}
  28. rowKey={(record: any) => record.prompt_name}
  29. renderItem={item => (
  30. <List.Item key={item.prompt_name} onClick={handleClick(item.content)}>
  31. <Tooltip title={item.content}>
  32. <List.Item.Meta
  33. style={{ cursor: 'copy' }}
  34. title={item.prompt_name}
  35. description={
  36. t('Prompt_Info_Scene') +
  37. `:${item.chat_scene},` +
  38. t('Prompt_Info_Sub_Scene') +
  39. `:${item.sub_chat_scene}`
  40. }
  41. />
  42. </Tooltip>
  43. </List.Item>
  44. )}
  45. />
  46. </div>
  47. );
  48. };
  49. type PromptBotProps = {
  50. submit: (prompt: string) => void;
  51. };
  52. const PromptBot: React.FC<PromptBotProps> = ({ submit }) => {
  53. const { t } = useTranslation();
  54. const [open, setOpen] = useState(false);
  55. const [current, setCurrent] = useState('common');
  56. const { data, loading } = useRequest(
  57. () => {
  58. const body = {
  59. prompt_type: current,
  60. };
  61. return sendSpacePostRequest('/prompt/list', body);
  62. },
  63. {
  64. refreshDeps: [current],
  65. onError: err => {
  66. message.error(err?.message);
  67. },
  68. },
  69. );
  70. const close = () => {
  71. setOpen(false);
  72. };
  73. const handleOpenChange = (newOpen: boolean) => {
  74. setOpen(newOpen);
  75. };
  76. const handleChange = (value: string) => {
  77. setCurrent(value);
  78. };
  79. return (
  80. <ConfigProvider
  81. theme={{
  82. components: {
  83. Popover: {
  84. minWidth: 250,
  85. },
  86. },
  87. }}
  88. >
  89. <Popover
  90. title={
  91. <Form.Item label={'Prompt ' + t('Type')}>
  92. <Select
  93. style={{ width: 150 }}
  94. value={current}
  95. onChange={handleChange}
  96. options={[
  97. {
  98. label: t('Public') + ' Prompts',
  99. value: 'common',
  100. },
  101. {
  102. label: t('Private') + ' Prompts',
  103. value: 'private',
  104. },
  105. ]}
  106. />
  107. </Form.Item>
  108. }
  109. content={<SelectTable {...{ data, loading, submit, close }} />}
  110. placement='topRight'
  111. trigger='click'
  112. open={open}
  113. onOpenChange={handleOpenChange}
  114. >
  115. <Tooltip title={t('Click_Select') + ' Prompt'}>
  116. <FloatButton className='bottom-[30%]' />
  117. </Tooltip>
  118. </Popover>
  119. </ConfigProvider>
  120. );
  121. };
  122. export default PromptBot;