feat: add integration access settings (#725)
* feat: add integration access settings * fix: typecheck and test issues * fix: test timeout * chore: address pull request feedback * chore: add throw if action forbidden for integration permissions * fix: unable to create new migrations because of duplicate prevId in sqlite snapshots * chore: add sqlite migration for integration permissions * test: add unit tests for integration access * test: add permission checks to integration router tests * test: add unit test for integration permissions * chore: add mysql migration * fix: format issues
This commit is contained in:
53
apps/nextjs/src/components/access/context.tsx
Normal file
53
apps/nextjs/src/components/access/context.tsx
Normal file
@@ -0,0 +1,53 @@
|
||||
import { createContext, useContext } from "react";
|
||||
import type { TablerIcon } from "@tabler/icons-react";
|
||||
|
||||
const AccessContext = createContext<{
|
||||
permissions: readonly string[];
|
||||
icons: Record<string, TablerIcon>;
|
||||
translate: (key: string) => string;
|
||||
defaultPermission: string;
|
||||
} | null>(null);
|
||||
|
||||
export const useAccessContext = <TPermission extends string>() => {
|
||||
const context = useContext(AccessContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error("useAccessContext must be used within a AccessProvider");
|
||||
}
|
||||
|
||||
return {
|
||||
icons: context.icons as Record<TPermission, TablerIcon>,
|
||||
getSelectData: () =>
|
||||
context.permissions.map((permission) => ({ value: permission, label: context.translate(permission) })),
|
||||
permissions: context.permissions as readonly TPermission[],
|
||||
translate: context.translate as (key: TPermission) => string,
|
||||
defaultPermission: context.defaultPermission as TPermission,
|
||||
};
|
||||
};
|
||||
|
||||
export const AccessProvider = <TPermission extends string>({
|
||||
defaultPermission,
|
||||
permissions,
|
||||
icons,
|
||||
translate,
|
||||
children,
|
||||
}: {
|
||||
defaultPermission: TPermission;
|
||||
permissions: readonly TPermission[];
|
||||
icons: Record<TPermission, TablerIcon>;
|
||||
translate: (key: TPermission) => string;
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
return (
|
||||
<AccessContext.Provider
|
||||
value={{
|
||||
defaultPermission,
|
||||
permissions,
|
||||
icons,
|
||||
translate: translate as (key: string) => string,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</AccessContext.Provider>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user