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 8b3935c commit 9aabcf1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
11 changes: 2 additions & 9 deletions src/containers/Tenant/Acl/Acl.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@
padding-bottom: 16px;
}

&__owner-container {
position: sticky;
z-index: 2;
top: 0;

width: 100%;
padding-bottom: 16px;

background-color: var(--g-color-base-background);
&__owner {
font-weight: 600;
}
&__definition-content {
display: flex;
Expand Down
79 changes: 47 additions & 32 deletions src/containers/Tenant/Acl/Acl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,30 @@ const prepareLogin = (value: string | undefined) => {

const aclParams = ['access', 'type', 'inheritance'] as const;

const aclParamToName: Record<(typeof aclParams)[number], string> = {
type AclParameter = (typeof aclParams)[number];

const aclParamToName: Record<AclParameter, string> = {
access: 'Access',
type: 'Access type',
inheritance: 'Inheritance type',
};

const defaultInheritanceType = new Set(['Object', 'Container']);
const defaultInheritanceType = ['Object', 'Container'];
const defaultAccessType = 'Allow';

const defaultInheritanceTypeSet = new Set(defaultInheritanceType);

function normalizeAcl(acl: TACE[]) {
return acl.map((ace) => {
const {AccessRules = [], AccessRights = [], AccessType, InheritanceType, Subject} = ace;
const access = AccessRules.concat(AccessRights);
//"Allow" is default access type. We want to show it only if it isn't default
const type = AccessType === 'Allow' ? undefined : AccessType;
const type = AccessType === defaultAccessType ? undefined : AccessType;
let inheritance;
// ['Object', 'Container'] - is default inheritance type. We want to show it only if it isn't default
if (
InheritanceType?.length !== defaultInheritanceType.size ||
InheritanceType.some((t) => !defaultInheritanceType.has(t))
InheritanceType?.length !== defaultInheritanceTypeSet.size ||
InheritanceType.some((t) => !defaultInheritanceTypeSet.has(t))
) {
inheritance = InheritanceType;
}
Expand All @@ -56,6 +61,21 @@ function normalizeAcl(acl: TACE[]) {
});
}

interface DefinitionValueProps {
value: string | string[];
}

function DefinitionValue({value}: DefinitionValueProps) {
const normalizedValue = typeof value === 'string' ? [value] : value;
return (
<div className={b('definition-content')}>
{normalizedValue.map((el) => (
<span key={el}>{el}</span>
))}
</div>
);
}

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

Expand All @@ -70,44 +90,42 @@ export const Acl = ({path}: {path: string}) => {
const normalizedAcl = normalizeAcl(acl);

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

if (definedDataEntries.length === 1 && definedDataEntries[0][0] === 'access') {
return {
name: Subject,
content: <DefinitionValue value={definedDataEntries[0][1]} />,
};
}
const definedData = Object.fromEntries(definedDataEntries);
return {
label: Subject,
items: aclParams
.map((key) => {
const value = data[key];
if (!value) {
return undefined;
}
const normalizedValue = typeof value === 'string' ? [value] : value;
const value = definedData[key];
return {
name: aclParamToName[key],
content: (
<div className={b('definition-content')}>
{normalizedValue.map((el) => (
<span key={el}>{el}</span>
))}
</div>
),
content: <DefinitionValue value={value} />,
};
})
.filter(Boolean),
};
}) as DefinitionListItem[];

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

const renderOwner = () => {
if (!owner) {
return null;
if (preparedOwner) {
items.unshift({
name: <span className={b('owner')}>{preparedOwner}</span>,
content: <span className={b('owner')}>{i18n('acl.owner')}</span>,
});
}

return (
<div className={b('owner-container')}>
<span className={b('owner-label')}>{`${i18n('acl.owner')}: `}</span>
{prepareLogin(owner)}
</div>
);
return <DefinitionList items={items} nameMaxWidth={200} />;
};

if (loading) {
Expand All @@ -124,10 +142,7 @@ export const Acl = ({path}: {path: string}) => {

return (
<div className={b()}>
<div className={b('result')}>
{renderOwner()}
{renderTable()}
</div>
<div className={b('result')}>{renderTable()}</div>
</div>
);
};
2 changes: 1 addition & 1 deletion src/types/api/acl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export interface TACE {
AccessRules?: string[];
Subject: string;
InheritanceType?: string[];
AccessRule: string;
AccessRule?: string;
}

0 comments on commit 9aabcf1

Please sign in to comment.