Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add url filter to drawer results for dependency apps #1698

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions client/src/app/hooks/table-controls/filtering/useFilterState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { usePersistentState } from "@app/hooks/usePersistentState";
import { serializeFilterUrlParams } from "./helpers";
import { deserializeFilterUrlParams } from "./helpers";
import { DiscriminatedArgs } from "@app/utils/type-utils";
import { useEffect, useState } from "react";

/**
* The "source of truth" state for the filter feature.
Expand Down Expand Up @@ -64,9 +65,22 @@ export const useFilterState = <
): IFilterState<TFilterCategoryKey> => {
const { isFilterEnabled, persistTo = "state", persistenceKeyPrefix } = args;

const initialFilterValues: IFilterValues<TFilterCategoryKey> = isFilterEnabled
? args?.initialFilterValues ?? {}
: {};
// We need to know if it's the initial load to avoid overwriting changes to the filter values
const [isInitialLoad, setIsInitialLoad] = useState(true);

let initialFilterValues = {};

if (isInitialLoad) {
initialFilterValues = isFilterEnabled
? args?.initialFilterValues ?? {}
: {};
}

useEffect(() => {
if (isInitialLoad) {
setIsInitialLoad(false);
}
}, [isInitialLoad]);

// We won't need to pass the latter two type params here if TS adds support for partial inference.
// See https://github.com/konveyor/tackle2-ui/issues/1456
Expand Down
23 changes: 21 additions & 2 deletions client/src/app/pages/dependencies/dependency-apps-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
useTableControlState,
useTableControlProps,
getHubRequestParams,
deserializeFilterUrlParams,
} from "@app/hooks/table-controls";
import { TablePersistenceKeyPrefix } from "@app/Constants";
import {
Expand All @@ -30,6 +31,7 @@ import { useFetchBusinessServices } from "@app/queries/businessservices";
import { useFetchTagsWithTagItems } from "@app/queries/tags";
import { getParsedLabel } from "@app/utils/rules-utils";
import { extractFirstSha } from "@app/utils/utils";
import { useHistory } from "react-router-dom";

export interface IDependencyAppsTableProps {
dependency: AnalysisDependency;
Expand All @@ -39,9 +41,15 @@ export const DependencyAppsTable: React.FC<IDependencyAppsTableProps> = ({
dependency,
}) => {
const { t } = useTranslation();
const history = useHistory();

const { businessServices } = useFetchBusinessServices();
const { tagItems } = useFetchTagsWithTagItems();

const urlParams = new URLSearchParams(window.location.search);
const filters = urlParams.get("filters");
const deserializedFilterValues = deserializeFilterUrlParams({ filters });

const tableControlState = useTableControlState({
persistTo: "urlParams",
persistenceKeyPrefix: TablePersistenceKeyPrefix.dependencyApplications,
Expand All @@ -56,9 +64,10 @@ export const DependencyAppsTable: React.FC<IDependencyAppsTableProps> = ({
isPaginationEnabled: true,
sortableColumns: ["name", "version"],
initialSort: { columnKey: "name", direction: "asc" },
initialFilterValues: deserializedFilterValues,
filterCategories: [
{
key: "name",
key: "application.name",
title: "Application Name",
type: FilterType.search,
placeholderText:
Expand Down Expand Up @@ -147,9 +156,19 @@ export const DependencyAppsTable: React.FC<IDependencyAppsTableProps> = ({
},
} = tableControls;

const clearFilters = () => {
const currentPath = history.location.pathname;
const newSearch = new URLSearchParams(history.location.search);
newSearch.delete("filters");
history.push(`${currentPath}`);
};
return (
<>
<Toolbar {...toolbarProps} className={spacing.mtSm}>
<Toolbar
{...toolbarProps}
className={spacing.mtSm}
clearAllFilters={clearFilters}
>
<ToolbarContent>
<FilterToolbar {...filterToolbarProps} />
<ToolbarItem {...paginationToolbarItemProps}>
Expand Down
Loading