umami/src/components/input/WebsiteSelect.tsx

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-12-27 22:20:36 +00:00
import { useState, Key } from 'react';
2023-02-15 10:27:18 +00:00
import { Dropdown, Item } from 'react-basics';
2024-02-03 01:49:17 +00:00
import { useWebsite, useWebsites, useMessages } from 'components/hooks';
2023-12-27 22:20:36 +00:00
import Empty from 'components/common/Empty';
2024-02-03 01:49:17 +00:00
import styles from './WebsiteSelect.module.css';
2023-02-15 10:27:18 +00:00
2023-12-03 11:07:03 +00:00
export function WebsiteSelect({
websiteId,
2024-02-03 01:49:17 +00:00
teamId,
2023-12-03 11:07:03 +00:00
onSelect,
}: {
2023-12-27 22:20:36 +00:00
websiteId?: string;
2024-02-03 01:49:17 +00:00
teamId?: string;
2023-12-03 11:07:03 +00:00
onSelect?: (key: any) => void;
}) {
2024-02-03 01:49:17 +00:00
const { formatMessage, labels, messages } = useMessages();
2023-12-27 22:20:36 +00:00
const [query, setQuery] = useState('');
const [selectedId, setSelectedId] = useState<Key>(websiteId);
2024-02-03 01:49:17 +00:00
const { data: website } = useWebsite(selectedId as string);
2024-02-19 19:31:11 +00:00
const queryResult = useWebsites({ teamId }, { query, pageSize: 5 });
2023-12-27 22:20:36 +00:00
const renderValue = () => {
return website?.name;
};
const renderEmpty = () => {
return <Empty message={formatMessage(messages.noResultsFound)} />;
};
const handleSelect = (value: any) => {
setSelectedId(value);
onSelect?.(value);
};
2023-02-15 10:27:18 +00:00
2023-12-27 22:20:36 +00:00
const handleSearch = (value: string) => {
setQuery(value);
2023-02-15 10:27:18 +00:00
};
return (
<Dropdown
2023-10-15 21:07:53 +00:00
menuProps={{ className: styles.dropdown }}
2024-02-03 01:49:17 +00:00
items={queryResult?.result?.data as any[]}
2023-12-27 22:20:36 +00:00
value={selectedId as string}
2023-02-15 10:27:18 +00:00
renderValue={renderValue}
2023-12-27 22:20:36 +00:00
renderEmpty={renderEmpty}
2024-01-19 00:46:40 +00:00
onChange={handleSelect}
2023-02-15 10:27:18 +00:00
alignment="end"
placeholder={formatMessage(labels.selectWebsite)}
2023-12-27 22:20:36 +00:00
allowSearch={true}
onSearch={handleSearch}
2024-02-03 01:49:17 +00:00
isLoading={queryResult.query.isLoading}
2023-02-15 10:27:18 +00:00
>
2023-02-24 10:41:02 +00:00
{({ id, name }) => <Item key={id}>{name}</Item>}
2023-02-15 10:27:18 +00:00
</Dropdown>
);
}
2023-04-21 15:00:42 +00:00
export default WebsiteSelect;