SurfSense/surfsense_web/atoms/members/members-query.atoms.ts
Anish Sarkar a8c1fb660d feat(rename): complete searchSpace to workspace transition across frontend and backend
- Introduced a comprehensive specification for renaming `searchSpace` to `workspace` in `surfsense_web` and `surfsense_desktop`, ensuring all TypeScript identifiers, React props, and local data structures are updated.
- Implemented migration shims for persisted local state to prevent data loss during the transition.
- Updated observability metrics and IPC channels to reflect the new naming convention.
- Removed legacy `active-search-space` module and replaced it with `active-workspace` to maintain consistency.
- Ensured no behavioral changes or data loss for users during the renaming process.
2026-07-06 15:12:40 +05:30

77 lines
2.3 KiB
TypeScript

import { useAtomValue } from "jotai";
import { atomWithQuery } from "jotai-tanstack-query";
import { activeWorkspaceIdAtom } from "@/atoms/workspaces/workspace-query.atoms";
import { membersApiService } from "@/lib/apis/members-api.service";
import { cacheKeys } from "@/lib/query-client/cache-keys";
export const membersAtom = atomWithQuery((get) => {
const workspaceId = get(activeWorkspaceIdAtom);
return {
queryKey: cacheKeys.members.all(workspaceId?.toString() ?? ""),
enabled: !!workspaceId,
staleTime: 3 * 1000, // 3 seconds - short staleness for live collaboration
refetchInterval: 2 * 60 * 1000, // 2 minutes
queryFn: async () => {
if (!workspaceId) {
return [];
}
return membersApiService.getMembers({
workspace_id: Number(workspaceId),
});
},
};
});
export const myAccessAtom = atomWithQuery((get) => {
const workspaceId = get(activeWorkspaceIdAtom);
return {
queryKey: cacheKeys.members.myAccess(workspaceId?.toString() ?? ""),
enabled: !!workspaceId,
staleTime: 5 * 60 * 1000, // 5 minutes
queryFn: async () => {
if (!workspaceId) {
return null;
}
return membersApiService.getMyAccess({
workspace_id: Number(workspaceId),
});
},
};
});
/**
* 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 { data: access } = useAtomValue(myAccessAtom);
return canPerform(access, permission);
}