Skip to content

Commit dbc713b

Browse files
authored
Merge pull request #250 from aali309/GITOPS-10130
fix: AppProjects's description based filter
2 parents 5a5aee0 + 3e17b26 commit dbc713b

1 file changed

Lines changed: 76 additions & 119 deletions

File tree

‎src/gitops/components/project/ProjectList.tsx‎

Lines changed: 76 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,59 @@ const getApplicationsCount = (
276276
return applications.filter((app) => app.spec?.project === project.metadata?.name).length;
277277
};
278278

279+
const getDescriptionFilterId = (project: K8sResourceCommon): string => {
280+
const description = (project as AppProjectKind)?.spec?.description;
281+
const hasDescription = Boolean(description && description.trim() !== '');
282+
return hasDescription ? 'has-description' : 'no-description';
283+
};
284+
285+
const getApplicationsFilterId = (
286+
project: K8sResourceCommon,
287+
applications: ApplicationKind[],
288+
appsLoaded: boolean,
289+
): string => {
290+
if (!applications || !appsLoaded) return 'unknown';
291+
return getApplicationsCount(project as AppProjectKind, applications, appsLoaded) > 0
292+
? 'has-applications'
293+
: 'no-applications';
294+
};
295+
296+
const getProjectTypeFilterId = (project: K8sResourceCommon): string =>
297+
project?.metadata?.name === 'default' ? 'default' : 'custom';
298+
299+
const getSourceReposFilterId = (project: K8sResourceCommon): string => {
300+
const sourceRepos = (project as AppProjectKind)?.spec?.sourceRepos;
301+
const hasSourceRepos = Boolean(sourceRepos && sourceRepos.length > 0);
302+
return hasSourceRepos ? 'has-source-repos' : 'no-source-repos';
303+
};
304+
305+
const getDestinationsFilterId = (project: K8sResourceCommon): string => {
306+
const destinations = (project as AppProjectKind)?.spec?.destinations;
307+
const hasDestinations = Boolean(destinations && destinations.length > 0);
308+
return hasDestinations ? 'has-destinations' : 'no-destinations';
309+
};
310+
311+
type ProjectFilterIdGetter = (project: K8sResourceCommon) => string;
312+
313+
const createProjectRowFilter = (
314+
filterGroupName: string,
315+
type: string,
316+
getFilterId: ProjectFilterIdGetter,
317+
items: { id: string; title: string }[],
318+
skipWhen = false,
319+
): RowFilter => ({
320+
filterGroupName,
321+
type,
322+
reducer: getFilterId,
323+
filter: (input, project) => {
324+
if (!input.selected?.length || !project || skipWhen) {
325+
return true;
326+
}
327+
return input.selected.includes(getFilterId(project));
328+
},
329+
items,
330+
});
331+
279332
export const sortData = (
280333
data: AppProjectKind[],
281334
sortBy: string | undefined,
@@ -503,128 +556,32 @@ const getFilters = (
503556
applications: ApplicationKind[],
504557
appsLoaded: boolean,
505558
): RowFilter[] => [
506-
{
507-
filterGroupName: t('Description'),
508-
type: 'has-description',
509-
reducer: (project) => {
510-
const hasDescription = project?.spec?.description && project.spec.description.trim() !== '';
511-
return hasDescription ? 'has-description' : 'no-description';
512-
},
513-
filter: (input, project) => {
514-
if (input.selected?.length) {
515-
const hasDescription = project?.spec?.description && project.spec.description.trim() !== '';
516-
if (input.selected.includes('has-description')) {
517-
return hasDescription;
518-
}
519-
if (input.selected.includes('no-description')) {
520-
return !hasDescription;
521-
}
522-
}
523-
return true;
524-
},
525-
items: [
526-
{ id: 'has-description', title: t('Has Description') },
527-
{ id: 'no-description', title: t('No Description') },
528-
],
529-
},
530-
{
531-
filterGroupName: t('Applications'),
532-
type: 'has-applications',
533-
reducer: (project) => {
534-
if (!applications || !appsLoaded) return 'unknown';
535-
const appsCount = getApplicationsCount(project as AppProjectKind, applications, appsLoaded);
536-
return appsCount > 0 ? 'has-applications' : 'no-applications';
537-
},
538-
filter: (input, project) => {
539-
if (input.selected?.length) {
540-
if (!applications || !appsLoaded) return true;
541-
const appsCount = getApplicationsCount(project as AppProjectKind, applications, appsLoaded);
542-
if (input.selected.includes('has-applications')) {
543-
return appsCount > 0;
544-
}
545-
if (input.selected.includes('no-applications')) {
546-
return appsCount === 0;
547-
}
548-
}
549-
return true;
550-
},
551-
items: [
559+
createProjectRowFilter(t('Description'), 'has-description', getDescriptionFilterId, [
560+
{ id: 'has-description', title: t('Has Description') },
561+
{ id: 'no-description', title: t('No Description') },
562+
]),
563+
createProjectRowFilter(
564+
t('Applications'),
565+
'has-applications',
566+
(project) => getApplicationsFilterId(project, applications, appsLoaded),
567+
[
552568
{ id: 'has-applications', title: t('Has Applications') },
553569
{ id: 'no-applications', title: t('No Applications') },
554570
],
555-
},
556-
{
557-
filterGroupName: t('Project Type'),
558-
type: 'project-type',
559-
reducer: (project) => {
560-
const isDefault = project?.metadata?.name === 'default';
561-
return isDefault ? 'default' : 'custom';
562-
},
563-
filter: (input, project) => {
564-
if (input.selected?.length) {
565-
const isDefault = project?.metadata?.name === 'default';
566-
if (input.selected.includes('default')) {
567-
return isDefault;
568-
}
569-
if (input.selected.includes('custom')) {
570-
return !isDefault;
571-
}
572-
}
573-
return true;
574-
},
575-
items: [
576-
{ id: 'default', title: t('Default Project') },
577-
{ id: 'custom', title: t('Custom Projects') },
578-
],
579-
},
580-
{
581-
filterGroupName: t('Source Repositories'),
582-
type: 'has-source-repos',
583-
reducer: (project) => {
584-
const hasSourceRepos = project?.spec?.sourceRepos && project.spec.sourceRepos.length > 0;
585-
return hasSourceRepos ? 'has-source-repos' : 'no-source-repos';
586-
},
587-
filter: (input, project) => {
588-
if (input.selected?.length) {
589-
const hasSourceRepos = project?.spec?.sourceRepos && project.spec.sourceRepos.length > 0;
590-
if (input.selected.includes('has-source-repos')) {
591-
return hasSourceRepos;
592-
}
593-
if (input.selected.includes('no-source-repos')) {
594-
return !hasSourceRepos;
595-
}
596-
}
597-
return true;
598-
},
599-
items: [
600-
{ id: 'has-source-repos', title: t('Has Source Repos') },
601-
{ id: 'no-source-repos', title: t('No Source Repos') },
602-
],
603-
},
604-
{
605-
filterGroupName: t('Destinations'),
606-
type: 'has-destinations',
607-
reducer: (project) => {
608-
const hasDestinations = project?.spec?.destinations && project.spec.destinations.length > 0;
609-
return hasDestinations ? 'has-destinations' : 'no-destinations';
610-
},
611-
filter: (input, project) => {
612-
if (input.selected?.length) {
613-
const hasDestinations = project?.spec?.destinations && project.spec.destinations.length > 0;
614-
if (input.selected.includes('has-destinations')) {
615-
return hasDestinations;
616-
}
617-
if (input.selected.includes('no-destinations')) {
618-
return !hasDestinations;
619-
}
620-
}
621-
return true;
622-
},
623-
items: [
624-
{ id: 'has-destinations', title: t('Has Destinations') },
625-
{ id: 'no-destinations', title: t('No Destinations') },
626-
],
627-
},
571+
!applications || !appsLoaded,
572+
),
573+
createProjectRowFilter(t('Project Type'), 'project-type', getProjectTypeFilterId, [
574+
{ id: 'default', title: t('Default Project') },
575+
{ id: 'custom', title: t('Custom Projects') },
576+
]),
577+
createProjectRowFilter(t('Source Repositories'), 'has-source-repos', getSourceReposFilterId, [
578+
{ id: 'has-source-repos', title: t('Has Source Repos') },
579+
{ id: 'no-source-repos', title: t('No Source Repos') },
580+
]),
581+
createProjectRowFilter(t('Destinations'), 'has-destinations', getDestinationsFilterId, [
582+
{ id: 'has-destinations', title: t('Has Destinations') },
583+
{ id: 'no-destinations', title: t('No Destinations') },
584+
]),
628585
];
629586

630587
export default ProjectList;

0 commit comments

Comments
 (0)