From e12434ab89ca35793671b11b232a8bfafd25dafd Mon Sep 17 00:00:00 2001 From: Yaacov Zamir Date: Mon, 26 Feb 2024 08:42:30 +0200 Subject: [PATCH] Cleanup unused mapping pages Signed-off-by: Yaacov Zamir --- .../src/components/mappings/MappingPage.tsx | 142 -------------- .../src/components/mappings/MappingRow.tsx | 173 ------------------ .../src/components/mappings/data.ts | 76 -------- .../components/mappings/mappingActions.tsx | 142 -------------- .../src/components/mappings/styles.css | 3 - 5 files changed, 536 deletions(-) delete mode 100644 packages/forklift-console-plugin/src/components/mappings/MappingPage.tsx delete mode 100644 packages/forklift-console-plugin/src/components/mappings/MappingRow.tsx delete mode 100644 packages/forklift-console-plugin/src/components/mappings/data.ts delete mode 100644 packages/forklift-console-plugin/src/components/mappings/mappingActions.tsx delete mode 100644 packages/forklift-console-plugin/src/components/mappings/styles.css diff --git a/packages/forklift-console-plugin/src/components/mappings/MappingPage.tsx b/packages/forklift-console-plugin/src/components/mappings/MappingPage.tsx deleted file mode 100644 index b8b56bc0e..000000000 --- a/packages/forklift-console-plugin/src/components/mappings/MappingPage.tsx +++ /dev/null @@ -1,142 +0,0 @@ -import React from 'react'; -import { ConditionalTooltip } from 'legacy/src/common/components/ConditionalTooltip'; -import { useHasSufficientProviders } from 'src/modules/Plans/data'; -import * as C from 'src/utils/constants'; -import { MAPPING_STATUS } from 'src/utils/enums'; - -import { EnumToTuple } from '@kubev2v/common'; -import { withQueryClient } from '@kubev2v/common'; -import { DefaultHeader, TableViewHeaderProps } from '@kubev2v/common'; -import { ResourceFieldFactory, ResourceFieldPartialFactory } from '@kubev2v/common'; -import { AddEditMappingModal } from '@kubev2v/legacy/Mappings/components/AddEditMappingModal'; -import { MappingType } from '@kubev2v/legacy/queries/types'; -import { useModal } from '@openshift-console/dynamic-plugin-sdk'; -import { Button } from '@patternfly/react-core'; -import { Th } from '@patternfly/react-table'; - -export const AddMappingButton: React.FC<{ - namespace: string; - label: string; - mappingType: MappingType; -}> = ({ namespace, label, mappingType }) => { - const launchModal = useModal(); - - const hasSufficientProviders = useHasSufficientProviders(namespace); - - return ( - - - - ); -}; -AddMappingButton.displayName = 'AddMappingButton'; - -const AddMappingModal: React.FC<{ - currentNamespace: string; - closeModal: () => void; - label: string; - mappingType: MappingType; -}> = ({ closeModal, currentNamespace, label, mappingType }) => { - return ( - undefined} - /> - ); -}; -AddMappingModal.displayName = 'AddMappingModal'; - -const byName: ResourceFieldPartialFactory = (t) => ({ - isVisible: true, - filter: { - type: 'freetext', - placeholderLabel: t('Filter by name'), - }, - sortable: true, -}); - -export const commonFieldsMetadataFactory: ResourceFieldFactory = (t) => [ - { - resourceFieldId: C.NAME, - label: t('Name'), - ...byName(t), - isIdentity: true, - }, - { - resourceFieldId: C.NAMESPACE, - label: t('Namespace'), - isVisible: true, - isIdentity: true, - filter: { - placeholderLabel: t('Filter by namespace'), - type: 'freetext', - }, - sortable: true, - }, - { - resourceFieldId: C.SOURCE, - label: t('Source provider'), - ...byName(t), - }, - { - resourceFieldId: C.TARGET, - label: t('Target provider'), - ...byName(t), - }, - { - resourceFieldId: C.FROM, - label: t('From'), - isVisible: true, - sortable: false, - }, - { - resourceFieldId: C.MANAGED, - label: t('Managed'), - isHidden: true, - filter: { - type: 'slider', - standalone: true, - placeholderLabel: t('Show managed'), - defaultValues: ['false'], - }, - }, - { - resourceFieldId: C.STATUS, - label: t('Status'), - isVisible: true, - filter: { - type: 'enum', - primary: true, - placeholderLabel: t('Status'), - values: EnumToTuple(MAPPING_STATUS(t)), - }, - sortable: true, - }, -]; - -export const StartWithEmptyColumnMapper = (props: TableViewHeaderProps) => ( - <> - - - -); diff --git a/packages/forklift-console-plugin/src/components/mappings/MappingRow.tsx b/packages/forklift-console-plugin/src/components/mappings/MappingRow.tsx deleted file mode 100644 index 62d61d11c..000000000 --- a/packages/forklift-console-plugin/src/components/mappings/MappingRow.tsx +++ /dev/null @@ -1,173 +0,0 @@ -import React, { useCallback, useState } from 'react'; -import * as C from 'src/utils/constants'; -import { MAPPING_STATUS } from 'src/utils/enums'; -import { useForkliftTranslation } from 'src/utils/i18n'; - -import { getResourceFieldValue } from '@kubev2v/common'; -import { RowProps } from '@kubev2v/common'; -import { MappingDetailView } from '@kubev2v/legacy/Mappings/components/MappingDetailView'; -import { Mapping, MappingType } from '@kubev2v/legacy/queries/types'; -import { - GreenCheckCircleIcon, - RedExclamationCircleIcon, - ResourceLink, -} from '@openshift-console/dynamic-plugin-sdk'; -import { Label } from '@patternfly/react-core'; -import spacing from '@patternfly/react-styles/css/utilities/Spacing/spacing'; -import { ExpandableRowContent, Td, Tr } from '@patternfly/react-table'; - -import { StatusCell } from '../cells/StatusCell'; - -import { CommonMapping } from './data'; - -import './styles.css'; -export interface CellProps { - value: string; - resourceData: T; - currentNamespace: string; - t: (key: string, params?: { [k: string]: string | number }) => string; -} - -const TextCell = ({ value }: CellProps) => <>{value ?? ''}; - -export const SourceCell = ({ - itemsInFirstGroup = [], - groups = [], - Icon, -}: { - itemsInFirstGroup: unknown[]; - groups: unknown[]; - Icon: React.ComponentClass; -}) => { - const { t } = useForkliftTranslation(); - const isSingleGroup = groups.length === 1; - return ( - <> - {/*keep ' ' spacer */} - {isSingleGroup && itemsInFirstGroup.length} - {!isSingleGroup && t('{{groupCount}} Groups', { groupCount: groups.length })} - - ); -}; - -export type CellCreator = Record< - string, - (props: CellProps) => JSX.Element ->; - -const NameCell: React.FC> = ({ resourceData }) => { - const { t } = useForkliftTranslation(); - - return ( - - - {resourceData.managed && ( - - )} - - ); -}; - -const MappingStatusCell: React.FC> = ({ resourceData }) => { - const { t } = useForkliftTranslation(); - - return ( - : - } - label={MAPPING_STATUS(t)[resourceData.status] ?? t('Not Ready')} - /> - ); -}; - -export const commonCells: CellCreator = { - [C.NAME]: NameCell, - [C.SOURCE]: ({ resourceData: e }: CellProps) => ( - - ), - [C.TARGET]: ({ resourceData: e }: CellProps) => ( - - ), - [C.NAMESPACE]: ({ value }: CellProps) => ( - - ), - [C.STATUS]: MappingStatusCell, -}; - -function MappingRow({ - rowProps: { resourceFields, resourceData, namespace: currentNamespace, resourceIndex: rowIndex }, - cellCreator, - mappingType, - mapping, -}: { - rowProps: RowProps; - cellCreator: CellCreator; - mappingType: MappingType; - mapping: Mapping; -}) { - const { t } = useForkliftTranslation(); - const [isRowExpanded, setIsRowExpanded] = useState(false); - const toggleExpand = useCallback( - () => setIsRowExpanded(!isRowExpanded), - [isRowExpanded, setIsRowExpanded], - ); - return ( - <> - - - {resourceFields.map(({ resourceFieldId, label }, columnIndex) => { - const Cell = cellCreator[resourceFieldId] ?? TextCell; - return ( - - - - ); - })} - - - - {isRowExpanded && ( - - - - )} - - - - ); -} - -export default MappingRow; diff --git a/packages/forklift-console-plugin/src/components/mappings/data.ts b/packages/forklift-console-plugin/src/components/mappings/data.ts deleted file mode 100644 index 71b5adb6e..000000000 --- a/packages/forklift-console-plugin/src/components/mappings/data.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { useMemo } from 'react'; -import * as C from 'src/utils/constants'; -import { useProviders } from 'src/utils/fetch'; -import { groupVersionKindForObj, ResourceKind } from 'src/utils/resources'; -import { MappingStatus } from 'src/utils/types'; - -import { Mapping } from '@kubev2v/legacy/queries/types'; -import { - V1beta1NetworkMapStatusConditions, - V1beta1Provider, - V1beta1StorageMapStatusConditions, -} from '@kubev2v/types'; -import { - K8sGroupVersionKind, - OwnerReference, - WatchK8sResource, - WatchK8sResult, -} from '@openshift-console/dynamic-plugin-sdk'; - -export interface CommonMapping { - [C.NAME]: string; - [C.NAMESPACE]: string; - [C.GVK]: K8sGroupVersionKind; - [C.SOURCE]: string; - [C.SOURCE_GVK]: K8sGroupVersionKind; - [C.SOURCE_RESOLVED]: boolean; - [C.SOURCE_READY]: boolean; - [C.TARGET]: string; - [C.TARGET_GVK]: K8sGroupVersionKind; - [C.TARGET_RESOLVED]: boolean; - [C.TARGET_READY]: boolean; - [C.TEMPLATE]: boolean; - [C.OWNER]: string; - [C.OWNER_GVK]: K8sGroupVersionKind; - [C.MANAGED]: boolean; - [C.OBJECT]: Mapping; - [C.STATUS]: MappingStatus; - conditions: V1beta1NetworkMapStatusConditions[] | V1beta1StorageMapStatusConditions[]; -} - -export interface OwnerRef { - name?: string; - gvk?: K8sGroupVersionKind; -} - -export const resolveOwnerRef = ([first, second]: OwnerReference[] = []): OwnerRef => { - // expect only one owner - the plan - if (!first || first.kind != ResourceKind.Plan || second) { - return {}; - } - - return { name: first.name, gvk: groupVersionKindForObj(first) }; -}; - -export const toStatus = ( - conditions: V1beta1NetworkMapStatusConditions[] | V1beta1StorageMapStatusConditions[], -): MappingStatus => - conditions.find((it) => it.type === 'Ready')?.status === 'True' ? 'Ready' : 'NotReady'; - -export const useMappings = ( - { namespace, name = undefined }: { namespace: string; name?: string }, - useMappings: (p: WatchK8sResource) => WatchK8sResult, - mergeData: (m: T[], p: V1beta1Provider[]) => K[], -): [K[], boolean, boolean] => { - const [providers] = useProviders({ namespace }); - const [mappings, loaded, error] = useMappings({ namespace, name }); - - const merged = useMemo( - () => (mappings && providers ? mergeData(mappings, providers) : []), - [mappings, providers], - ); - // extra memo to keep the tuple reference stable - // the tuple is used as data source and passed as prop - // which triggers unnecessary re-renders - return useMemo(() => [merged, loaded, error], [merged, loaded, error]); -}; diff --git a/packages/forklift-console-plugin/src/components/mappings/mappingActions.tsx b/packages/forklift-console-plugin/src/components/mappings/mappingActions.tsx deleted file mode 100644 index c77c1b0a9..000000000 --- a/packages/forklift-console-plugin/src/components/mappings/mappingActions.tsx +++ /dev/null @@ -1,142 +0,0 @@ -import React, { useMemo } from 'react'; -import { useForkliftTranslation } from 'src/utils/i18n'; - -import { withQueryClient } from '@kubev2v/common'; -import { ConfirmModal } from '@kubev2v/legacy/common/components/ConfirmModal'; -import { AddEditMappingModal } from '@kubev2v/legacy/Mappings/components/AddEditMappingModal'; -import { useDeleteMappingMutation } from '@kubev2v/legacy/queries'; -import { Mapping, MappingType } from '@kubev2v/legacy/queries/types'; -import { NetworkMapModel } from '@kubev2v/types'; -import { useAccessReview, useModal } from '@openshift-console/dynamic-plugin-sdk'; - -import { CommonMapping } from './data'; - -export function useMappingActions({ - resourceData, - mappingType, -}: { - resourceData: T; - mappingType: MappingType; -}) { - const { t } = useForkliftTranslation(); - const launchModal = useModal(); - - const [canDelete] = useAccessReview({ - group: NetworkMapModel.apiGroup, - resource: NetworkMapModel.plural, - verb: 'delete', - name: resourceData.name, - namespace: resourceData.namespace, - }); - - const [canPatch] = useAccessReview({ - group: NetworkMapModel.apiGroup, - resource: NetworkMapModel.plural, - verb: 'patch', - name: resourceData.name, - namespace: resourceData.namespace, - }); - - const actions = useMemo( - () => [ - { - id: 'edit', - cta: () => - launchModal(withQueryClient(EditMappingModal), { - mapping: resourceData.object, - mappingType, - namespace: resourceData.namespace, - }), - label: t('Edit Mapping'), - disabled: !canPatch || resourceData.managed, - disabledTooltip: t('Managed mappings can not be edited'), - }, - { - id: 'delete', - cta: () => - launchModal(withQueryClient(DeleteMappingModal), { - mapping: resourceData.object, - mappingType, - namespace: resourceData.namespace, - name: resourceData.name, - }), - label: t('Delete Mapping'), - disabled: !canDelete || resourceData.managed, - disabledTooltip: t('Managed mappings can not be deleted'), - }, - ], - [t, resourceData, canPatch, canDelete], - ); - - return [actions, true, undefined]; -} - -const EditMappingModal = ({ - mapping, - closeModal, - mappingType, - namespace, -}: { - closeModal: () => void; - mapping: Mapping; - mappingType: MappingType; - namespace: string; -}) => { - const { t } = useForkliftTranslation(); - const title = mappingType === 'Network' ? t('Edit NetworkMap') : t('Edit StorageMap'); - return ( - undefined} - namespace={namespace} - /> - ); -}; -EditMappingModal.displayName = 'EditMappingModal'; - -const DeleteMappingModal = ({ - mapping, - closeModal, - mappingType, - namespace, - name, -}: { - closeModal: () => void; - mapping: Mapping; - mappingType: MappingType; - namespace: string; - name: string; -}) => { - const { t } = useForkliftTranslation(); - const deleteMappingMutation = useDeleteMappingMutation(mappingType, namespace, closeModal); - const msg = { - title: mappingType === 'Network' ? t('Delete NetworkMap?') : t('Delete StorageMap?'), - body: t( - 'You will no longer be able to select mapping "{{name}}" when you create a migration plan.', - { name }, - ), - errorText: - mappingType === 'Network' - ? t('Cannot delete network mapping') - : t('Cannot delete storage mapping'), - }; - return ( - deleteMappingMutation.mutate(mapping)} - mutateResult={deleteMappingMutation} - title={msg.title} - body={msg.body} - confirmButtonText={t('Delete')} - errorText={msg.errorText} - /> - ); -}; -DeleteMappingModal.displayName = 'DeleteMappingModal'; diff --git a/packages/forklift-console-plugin/src/components/mappings/styles.css b/packages/forklift-console-plugin/src/components/mappings/styles.css deleted file mode 100644 index 43513472a..000000000 --- a/packages/forklift-console-plugin/src/components/mappings/styles.css +++ /dev/null @@ -1,3 +0,0 @@ -.forklift-table__flex-cell-label { - margin-left: var(--pf-global--spacer--sm); -}