@@ -25,6 +25,7 @@ import {
25
25
import { GraphQLError } from "graphql" ;
26
26
import { isUndefined } from "../../../utils" ;
27
27
import { useSessionStorage } from "../hooks/useSessionStorage" ;
28
+ import { fetchRestrictions , Role } from "../utils/fetchRestrictions" ;
28
29
29
30
interface IAtlasProvider {
30
31
isVerified : boolean ;
@@ -44,6 +45,7 @@ interface IAtlasProvider {
44
45
isError : boolean ;
45
46
}
46
47
> ;
48
+ roleRestrictions : Role [ ] | undefined ;
47
49
}
48
50
49
51
const Context = createContext < IAtlasProvider | undefined > ( undefined ) ;
@@ -73,7 +75,7 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
73
75
}
74
76
: undefined ;
75
77
return new GraphQLClient ( `${ config . uri } /graphql` , { headers } ) ;
76
- } , [ authToken ] ) ;
78
+ } , [ authToken , config . uri ] ) ;
77
79
78
80
/**
79
81
* @description verifies user authorisation
@@ -142,6 +144,22 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
142
144
queryClient
143
145
) ;
144
146
147
+ const { data : roleRestrictions } = useQuery (
148
+ {
149
+ queryKey : [ `RoleRestrictions` ] ,
150
+ enabled : Boolean ( config . product ) ,
151
+ staleTime : Infinity ,
152
+ queryFn : async ( ) => {
153
+ try {
154
+ return await fetchRestrictions ( atlasGqlClient , config . product ) ;
155
+ } catch {
156
+ return undefined ;
157
+ }
158
+ } ,
159
+ } ,
160
+ queryClient
161
+ ) ;
162
+
145
163
useEffect ( ( ) => {
146
164
if ( ! isVerified ) return ;
147
165
refetchUser ( ) ;
@@ -255,6 +273,17 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
255
273
async ( file : File , role : Roles ) => {
256
274
try {
257
275
if ( ! address || ! isVerified || ! config . uri || ! authToken ) return null ;
276
+
277
+ if ( roleRestrictions ) {
278
+ const restrictions = roleRestrictions . find ( ( supportedRoles ) => Roles [ supportedRoles . name ] === role ) ;
279
+
280
+ if ( ! restrictions ) throw new Error ( "Unsupported role." ) ;
281
+ if ( ! restrictions . restriction . allowedMimeTypes . includes ( file . type ) ) throw new Error ( "Unsupported file type." ) ;
282
+ if ( file . size > restrictions . restriction . maxSize )
283
+ throw new Error (
284
+ `File too big. Max allowed size : ${ ( ( restrictions . restriction . maxSize / 1024 ) * 1024 ) . toFixed ( 2 ) } mb.`
285
+ ) ;
286
+ }
258
287
setIsUploadingFile ( true ) ;
259
288
260
289
const hash = await fetchWithAuthErrorHandling ( ( ) =>
@@ -267,7 +296,7 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
267
296
setIsUploadingFile ( false ) ;
268
297
}
269
298
} ,
270
- [ address , isVerified , setIsUploadingFile , authToken , config . uri , config . product ]
299
+ [ address , isVerified , setIsUploadingFile , authToken , config . uri , config . product , roleRestrictions ]
271
300
) ;
272
301
273
302
/**
@@ -309,6 +338,7 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
309
338
isUploadingFile,
310
339
uploadFile,
311
340
confirmEmail,
341
+ roleRestrictions,
312
342
} ) ,
313
343
[
314
344
isVerified ,
@@ -324,6 +354,7 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
324
354
isUploadingFile ,
325
355
uploadFile ,
326
356
confirmEmail ,
357
+ roleRestrictions ,
327
358
]
328
359
) }
329
360
>
0 commit comments