mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-25 19:15:18 +02:00
Merge pull request #1428 from guangyang1206/fix/extract-shared-haspermission-helper-1366
refactor: extract shared hasPermission helper (MODSetter/SurfSense#1366)
This commit is contained in:
commit
ee87747b37
3 changed files with 41 additions and 8 deletions
|
|
@ -31,7 +31,7 @@ import {
|
|||
deleteMemberMutationAtom,
|
||||
updateMemberMutationAtom,
|
||||
} from "@/atoms/members/members-mutation.atoms";
|
||||
import { membersAtom, myAccessAtom } from "@/atoms/members/members-query.atoms";
|
||||
import { membersAtom, myAccessAtom, canPerform } from "@/atoms/members/members-query.atoms";
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
|
|
@ -126,14 +126,9 @@ export function TeamContent({ searchSpaceId }: TeamContentProps) {
|
|||
const { data: access = null, isLoading: accessLoading } = useAtomValue(myAccessAtom);
|
||||
|
||||
const hasPermission = useCallback(
|
||||
(permission: string) => {
|
||||
if (!access) return false;
|
||||
if (access.is_owner) return true;
|
||||
return access.permissions?.includes(permission) ?? false;
|
||||
},
|
||||
(permission: string) => canPerform(access, permission),
|
||||
[access]
|
||||
);
|
||||
|
||||
const { data: members = [], isLoading: membersLoading } = useAtomValue(membersAtom);
|
||||
|
||||
const { mutateAsync: updateMember } = useAtomValue(updateMemberMutationAtom);
|
||||
|
|
|
|||
|
|
@ -39,3 +39,38 @@ export const myAccessAtom = atomWithQuery((get) => {
|
|||
},
|
||||
};
|
||||
});
|
||||
|
||||
/**
|
||||
* Helper function to check if the current user has a specific permission.
|
||||
*
|
||||
* @param access - The access object from useAtomValue(myAccessAtom)
|
||||
* @param permission - The permission string to check
|
||||
* @returns boolean indicating if the user has the permission
|
||||
*
|
||||
* @example
|
||||
* const access = useAtomValue(myAccessAtom);
|
||||
* if (canPerform(access, 'manage_members')) { ... }
|
||||
*/
|
||||
export function canPerform(
|
||||
access: { is_owner: boolean; permissions?: string[] } | null | undefined,
|
||||
permission: string
|
||||
): boolean {
|
||||
if (!access) return false;
|
||||
if (access.is_owner) return true;
|
||||
return access.permissions?.includes(permission) ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook wrapper for canPerform that reads from myAccessAtom internally.
|
||||
* Use this if you want to avoid calling useAtomValue(myAccessAtom) separately.
|
||||
*
|
||||
* @param permission - The permission string to check
|
||||
* @returns boolean indicating if the user has the permission
|
||||
*
|
||||
* @example
|
||||
* const canManageMembers = usePermissionGate('manage_members');
|
||||
*/
|
||||
export function usePermissionGate(permission: string): boolean {
|
||||
const access = useAtomValue(myAccessAtom);
|
||||
return canPerform(access, permission);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import {
|
|||
} from "lucide-react";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { myAccessAtom } from "@/atoms/members/members-query.atoms";
|
||||
import { myAccessAtom, canPerform } from "@/atoms/members/members-query.atoms";
|
||||
import { permissionsAtom } from "@/atoms/permissions/permissions-query.atoms";
|
||||
import {
|
||||
createRoleMutationAtom,
|
||||
|
|
@ -257,6 +257,9 @@ export function RolesManager({ searchSpaceId }: { searchSpaceId: number }) {
|
|||
const { data: access = null } = useAtomValue(myAccessAtom);
|
||||
|
||||
const hasPermission = useCallback(
|
||||
(permission: string) => canPerform(access, permission),
|
||||
[access]
|
||||
);
|
||||
(permission: string) => {
|
||||
if (!access) return false;
|
||||
if (access.is_owner) return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue