@@ -25,6 +25,7 @@ import {
2525import { GraphQLError } from "graphql" ;
2626import { isUndefined } from "../../../utils" ;
2727import { useSessionStorage } from "../hooks/useSessionStorage" ;
28+ import { fetchRestrictions , Role } from "../utils/fetchRestrictions" ;
2829
2930interface IAtlasProvider {
3031 isVerified : boolean ;
@@ -44,6 +45,7 @@ interface IAtlasProvider {
4445 isError : boolean ;
4546 }
4647 > ;
48+ roleRestrictions : Role [ ] | undefined ;
4749}
4850
4951const Context = createContext < IAtlasProvider | undefined > ( undefined ) ;
@@ -73,7 +75,7 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
7375 }
7476 : undefined ;
7577 return new GraphQLClient ( `${ config . uri } /graphql` , { headers } ) ;
76- } , [ authToken ] ) ;
78+ } , [ authToken , config . uri ] ) ;
7779
7880 /**
7981 * @description verifies user authorisation
@@ -142,6 +144,22 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
142144 queryClient
143145 ) ;
144146
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+
145163 useEffect ( ( ) => {
146164 if ( ! isVerified ) return ;
147165 refetchUser ( ) ;
@@ -255,6 +273,17 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
255273 async ( file : File , role : Roles ) => {
256274 try {
257275 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+ }
258287 setIsUploadingFile ( true ) ;
259288
260289 const hash = await fetchWithAuthErrorHandling ( ( ) =>
@@ -267,7 +296,7 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
267296 setIsUploadingFile ( false ) ;
268297 }
269298 } ,
270- [ address , isVerified , setIsUploadingFile , authToken , config . uri , config . product ]
299+ [ address , isVerified , setIsUploadingFile , authToken , config . uri , config . product , roleRestrictions ]
271300 ) ;
272301
273302 /**
@@ -309,6 +338,7 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
309338 isUploadingFile,
310339 uploadFile,
311340 confirmEmail,
341+ roleRestrictions,
312342 } ) ,
313343 [
314344 isVerified ,
@@ -324,6 +354,7 @@ export const AtlasProvider: React.FC<{ config: AtlasConfig; children?: React.Rea
324354 isUploadingFile ,
325355 uploadFile ,
326356 confirmEmail ,
357+ roleRestrictions ,
327358 ]
328359 ) }
329360 >
0 commit comments