返回 AiToEarn
CommonLocationSelect.tsx
根目录 / project / aitoearn-electron / src / views / publish / components / CommonComponents / CommonLocationSelect.tsx
1 import React, { useEffect, useRef } from 'react';
2 import { Select, SelectProps, Spin } from 'antd';
3 import { icpGetLocationData } from '@/icp/publish';
4 import { ILocationDataItem } from '@/../electron/main/plat/plat.type';
5 import useDebounceFetcher from '@/views/publish/children/videoPage/components/VideoPubSetModal/components/useDebounceFetcher';
6 import styles from './commonComponents.module.scss';
7 import { icpGetLocation } from '@/icp/view';
8 import { AccountInfo } from '../../../account/comment';
9 import { accountFailureDispose } from '../../comment';
10
11 interface CommonLocationSelectProps<ValueType = any>
12 extends Omit<SelectProps<ValueType | ValueType[]>, 'options' | 'children'> {
13 account?: AccountInfo;
14 onAccountChange?: (account: AccountInfo) => void;
15 children?: React.ReactNode;
16 }
17
18 // 通用位置选择器
19 export default function CommonLocationSelect({
20 children,
21 account,
22 onAccountChange,
23 ...props
24 }: CommonLocationSelectProps<ILocationDataItem>) {
25 if (!account || !onAccountChange) return '';
26
27 const { fetching, options, debounceFetcher } =
28 useDebounceFetcher<ILocationDataItem>(async (keywords) => {
29 if (location.current.loca.length === 0) await getLocation();
30 const locationData = await icpGetLocationData({
31 account: account!,
32 keywords,
33 latitude: location.current.loca[1],
34 longitude: location.current.loca[0],
35 cityName: location.current.city,
36 });
37 return await accountFailureDispose(
38 locationData,
39 account,
40 onAccountChange,
41 );
42 });
43 // 位置 0=经度 1=纬度
44 const location = useRef<{
45 loca: number[];
46 city: string;
47 }>({
48 loca: [],
49 city: '',
50 });
51
52 useEffect(() => {
53 getLocation();
54 }, []);
55
56 const getLocation = async () => {
57 const res = await icpGetLocation();
58 location.current.loca = res.gcj02;
59 location.current.city = res.city;
60 };
61
62 return (
63 <>
64 <h1>位置</h1>
65 <Select
66 showSearch
67 allowClear
68 style={{ width: '100%' }}
69 placeholder="请选择关联位置"
70 labelInValue
71 filterOption={false}
72 onSearch={debounceFetcher}
73 notFoundContent={fetching ? <Spin size="small" /> : null}
74 {...props}
75 options={options?.map((v) => {
76 return {
77 value: v.id,
78 label: v.name,
79 ...v,
80 };
81 })}
82 optionRender={({ data }) => {
83 return (
84 <div className={styles.locationSelect}>
85 <p className="location-name">{data.name}</p>
86 <p className="location-simpleAddress">{data.simpleAddress}</p>
87 </div>
88 );
89 }}
90 />
91 {children}
92 </>
93 );
94 }
95
95 lines Plain Text