diff --git a/.changeset/wise-flies-laugh.md b/.changeset/wise-flies-laugh.md new file mode 100644 index 0000000000..79a9c3bf5a --- /dev/null +++ b/.changeset/wise-flies-laugh.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-home': minor +--- + +Added filter support for HomePageVisitedByType in order to enable filtering entites from the list diff --git a/plugins/home/src/homePageComponents/VisitedByType/Content.test.tsx b/plugins/home/src/homePageComponents/VisitedByType/Content.test.tsx index 4442c319e6..87b5b672a3 100644 --- a/plugins/home/src/homePageComponents/VisitedByType/Content.test.tsx +++ b/plugins/home/src/homePageComponents/VisitedByType/Content.test.tsx @@ -117,6 +117,25 @@ describe('', () => { expect(container.querySelectorAll('li')[0]).toBeVisible(); expect(container.querySelectorAll('li')[1]).not.toBeVisible(); }); + + it('allows items to be filtered', async () => { + const { getByText, queryByText } = await renderInTestApp( + + + + + , + ); + await waitFor(() => + expect(getByText('Explore Backstage')).toBeInTheDocument(), + ); + await waitFor(() => expect(queryByText('Tech Radar')).toBeNull()); + }); }); describe('', () => { diff --git a/plugins/home/src/homePageComponents/VisitedByType/Content.tsx b/plugins/home/src/homePageComponents/VisitedByType/Content.tsx index 4e847b717f..400cb48e09 100644 --- a/plugins/home/src/homePageComponents/VisitedByType/Content.tsx +++ b/plugins/home/src/homePageComponents/VisitedByType/Content.tsx @@ -31,6 +31,11 @@ export type VisitedByTypeProps = { numVisitsTotal?: number; loading?: boolean; kind: VisitedByTypeKind; + filterBy?: Array<{ + field: keyof Visit; + operator: '<' | '<=' | '==' | '!=' | '>' | '>=' | 'contains'; + value: string | number; + }>; }; /** @@ -43,6 +48,7 @@ export const Content = ({ numVisitsTotal, loading, kind, + filterBy, }: VisitedByTypeProps) => { const { setContext, setVisits, setLoading } = useContext(); // Allows behavior override from properties @@ -65,18 +71,34 @@ export const Content = ({ const { loading: reqLoading } = useAsync(async () => { if (!visits && !loading && kind === 'recent') { return await visitsApi - .list({ - limit: numVisitsTotal ?? 8, - orderBy: [{ field: 'timestamp', direction: 'desc' }], - }) + .list( + filterBy + ? { + limit: numVisitsTotal ?? 8, + orderBy: [{ field: 'timestamp', direction: 'desc' }], + filterBy, + } + : { + limit: numVisitsTotal ?? 8, + orderBy: [{ field: 'timestamp', direction: 'desc' }], + }, + ) .then(setVisits); } if (!visits && !loading && kind === 'top') { return await visitsApi - .list({ - limit: numVisitsTotal ?? 8, - orderBy: [{ field: 'hits', direction: 'desc' }], - }) + .list( + filterBy + ? { + limit: numVisitsTotal ?? 8, + orderBy: [{ field: 'hits', direction: 'desc' }], + filterBy, + } + : { + limit: numVisitsTotal ?? 8, + orderBy: [{ field: 'hits', direction: 'desc' }], + }, + ) .then(setVisits); } return undefined;