Skip to content

Commit

Permalink
Simplify example repositories (#20156)
Browse files Browse the repository at this point in the history
* Simplify example repositories

* Actually fix the bug

* Fix resolved repo name

* Fix display names for examples
  • Loading branch information
filiptronicek authored Aug 30, 2024
1 parent acae8de commit c21595c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 42 deletions.
51 changes: 25 additions & 26 deletions components/dashboard/src/components/RepositoryFinder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ import { SuggestedRepository } from "@gitpod/public-api/lib/gitpod/v1/scm_pb";
import { PREDEFINED_REPOS } from "../data/git-providers/predefined-repos";
import { useConfiguration, useListConfigurations } from "../data/configurations/configuration-queries";

const isPredefined = (repo: SuggestedRepository): boolean => {
return PREDEFINED_REPOS.some((predefined) => predefined.url === repo.url) && !repo.configurationId;
};

const resolveIcon = (contextUrl?: string): string => {
if (!contextUrl) return RepositorySVG;
return PREDEFINED_REPOS.some((repo) => repo.url === contextUrl) ? GitpodRepositoryTemplateSVG : RepositorySVG;
};

interface RepositoryFinderProps {
selectedContextURL?: string;
selectedConfigurationId?: string;
Expand Down Expand Up @@ -54,7 +63,6 @@ export default function RepositoryFinder({
searchString,
excludeConfigurations,
onlyConfigurations,
showExamples,
});

// We search for the current context URL in order to have data for the selected suggestion
Expand Down Expand Up @@ -112,12 +120,6 @@ export default function RepositoryFinder({

const authProviders = useAuthProviderDescriptions();

// This approach creates a memoized Map of the predefined repos,
// which can be more efficient for lookups if we would have a large number of predefined repos
const memoizedPredefinedRepos = useMemo(() => {
return new Map(PREDEFINED_REPOS.map((repo) => [repo.url, repo]));
}, []);

const handleSelectionChange = useCallback(
(selectedID: string) => {
const matchingSuggestion = repos?.find(
Expand All @@ -129,7 +131,7 @@ export default function RepositoryFinder({
return;
}

const matchingPredefinedRepo = memoizedPredefinedRepos.get(selectedID);
const matchingPredefinedRepo = PREDEFINED_REPOS.find((repo) => repo.url === selectedID);
if (matchingPredefinedRepo) {
onChange?.(
new SuggestedRepository({
Expand All @@ -142,20 +144,15 @@ export default function RepositoryFinder({

onChange?.(new SuggestedRepository({ url: selectedID }));
},
[onChange, repos, memoizedPredefinedRepos],
[onChange, repos],
);

const [selectedSuggestion, setSelectedSuggestion] = useState<SuggestedRepository | undefined>(undefined);
const [hasStartedSearching, setHasStartedSearching] = useState(false);
const [isShowingExamples, setIsShowingExamples] = useState(showExamples);

type PredefinedRepositoryOptionProps = {
repo: {
url: string;
repoName: string;
description: string;
repoPath: string;
};
repo: typeof PREDEFINED_REPOS[number];
};

const PredefinedRepositoryOption: FC<PredefinedRepositoryOptionProps> = ({ repo }) => {
Expand Down Expand Up @@ -235,6 +232,10 @@ export default function RepositoryFinder({
return;
}

if (isPredefined(selectedSuggestion)) {
return PREDEFINED_REPOS.find((repo) => repo.url === selectedSuggestion.url)?.repoName;
}

if (!selectedSuggestion?.configurationName) {
return displayContextUrl(selectedSuggestion?.repoName || selectedSuggestion?.url);
}
Expand All @@ -256,19 +257,22 @@ export default function RepositoryFinder({

const getElements = useCallback(
(searchString: string): ComboboxElement[] => {
if (isShowingExamples && searchString.length === 0 && !onlyConfigurations) {
if (isShowingExamples && !onlyConfigurations) {
return PREDEFINED_REPOS.map((repo) => ({
id: repo.url,
element: <PredefinedRepositoryOption repo={repo} />,
isSelectable: true,
}));
}

const result = repos.map((repo) => ({
id: repo.configurationId || repo.url,
element: <SuggestedRepositoryOption repo={repo} />,
isSelectable: true,
}));
// We deduplicate predefined repos, because we artificially add them to the list just below
const result = repos
.filter((repo) => !isPredefined(repo))
.map((repo) => ({
id: repo.configurationId || repo.url,
element: <SuggestedRepositoryOption repo={repo} />,
isSelectable: true,
}));

if (!onlyConfigurations) {
// Add predefined repos to end of the list.
Expand Down Expand Up @@ -332,11 +336,6 @@ export default function RepositoryFinder({
[repos, hasMore, authProviders.data, onlyConfigurations, isShowingExamples],
);

const resolveIcon = useCallback((contextUrl?: string) => {
if (!contextUrl) return RepositorySVG;
return PREDEFINED_REPOS.some((repo) => repo.url === contextUrl) ? GitpodRepositoryTemplateSVG : RepositorySVG;
}, []);

return (
<Combobox
getElements={getElements}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export const PREDEFINED_REPOS = [
description: "A fully configured demo with Java, Maven and Spring Boot",
repoPath: "github.com/gitpod-demos/spring-petclinic",
},
];
] as const;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { SuggestedRepository } from "@gitpod/public-api/lib/gitpod/v1/scm_pb";
import { useSearchRepositories } from "./search-repositories-query";
import { useSuggestedRepositories } from "./suggested-repositories-query";
import { PREDEFINED_REPOS } from "./predefined-repos";
import { useMemo } from "react";
import { useListConfigurations } from "../configurations/configuration-queries";
import type { UseInfiniteQueryResult } from "@tanstack/react-query";
Expand All @@ -25,15 +24,12 @@ type UnifiedRepositorySearchArgs = {
excludeConfigurations?: boolean;
// If true, only shows entries with a corresponding configuration
onlyConfigurations?: boolean;
// If true, only shows example repositories
showExamples?: boolean;
};
// Combines the suggested repositories and the search repositories query into one hook
export const useUnifiedRepositorySearch = ({
searchString,
excludeConfigurations = false,
onlyConfigurations = false,
showExamples = false,
}: UnifiedRepositorySearchArgs) => {
// 1st data source: suggested SCM repos + up to 100 imported repos.
// todo(ft): look into deduplicating and merging these on the server
Expand Down Expand Up @@ -65,20 +61,9 @@ export const useUnifiedRepositorySearch = ({
}, [configurationSearch.data, excludeConfigurations]);

const filteredRepos = useMemo(() => {
if (showExamples && searchString.length === 0) {
return PREDEFINED_REPOS.map(
(repo) =>
new SuggestedRepository({
url: repo.url,
repoName: repo.repoName,
}),
);
}

const repos = [suggestedQuery.data || [], searchQuery.data || [], flattenedConfigurations ?? []].flat();
return deduplicateAndFilterRepositories(searchString, excludeConfigurations, onlyConfigurations, repos);
}, [
showExamples,
searchString,
suggestedQuery.data,
searchQuery.data,
Expand Down

0 comments on commit c21595c

Please sign in to comment.