1+ import {
2+ Table , TableHeader , TableRow , TableCell , TableBody ,
3+ Link , Text
4+ } from '@fluentui/react-components' ;
5+ import { useContext , useEffect , useState } from 'react' ;
6+ import { useAppDispatch , useAppSelector } from '../../../../../store' ;
7+ import { IPermission } from '../../../../../types/permissions' ;
8+ import { ValidationContext } from '../../../../services/context/validation-context/ValidationContext' ;
9+ import { usePopups } from '../../../../services/hooks' ;
10+ import { fetchAllPrincipalGrants } from '../../../../services/slices/permission-grants.slice' ;
11+ import { fetchScopes } from '../../../../services/slices/scopes.slice' ;
12+ import { ScopesError } from '../../../../utils/error-utils/ScopesError' ;
13+ import { translateMessage } from '../../../../utils/translate-messages' ;
14+ import { convertVhToPx } from '../../../common/dimensions/dimensions-adjustment' ;
15+ import { getColumns } from './columnsV9' ;
16+ import { setConsentedStatus , sortPermissionsWithPrivilege } from './util' ;
17+ import permissionStyles from './Permission.stylesV9' ;
18+
19+
20+ export const Permissions = ( ) : JSX . Element => {
21+ const dispatch = useAppDispatch ( ) ;
22+ const validation = useContext ( ValidationContext ) ;
23+ const sampleQuery = useAppSelector ( ( state ) => state . sampleQuery ) ;
24+ const scopes = useAppSelector ( ( state ) => state . scopes ) ;
25+ const authToken = useAppSelector ( ( state ) => state . auth . authToken ) ;
26+ const consentedScopes = useAppSelector ( ( state ) => state . auth . consentedScopes ) ;
27+ const dimensions = useAppSelector ( ( state ) => state . dimensions ) ;
28+ const { show : showPermissions } = usePopups ( 'full-permissions' , 'panel' ) ;
29+
30+ const tokenPresent = ! ! authToken . token ;
31+ const { pending : loading , error } = scopes ;
32+ const [ permissions , setPermissions ] = useState < { item : IPermission ; index : number } [ ] > ( [ ] ) ;
33+ const [ permissionsError , setPermissionsError ] = useState < ScopesError | null > ( error ) ;
34+
35+ const styles = permissionStyles ( ) ;
36+ const tabHeight = convertVhToPx ( dimensions . request . height , 110 ) ;
37+
38+ useEffect ( ( ) => {
39+ if ( error && error ?. url . includes ( 'permissions' ) ) {
40+ setPermissionsError ( error ) ;
41+ }
42+ } , [ error ] ) ;
43+
44+ const openPermissionsPanel = ( ) => {
45+ showPermissions ( {
46+ settings : {
47+ title : translateMessage ( 'Permissions' ) ,
48+ width : 'lg'
49+ }
50+ } ) ;
51+ } ;
52+
53+ const getPermissions = ( ) : void => {
54+ dispatch ( fetchScopes ( 'query' ) ) ;
55+ fetchPermissionGrants ( ) ;
56+ } ;
57+
58+ const fetchPermissionGrants = ( ) : void => {
59+ if ( tokenPresent ) {
60+ dispatch ( fetchAllPrincipalGrants ( ) ) ;
61+ }
62+ } ;
63+
64+ useEffect ( ( ) => {
65+ if ( validation . isValid ) {
66+ getPermissions ( ) ;
67+ }
68+ } , [ sampleQuery ] ) ;
69+
70+ useEffect ( ( ) => {
71+ if ( tokenPresent && validation . isValid ) {
72+ dispatch ( fetchAllPrincipalGrants ( ) ) ;
73+ }
74+ } , [ ] ) ;
75+
76+ useEffect ( ( ) => {
77+ let updatedPermissions = scopes . data . specificPermissions || [ ] ;
78+ updatedPermissions = sortPermissionsWithPrivilege ( updatedPermissions ) ;
79+ updatedPermissions = setConsentedStatus ( tokenPresent , updatedPermissions , consentedScopes ) ;
80+ setPermissions ( updatedPermissions . map ( ( item , index ) => ( { item, index } ) ) ) ;
81+ } , [ scopes . data . specificPermissions , tokenPresent , consentedScopes ] ) ;
82+
83+ const columns = getColumns ( { source : 'tab' , tokenPresent } ) ;
84+
85+ if ( loading . isSpecificPermissions ) {
86+ return < div className = { styles . label } > < Text > { translateMessage ( 'Fetching permissions' ) } ... </ Text > </ div > ;
87+ }
88+
89+ if ( ! validation . isValid ) {
90+ return < div className = { styles . label } > < Text > { translateMessage ( 'Invalid URL' ) } !</ Text > </ div > ;
91+ }
92+
93+ const displayNoPermissionsFoundMessage = ( ) : JSX . Element => (
94+ < div className = { styles . root } >
95+ < Text >
96+ { translateMessage ( 'permissions not found in permissions tab' ) }
97+ < Link inline onClick = { openPermissionsPanel } >
98+ { translateMessage ( 'open permissions panel' ) }
99+ </ Link >
100+ { translateMessage ( 'permissions list' ) }
101+ </ Text >
102+ </ div >
103+ ) ;
104+
105+ const displayNotSignedInMessage = ( ) : JSX . Element => (
106+ < div className = { styles . root } >
107+ < Text > { translateMessage ( 'sign in to view a list of all permissions' ) } </ Text >
108+ </ div >
109+ ) ;
110+
111+ const displayErrorFetchingPermissionsMessage = ( ) : JSX . Element => (
112+ < div className = { styles . errorLabel } > < Text > { translateMessage ( 'Fetching permissions failing' ) } </ Text > </ div >
113+ ) ;
114+
115+ if ( ! tokenPresent && permissions . length === 0 ) {
116+ return displayNotSignedInMessage ( ) ;
117+ }
118+
119+ if ( permissions . length === 0 ) {
120+ return permissionsError ?. status && ( permissionsError ?. status === 404 || permissionsError ?. status === 400 )
121+ ? displayNoPermissionsFoundMessage ( )
122+ : displayErrorFetchingPermissionsMessage ( ) ;
123+ }
124+
125+ return (
126+ < div >
127+ < div className = { styles . permissionText } >
128+ < Text >
129+ { translateMessage ( tokenPresent ? 'permissions required to run the query' :'sign in to consent to permissions' ) }
130+ </ Text >
131+ </ div >
132+ < div className = { styles . tableWrapper } style = { { height : tabHeight } } >
133+ < Table aria-label = { translateMessage ( 'Permissions Table' ) } >
134+ < TableHeader >
135+ < TableRow >
136+ { columns . map ( ( column ) => (
137+ < TableCell key = { column . columnId } > { column . renderHeaderCell ( ) } </ TableCell >
138+ ) ) }
139+ </ TableRow >
140+ </ TableHeader >
141+ < TableBody >
142+ { permissions . map ( ( { item, index } ) => (
143+ < TableRow key = { item . value } >
144+ { columns . map ( ( column ) => (
145+ < TableCell key = { column . columnId } > { column . renderCell ( { item, index } ) } </ TableCell >
146+ ) ) }
147+ </ TableRow >
148+ ) ) }
149+ </ TableBody >
150+ </ Table >
151+ </ div >
152+ </ div >
153+ ) ;
154+ } ;
0 commit comments