Updated CatalogKindHeader to respect query param changes

Signed-off-by: Andre Wanlin <awanlin@rapidrtc.com>
This commit is contained in:
Andre Wanlin
2022-03-07 08:01:06 -06:00
parent c8f5f5c1d2
commit 251688a75e
3 changed files with 52 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog': patch
---
Updated `CatalogKindHeader` to respond to external changes to query parameters in the URL, such as two sidebar links that apply different catalog filters.
@@ -135,4 +135,34 @@ describe('<CatalogKindHeader />', () => {
kind: new EntityKindFilter('template'),
});
});
it('responds to external queryParameters changes', async () => {
const updateFilters = jest.fn();
const rendered = await renderWithEffects(
<MockEntityListContextProvider
value={{
updateFilters,
queryParameters: { kind: ['Component'] },
}}
>
<CatalogKindHeader />
</MockEntityListContextProvider>,
);
expect(updateFilters).toHaveBeenLastCalledWith({
kind: new EntityKindFilter('Components'),
});
rendered.rerender(
<MockEntityListContextProvider
value={{
updateFilters,
queryParameters: { kind: ['Template'] },
}}
>
<CatalogKindHeader />
</MockEntityListContextProvider>,
);
expect(updateFilters).toHaveBeenLastCalledWith({
kind: new EntityKindFilter('Template'),
});
});
});
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useMemo } from 'react';
import {
capitalize,
createStyles,
@@ -61,6 +61,14 @@ export function CatalogKindHeader(props: CatalogKindHeaderProps) {
});
const { updateFilters, queryParameters } = useEntityList();
const queryParamKind = useMemo(
() =>
([queryParameters.kind].flat()[0] ?? initialFilter).toLocaleLowerCase(
'en-US',
),
[initialFilter, queryParameters],
);
const [selectedKind, setSelectedKind] = useState(
([queryParameters.kind].flat()[0] ?? initialFilter).toLocaleLowerCase(
'en-US',
@@ -73,6 +81,14 @@ export function CatalogKindHeader(props: CatalogKindHeaderProps) {
});
}, [selectedKind, updateFilters]);
// Set selected Kind on query parameter updates; this happens at initial page load and from
// external updates to the page location.
useEffect(() => {
if (queryParamKind) {
setSelectedKind(queryParamKind);
}
}, [queryParamKind]);
// Before allKinds is loaded, or when a kind is entered manually in the URL, selectedKind may not
// be present in allKinds. It should still be shown in the dropdown, but may not have the nice
// enforced casing from the catalog-backend. This makes a key/value record for the Select options,