@noya-app/noya-api-client-react 0.1.46 → 0.1.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noya-app/noya-api-client-react",
3
- "version": "0.1.46",
3
+ "version": "0.1.48",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "@legendapp/state": "^2.1.1",
13
- "@noya-app/noya-api": "0.1.46",
13
+ "@noya-app/noya-api": "0.1.48",
14
14
  "@noya-app/noya-utils": "0.1.9",
15
15
  "@noya-app/observable": "0.1.12",
16
16
  "@noya-app/observable-store": "0.1.2",
@@ -214,6 +214,45 @@ export function useNoyaWorkspaces() {
214
214
  return workspaces;
215
215
  }
216
216
 
217
+ export type WorkspaceMemberInfo = {
218
+ userId: string;
219
+ name: string | null;
220
+ email: string | null;
221
+ image: string | null;
222
+ role: "OWNER" | "ADMIN" | "MEMBER";
223
+ };
224
+
225
+ /**
226
+ * Hook to get workspace members for a specific workspace.
227
+ * Returns the members array and a lookup map for easy access by userId.
228
+ */
229
+ export function useNoyaWorkspaceMembers(workspaceId: string): {
230
+ members: WorkspaceMemberInfo[];
231
+ membersById: Map<string, WorkspaceMemberInfo>;
232
+ loading: boolean;
233
+ } {
234
+ const { workspaces, loading } = useNoyaWorkspaces();
235
+
236
+ return useMemo(() => {
237
+ const workspace = workspaces.find((w) => w.id === workspaceId);
238
+ const members: WorkspaceMemberInfo[] =
239
+ workspace?.members.map((m) => ({
240
+ userId: m.userId,
241
+ name: m.user.name,
242
+ email: m.user.email,
243
+ image: m.user.image,
244
+ role: m.role,
245
+ })) ?? [];
246
+
247
+ const membersById = new Map<string, WorkspaceMemberInfo>();
248
+ for (const member of members) {
249
+ membersById.set(member.userId, member);
250
+ }
251
+
252
+ return { members, membersById, loading };
253
+ }, [workspaces, workspaceId, loading]);
254
+ }
255
+
217
256
  export function useNoyaMultiplayerRoomToken(fileId: string) {
218
257
  const client = useNoyaClientOrFallback();
219
258
  const multiplayerRoomToken = useObservable(
@@ -298,3 +337,169 @@ export function useNoyaTemplates() {
298
337
  loading: loading === "fetching",
299
338
  };
300
339
  }
340
+
341
+ export type WorkspaceRole = "OWNER" | "ADMIN" | "MEMBER";
342
+
343
+ export type WorkspaceAccessInfo = {
344
+ isMember: boolean;
345
+ role: WorkspaceRole | null;
346
+ isOwner: boolean;
347
+ isAdmin: boolean;
348
+ workspaceId: string;
349
+ loading: boolean;
350
+ };
351
+
352
+ /**
353
+ * Get workspace membership and role information for the current user.
354
+ * Returns workspace access details including membership status and role.
355
+ */
356
+ export function useWorkspaceAccess(workspaceId: string): WorkspaceAccessInfo {
357
+ const session = useNoyaSession();
358
+ const { workspaces, loading } = useNoyaWorkspaces();
359
+
360
+ return useMemo(() => {
361
+ const userId = session?.user?.id;
362
+ const workspace = workspaces.find((w) => w.id === workspaceId);
363
+
364
+ if (!workspace || !userId) {
365
+ return {
366
+ isMember: false,
367
+ role: null,
368
+ isOwner: false,
369
+ isAdmin: false,
370
+ workspaceId,
371
+ loading,
372
+ };
373
+ }
374
+
375
+ const member = workspace.members.find((m) => m.userId === userId);
376
+ const role = member?.role ?? null;
377
+
378
+ return {
379
+ isMember: !!member,
380
+ role,
381
+ isOwner: role === "OWNER",
382
+ isAdmin: role === "OWNER" || role === "ADMIN",
383
+ workspaceId,
384
+ loading,
385
+ };
386
+ }, [session?.user?.id, workspaces, workspaceId, loading]);
387
+ }
388
+
389
+ export type FileAccessSource = "owner" | "workspace" | "public" | "none";
390
+
391
+ export type FileAccessInfo = {
392
+ isOwner: boolean;
393
+ canView: boolean;
394
+ canEdit: boolean;
395
+ accessSource: FileAccessSource;
396
+ loading: boolean;
397
+ };
398
+
399
+ type FileWithAccess = {
400
+ user?: { id: string } | null;
401
+ access?: NoyaAPI.FileAccess;
402
+ workspaceAccess?: NoyaAPI.FileAccess;
403
+ workspaceId?: string;
404
+ };
405
+
406
+ /**
407
+ * Get file access permissions for the current user.
408
+ * Considers ownership, workspace membership, and public access settings.
409
+ */
410
+ export function useFileAccess(
411
+ file: FileWithAccess | null | undefined
412
+ ): FileAccessInfo {
413
+ const session = useNoyaSession();
414
+ const { workspaces, loading: workspacesLoading } = useNoyaWorkspaces();
415
+
416
+ return useMemo(() => {
417
+ const userId = session?.user?.id;
418
+
419
+ // Default: no access
420
+ const noAccess: FileAccessInfo = {
421
+ isOwner: false,
422
+ canView: false,
423
+ canEdit: false,
424
+ accessSource: "none",
425
+ loading: workspacesLoading,
426
+ };
427
+
428
+ if (!file) return noAccess;
429
+
430
+ // Check if user is the file owner
431
+ const isOwner = !!(userId && file.user?.id === userId);
432
+
433
+ if (isOwner) {
434
+ return {
435
+ isOwner: true,
436
+ canView: true,
437
+ canEdit: true,
438
+ accessSource: "owner",
439
+ loading: false,
440
+ };
441
+ }
442
+
443
+ // Check workspace access if user is a workspace member
444
+ if (file.workspaceId && userId) {
445
+ const workspace = workspaces.find((w) => w.id === file.workspaceId);
446
+ const isMember = workspace?.members.some((m) => m.userId === userId);
447
+
448
+ if (isMember) {
449
+ const workspaceAccess = file.workspaceAccess ?? "private";
450
+ if (workspaceAccess === "write") {
451
+ return {
452
+ isOwner: false,
453
+ canView: true,
454
+ canEdit: true,
455
+ accessSource: "workspace",
456
+ loading: false,
457
+ };
458
+ }
459
+ if (workspaceAccess === "read") {
460
+ return {
461
+ isOwner: false,
462
+ canView: true,
463
+ canEdit: false,
464
+ accessSource: "workspace",
465
+ loading: false,
466
+ };
467
+ }
468
+ }
469
+ }
470
+
471
+ // Check public access
472
+ const publicAccess = file.access ?? "private";
473
+ if (publicAccess === "write") {
474
+ return {
475
+ isOwner: false,
476
+ canView: true,
477
+ canEdit: true,
478
+ accessSource: "public",
479
+ loading: false,
480
+ };
481
+ }
482
+ if (publicAccess === "read") {
483
+ return {
484
+ isOwner: false,
485
+ canView: true,
486
+ canEdit: false,
487
+ accessSource: "public",
488
+ loading: false,
489
+ };
490
+ }
491
+
492
+ return noAccess;
493
+ }, [session?.user?.id, file, workspaces, workspacesLoading]);
494
+ }
495
+
496
+ export function useIsFileOwner(
497
+ file: { user?: { id: string } | null } | null | undefined
498
+ ): boolean {
499
+ const session = useNoyaSession();
500
+
501
+ return useMemo(() => {
502
+ if (!file || !session?.user?.id) return false;
503
+ return file.user?.id === session.user.id;
504
+ }, [file, session?.user?.id]);
505
+ }