1+ import { ParsedSchema } from "@authzed/spicedb-parser-js" ;
12import {
23 interpolateBlues ,
34 interpolateGreens ,
@@ -22,11 +23,6 @@ export interface RelationshipsService {
2223 */
2324 relationships : Relationship [ ] ;
2425
25- /**
26- * resourceTypes is the set of object types used for resources.
27- */
28- resourceTypes : string [ ] ;
29-
3026 /**
3127 * resources is the set of resources defined, without relations, in the form
3228 * `namespacename:objectid`.
@@ -40,11 +36,6 @@ export interface RelationshipsService {
4036 */
4137 subjects : string [ ] ;
4238
43- /**
44- * subjectTypes is the set of object types used for subjects.
45- */
46- subjectTypes : string [ ] ;
47-
4839 /**
4940 * getObjectIds returns the set of IDs for objects used as resources or subjects
5041 * for the given object type.
@@ -89,7 +80,10 @@ const colorSchemes = [
8980 * based on entered test relationships.
9081 * @param relationshipsString The encoded string of test relationships.
9182 */
92- export function useRelationshipsService ( relationships : Relationship [ ] ) : RelationshipsService {
83+ export function useRelationshipsService (
84+ relationships : Relationship [ ] ,
85+ parsedSchema ?: ParsedSchema ,
86+ ) : RelationshipsService {
9387 return useMemo ( ( ) => {
9488 const buildingObjectsByType : Map < string , Set < string > > = new Map < string , Set < string > > ( ) ;
9589
@@ -113,17 +107,6 @@ export function useRelationshipsService(relationships: Relationship[]): Relation
113107 } ) ,
114108 ) ;
115109
116- const resourceTypes = filter (
117- relationships . map ( ( rt ) => {
118- const onr = rt . resourceAndRelation ;
119- if ( onr === undefined ) {
120- return null ;
121- }
122-
123- return onr . namespace ;
124- } ) ,
125- ) ;
126-
127110 const subjects = filter (
128111 relationships . map ( ( rt ) => {
129112 const subject = rt . subject ;
@@ -139,27 +122,17 @@ export function useRelationshipsService(relationships: Relationship[]): Relation
139122 } ) ,
140123 ) ;
141124
142- const subjectTypes = filter (
143- relationships . map ( ( rt ) => {
144- const subject = rt . subject ;
145- if ( subject === undefined ) {
146- return null ;
147- }
148-
149- return subject . namespace ;
150- } ) ,
151- ) ;
152-
153125 const calculateColor = ( colorSet : ( n : number ) => string , valueSet : string [ ] , value : string ) => {
154126 if ( valueSet . indexOf ( value ) < 0 ) {
155127 return undefined ;
156128 }
157- return colorSet ( 1 - valueSet . indexOf ( value ) / 9 ) ;
129+ const offset = 1 - valueSet . indexOf ( value ) / 9 ;
130+ return colorSet ( offset ) ;
158131 } ;
159132
160- const possibleObjectTypes = [ ... resourceTypes , ... subjectTypes ] ;
161-
162- const objectTypes = Array . from ( new Set ( possibleObjectTypes ) ) ;
133+ const objectTypes =
134+ parsedSchema ?. definitions . filter ( ( tld ) => tld . kind === "objectDef" ) . map ( ( def ) => def . name ) ??
135+ [ ] ;
163136
164137 const objectsByType : Record < string , string [ ] > = { } ;
165138 buildingObjectsByType . forEach ( ( idSet : Set < string > , objectType : string ) => {
@@ -171,8 +144,6 @@ export function useRelationshipsService(relationships: Relationship[]): Relation
171144 return {
172145 relationships : relationships ,
173146 resources : resources ,
174- resourceTypes : resourceTypes ,
175- subjectTypes : subjectTypes ,
176147 subjects : subjects ,
177148 getObjectIds : ( objectType : string ) => {
178149 const resourceIDs = relationships
@@ -202,5 +173,5 @@ export function useRelationshipsService(relationships: Relationship[]): Relation
202173 return scheme ( 0.65 ) ;
203174 } ,
204175 } ;
205- } , [ relationships ] ) ;
176+ } , [ relationships , parsedSchema ] ) ;
206177}
0 commit comments