* 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
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
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>
|
|
);
|
|
};
|