import { Box, Center, Flex } from "@chakra-ui/react"; import { Trans } from "@lingui/macro"; import { GetAllUsersLeanData } from "prisma/queries/getAllUsersLean"; import { components } from "react-select"; import useSWR from "swr"; import MySelect from "./MySelect"; import UserAvatar from "./UserAvatar"; interface SingleSelectorProps { value?: number; setValue: (value: number) => void; isMulti: false | undefined; maxMultiCount: undefined; } interface MultiSelectorProps { value: number[]; setValue: (value: number[]) => void; isMulti: true; maxMultiCount: number; } const UserSelector: React.FC = ({ value, setValue, isMulti, maxMultiCount, }) => { const { data } = useSWR("/api/users"); const singleOption = (props: any) => { return ( {props.label} ); }; return ( ({ label: `${user.username}#${user.discriminator}`, value: "" + user.id, data: user, }))} setValue={(newValue) => { if (isMulti) { setValue(newValue.map((value: string) => parseInt(value))); } else { setValue(newValue ? parseInt(newValue) : newValue); } }} isSearchable isMulti={isMulti} components={{ IndicatorSeparator: () => null, Option: singleOption, NoOptionsMessage: () => (
{isTooManyItems() ? ( Only {maxMultiCount} users allowed ) : ( No results with this filter )}
), }} hideMenuBeforeTyping isClearable={!isMulti} /> ); function getUsersArray() { if (!data) return []; if (isTooManyItems()) { return []; } return data; } function isTooManyItems() { return ( maxMultiCount && Array.isArray(value) && maxMultiCount <= value.length ); } }; export default UserSelector;