Skip to content

Commit c21595c

Browse files
Simplify example repositories (#20156)
* Simplify example repositories * Actually fix the bug * Fix resolved repo name * Fix display names for examples
1 parent acae8de commit c21595c

File tree

3 files changed

+26
-42
lines changed

3 files changed

+26
-42
lines changed

components/dashboard/src/components/RepositoryFinder.tsx

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ import { SuggestedRepository } from "@gitpod/public-api/lib/gitpod/v1/scm_pb";
2323
import { PREDEFINED_REPOS } from "../data/git-providers/predefined-repos";
2424
import { useConfiguration, useListConfigurations } from "../data/configurations/configuration-queries";
2525

26+
const isPredefined = (repo: SuggestedRepository): boolean => {
27+
return PREDEFINED_REPOS.some((predefined) => predefined.url === repo.url) && !repo.configurationId;
28+
};
29+
30+
const resolveIcon = (contextUrl?: string): string => {
31+
if (!contextUrl) return RepositorySVG;
32+
return PREDEFINED_REPOS.some((repo) => repo.url === contextUrl) ? GitpodRepositoryTemplateSVG : RepositorySVG;
33+
};
34+
2635
interface RepositoryFinderProps {
2736
selectedContextURL?: string;
2837
selectedConfigurationId?: string;
@@ -54,7 +63,6 @@ export default function RepositoryFinder({
5463
searchString,
5564
excludeConfigurations,
5665
onlyConfigurations,
57-
showExamples,
5866
});
5967

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

113121
const authProviders = useAuthProviderDescriptions();
114122

115-
// This approach creates a memoized Map of the predefined repos,
116-
// which can be more efficient for lookups if we would have a large number of predefined repos
117-
const memoizedPredefinedRepos = useMemo(() => {
118-
return new Map(PREDEFINED_REPOS.map((repo) => [repo.url, repo]));
119-
}, []);
120-
121123
const handleSelectionChange = useCallback(
122124
(selectedID: string) => {
123125
const matchingSuggestion = repos?.find(
@@ -129,7 +131,7 @@ export default function RepositoryFinder({
129131
return;
130132
}
131133

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

143145
onChange?.(new SuggestedRepository({ url: selectedID }));
144146
},
145-
[onChange, repos, memoizedPredefinedRepos],
147+
[onChange, repos],
146148
);
147149

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

152154
type PredefinedRepositoryOptionProps = {
153-
repo: {
154-
url: string;
155-
repoName: string;
156-
description: string;
157-
repoPath: string;
158-
};
155+
repo: typeof PREDEFINED_REPOS[number];
159156
};
160157

161158
const PredefinedRepositoryOption: FC<PredefinedRepositoryOptionProps> = ({ repo }) => {
@@ -235,6 +232,10 @@ export default function RepositoryFinder({
235232
return;
236233
}
237234

235+
if (isPredefined(selectedSuggestion)) {
236+
return PREDEFINED_REPOS.find((repo) => repo.url === selectedSuggestion.url)?.repoName;
237+
}
238+
238239
if (!selectedSuggestion?.configurationName) {
239240
return displayContextUrl(selectedSuggestion?.repoName || selectedSuggestion?.url);
240241
}
@@ -256,19 +257,22 @@ export default function RepositoryFinder({
256257

257258
const getElements = useCallback(
258259
(searchString: string): ComboboxElement[] => {
259-
if (isShowingExamples && searchString.length === 0 && !onlyConfigurations) {
260+
if (isShowingExamples && !onlyConfigurations) {
260261
return PREDEFINED_REPOS.map((repo) => ({
261262
id: repo.url,
262263
element: <PredefinedRepositoryOption repo={repo} />,
263264
isSelectable: true,
264265
}));
265266
}
266267

267-
const result = repos.map((repo) => ({
268-
id: repo.configurationId || repo.url,
269-
element: <SuggestedRepositoryOption repo={repo} />,
270-
isSelectable: true,
271-
}));
268+
// We deduplicate predefined repos, because we artificially add them to the list just below
269+
const result = repos
270+
.filter((repo) => !isPredefined(repo))
271+
.map((repo) => ({
272+
id: repo.configurationId || repo.url,
273+
element: <SuggestedRepositoryOption repo={repo} />,
274+
isSelectable: true,
275+
}));
272276

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

335-
const resolveIcon = useCallback((contextUrl?: string) => {
336-
if (!contextUrl) return RepositorySVG;
337-
return PREDEFINED_REPOS.some((repo) => repo.url === contextUrl) ? GitpodRepositoryTemplateSVG : RepositorySVG;
338-
}, []);
339-
340339
return (
341340
<Combobox
342341
getElements={getElements}

components/dashboard/src/data/git-providers/predefined-repos.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export const PREDEFINED_REPOS = [
1717
description: "A fully configured demo with Java, Maven and Spring Boot",
1818
repoPath: "github.com/gitpod-demos/spring-petclinic",
1919
},
20-
];
20+
] as const;

components/dashboard/src/data/git-providers/unified-repositories-search-query.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import { SuggestedRepository } from "@gitpod/public-api/lib/gitpod/v1/scm_pb";
88
import { useSearchRepositories } from "./search-repositories-query";
99
import { useSuggestedRepositories } from "./suggested-repositories-query";
10-
import { PREDEFINED_REPOS } from "./predefined-repos";
1110
import { useMemo } from "react";
1211
import { useListConfigurations } from "../configurations/configuration-queries";
1312
import type { UseInfiniteQueryResult } from "@tanstack/react-query";
@@ -25,15 +24,12 @@ type UnifiedRepositorySearchArgs = {
2524
excludeConfigurations?: boolean;
2625
// If true, only shows entries with a corresponding configuration
2726
onlyConfigurations?: boolean;
28-
// If true, only shows example repositories
29-
showExamples?: boolean;
3027
};
3128
// Combines the suggested repositories and the search repositories query into one hook
3229
export const useUnifiedRepositorySearch = ({
3330
searchString,
3431
excludeConfigurations = false,
3532
onlyConfigurations = false,
36-
showExamples = false,
3733
}: UnifiedRepositorySearchArgs) => {
3834
// 1st data source: suggested SCM repos + up to 100 imported repos.
3935
// todo(ft): look into deduplicating and merging these on the server
@@ -65,20 +61,9 @@ export const useUnifiedRepositorySearch = ({
6561
}, [configurationSearch.data, excludeConfigurations]);
6662

6763
const filteredRepos = useMemo(() => {
68-
if (showExamples && searchString.length === 0) {
69-
return PREDEFINED_REPOS.map(
70-
(repo) =>
71-
new SuggestedRepository({
72-
url: repo.url,
73-
repoName: repo.repoName,
74-
}),
75-
);
76-
}
77-
7864
const repos = [suggestedQuery.data || [], searchQuery.data || [], flattenedConfigurations ?? []].flat();
7965
return deduplicateAndFilterRepositories(searchString, excludeConfigurations, onlyConfigurations, repos);
8066
}, [
81-
showExamples,
8267
searchString,
8368
suggestedQuery.data,
8469
searchQuery.data,

0 commit comments

Comments
 (0)