| 1 | import React from 'react'; |
| 2 | import { SelectProps } from 'antd'; |
| 3 | import { Select, Spin } from 'antd'; |
| 4 | import { icpGetTopic } from '@/icp/publish'; |
| 5 | import useDebounceFetcher from '@/views/publish/children/videoPage/components/VideoPubSetModal/components/useDebounceFetcher'; |
| 6 | import { AccountInfo } from '../../../account/comment'; |
| 7 | import styles from './commonComponents.module.scss'; |
| 8 | import { accountFailureDispose } from '../../comment'; |
| 9 | |
| 10 | export interface CommonTopicSelectProps<ValueType = any> |
| 11 | extends Omit<SelectProps<ValueType | ValueType[]>, 'options' | 'children'> { |
| 12 | tips?: string; |
| 13 | account?: AccountInfo; |
| 14 | onAccountChange?: (account: AccountInfo) => void; |
| 15 | children?: React.ReactNode; |
| 16 | } |
| 17 | |
| 18 | export type CommonTopicSelectValueType = { |
| 19 | label: string; |
| 20 | value: string | number; |
| 21 | }; |
| 22 | |
| 23 | // 通用话题选择器 |
| 24 | export default function CommonTopicSelect({ |
| 25 | tips, |
| 26 | children, |
| 27 | account, |
| 28 | onAccountChange, |
| 29 | ...props |
| 30 | }: CommonTopicSelectProps<CommonTopicSelectValueType>) { |
| 31 | if (!account || !onAccountChange) return ''; |
| 32 | |
| 33 | const { fetching, options, debounceFetcher } = |
| 34 | useDebounceFetcher<CommonTopicSelectValueType>( |
| 35 | async (keyword: string): Promise<CommonTopicSelectValueType[]> => { |
| 36 | const topics = await icpGetTopic(account!, keyword); |
| 37 | |
| 38 | const res = await accountFailureDispose( |
| 39 | topics, |
| 40 | account, |
| 41 | onAccountChange, |
| 42 | ); |
| 43 | return res!.map((v) => { |
| 44 | return { |
| 45 | label: v.name, |
| 46 | value: v.name, |
| 47 | }; |
| 48 | }) as CommonTopicSelectValueType[]; |
| 49 | }, |
| 50 | ); |
| 51 | |
| 52 | return ( |
| 53 | <> |
| 54 | <h1>话题</h1> |
| 55 | <Select |
| 56 | allowClear |
| 57 | mode="multiple" |
| 58 | style={{ width: '100%' }} |
| 59 | placeholder="输入关键字搜索" |
| 60 | labelInValue |
| 61 | filterOption={false} |
| 62 | onSearch={debounceFetcher} |
| 63 | notFoundContent={fetching ? <Spin size="small" /> : null} |
| 64 | {...props} |
| 65 | options={options} |
| 66 | /> |
| 67 | {children} |
| 68 | <p className={styles.tips}>{tips}</p> |
| 69 | </> |
| 70 | ); |
| 71 | } |
| 72 |