import React, { useState } from "react"; import Image from "next/image"; import useSWR, { mutate } from "swr"; import useSWRInfinite from "swr/infinite"; // headless ui import { Combobox, Transition } from "@headlessui/react"; // services import projectService from "services/project.service"; // hooks import { useRouter } from "next/router"; import useToast from "hooks/use-toast"; // icons import { CheckIcon, ChevronDownIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline"; import GithubLogo from "public/logos/github-square.png"; // helpers import { truncateText } from "helpers/string.helper"; // types import { IWorkspaceIntegrations } from "types"; // fetch-keys import { PROJECT_GITHUB_REPOSITORY } from "constants/fetch-keys"; type Props = { integration: IWorkspaceIntegrations; }; export const SingleIntegration: React.FC = ({ integration }) => { const [query, setQuery] = useState(""); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { data: syncedGithubRepository } = useSWR( projectId ? PROJECT_GITHUB_REPOSITORY(projectId as string) : null, () => workspaceSlug && projectId && integration ? projectService.getProjectGithubRepository( workspaceSlug as string, projectId as string, integration.id ) : null ); const getKey = (pageIndex: number) => { if (!workspaceSlug || !integration) return; return `${ process.env.NEXT_PUBLIC_API_BASE_URL }/api/workspaces/${workspaceSlug}/workspace-integrations/${ integration.id }/github-repositories/?page=${++pageIndex}`; }; const fetchGithubRepos = async (url: string) => { const data = await projectService.getGithubRepositories(url); return data; }; const { data: paginatedData, size, setSize, isValidating, } = useSWRInfinite(getKey, fetchGithubRepos); const handleChange = (repo: any) => { if (!workspaceSlug || !projectId || !integration) return; const { html_url, owner: { login }, id, name, } = repo; projectService .syncGiuthubRepository(workspaceSlug as string, projectId as string, integration.id, { name, owner: login, repository_id: id, url: html_url, }) .then((res) => { console.log(res); mutate(PROJECT_GITHUB_REPOSITORY(projectId as string)); setToastAlert({ type: "success", title: "Success!", message: `${login}/${name} respository synced with the project successfully.`, }); }) .catch((err) => { console.log(err); setToastAlert({ type: "error", title: "Error!", message: "Respository could not be synced with the project. Please try again.", }); }); }; const userRepositories = (paginatedData ?? []).map((data) => data.repositories).flat(); const totalCount = paginatedData && paginatedData.length > 0 ? paginatedData[0].total_count : 0; const options = userRepositories.map((repo) => ({ value: repo.id, query: repo.full_name, content:

{truncateText(repo.full_name, 25)}

, })) ?? []; const filteredOptions = query === "" ? options : options?.filter((option) => option.query.toLowerCase().includes(query.toLowerCase())); return ( <> {integration && (
GithubLogo

{integration.integration_detail.title}

Select GitHub repository to enable sync.

0 ? `${syncedGithubRepository[0].repo_detail.owner}/${syncedGithubRepository[0].repo_detail.name}` : null } onChange={(val: string) => { const repo = userRepositories.find((repo) => repo.id === val); handleChange(repo); }} className="relative flex-shrink-0 text-left" > {({ open }: any) => ( <> {syncedGithubRepository && syncedGithubRepository.length > 0 ? `${syncedGithubRepository[0].repo_detail.owner}/${syncedGithubRepository[0].repo_detail.name}` : "Select Repository"}
setQuery(e.target.value)} placeholder="Type to search..." displayValue={(assigned: any) => assigned?.name} />

{options.length} of {totalCount} repositories

{paginatedData ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `${active || selected ? "bg-hover-gray" : ""} ${ selected ? "font-medium" : "" } flex cursor-pointer select-none items-center justify-between gap-2 truncate rounded px-1 py-1.5 text-gray-500` } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

) ) : (

Loading...

)} {userRepositories && options.length < totalCount && ( )}
)}
)} ); };