import React, { createContext, useContext, useMemo } from "react"; import { License } from "@webiny/wcp"; import type { DecryptedWcpProjectLicense } from "@webiny/wcp/types.js"; export interface WcpProjectLicenseContextValue { hasLicense: boolean; canUseMultiTenancy: () => boolean; canUseLanguages: () => boolean; canUseTeams: () => boolean; canUsePrivateFiles: () => boolean; canUseFileManagerThreatDetection: () => boolean; canUseWorkflows: () => boolean; canUseHcmsFieldPermissions: () => boolean; } const WcpProjectLicenseContext = createContext(null); export const WcpProjectLicenseProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const license = useMemo(() => { const licenseEnv = process.env.WCP_PROJECT_LICENSE; if (!licenseEnv) { return null; } try { const licenseDto = JSON.parse(licenseEnv) as DecryptedWcpProjectLicense; return License.fromLicenseDto(licenseDto); } catch (e) { console.warn( "Failed to parse WCP_PROJECT_LICENSE environment variable. Expected valid JSON with DecryptedWcpProjectLicense format:", e ); return null; } }, []); const hasLicense = !!license; const value: WcpProjectLicenseContextValue = { hasLicense, canUseMultiTenancy: () => hasLicense, canUseLanguages: () => license?.canUseLanguages() ?? false, canUseTeams: () => license?.canUseTeams() ?? false, canUsePrivateFiles: () => license?.canUsePrivateFiles() ?? false, canUseFileManagerThreatDetection: () => license?.canUseFileManagerThreatDetection() ?? false, canUseWorkflows: () => license?.canUseWorkflows() ?? false, canUseHcmsFieldPermissions: () => license?.canUseHcmsFieldPermissions() ?? false }; return ( {children} ); }; export const useWcpProjectLicense = () => { const context = useContext(WcpProjectLicenseContext); if (!context) { throw new Error("useWcpProjectLicense must be used within a WcpProjectLicenseProvider"); } return context; };