@noya-app/noya-api-client-react 0.1.47 → 0.1.49

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.47",
3
+ "version": "0.1.49",
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.47",
13
+ "@noya-app/noya-api": "0.1.49",
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",
@@ -337,3 +337,169 @@ export function useNoyaTemplates() {
337
337
  loading: loading === "fetching",
338
338
  };
339
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
+ }