Skip to content

Commit

Permalink
fix: code-review
Browse files Browse the repository at this point in the history
  • Loading branch information
Raubzeug committed Jul 2, 2024
1 parent 019352f commit b5a1805
Showing 1 changed file with 57 additions and 45 deletions.
102 changes: 57 additions & 45 deletions src/containers/Tenant/Acl/Acl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from 'react';

import type {DefinitionListItem} from '@gravity-ui/components';
import {DefinitionList} from '@gravity-ui/components';
//TODO: fix import
import type {DefinitionListSingleItem} from '@gravity-ui/components/build/esm/components/DefinitionList/types';

import {ResponseError} from '../../../components/Errors/ResponseError';
import {Loader} from '../../../components/Loader';
Expand Down Expand Up @@ -76,58 +78,64 @@ function DefinitionValue({value}: DefinitionValueProps) {
);
}

export const Acl = ({path}: {path: string}) => {
const {currentData, isFetching, error} = schemaAclApi.useGetSchemaAclQuery({path});

const loading = isFetching && !currentData;
const {acl, owner} = currentData || {};

const renderTable = () => {
if (!acl || !acl.length) {
return null;
}
function getAclListItems(acl?: TACE[]): DefinitionListItem[] {
if (!acl || !acl.length) {
return [];
}

const normalizedAcl = normalizeAcl(acl);
const normalizedAcl = normalizeAcl(acl);

const items = normalizedAcl.map(({Subject, ...data}) => {
const definedDataEntries = Object.entries(data).filter(([_key, value]) =>
Boolean(value),
) as [AclParameter, string | string[]][];
return normalizedAcl.map(({Subject, ...data}) => {
const definedDataEntries = Object.entries(data).filter(([_key, value]) =>
Boolean(value),
) as [AclParameter, string | string[]][];

if (definedDataEntries.length === 1 && definedDataEntries[0][0] === 'access') {
return {
name: Subject,
content: <DefinitionValue value={definedDataEntries[0][1]} />,
};
}
if (definedDataEntries.length === 1 && definedDataEntries[0][0] === 'access') {
return {
label: Subject,
items: aclParams
.map((key) => {
const value = data[key];
if (value) {
return {
name: aclParamToName[key],
content: <DefinitionValue value={value} />,
};
}
return undefined;
})
.filter(Boolean),
name: Subject,
content: <DefinitionValue value={definedDataEntries[0][1]} />,
};
}) as DefinitionListItem[];
}
return {
label: Subject,
items: aclParams
.map((key) => {
const value = data[key];
if (value) {
return {
name: aclParamToName[key],
content: <DefinitionValue value={value} />,
};
}
return undefined;
})
.filter(Boolean) as DefinitionListSingleItem[],
};
});
}

const preparedOwner = prepareLogin(owner);
function getOwnerItem(owner?: string) {
const preparedOwner = prepareLogin(owner);
if (!preparedOwner) {
return [];
}
return [
{
name: <span className={b('owner')}>{preparedOwner}</span>,
content: <span className={b('owner')}>{i18n('acl.owner')}</span>,
},
] as DefinitionListItem[];
}

if (preparedOwner) {
items.unshift({
name: <span className={b('owner')}>{preparedOwner}</span>,
content: <span className={b('owner')}>{i18n('acl.owner')}</span>,
});
}
export const Acl = ({path}: {path: string}) => {
const {currentData, isFetching, error} = schemaAclApi.useGetSchemaAclQuery({path});

const loading = isFetching && !currentData;
const {acl, owner} = currentData || {};

const aclListItems = getAclListItems(acl);

return <DefinitionList items={items} nameMaxWidth={200} />;
};
const ownerItem = getOwnerItem(owner);

if (loading) {
return <Loader />;
Expand All @@ -143,7 +151,11 @@ export const Acl = ({path}: {path: string}) => {

return (
<div className={b()}>
<div className={b('result')}>{renderTable()}</div>
<DefinitionList
items={ownerItem.concat(aclListItems)}
nameMaxWidth={200}
className={b('result')}
/>
</div>
);
};

0 comments on commit b5a1805

Please sign in to comment.