import React from "react"; import { useTranslation } from "react-i18next"; import { Autocomplete, AutocompleteItem, AutocompleteSection, } from "@nextui-org/react"; import { useDispatch } from "react-redux"; import posthog from "posthog-js"; import { I18nKey } from "#/i18n/declaration"; import { setSelectedRepository } from "#/state/initial-query-slice"; import { useConfig } from "#/hooks/query/use-config"; import { sanitizeQuery } from "#/utils/sanitize-query"; interface GitHubRepositorySelectorProps { onInputChange: (value: string) => void; onSelect: () => void; userRepositories: GitHubRepository[]; publicRepositories: GitHubRepository[]; } export function GitHubRepositorySelector({ onInputChange, onSelect, userRepositories, publicRepositories, }: GitHubRepositorySelectorProps) { const { t } = useTranslation(); const { data: config } = useConfig(); const [selectedKey, setSelectedKey] = React.useState(null); const allRepositories: GitHubRepository[] = [ ...publicRepositories.filter( (repo) => !publicRepositories.find((r) => r.id === repo.id), ), ...userRepositories, ]; const dispatch = useDispatch(); const handleRepoSelection = (id: string | null) => { const repo = allRepositories.find((r) => r.id.toString() === id); if (repo) { dispatch(setSelectedRepository(repo.full_name)); posthog.capture("repository_selected"); onSelect(); setSelectedKey(id); } }; const handleClearSelection = () => { dispatch(setSelectedRepository(null)); }; const emptyContent = t(I18nKey.GITHUB$NO_RESULTS); return ( handleRepoSelection(id?.toString() ?? null)} onInputChange={onInputChange} clearButtonProps={{ onClick: handleClearSelection }} listboxProps={{ emptyContent, }} defaultFilter={(textValue, inputValue) => !inputValue || sanitizeQuery(textValue).includes(sanitizeQuery(inputValue)) } > {config?.APP_MODE === "saas" && config?.APP_SLUG && (( e.stopPropagation()} > {t(I18nKey.GITHUB$ADD_MORE_REPOS)} // eslint-disable-next-line @typescript-eslint/no-explicit-any ) as any)} {userRepositories.length > 0 && ( {userRepositories.map((repo) => ( {repo.full_name} ))} )} {publicRepositories.length > 0 && ( {publicRepositories.map((repo) => ( {repo.full_name} ({repo.stargazers_count || 0}⭐) ))} )} ); }