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

🐛 Refactor dependencies, select row by name and provider #1554

Merged
merged 1 commit into from
Nov 17, 2023
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
2 changes: 2 additions & 0 deletions client/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
"selectMany": "Select {{what}}",
"selectOne": "Select a {{what}}",
"selectAn": "Select an {{what}}",
"totalApplications": "{{count}} application",
"totalApplications_plural": "{{count}} applications",
"workPriority": "Work priority (1=low, 10=high)"
},
"dialog": {
Expand Down
54 changes: 23 additions & 31 deletions client/src/app/pages/dependencies/dependencies.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from "react";
import {
Button,
Label,
LabelGroup,
PageSection,
Expand Down Expand Up @@ -30,7 +29,6 @@ import { useFetchDependencies } from "@app/queries/dependencies";
import { useSelectionState } from "@migtools/lib-ui";
import { DependencyAppsDetailDrawer } from "./dependency-apps-detail-drawer";
import { useSharedAffectedApplicationFilterCategories } from "../issues/helpers";
import spacing from "@patternfly/react-styles/css/utilities/Spacing/spacing";
import { getParsedLabel } from "@app/utils/rules-utils";

export const Dependencies: React.FC = () => {
Expand All @@ -47,8 +45,6 @@ export const Dependencies: React.FC = () => {
foundIn: "Found in",
provider: "Language",
labels: "Labels",
sha: "SHA",
version: "Version",
},
isFilterEnabled: true,
isSortEnabled: true,
Expand Down Expand Up @@ -101,7 +97,7 @@ export const Dependencies: React.FC = () => {

const tableControls = useTableControlProps({
...tableControlState, // Includes filterState, sortState and paginationState
idProperty: "name",
idProperty: "_ui_unique_id",
currentPageItems,
totalItemCount,
isLoading: isFetching,
Expand All @@ -123,7 +119,7 @@ export const Dependencies: React.FC = () => {
getTrProps,
getTdProps,
},
activeItemDerivedState: { activeItem, clearActiveItem, setActiveItem },
activeItemDerivedState: { activeItem, clearActiveItem },
} = tableControls;

return (
Expand Down Expand Up @@ -151,14 +147,18 @@ export const Dependencies: React.FC = () => {
</ToolbarItem>
</ToolbarContent>
</Toolbar>
<Table {...tableProps} aria-label="Migration waves table">
<Table
{...tableProps}
id="dependencies-table"
aria-label="Dependencies table"
>
<Thead>
<Tr>
<TableHeaderContentWithControls {...tableControls}>
<Th {...getThProps({ columnKey: "name" })} />
<Th {...getThProps({ columnKey: "foundIn" })} />
<Th {...getThProps({ columnKey: "provider" })} />
<Th {...getThProps({ columnKey: "labels" })} />
<Th {...getThProps({ columnKey: "foundIn" })} />
</TableHeaderContentWithControls>
</Tr>
</Thead>
Expand All @@ -182,36 +182,28 @@ export const Dependencies: React.FC = () => {
<Td width={25} {...getTdProps({ columnKey: "name" })}>
{dependency.name}
</Td>
<Td width={10} {...getTdProps({ columnKey: "foundIn" })}>
<Button
className={spacing.pl_0}
variant="link"
onClick={(_) => {
if (activeItem && activeItem === dependency) {
clearActiveItem();
} else {
setActiveItem(dependency);
}
}}
>
{`${dependency.applications} application(s)`}
</Button>
</Td>
<Td width={10} {...getTdProps({ columnKey: "provider" })}>
{dependency.provider}
</Td>
<Td width={10} {...getTdProps({ columnKey: "labels" })}>
<LabelGroup>
{dependency?.labels?.map((label) => {
if (getParsedLabel(label).labelType !== "language")
return (
<Label>
{getParsedLabel(label).labelValue}
</Label>
);
{dependency?.labels?.map((label, index) => {
const { labelType, labelValue } =
getParsedLabel(label);

return labelType !== "language" ? (
<Label key={`${index}-${labelValue}`}>
{labelValue}
</Label>
) : undefined;
})}
</LabelGroup>
</Td>
<Td width={10} {...getTdProps({ columnKey: "foundIn" })}>
{t("composed.totalApplications", {
count: dependency.applications,
})}
</Td>
</TableRowContentWithControls>
</Tr>
))}
Expand All @@ -227,7 +219,7 @@ export const Dependencies: React.FC = () => {
</PageSection>
<DependencyAppsDetailDrawer
dependency={activeItem || null}
onCloseClick={() => setActiveItem(null)}
onCloseClick={() => clearActiveItem()}
/>
</>
);
Expand Down
26 changes: 24 additions & 2 deletions client/src/app/queries/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useMemo } from "react";
import { useQuery } from "@tanstack/react-query";
import {
AnalysisAppDependency,
AnalysisDependency,
HubPaginatedResult,
HubRequestParams,
WithUiId,
} from "@app/api/models";
import { getAppDependencies, getDependencies } from "@app/api/rest";

export interface IDependenciesFetchState {
result: HubPaginatedResult<AnalysisDependency>;
result: HubPaginatedResult<WithUiId<AnalysisDependency>>;
isFetching: boolean;
fetchError: unknown;
refetch: () => void;
Expand All @@ -32,8 +34,28 @@ export const useFetchDependencies = (
onError: (error) => console.log("error, ", error),
keepPreviousData: true,
});

const result = useMemo(() => {
if (!data) {
return { data: [], total: 0, params };
}

const syntheticData: WithUiId<AnalysisDependency>[] = data.data.map(
(dep) => ({
...dep,
_ui_unique_id: `${dep.name}/${dep.provider}`,
})
);

return {
data: syntheticData,
total: data.total,
params: data.params,
};
}, [data, params]);
Comment on lines +38 to +55
Copy link
Collaborator

@mturley mturley Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can abstract out something like:

useWithUiId(data, (dep) => `${dep.name}/${dep.provider}`);

This indicates to me that maybe internally the table-controls/batteries/helpers could just take a getItemId callback in the first place if you don't have a usable idProperty. Will factor that into thinking on current refactors over there.

LGTM as-is though if you want to merge.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idProperty: string | (item: TItem) -> string?

;-)


return {
result: data || { data: [], total: 0, params },
result,
isFetching: isLoading,
fetchError: error,
refetch,
Expand Down
Loading