|
| 1 | +import {useDispatch} from 'react-redux'; |
| 2 | +import {useEffect} from 'react'; |
| 3 | +import cn from 'bem-cn-lite'; |
| 4 | + |
| 5 | +import DataTable, {Column} from '@gravity-ui/react-data-table'; |
| 6 | + |
| 7 | +import type {TACE} from '../../../types/api/acl'; |
| 8 | +import {DEFAULT_TABLE_SETTINGS} from '../../../utils/constants'; |
| 9 | +import {useTypedSelector} from '../../../utils/hooks'; |
| 10 | +import {getSchemaAcl, setAclWasNotLoaded} from '../../../store/reducers/schemaAcl/schemaAcl'; |
| 11 | + |
| 12 | +import {ResponseError} from '../../../components/Errors/ResponseError'; |
| 13 | +import {Loader} from '../../../components/Loader'; |
| 14 | + |
| 15 | +import './Acl.scss'; |
| 16 | +import i18n from '../i18n'; |
| 17 | + |
| 18 | +const b = cn('ydb-acl'); |
| 19 | + |
| 20 | +const TABLE_SETTINGS = { |
| 21 | + ...DEFAULT_TABLE_SETTINGS, |
| 22 | + dynamicRender: false, |
| 23 | + stickyTop: 36, |
| 24 | +}; |
| 25 | + |
| 26 | +const prepareLogin = (value: string | undefined) => { |
| 27 | + if (value && value.endsWith('@staff') && !value.startsWith('svc_')) { |
| 28 | + const login = value.split('@')[0]; |
| 29 | + return login; |
| 30 | + } |
| 31 | + |
| 32 | + return value; |
| 33 | +}; |
| 34 | + |
| 35 | +const columns: Column<TACE>[] = [ |
| 36 | + { |
| 37 | + name: 'AccessType', |
| 38 | + header: 'Access Type', |
| 39 | + sortable: false, |
| 40 | + render: ({row}) => row.AccessType, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: 'AccessRights', |
| 44 | + header: 'Access Rights', |
| 45 | + render: ({row}) => { |
| 46 | + return row.AccessRights?.map((item, index) => { |
| 47 | + return <div key={index}>{item}</div>; |
| 48 | + }); |
| 49 | + }, |
| 50 | + sortable: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'Subject', |
| 54 | + sortable: false, |
| 55 | + render: ({row}) => { |
| 56 | + return prepareLogin(row.Subject); |
| 57 | + }, |
| 58 | + width: 140, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'InheritanceType', |
| 62 | + header: 'Inheritance Type', |
| 63 | + render: ({row}) => { |
| 64 | + return row.InheritanceType?.map((item, index) => { |
| 65 | + return <div key={index}>{item}</div>; |
| 66 | + }); |
| 67 | + }, |
| 68 | + sortable: false, |
| 69 | + }, |
| 70 | +]; |
| 71 | + |
| 72 | +export const Acl = () => { |
| 73 | + const dispatch = useDispatch(); |
| 74 | + |
| 75 | + const {currentSchemaPath} = useTypedSelector((state) => state.schema); |
| 76 | + const {loading, error, acl, owner, wasLoaded} = useTypedSelector((state) => state.schemaAcl); |
| 77 | + |
| 78 | + useEffect(() => { |
| 79 | + if (currentSchemaPath) { |
| 80 | + dispatch(getSchemaAcl({path: currentSchemaPath})); |
| 81 | + } |
| 82 | + |
| 83 | + return () => { |
| 84 | + // Ensures correct acl on path change |
| 85 | + dispatch(setAclWasNotLoaded()); |
| 86 | + }; |
| 87 | + }, [currentSchemaPath, dispatch]); |
| 88 | + |
| 89 | + const renderTable = () => { |
| 90 | + if (!acl || !acl.length) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + return ( |
| 95 | + <DataTable |
| 96 | + theme="yandex-cloud" |
| 97 | + columns={columns} |
| 98 | + data={acl} |
| 99 | + settings={TABLE_SETTINGS} |
| 100 | + /> |
| 101 | + ); |
| 102 | + }; |
| 103 | + |
| 104 | + const renderOwner = () => { |
| 105 | + if (!owner) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return ( |
| 110 | + <div className={b('owner-container')}> |
| 111 | + <span className={b('owner-label')}>{`${i18n('acl.owner')}: `}</span> |
| 112 | + {prepareLogin(owner)} |
| 113 | + </div> |
| 114 | + ); |
| 115 | + }; |
| 116 | + |
| 117 | + if (loading && !wasLoaded) { |
| 118 | + return <Loader />; |
| 119 | + } |
| 120 | + |
| 121 | + if (error) { |
| 122 | + return <ResponseError error={error} className={b('message-container')} />; |
| 123 | + } |
| 124 | + |
| 125 | + if (!loading && !acl && !owner) { |
| 126 | + return <div className={b('message-container')}>{i18n('acl.empty')}</div>; |
| 127 | + } |
| 128 | + |
| 129 | + return ( |
| 130 | + <div className={b()}> |
| 131 | + <div className={b('result')}> |
| 132 | + {renderOwner()} |
| 133 | + {renderTable()} |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + ); |
| 137 | +}; |
0 commit comments