File size: 1,590 Bytes
339aa1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { useState, Key } from 'react';
import { Dropdown, Item } from 'react-basics';
import { useWebsite, useWebsites, useMessages } from 'components/hooks';
import Empty from 'components/common/Empty';
import styles from './WebsiteSelect.module.css';

export function WebsiteSelect({
  websiteId,
  teamId,
  onSelect,
}: {
  websiteId?: string;
  teamId?: string;
  onSelect?: (key: any) => void;
}) {
  const { formatMessage, labels, messages } = useMessages();
  const [query, setQuery] = useState('');
  const [selectedId, setSelectedId] = useState<Key>(websiteId);

  const { data: website } = useWebsite(selectedId as string);

  const queryResult = useWebsites({ teamId }, { query, pageSize: 5 });

  const renderValue = () => {
    return website?.name;
  };

  const renderEmpty = () => {
    return <Empty message={formatMessage(messages.noResultsFound)} />;
  };

  const handleSelect = (value: any) => {
    setSelectedId(value);
    onSelect?.(value);
  };

  const handleSearch = (value: string) => {
    setQuery(value);
  };

  return (
    <Dropdown
      menuProps={{ className: styles.dropdown }}
      items={queryResult?.result?.data as any[]}
      value={selectedId as string}
      renderValue={renderValue}
      renderEmpty={renderEmpty}
      onChange={handleSelect}
      alignment="end"
      placeholder={formatMessage(labels.selectWebsite)}
      allowSearch={true}
      onSearch={handleSearch}
      isLoading={queryResult.query.isLoading}
    >
      {({ id, name }) => <Item key={id}>{name}</Item>}
    </Dropdown>
  );
}

export default WebsiteSelect;