@@ -7,9 +7,10 @@ import type {
77 AccessRequestPreviewResponse ,
88 AccessRequestTarget ,
99} from '@/lib/api/contracts/access-requests'
10- import { BLOCK_NAMES } from '@/lib/block-metadata/names.generated'
11- import { PERMISSION_GROUP_FIELDS } from '@/lib/permission-groups/fields'
12- import { resolveAccessControlBlockType } from '@/lib/permission-groups/integration-allowlist'
10+ import {
11+ describePolicyChange ,
12+ describePolicyValue ,
13+ } from '@/ee/access-requests/lib/policy-description'
1314
1415interface PolicyChangesProps {
1516 changes : AccessRequestPolicyChange [ ]
@@ -18,74 +19,6 @@ interface PolicyChangesProps {
1819 targetLabel : string
1920}
2021
21- function describePolicyItems (
22- values : string [ ] ,
23- configKey : AccessRequestPolicyChange [ 'configKey' ] ,
24- target : AccessRequestTarget ,
25- targetLabel : string
26- ) : ReadonlyMap < string , string > {
27- const items = new Map < string , string > ( )
28- const integrationTarget =
29- target . kind === 'integration'
30- ? resolveAccessControlBlockType ( target . id . toLowerCase ( ) ) . toLowerCase ( )
31- : null
32- for ( const value of values ) {
33- if ( configKey === 'allowedIntegrations' ) {
34- const canonical = resolveAccessControlBlockType ( value . toLowerCase ( ) ) . toLowerCase ( )
35- const label = Object . hasOwn ( BLOCK_NAMES , canonical )
36- ? BLOCK_NAMES [ canonical ] !
37- : canonical === integrationTarget
38- ? targetLabel
39- : value
40- items . set ( canonical , label )
41- } else {
42- items . set ( value , target . kind !== 'feature' && value === target . id ? targetLabel : value )
43- }
44- }
45- return items
46- }
47-
48- export function describePolicyValue (
49- value : AccessRequestPolicyChange [ 'before' ] ,
50- configKey : AccessRequestPolicyChange [ 'configKey' ] ,
51- target : AccessRequestTarget ,
52- targetLabel : string
53- ) : string {
54- if ( value === null ) return 'All allowed'
55- if ( typeof value === 'boolean' ) return value ? 'Restricted' : 'Allowed'
56- return value . length
57- ? [ ...describePolicyItems ( value , configKey , target , targetLabel ) . values ( ) ] . join ( ', ' )
58- : 'None'
59- }
60-
61- export function describePolicyChange (
62- change : AccessRequestPolicyChange ,
63- target : AccessRequestTarget ,
64- targetLabel : string
65- ) : string {
66- const { before, after } = change
67- const describe = ( value : AccessRequestPolicyChange [ 'before' ] ) =>
68- describePolicyValue ( value , change . configKey , target , targetLabel )
69- if ( typeof after === 'boolean' ) return `${ describe ( before ) } → ${ describe ( after ) } `
70- if ( after === null ) return 'Allow all'
71- const next = describePolicyItems ( after , change . configKey , target , targetLabel )
72- if ( before === null )
73- return next . size ? `Allow only ${ [ ...next . values ( ) ] . join ( ', ' ) } ` : 'Allow none'
74- if ( ! Array . isArray ( before ) ) return `${ describe ( before ) } → ${ describe ( after ) } `
75- const previous = describePolicyItems ( before , change . configKey , target , targetLabel )
76- const added = [ ...next ] . filter ( ( [ id ] ) => ! previous . has ( id ) ) . map ( ( [ , label ] ) => label )
77- const removed = [ ...previous ] . filter ( ( [ id ] ) => ! next . has ( id ) ) . map ( ( [ , label ] ) => label )
78- const denylist = PERMISSION_GROUP_FIELDS [ change . configKey ] . kind === 'denylist'
79- return (
80- [
81- added . length ? `${ denylist ? 'Block' : 'Allow' } ${ added . join ( ', ' ) } ` : '' ,
82- removed . length ? `${ denylist ? 'Unblock' : 'Remove' } ${ removed . join ( ', ' ) } ` : '' ,
83- ]
84- . filter ( Boolean )
85- . join ( '; ' ) || 'No membership change'
86- )
87- }
88-
8922export function PolicyChanges ( { changes, impact, target, targetLabel } : PolicyChangesProps ) {
9023 const [ expanded , setExpanded ] = useState ( false )
9124 const detailsId = useId ( )
0 commit comments