mirror of
https://github.com/ajnart/homarr.git
synced 2026-02-27 08:50:56 +01:00
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
|
|
"use client";
|
||
|
|
|
||
|
|
import type { PropsWithChildren } from "react";
|
||
|
|
import { createContext, useContext } from "react";
|
||
|
|
|
||
|
|
interface IntegrationContextProps {
|
||
|
|
integrations: {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
url: string;
|
||
|
|
kind: string;
|
||
|
|
permissions: {
|
||
|
|
hasFullAccess: boolean;
|
||
|
|
hasInteractAccess: boolean;
|
||
|
|
hasUseAccess: boolean;
|
||
|
|
};
|
||
|
|
}[];
|
||
|
|
}
|
||
|
|
|
||
|
|
const IntegrationContext = createContext<IntegrationContextProps | null>(null);
|
||
|
|
|
||
|
|
export const IntegrationProvider = ({ integrations, children }: PropsWithChildren<IntegrationContextProps>) => {
|
||
|
|
return <IntegrationContext.Provider value={{ integrations }}>{children}</IntegrationContext.Provider>;
|
||
|
|
};
|
||
|
|
|
||
|
|
export const useIntegrationsWithUseAccess = () => {
|
||
|
|
const context = useContext(IntegrationContext);
|
||
|
|
|
||
|
|
if (!context) {
|
||
|
|
throw new Error("useIntegrationsWithUseAccess must be used within an IntegrationProvider");
|
||
|
|
}
|
||
|
|
|
||
|
|
return context.integrations.filter((integration) => integration.permissions.hasUseAccess);
|
||
|
|
};
|
||
|
|
|
||
|
|
export const useIntegrationsWithInteractAccess = () => {
|
||
|
|
const context = useContext(IntegrationContext);
|
||
|
|
|
||
|
|
if (!context) {
|
||
|
|
throw new Error("useIntegrationsWithInteractAccess must be used within an IntegrationProvider");
|
||
|
|
}
|
||
|
|
|
||
|
|
return context.integrations.filter((integration) => integration.permissions.hasInteractAccess);
|
||
|
|
};
|
||
|
|
|
||
|
|
export const useIntegrationsWithFullAccess = () => {
|
||
|
|
const context = useContext(IntegrationContext);
|
||
|
|
|
||
|
|
if (!context) {
|
||
|
|
throw new Error("useIntegrationsWithFullAccess must be used within an IntegrationProvider");
|
||
|
|
}
|
||
|
|
|
||
|
|
return context.integrations.filter((integration) => integration.permissions.hasFullAccess);
|
||
|
|
};
|