Skip to content

Commit 3e17b26

Browse files
committed
fix: AppProjects's description based filter
Signed-off-by: Atif Ali <atali@redhat.com>
1 parent 01e9db1 commit 3e17b26

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
@@ -267,6 +267,59 @@ const getApplicationsCount = (
267267
return applications.filter((app) => app.spec?.project === project.metadata?.name).length;
268268
};
269269

270+
const getDescriptionFilterId = (project: K8sResourceCommon): string => {
271+
const description = (project as AppProjectKind)?.spec?.description;
272+
const hasDescription = Boolean(description && description.trim() !== '');
273+
return hasDescription ? 'has-description' : 'no-description';
274+
};
275+
276+
const getApplicationsFilterId = (
277+
project: K8sResourceCommon,
278+
applications: ApplicationKind[],
279+
appsLoaded: boolean,
280+
): string => {
281+
if (!applications || !appsLoaded) return 'unknown';
282+
return getApplicationsCount(project as AppProjectKind, applications, appsLoaded) > 0
283+
? 'has-applications'
284+
: 'no-applications';
285+
};
286+
287+
const getProjectTypeFilterId = (project: K8sResourceCommon): string =>
288+
project?.metadata?.name === 'default' ? 'default' : 'custom';
289+
290+
const getSourceReposFilterId = (project: K8sResourceCommon): string => {
291+
const sourceRepos = (project as AppProjectKind)?.spec?.sourceRepos;
292+
const hasSourceRepos = Boolean(sourceRepos && sourceRepos.length > 0);
293+
return hasSourceRepos ? 'has-source-repos' : 'no-source-repos';
294+
};
295+
296+
const getDestinationsFilterId = (project: K8sResourceCommon): string => {
297+
const destinations = (project as AppProjectKind)?.spec?.destinations;
298+
const hasDestinations = Boolean(destinations && destinations.length > 0);
299+
return hasDestinations ? 'has-destinations' : 'no-destinations';
300+
};
301+
302+
type ProjectFilterIdGetter = (project: K8sResourceCommon) => string;
303+
304+
const createProjectRowFilter = (
305+
filterGroupName: string,
306+
type: string,
307+
getFilterId: ProjectFilterIdGetter,
308+
items: { id: string; title: string }[],
309+
skipWhen = false,
310+
): RowFilter => ({
311+
filterGroupName,
312+
type,
313+
reducer: getFilterId,
314+
filter: (input, project) => {
315+
if (!input.selected?.length || !project || skipWhen) {
316+
return true;
317+
}
318+
return input.selected.includes(getFilterId(project));
319+
},
320+
items,
321+
});
322+
270323
export const sortData = (
271324
data: AppProjectKind[],
272325
sortBy: string | undefined,
@@ -493,128 +546,32 @@ const getFilters = (
493546
applications: ApplicationKind[],
494547
appsLoaded: boolean,
495548
): RowFilter[] => [
496-
{
497-
filterGroupName: t('Description'),
498-
type: 'has-description',
499-
reducer: (project) => {
500-
const hasDescription = project?.spec?.description && project.spec.description.trim() !== '';
501-
return hasDescription ? 'has-description' : 'no-description';
502-
},
503-
filter: (input, project) => {
504-
if (input.selected?.length) {
505-
const hasDescription = project?.spec?.description && project.spec.description.trim() !== '';
506-
if (input.selected.includes('has-description')) {
507-
return hasDescription;
508-
}
509-
if (input.selected.includes('no-description')) {
510-
return !hasDescription;
511-
}
512-
}
513-
return true;
514-
},
515-
items: [
516-
{ id: 'has-description', title: t('Has Description') },
517-
{ id: 'no-description', title: t('No Description') },
518-
],
519-
},
520-
{
521-
filterGroupName: t('Applications'),
522-
type: 'has-applications',
523-
reducer: (project) => {
524-
if (!applications || !appsLoaded) return 'unknown';
525-
const appsCount = getApplicationsCount(project as AppProjectKind, applications, appsLoaded);
526-
return appsCount > 0 ? 'has-applications' : 'no-applications';
527-
},
528-
filter: (input, project) => {
529-
if (input.selected?.length) {
530-
if (!applications || !appsLoaded) return true;
531-
const appsCount = getApplicationsCount(project as AppProjectKind, applications, appsLoaded);
532-
if (input.selected.includes('has-applications')) {
533-
return appsCount > 0;
534-
}
535-
if (input.selected.includes('no-applications')) {
536-
return appsCount === 0;
537-
}
538-
}
539-
return true;
540-
},
541-
items: [
549+
createProjectRowFilter(t('Description'), 'has-description', getDescriptionFilterId, [
550+
{ id: 'has-description', title: t('Has Description') },
551+
{ id: 'no-description', title: t('No Description') },
552+
]),
553+
createProjectRowFilter(
554+
t('Applications'),
555+
'has-applications',
556+
(project) => getApplicationsFilterId(project, applications, appsLoaded),
557+
[
542558
{ id: 'has-applications', title: t('Has Applications') },
543559
{ id: 'no-applications', title: t('No Applications') },
544560
],
545-
},
546-
{
547-
filterGroupName: t('Project Type'),
548-
type: 'project-type',
549-
reducer: (project) => {
550-
const isDefault = project?.metadata?.name === 'default';
551-
return isDefault ? 'default' : 'custom';
552-
},
553-
filter: (input, project) => {
554-
if (input.selected?.length) {
555-
const isDefault = project?.metadata?.name === 'default';
556-
if (input.selected.includes('default')) {
557-
return isDefault;
558-
}
559-
if (input.selected.includes('custom')) {
560-
return !isDefault;
561-
}
562-
}
563-
return true;
564-
},
565-
items: [
566-
{ id: 'default', title: t('Default Project') },
567-
{ id: 'custom', title: t('Custom Projects') },
568-
],
569-
},
570-
{
571-
filterGroupName: t('Source Repositories'),
572-
type: 'has-source-repos',
573-
reducer: (project) => {
574-
const hasSourceRepos = project?.spec?.sourceRepos && project.spec.sourceRepos.length > 0;
575-
return hasSourceRepos ? 'has-source-repos' : 'no-source-repos';
576-
},
577-
filter: (input, project) => {
578-
if (input.selected?.length) {
579-
const hasSourceRepos = project?.spec?.sourceRepos && project.spec.sourceRepos.length > 0;
580-
if (input.selected.includes('has-source-repos')) {
581-
return hasSourceRepos;
582-
}
583-
if (input.selected.includes('no-source-repos')) {
584-
return !hasSourceRepos;
585-
}
586-
}
587-
return true;
588-
},
589-
items: [
590-
{ id: 'has-source-repos', title: t('Has Source Repos') },
591-
{ id: 'no-source-repos', title: t('No Source Repos') },
592-
],
593-
},
594-
{
595-
filterGroupName: t('Destinations'),
596-
type: 'has-destinations',
597-
reducer: (project) => {
598-
const hasDestinations = project?.spec?.destinations && project.spec.destinations.length > 0;
599-
return hasDestinations ? 'has-destinations' : 'no-destinations';
600-
},
601-
filter: (input, project) => {
602-
if (input.selected?.length) {
603-
const hasDestinations = project?.spec?.destinations && project.spec.destinations.length > 0;
604-
if (input.selected.includes('has-destinations')) {
605-
return hasDestinations;
606-
}
607-
if (input.selected.includes('no-destinations')) {
608-
return !hasDestinations;
609-
}
610-
}
611-
return true;
612-
},
613-
items: [
614-
{ id: 'has-destinations', title: t('Has Destinations') },
615-
{ id: 'no-destinations', title: t('No Destinations') },
616-
],
617-
},
561+
!applications || !appsLoaded,
562+
),
563+
createProjectRowFilter(t('Project Type'), 'project-type', getProjectTypeFilterId, [
564+
{ id: 'default', title: t('Default Project') },
565+
{ id: 'custom', title: t('Custom Projects') },
566+
]),
567+
createProjectRowFilter(t('Source Repositories'), 'has-source-repos', getSourceReposFilterId, [
568+
{ id: 'has-source-repos', title: t('Has Source Repos') },
569+
{ id: 'no-source-repos', title: t('No Source Repos') },
570+
]),
571+
createProjectRowFilter(t('Destinations'), 'has-destinations', getDestinationsFilterId, [
572+
{ id: 'has-destinations', title: t('Has Destinations') },
573+
{ id: 'no-destinations', title: t('No Destinations') },
574+
]),
618575
];
619576

620577
export default ProjectList;

0 commit comments

Comments
 (0)