2023-04-05 19:21:15 +00:00
|
|
|
import Image from "next/image";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// services
|
2023-04-17 11:11:16 +00:00
|
|
|
import workspaceService from "services/workspace.service";
|
2023-04-05 19:21:15 +00:00
|
|
|
// ui
|
|
|
|
import { Avatar, CustomSearchSelect, CustomSelect, Input } from "components/ui";
|
|
|
|
// types
|
|
|
|
import { IGithubRepoCollaborator } from "types";
|
|
|
|
import { IUserDetails } from "./root";
|
|
|
|
// fetch-keys
|
2023-04-17 11:11:16 +00:00
|
|
|
import { WORKSPACE_MEMBERS } from "constants/fetch-keys";
|
2023-04-05 19:21:15 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
collaborator: IGithubRepoCollaborator;
|
|
|
|
index: number;
|
|
|
|
users: IUserDetails[];
|
|
|
|
setUsers: React.Dispatch<React.SetStateAction<IUserDetails[]>>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const importOptions = [
|
|
|
|
{
|
|
|
|
key: "map",
|
|
|
|
label: "Map to existing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "invite",
|
|
|
|
label: "Invite by email",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: false,
|
|
|
|
label: "Do not import",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2023-04-17 11:11:16 +00:00
|
|
|
export const SingleUserSelect: React.FC<Props> = ({ collaborator, index, users, setUsers }) => {
|
2023-04-05 19:21:15 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
|
|
|
|
const { data: members } = useSWR(
|
2023-04-17 11:11:16 +00:00
|
|
|
workspaceSlug ? WORKSPACE_MEMBERS(workspaceSlug.toString()) : null,
|
|
|
|
workspaceSlug ? () => workspaceService.workspaceMembers(workspaceSlug.toString()) : null
|
2023-04-05 19:21:15 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const options =
|
|
|
|
members?.map((member) => ({
|
|
|
|
value: member.member.email,
|
|
|
|
query:
|
|
|
|
(member.member.first_name && member.member.first_name !== ""
|
|
|
|
? member.member.first_name
|
|
|
|
: member.member.email) +
|
|
|
|
" " +
|
|
|
|
member.member.last_name ?? "",
|
|
|
|
content: (
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<Avatar user={member.member} />
|
|
|
|
{member.member.first_name && member.member.first_name !== ""
|
|
|
|
? member.member.first_name + "(" + member.member.email + ")"
|
|
|
|
: member.member.email}
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
})) ?? [];
|
|
|
|
|
|
|
|
return (
|
2023-04-24 07:50:41 +00:00
|
|
|
<div className="grid grid-cols-3 items-center gap-2 rounded-md bg-brand-surface-2 px-2 py-3">
|
2023-04-05 19:21:15 +00:00
|
|
|
<div className="flex items-center gap-2">
|
2023-04-24 07:50:41 +00:00
|
|
|
<div className="relative h-8 w-8 flex-shrink-0 rounded">
|
2023-04-05 19:21:15 +00:00
|
|
|
<Image
|
|
|
|
src={collaborator.avatar_url}
|
|
|
|
layout="fill"
|
|
|
|
objectFit="cover"
|
|
|
|
className="rounded"
|
|
|
|
alt={`${collaborator.login} GitHub user`}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<p className="text-sm">{collaborator.login}</p>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<CustomSelect
|
|
|
|
value={users[index].import}
|
|
|
|
label={
|
|
|
|
<div className="text-xs">
|
|
|
|
{importOptions.find((o) => o.key === users[index].import)?.label}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
onChange={(val: any) => {
|
|
|
|
const newUsers = [...users];
|
|
|
|
newUsers[index].import = val;
|
|
|
|
newUsers[index].email = "";
|
|
|
|
setUsers(newUsers);
|
|
|
|
}}
|
|
|
|
optionsClassName="w-full"
|
|
|
|
noChevron
|
|
|
|
>
|
|
|
|
{importOptions.map((option) => (
|
|
|
|
<CustomSelect.Option key={option.label} value={option.key}>
|
|
|
|
<div>{option.label}</div>
|
|
|
|
</CustomSelect.Option>
|
|
|
|
))}
|
|
|
|
</CustomSelect>
|
|
|
|
</div>
|
|
|
|
{users[index].import === "invite" && (
|
|
|
|
<Input
|
|
|
|
type="email"
|
|
|
|
name={`userEmail${index}`}
|
|
|
|
value={users[index].email}
|
|
|
|
onChange={(e) => {
|
|
|
|
const newUsers = [...users];
|
|
|
|
newUsers[index].email = e.target.value;
|
|
|
|
setUsers(newUsers);
|
|
|
|
}}
|
|
|
|
placeholder="Enter email of the user"
|
2023-04-24 07:50:41 +00:00
|
|
|
className="py-1 text-xs"
|
2023-04-05 19:21:15 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{users[index].import === "map" && members && (
|
|
|
|
<CustomSearchSelect
|
|
|
|
value={users[index].email}
|
|
|
|
label={users[index].email !== "" ? users[index].email : "Select user from project"}
|
|
|
|
options={options}
|
|
|
|
onChange={(val: string) => {
|
|
|
|
const newUsers = [...users];
|
|
|
|
newUsers[index].email = val;
|
|
|
|
setUsers(newUsers);
|
|
|
|
}}
|
|
|
|
optionsClassName="w-full"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|