Skip to content

Commit

Permalink
Add filter to Policies and Roles tables, refactor filter
Browse files Browse the repository at this point in the history
  • Loading branch information
fiskus committed Aug 22, 2023
1 parent 683809b commit aa875ae
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 188 deletions.
31 changes: 15 additions & 16 deletions catalog/app/containers/Admin/Buckets/Buckets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { useTracker } from 'utils/tracking'
import * as Types from 'utils/types'
import * as validators from 'utils/validators'

import Filter from '../Filter'
import * as Form from '../Form'
import * as Table from '../Table'

Expand Down Expand Up @@ -1266,23 +1265,25 @@ const columns = [
},
]

type BucketRow = Pick<
Model.GQLTypes.BucketConfig,
'name' | 'title' | 'description' | 'lastIndexed' | 'iconUrl'
>

interface CRUDProps {
bucketName?: string
}

function CRUD({ bucketName }: CRUDProps) {
const { bucketConfigs: rows } = GQL.useQueryS(BUCKET_CONFIGS_QUERY)
const [filter, setFilter] = React.useState('')
const filtered = React.useMemo(
() =>
filter
? rows.filter(({ name, title }) =>
(name + title).toLowerCase().includes(filter.toLowerCase()),
)
: rows,
[filter, rows],
)
const ordering = Table.useOrdering({ rows: filtered, column: columns[0] })
const filtering = Table.useFiltering<BucketRow>({

Check warning on line 1279 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1279

Added line #L1279 was not covered by tests
rows,
filterBy: ({ name, title }) => name + title,

Check warning on line 1281 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1281

Added line #L1281 was not covered by tests
})
const ordering = Table.useOrdering<BucketRow>({

Check warning on line 1283 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1283

Added line #L1283 was not covered by tests
rows: filtering.filtered,
column: columns[0],
})
const pagination = Pagination.use(ordering.ordered, {
// @ts-expect-error
getItemId: R.prop('name'),
Expand All @@ -1293,6 +1294,7 @@ function CRUD({ bucketName }: CRUDProps) {
const history = RRDom.useHistory()

const toolbarActions = [
<Table.Filter {...filtering} key="toolbar-filter" />,
{
title: 'Add bucket',
icon: <M.Icon>add</M.Icon>,
Expand Down Expand Up @@ -1345,10 +1347,7 @@ function CRUD({ bucketName }: CRUDProps) {
{editingBucket && <Edit bucket={editingBucket} close={onBucketClose} />}
</M.Dialog>

<Table.Toolbar heading="Buckets" actions={toolbarActions}>
{/* @ts-expect-error */}
<Filter value={filter} onChange={setFilter} />
</Table.Toolbar>
<Table.Toolbar heading="Buckets" actions={toolbarActions} />
<Table.Wrapper>
<M.Table size="small">
<Table.Head columns={columns} ordering={ordering} withInlineActions />
Expand Down
27 changes: 24 additions & 3 deletions catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as React from 'react'
import * as R from 'ramda'

Check warning on line 2 in catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx#L2

Added line #L2 was not covered by tests
import * as RF from 'react-final-form'
import * as M from '@material-ui/core'

import * as GQL from 'utils/GraphQL'

import * as Table from '../Table'
import Filter from './Filter'

Check warning on line 9 in catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx#L8-L9

Added lines #L8 - L9 were not covered by tests
import { MAX_POLICIES_PER_ROLE } from './shared'

import ROLES_QUERY from './gql/Roles.generated'
Expand Down Expand Up @@ -45,12 +48,26 @@ function RoleSelectionDialog({
[setSelected],
)

const filtering = Table.useFiltering<ManagedRole>({

Check warning on line 51 in catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx#L51

Added line #L51 was not covered by tests
rows: roles,
filterBy: ({ name }: ManagedRole) => name,

Check warning on line 53 in catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx#L53

Added line #L53 was not covered by tests
})
const ordered = React.useMemo(
() => R.sortBy(({ name }: ManagedRole) => name, filtering.filtered),

Check warning on line 56 in catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx#L55-L56

Added lines #L55 - L56 were not covered by tests
[filtering.filtered],
)

return (
<M.Dialog maxWidth="xs" open={open} onClose={onClose} onExited={handleExited}>
<M.DialogTitle>Attach policy to roles</M.DialogTitle>
{roles.length && (
<M.Box ml={3} mr={1.5}>

Check warning on line 64 in catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx#L64

Added line #L64 was not covered by tests
<Filter {...filtering} />
</M.Box>
)}
<M.DialogContent dividers>
{roles.length ? (
roles.map((role) => (
{ordered.length ? (
ordered.map((role) => (

Check warning on line 70 in catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx#L70

Added line #L70 was not covered by tests
<M.FormControlLabel
key={role.id}
style={{ display: 'flex', marginRight: 0 }}
Expand All @@ -73,7 +90,11 @@ function RoleSelectionDialog({
/>
))
) : (
<M.Typography>No more roles to attach this policy to</M.Typography>
<M.Typography>

Check warning on line 93 in catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx#L93

Added line #L93 was not covered by tests
{filtering.value
? 'No roles found, try resetting filter'
: 'No more roles to attach this policy to'}

Check warning on line 96 in catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AssociatedRoles.tsx#L95-L96

Added lines #L95 - L96 were not covered by tests
</M.Typography>
)}
</M.DialogContent>
<M.DialogActions>
Expand Down
71 changes: 46 additions & 25 deletions catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as React from 'react'
import * as R from 'ramda'

Check warning on line 2 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L2

Added line #L2 was not covered by tests
import * as RF from 'react-final-form'
import * as M from '@material-ui/core'

import * as GQL from 'utils/GraphQL'
import StyledLink from 'utils/StyledLink'

import * as Table from '../Table'
import Filter from './Filter'

Check warning on line 10 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L9-L10

Added lines #L9 - L10 were not covered by tests
import { MAX_POLICIES_PER_ROLE } from './shared'

import POLICIES_QUERY from './gql/Policies.generated'
Expand Down Expand Up @@ -38,34 +41,52 @@ function PolicySelectionDialog({
[setSelected, onClose],
)

const filtering = Table.useFiltering<Policy>({

Check warning on line 44 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L44

Added line #L44 was not covered by tests
rows: policies,
filterBy: ({ title }: Policy) => title,

Check warning on line 46 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L46

Added line #L46 was not covered by tests
})
const ordered = React.useMemo(
() => R.sortBy(({ title }: Policy) => title, filtering.filtered),

Check warning on line 49 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L48-L49

Added lines #L48 - L49 were not covered by tests
[filtering.filtered],
)

return (
<M.Dialog maxWidth="xs" open={open} onClose={onClose} onExited={handleExited}>
<M.DialogTitle>Attach a policy</M.DialogTitle>
<M.List dense>
{policies.length ? (
policies.map((policy) => (
<M.ListItem button key={policy.id} onClick={() => select(policy)}>
<M.ListItemText>
{policy.title}
<M.Box component="span" color="text.secondary">
{' '}
(
{policy.managed ? (
<>{policy.permissions.length} buckets</>
) : (
<>unmanaged</>
)}
)
</M.Box>
</M.ListItemText>
</M.ListItem>
))
) : (
<M.DialogContent dividers>
<M.Typography>No more policies to attach</M.Typography>
</M.DialogContent>
)}
</M.List>
{policies.length && (
<M.Box ml={3} mr={1.5}>

Check warning on line 57 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L57

Added line #L57 was not covered by tests
<Filter {...filtering} />
</M.Box>
)}
<M.DialogContent dividers>
<M.List dense>
{ordered.length ? (
ordered.map((policy) => (
<M.ListItem button key={policy.id} onClick={() => select(policy)}>

Check warning on line 65 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L64-L65

Added lines #L64 - L65 were not covered by tests
<M.ListItemText>
{policy.title}
<M.Box component="span" color="text.secondary">
{' '}
(
{policy.managed ? (
<>{policy.permissions.length} buckets</>

Check warning on line 72 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L72

Added line #L72 was not covered by tests
) : (
<>unmanaged</>

Check warning on line 74 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L74

Added line #L74 was not covered by tests
)}
)
</M.Box>
</M.ListItemText>
</M.ListItem>
))
) : (
<M.Typography>

Check warning on line 82 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L82

Added line #L82 was not covered by tests
{filtering.value
? 'No policies found, try resetting filter'
: 'No more policies to attach'}

Check warning on line 85 in catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/AttachedPolicies.tsx#L84-L85

Added lines #L84 - L85 were not covered by tests
</M.Typography>
)}
</M.List>
</M.DialogContent>
<M.DialogActions>
<M.Button autoFocus onClick={onClose} color="primary">
Cancel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react'
import * as R from 'ramda'

Check warning on line 2 in catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx#L2

Added line #L2 was not covered by tests
import * as RF from 'react-final-form'
import * as M from '@material-ui/core'

Expand All @@ -7,6 +8,9 @@ import * as Model from 'model'
import * as GQL from 'utils/GraphQL'
import StyledLink from 'utils/StyledLink'

import * as Table from '../Table'
import Filter from './Filter'

Check warning on line 12 in catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx#L11-L12

Added lines #L11 - L12 were not covered by tests

import BUCKETS_QUERY from './gql/Buckets.generated'
import { BucketPermissionSelectionFragment as BucketPermission } from './gql/BucketPermissionSelection.generated'

Expand Down Expand Up @@ -39,33 +43,51 @@ function BucketAddDialog({ open, onClose, buckets, addBucket }: BucketAddDialogP
[onClose, select],
)

const filtering = Table.useFiltering<Bucket>({

Check warning on line 46 in catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx#L46

Added line #L46 was not covered by tests
rows: buckets,
filterBy: ({ name, title }: Bucket) => name + title,

Check warning on line 48 in catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx#L48

Added line #L48 was not covered by tests
})
const ordered = React.useMemo(
() => R.sortBy(({ name }: Bucket) => name, filtering.filtered),

Check warning on line 51 in catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx#L50-L51

Added lines #L50 - L51 were not covered by tests
[filtering.filtered],
)

return (
<M.Dialog maxWidth="xs" open={open} onClose={onClose} onExited={handleExited}>
<M.DialogTitle>Add a bucket</M.DialogTitle>
{buckets.length ? (
<M.List>
{buckets.map((bucket) => (
<M.ListItem key={bucket.name} button onClick={() => handleAdd(bucket)}>
<M.ListItemAvatar style={{ minWidth: 44 }}>
<M.Avatar
style={{ width: 32, height: 32 }}
src={bucket.iconUrl || defaultBucketIcon}
/>
</M.ListItemAvatar>
<M.ListItemText>
s3://{bucket.name}{' '}
<M.Box component="span" color="text.secondary" ml={0.5}>
{bucket.title}
</M.Box>
</M.ListItemText>
</M.ListItem>
))}
</M.List>
) : (
<M.DialogContent>
<M.Typography>No more buckets to add</M.Typography>
</M.DialogContent>
{buckets.length && (
<M.Box ml={3} mr={1.5}>

Check warning on line 59 in catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx#L59

Added line #L59 was not covered by tests
<Filter {...filtering} />
</M.Box>
)}
<M.DialogContent dividers>
{ordered.length ? (
<M.List>
{ordered.map((bucket) => (
<M.ListItem key={bucket.name} button onClick={() => handleAdd(bucket)}>

Check warning on line 67 in catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx#L65-L67

Added lines #L65 - L67 were not covered by tests
<M.ListItemAvatar style={{ minWidth: 44 }}>
<M.Avatar
style={{ width: 32, height: 32 }}
src={bucket.iconUrl || defaultBucketIcon}
/>
</M.ListItemAvatar>
<M.ListItemText>
s3://{bucket.name}{' '}
<M.Box component="span" color="text.secondary" ml={0.5}>
{bucket.title}
</M.Box>
</M.ListItemText>
</M.ListItem>
))}
</M.List>
) : (
<M.Typography>

Check warning on line 84 in catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx#L84

Added line #L84 was not covered by tests
{filtering.value
? 'No buckets found, try resetting filter'
: 'No more buckets to add'}

Check warning on line 87 in catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/BucketsPermissions.tsx#L86-L87

Added lines #L86 - L87 were not covered by tests
</M.Typography>
)}
</M.DialogContent>
<M.DialogActions>
<M.Button autoFocus onClick={onClose} color="primary">
Cancel
Expand Down
26 changes: 26 additions & 0 deletions catalog/app/containers/Admin/RolesAndPolicies/Filter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as React from 'react'
import * as M from '@material-ui/core'

Check warning on line 2 in catalog/app/containers/Admin/RolesAndPolicies/Filter.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Filter.tsx#L1-L2

Added lines #L1 - L2 were not covered by tests

interface FilterProps {
value: string
onChange: (v: string) => void
}

export default function Filter({ value, onChange }: FilterProps) {
return (

Check warning on line 10 in catalog/app/containers/Admin/RolesAndPolicies/Filter.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Filter.tsx#L9-L10

Added lines #L9 - L10 were not covered by tests
<M.InputBase
endAdornment={
value && (
<M.IconButton onClick={() => onChange('')}>

Check warning on line 14 in catalog/app/containers/Admin/RolesAndPolicies/Filter.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Filter.tsx#L14

Added line #L14 was not covered by tests
<M.Icon fontSize="small">clear</M.Icon>
</M.IconButton>
)
}
fullWidth
onChange={(event) => onChange(event.target.value)}

Check warning on line 20 in catalog/app/containers/Admin/RolesAndPolicies/Filter.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Filter.tsx#L20

Added line #L20 was not covered by tests
placeholder="Filter"
startAdornment={<M.Icon fontSize="small">search</M.Icon>}
value={value}
/>
)
}
15 changes: 11 additions & 4 deletions catalog/app/containers/Admin/RolesAndPolicies/Policies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,18 @@ interface DialogsOpenProps {
export default function Policies() {
const { policies: rows } = GQL.useQueryS(POLICIES_QUERY)

const ordering = Table.useOrdering({ rows, column: columns[0] })
const filtering = Table.useFiltering<Policy>({

Check warning on line 652 in catalog/app/containers/Admin/RolesAndPolicies/Policies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Policies.tsx#L652

Added line #L652 was not covered by tests
rows,
filterBy: ({ title }) => title,

Check warning on line 654 in catalog/app/containers/Admin/RolesAndPolicies/Policies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Policies.tsx#L654

Added line #L654 was not covered by tests
})
const ordering = Table.useOrdering<Policy>({

Check warning on line 656 in catalog/app/containers/Admin/RolesAndPolicies/Policies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Policies.tsx#L656

Added line #L656 was not covered by tests
rows: filtering.filtered,
column: columns[0],
})
const dialogs = Dialogs.use()

const toolbarActions = [
<Table.Filter {...filtering} key="toolbar-filter" />,
{
title: 'Create',
icon: <M.Icon>add</M.Icon>,
Expand All @@ -664,11 +672,11 @@ export default function Policies() {

const inlineActions = (policy: Policy) => [
policy.arn
? {
? ({

Check warning on line 675 in catalog/app/containers/Admin/RolesAndPolicies/Policies.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Policies.tsx#L675

Added line #L675 was not covered by tests
title: 'Open AWS Console',
icon: <M.Icon>launch</M.Icon>,
href: getArnLink(policy.arn),
}
} as Table.Action)
: null,
{
title: 'Edit',
Expand Down Expand Up @@ -712,7 +720,6 @@ export default function Policies() {
))}
<M.TableCell align="right" padding="none">
<Table.InlineActions actions={inlineActions(i)}>
{/* @ts-expect-error */}
<SettingsMenu policy={i} openDialog={dialogs.open} />
</Table.InlineActions>
</M.TableCell>
Expand Down
Loading

0 comments on commit aa875ae

Please sign in to comment.